From 8f8410f01b650cd7747fab1e3a3321d52a084967 Mon Sep 17 00:00:00 2001 From: AMiniLegend Date: Sun, 18 May 2014 13:51:46 -0400 Subject: [PATCH 1/3] 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); + } }]; }); From 470efff7828a595d8cea7defa70c085ad1b59f70 Mon Sep 17 00:00:00 2001 From: AMiniLegend Date: Sun, 18 May 2014 20:36:06 -0400 Subject: [PATCH 2/3] Changed the name of the "joinSelection" function to "joinlines". Also changed it's behavior so that when there is no selection, it will join with the next line. --- lib/ace/commands/default_commands.js | 43 ++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/lib/ace/commands/default_commands.js b/lib/ace/commands/default_commands.js index c2f27c05..5cd9f61f 100644 --- a/lib/ace/commands/default_commands.js +++ b/lib/ace/commands/default_commands.js @@ -619,27 +619,46 @@ exports.commands = [{ multiSelectAction: "forEach", scrollIntoView: "cursor" }, { - name: "joinSelection", + name: "joinlines", 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) + " "; + var firstLineEndCol = editor.session.doc.getLine(selectionStart.row).length + var selectedText = editor.session.doc.getTextRange(editor.selection.getRange()); + var insertLine = 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)) + " "; + selectedText = selectedText.replace(/\n\s*/, " "); + var selectedCount = selectedText.length; + + for (var i = selectionStart.row + 1; i <= selectionEnd.row + 1; i++) { + var curLine = lang.stringTrimLeft(lang.stringTrimRight(editor.session.doc.getLine(i))); + + if (curLine.length !== 0) { + curLine = " " + curLine; + } + + insertLine += curLine; }; - newLine += editor.session.doc.getNewLineCharacter(); + if (selectionEnd.row + 1 < (editor.session.doc.getLength() - 1)) { + // Don't insert a newline at the end of the document + insertLine += 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); + editor.session.doc.replace(new Range(selectionStart.row, 0, selectionEnd.row + 2, 0), insertLine); + + if (selectedCount > 0) { + // Select the text that was previously selected + editor.selection.moveCursorTo(selectionStart.row, selectionStart.column); + editor.selection.selectTo(selectionStart.row, selectionStart.column + selectedCount); + } else { + // If the joined line had something in it, start the cursor at that something + firstLineEndCol = editor.session.doc.getLine(selectionStart.row).length > firstLineEndCol ? (firstLineEndCol + 1) : firstLineEndCol; + editor.selection.moveCursorTo(selectionStart.row, firstLineEndCol); + } }, multiSelectAction: "forEach" }, { From 03eee835df3e62ecf67c5f65dbc1d4ff8e35df23 Mon Sep 17 00:00:00 2001 From: AMiniLegend Date: Wed, 21 May 2014 16:39:47 -0400 Subject: [PATCH 3/3] Updated ExpandToLine, JoinLines, and InvertSelection based on feedback. --- lib/ace/commands/default_commands.js | 47 +++++++++++----------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/lib/ace/commands/default_commands.js b/lib/ace/commands/default_commands.js index 5cd9f61f..77cdfc61 100644 --- a/lib/ace/commands/default_commands.js +++ b/lib/ace/commands/default_commands.js @@ -606,39 +606,33 @@ exports.commands = [{ }, { 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); + exec: function(editor) { + var range = editor.selection.getRange(); + + range.start.column = range.end.column = 0; + range.end.row++; + editor.selection.setRange(range, false); }, multiSelectAction: "forEach", - scrollIntoView: "cursor" + scrollIntoView: "cursor", + readOnly: true }, { name: "joinlines", bindKey: bindKey(null, null), - exec: function(editor) { + 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 firstLineEndCol = editor.session.doc.getLine(selectionStart.row).length var selectedText = editor.session.doc.getTextRange(editor.selection.getRange()); + var selectedCount = selectedText.replace(/\n\s*/, " ").length; var insertLine = editor.session.doc.getLine(selectionStart.row); - selectedText = selectedText.replace(/\n\s*/, " "); - var selectedCount = selectedText.length; - for (var i = selectionStart.row + 1; i <= selectionEnd.row + 1; i++) { var curLine = lang.stringTrimLeft(lang.stringTrimRight(editor.session.doc.getLine(i))); - if (curLine.length !== 0) { - curLine = " " + curLine; + curLine = " " + curLine; } - insertLine += curLine; }; @@ -660,17 +654,16 @@ exports.commands = [{ editor.selection.moveCursorTo(selectionStart.row, firstLineEndCol); } }, - multiSelectAction: "forEach" + multiSelectAction: "forEach", + readOnly: true }, { name: "invertSelection", bindKey: bindKey(null, null), - exec: function(editor) { + 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) { @@ -694,20 +687,16 @@ exports.commands = [{ 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); - } + }, + readOnly: true, + scrollIntoView: "none" }]; });