From 917420636bf1211d09ca2c77f58a2bbc1fa43b60 Mon Sep 17 00:00:00 2001 From: nightwing Date: Sun, 3 Mar 2013 14:48:36 +0400 Subject: [PATCH] add multiSelectAction "forEachLine" --- lib/ace/commands/default_commands.js | 23 ++++++++++++++--------- lib/ace/multi_select.js | 10 ++++++++-- lib/ace/range_list.js | 7 +++++++ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/lib/ace/commands/default_commands.js b/lib/ace/commands/default_commands.js index 94f68d76..03fdce5c 100644 --- a/lib/ace/commands/default_commands.js +++ b/lib/ace/commands/default_commands.js @@ -295,6 +295,7 @@ exports.commands = [{ name: "selecttomatching", bindKey: bindKey("Ctrl-Shift-P", null), exec: function(editor) { editor.jumpToMatching(true); }, + multiSelectAction: "forEach", readOnly: true }, @@ -315,7 +316,7 @@ exports.commands = [{ name: "removeline", bindKey: bindKey("Ctrl-D", "Command-D"), exec: function(editor) { editor.removeLines(); }, - multiSelectAction: "forEach" + multiSelectAction: "forEachLine" }, { name: "duplicateSelection", bindKey: bindKey("Ctrl-Shift-D", "Command-Shift-D"), @@ -325,12 +326,12 @@ exports.commands = [{ name: "sortlines", bindKey: bindKey("Ctrl-Alt-S", "Command-Alt-S"), exec: function(editor) { editor.sortLines(); }, - multiSelectAction: "forEach" + multiSelectAction: "forEachLine" }, { name: "togglecomment", bindKey: bindKey("Ctrl-/", "Command-/"), exec: function(editor) { editor.toggleCommentLines(); }, - multiSelectAction: "forEach" + multiSelectAction: "forEachLine" }, { name: "modifyNumberUp", bindKey: bindKey("Ctrl-Shift-Up", "Alt-Shift-Up"), @@ -358,19 +359,23 @@ exports.commands = [{ }, { name: "copylinesup", bindKey: bindKey("Alt-Shift-Up", "Command-Option-Up"), - exec: function(editor) { editor.copyLinesUp(); } + exec: function(editor) { editor.copyLinesUp(); }, + multiSelectAction: "forEachLine" }, { name: "movelinesup", bindKey: bindKey("Alt-Up", "Option-Up"), - exec: function(editor) { editor.moveLinesUp(); } + exec: function(editor) { editor.moveLinesUp(); }, + multiSelectAction: "forEachLine" }, { name: "copylinesdown", bindKey: bindKey("Alt-Shift-Down", "Command-Option-Down"), - exec: function(editor) { editor.copyLinesDown(); } + exec: function(editor) { editor.copyLinesDown(); }, + multiSelectAction: "forEachLine" }, { name: "movelinesdown", bindKey: bindKey("Alt-Down", "Option-Down"), - exec: function(editor) { editor.moveLinesDown(); } + exec: function(editor) { editor.moveLinesDown(); }, + multiSelectAction: "forEachLine" }, { name: "del", bindKey: bindKey("Delete", "Delete|Ctrl-D"), @@ -418,12 +423,12 @@ exports.commands = [{ name: "blockoutdent", bindKey: bindKey("Ctrl-[", "Ctrl-["), exec: function(editor) { editor.blockOutdent(); }, - multiSelectAction: "forEach" + multiSelectAction: "forEachLine" },{ name: "blockindent", bindKey: bindKey("Ctrl-]", "Ctrl-]"), exec: function(editor) { editor.blockIndent(); }, - multiSelectAction: "forEach" + multiSelectAction: "forEachLine" }, { name: "insertstring", exec: function(editor, str) { editor.insert(str); }, diff --git a/lib/ace/multi_select.js b/lib/ace/multi_select.js index 3c2a526f..f5b66a92 100644 --- a/lib/ace/multi_select.js +++ b/lib/ace/multi_select.js @@ -450,6 +450,8 @@ var Editor = require("./editor").Editor; editor.multiSelect.mergeOverlappingRanges(); } else if (command.multiSelectAction == "forEach") { editor.forEachSelection(command, e.args); + } else if (command.multiSelectAction == "forEachLine") { + editor.forEachSelection(command, e.args, true); } else if (command.multiSelectAction == "single") { editor.exitMultiSelectMode(); command.exec(editor, e.args || {}); @@ -465,7 +467,7 @@ var Editor = require("./editor").Editor; * @param {String} args Any arguments for the command * @method Editor.forEachSelection **/ - this.forEachSelection = function(cmd, args) { + this.forEachSelection = function(cmd, args, $byLines) { if (this.inVirtualSelectionMode) return; @@ -479,10 +481,14 @@ var Editor = require("./editor").Editor; var tmpSel = new Selection(session); this.inVirtualSelectionMode = true; for (var i = rangeList.ranges.length; i--;) { + if ($byLines) { + while (i > 0 && rangeList.ranges[i].start.row == rangeList.ranges[i].end.row) + i--; + } tmpSel.fromOrientedRange(rangeList.ranges[i]); this.selection = session.selection = tmpSel; cmd.exec(this, args || {}); - tmpSel.toOrientedRange(rangeList.ranges[i]); + tmpSel.toOrientedRange(rangeList.ranges[i]); } tmpSel.detach(); diff --git a/lib/ace/range_list.js b/lib/ace/range_list.js index ed0a5d2d..e186959b 100644 --- a/lib/ace/range_list.js +++ b/lib/ace/range_list.js @@ -93,6 +93,11 @@ var RangeList = function() { this.merge = function() { var removed = []; var list = this.ranges; + + list = list.sort(function(a, b) { + return comparePoints(a.start, b.start); + }); + var next = list[0], range; for (var i = 1; i < list.length; i++) { range = next; @@ -114,6 +119,8 @@ var RangeList = function() { next = range; i--; } + + this.ranges = list; return removed; };