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", name: "selecttomatching",
bindKey: bindKey("Ctrl-Shift-P", null), bindKey: bindKey("Ctrl-Shift-P", null),
exec: function(editor) { editor.jumpToMatching(true); }, exec: function(editor) { editor.jumpToMatching(true); },
multiSelectAction: "forEach",
readOnly: true readOnly: true
}, },
@ -315,7 +316,7 @@ exports.commands = [{
name: "removeline", name: "removeline",
bindKey: bindKey("Ctrl-D", "Command-D"), bindKey: bindKey("Ctrl-D", "Command-D"),
exec: function(editor) { editor.removeLines(); }, exec: function(editor) { editor.removeLines(); },
multiSelectAction: "forEach" multiSelectAction: "forEachLine"
}, { }, {
name: "duplicateSelection", name: "duplicateSelection",
bindKey: bindKey("Ctrl-Shift-D", "Command-Shift-D"), bindKey: bindKey("Ctrl-Shift-D", "Command-Shift-D"),
@ -325,12 +326,12 @@ exports.commands = [{
name: "sortlines", name: "sortlines",
bindKey: bindKey("Ctrl-Alt-S", "Command-Alt-S"), bindKey: bindKey("Ctrl-Alt-S", "Command-Alt-S"),
exec: function(editor) { editor.sortLines(); }, exec: function(editor) { editor.sortLines(); },
multiSelectAction: "forEach" multiSelectAction: "forEachLine"
}, { }, {
name: "togglecomment", name: "togglecomment",
bindKey: bindKey("Ctrl-/", "Command-/"), bindKey: bindKey("Ctrl-/", "Command-/"),
exec: function(editor) { editor.toggleCommentLines(); }, exec: function(editor) { editor.toggleCommentLines(); },
multiSelectAction: "forEach" multiSelectAction: "forEachLine"
}, { }, {
name: "modifyNumberUp", name: "modifyNumberUp",
bindKey: bindKey("Ctrl-Shift-Up", "Alt-Shift-Up"), bindKey: bindKey("Ctrl-Shift-Up", "Alt-Shift-Up"),
@ -358,19 +359,23 @@ exports.commands = [{
}, { }, {
name: "copylinesup", name: "copylinesup",
bindKey: bindKey("Alt-Shift-Up", "Command-Option-Up"), bindKey: bindKey("Alt-Shift-Up", "Command-Option-Up"),
exec: function(editor) { editor.copyLinesUp(); } exec: function(editor) { editor.copyLinesUp(); },
multiSelectAction: "forEachLine"
}, { }, {
name: "movelinesup", name: "movelinesup",
bindKey: bindKey("Alt-Up", "Option-Up"), bindKey: bindKey("Alt-Up", "Option-Up"),
exec: function(editor) { editor.moveLinesUp(); } exec: function(editor) { editor.moveLinesUp(); },
multiSelectAction: "forEachLine"
}, { }, {
name: "copylinesdown", name: "copylinesdown",
bindKey: bindKey("Alt-Shift-Down", "Command-Option-Down"), bindKey: bindKey("Alt-Shift-Down", "Command-Option-Down"),
exec: function(editor) { editor.copyLinesDown(); } exec: function(editor) { editor.copyLinesDown(); },
multiSelectAction: "forEachLine"
}, { }, {
name: "movelinesdown", name: "movelinesdown",
bindKey: bindKey("Alt-Down", "Option-Down"), bindKey: bindKey("Alt-Down", "Option-Down"),
exec: function(editor) { editor.moveLinesDown(); } exec: function(editor) { editor.moveLinesDown(); },
multiSelectAction: "forEachLine"
}, { }, {
name: "del", name: "del",
bindKey: bindKey("Delete", "Delete|Ctrl-D"), bindKey: bindKey("Delete", "Delete|Ctrl-D"),
@ -418,12 +423,12 @@ exports.commands = [{
name: "blockoutdent", name: "blockoutdent",
bindKey: bindKey("Ctrl-[", "Ctrl-["), bindKey: bindKey("Ctrl-[", "Ctrl-["),
exec: function(editor) { editor.blockOutdent(); }, exec: function(editor) { editor.blockOutdent(); },
multiSelectAction: "forEach" multiSelectAction: "forEachLine"
},{ },{
name: "blockindent", name: "blockindent",
bindKey: bindKey("Ctrl-]", "Ctrl-]"), bindKey: bindKey("Ctrl-]", "Ctrl-]"),
exec: function(editor) { editor.blockIndent(); }, exec: function(editor) { editor.blockIndent(); },
multiSelectAction: "forEach" multiSelectAction: "forEachLine"
}, { }, {
name: "insertstring", name: "insertstring",
exec: function(editor, str) { editor.insert(str); }, exec: function(editor, str) { editor.insert(str); },

View file

@ -450,6 +450,8 @@ var Editor = require("./editor").Editor;
editor.multiSelect.mergeOverlappingRanges(); editor.multiSelect.mergeOverlappingRanges();
} else if (command.multiSelectAction == "forEach") { } else if (command.multiSelectAction == "forEach") {
editor.forEachSelection(command, e.args); editor.forEachSelection(command, e.args);
} else if (command.multiSelectAction == "forEachLine") {
editor.forEachSelection(command, e.args, true);
} else if (command.multiSelectAction == "single") { } else if (command.multiSelectAction == "single") {
editor.exitMultiSelectMode(); editor.exitMultiSelectMode();
command.exec(editor, e.args || {}); command.exec(editor, e.args || {});
@ -465,7 +467,7 @@ var Editor = require("./editor").Editor;
* @param {String} args Any arguments for the command * @param {String} args Any arguments for the command
* @method Editor.forEachSelection * @method Editor.forEachSelection
**/ **/
this.forEachSelection = function(cmd, args) { this.forEachSelection = function(cmd, args, $byLines) {
if (this.inVirtualSelectionMode) if (this.inVirtualSelectionMode)
return; return;
@ -479,10 +481,14 @@ var Editor = require("./editor").Editor;
var tmpSel = new Selection(session); var tmpSel = new Selection(session);
this.inVirtualSelectionMode = true; this.inVirtualSelectionMode = true;
for (var i = rangeList.ranges.length; i--;) { 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]); tmpSel.fromOrientedRange(rangeList.ranges[i]);
this.selection = session.selection = tmpSel; this.selection = session.selection = tmpSel;
cmd.exec(this, args || {}); cmd.exec(this, args || {});
tmpSel.toOrientedRange(rangeList.ranges[i]); tmpSel.toOrientedRange(rangeList.ranges[i]);
} }
tmpSel.detach(); tmpSel.detach();

View file

@ -93,6 +93,11 @@ var RangeList = function() {
this.merge = function() { this.merge = function() {
var removed = []; var removed = [];
var list = this.ranges; var list = this.ranges;
list = list.sort(function(a, b) {
return comparePoints(a.start, b.start);
});
var next = list[0], range; var next = list[0], range;
for (var i = 1; i < list.length; i++) { for (var i = 1; i < list.length; i++) {
range = next; range = next;
@ -114,6 +119,8 @@ var RangeList = function() {
next = range; next = range;
i--; i--;
} }
this.ranges = list;
return removed; return removed;
}; };