From 8f8410f01b650cd7747fab1e3a3321d52a084967 Mon Sep 17 00:00:00 2001 From: AMiniLegend Date: Sun, 18 May 2014 13:51:46 -0400 Subject: [PATCH] Added three new functions for selections: expandtoline - selects the entire line for each line in a selection (newline included) joinSelection - joins selected lines into a single space-delimited line invertSelection - inverts all selected lines --- lib/ace/commands/default_commands.js | 87 ++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/lib/ace/commands/default_commands.js b/lib/ace/commands/default_commands.js index 108c7efb..c2f27c05 100644 --- a/lib/ace/commands/default_commands.js +++ b/lib/ace/commands/default_commands.js @@ -33,6 +33,7 @@ define(function(require, exports, module) { var lang = require("../lib/lang"); var config = require("../config"); +var Range = require("../range").Range; function bindKey(win, mac) { return {win: win, mac: mac}; @@ -602,6 +603,92 @@ exports.commands = [{ exec: function(editor) { editor.toLowerCase(); }, multiSelectAction: "forEach", scrollIntoView: "cursor" +}, { + name: "expandtoline", + bindKey: bindKey("Ctrl-Shift-L", "Command-Shift-L"), + exec: function(editor) { + var isBackwards = editor.selection.isBackwards(); + var selectionStart = isBackwards ? editor.selection.getSelectionLead() : editor.selection.getSelectionAnchor(); + var selectionEnd = isBackwards ? editor.selection.getSelectionAnchor() : editor.selection.getSelectionLead(); + + editor.clearSelection(); + + editor.selection.moveCursorTo(selectionStart.row, 0); + editor.selection.selectTo(selectionEnd.row + 1, 0); + }, + multiSelectAction: "forEach", + scrollIntoView: "cursor" +}, { + name: "joinSelection", + bindKey: bindKey(null, null), + exec: function(editor) { + var isBackwards = editor.selection.isBackwards(); + var selectionStart = isBackwards ? editor.selection.getSelectionLead() : editor.selection.getSelectionAnchor(); + var selectionEnd = isBackwards ? editor.selection.getSelectionAnchor() : editor.selection.getSelectionLead(); + var selectedRange = editor.selection.getRange(); + var selectedText = editor.session.doc.getTextRange(selectedRange); + var newLine = editor.session.doc.getLine(selectionStart.row) + " "; + + for (var i = selectionStart.row + 1; i <= selectionEnd.row; i++) { + var curLine = editor.session.doc.getLine(i); + newLine += lang.stringTrimLeft(lang.stringTrimRight(curLine)) + " "; + }; + + newLine += editor.session.doc.getNewLineCharacter(); + editor.clearSelection(); + editor.selection.moveCursorTo(selectionStart.row, 0); + editor.selection.selectTo(selectionEnd.row + 1, 0); + selectedRange = editor.selection.getRange(); + editor.session.doc.replace(selectedRange, newLine); + }, + multiSelectAction: "forEach" +}, { + name: "invertSelection", + bindKey: bindKey(null, null), + exec: function(editor) { + var endRow = editor.session.doc.getLength() - 1; + var endCol = editor.session.doc.getLine(endRow).length; + var initialScroll = editor.session.getScrollTop(); + var ranges = editor.selection.rangeList.ranges; + var newRanges = []; + var tmpRanges = ranges; + + // If multiple selections don't exist, rangeList will return 0 so replace with single range + if (ranges.length < 1) { + ranges = [editor.selection.getRange()]; + } + + for (var i = 0; i < ranges.length; i++) { + if (i == (ranges.length - 1)) { + // The last selection must connect to the end of the document, unless it already does + if (!(ranges[i].end.row === endRow && ranges[i].end.column === endCol)) { + newRanges.push(new Range(ranges[i].end.row, ranges[i].end.column, endRow, endCol)); + } + } + + if (i === 0) { + // The first selection must connect to the start of the document, unless it already does + if (!(ranges[i].start.row === 0 && ranges[i].start.column === 0)) { + newRanges.push(new Range(0, 0, ranges[i].start.row, ranges[i].start.column)); + } + } else { + newRanges.push(new Range(ranges[i-1].end.row, ranges[i-1].end.column, ranges[i].start.row, ranges[i].start.column)); + } + } + + editor.exitMultiSelectMode(); + editor.clearSelection(); + + // Set the main cursor to the last range that will be seen + editor.selection.moveCursorTo(newRanges[newRanges.length-1].end.row, newRanges[newRanges.length-1].end.column, false); + + for(var i = 0; i < newRanges.length; i++) { + editor.selection.addRange(newRanges[i], false); + } + + // Make it so the user sees no change in scrolling + editor.session.setScrollTop(initialScroll); + } }]; });