add multiSelectAction "forEachLine"

This commit is contained in:
nightwing 2013-03-03 14:48:36 +04:00
commit 917420636b
3 changed files with 29 additions and 11 deletions

View file

@ -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); },

View file

@ -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();

View file

@ -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;
};