add sortLines functionality in all modes

This commit is contained in:
Vlad Zinculescu 2012-10-19 12:56:44 +02:00
commit 281a1ca2a8
2 changed files with 39 additions and 1 deletions

View file

@ -321,6 +321,11 @@ exports.commands = [{
bindKey: bindKey("Ctrl-Shift-D", "Command-Shift-D"),
exec: function(editor) { editor.duplicateSelection(); },
multiSelectAction: "forEach"
}, {
name: "sortlines",
bindKey: bindKey("Ctrl-Alt-S", "Command-Alt-S"),
exec: function(editor) { editor.sortLines(); },
multiSelectAction: "forEach"
}, {
name: "togglecomment",
bindKey: bindKey("Ctrl-/", "Command-/"),

View file

@ -1277,10 +1277,43 @@ var Editor = function(renderer, session) {
};
/**
* Editor.toggleCommentLines()
* Editor.sortLines()
*
* Given the currently selected range, this function either comments all lines or uncomments all lines (depending on whether it's commented or not).
**/
this.sortLines = function() {
var state = this.session.getState(this.getCursorPosition().row);
var rows = this.$getSelectedRows();
var doc = this.session;
var lines = [];
for(i = rows.first; i <= rows.last; i++) {
lines.push( doc.getLine(i) );
}
lines.sort(
function(a, b) {
if (a.toLowerCase() < b.toLowerCase()) return -1;
if (a.toLowerCase() > b.toLowerCase()) return 1;
return 0;
}
);
var deleteRange = new Range(0, 0, 0, 0);
for (var i=rows.first; i<= rows.last; i++)
{
var line = doc.getLine(i);
deleteRange.start.row = i;
deleteRange.end.row = i;
deleteRange.end.column = line.length;
doc.replace(deleteRange, lines[i-rows.first]);
}
};
/**
* Editor.toggleCommentLines()
*
* Given the currently selected range, this function sorts the lines alphabetically ascending.
**/
this.toggleCommentLines = function() {
var state = this.session.getState(this.getCursorPosition().row);
var rows = this.$getSelectedRows();