fix move lines with multiple cursors

This commit is contained in:
nightwing 2013-03-03 16:20:14 +04:00
commit f6d88bc10c
4 changed files with 159 additions and 144 deletions

View file

@ -359,23 +359,19 @@ 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"),

View file

@ -3,7 +3,7 @@
* *
* Copyright (c) 2010, Ajax.org B.V. * Copyright (c) 2010, Ajax.org B.V.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright * * Redistributions of source code must retain the above copyright
@ -14,7 +14,7 @@
* * Neither the name of Ajax.org B.V. nor the * * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products * names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission. * derived from this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@ -51,9 +51,9 @@ var config = require("./config");
/** /**
* *
* *
* The main entry point into the Ace functionality. * The main entry point into the Ace functionality.
* *
* The `Editor` manages the [[EditSession]] (which manages [[Document]]s), as well as the [[VirtualRenderer]], which draws everything to the screen. * The `Editor` manages the [[EditSession]] (which manages [[Document]]s), as well as the [[VirtualRenderer]], which draws everything to the screen.
* *
* Event sessions dealing with the mouse and keyboard are bubbled up from `Document` to the `Editor`, which decides what to do with them. * Event sessions dealing with the mouse and keyboard are bubbled up from `Document` to the `Editor`, which decides what to do with them.
* @class Editor * @class Editor
@ -100,7 +100,7 @@ var Editor = function(renderer, session) {
* Sets a new key handler, such as "vim" or "windows". * Sets a new key handler, such as "vim" or "windows".
* @param {String} keyboardHandler The new key handler * @param {String} keyboardHandler The new key handler
* *
* *
**/ **/
this.setKeyboardHandler = function(keyboardHandler) { this.setKeyboardHandler = function(keyboardHandler) {
if (typeof keyboardHandler == "string" && keyboardHandler) { if (typeof keyboardHandler == "string" && keyboardHandler) {
@ -116,11 +116,11 @@ var Editor = function(renderer, session) {
} }
}; };
/** /**
* Returns the keyboard handler, such as "vim" or "windows". * Returns the keyboard handler, such as "vim" or "windows".
* *
* @returns {String} * @returns {String}
* *
**/ **/
this.getKeyboardHandler = function() { this.getKeyboardHandler = function() {
return this.keyBinding.getKeyboardHandler(); return this.keyBinding.getKeyboardHandler();
@ -247,7 +247,7 @@ var Editor = function(renderer, session) {
return this.session; return this.session;
}; };
/** /**
* Sets the current document to `val`. * Sets the current document to `val`.
* @param {String} val The new value to set for the document * @param {String} val The new value to set for the document
* @param {Number} cursorPos Where to set the new value. `undefined` or 0 is selectAll, -1 is at the document start, and 1 is at the end * @param {Number} cursorPos Where to set the new value. `undefined` or 0 is selectAll, -1 is at the document start, and 1 is at the end
@ -268,7 +268,7 @@ var Editor = function(renderer, session) {
return val; return val;
}; };
/** /**
* Returns the current session's content. * Returns the current session's content.
* *
* @returns {String} * @returns {String}
@ -279,7 +279,7 @@ var Editor = function(renderer, session) {
}; };
/** /**
* *
* Returns the currently highlighted selection. * Returns the currently highlighted selection.
* @returns {String} The highlighted selection * @returns {String} The highlighted selection
**/ **/
@ -287,11 +287,11 @@ var Editor = function(renderer, session) {
return this.selection; return this.selection;
}; };
/** /**
* {:VirtualRenderer.onResize} * {:VirtualRenderer.onResize}
* @param {Boolean} force If `true`, recomputes the size, even if the height and width haven't changed * @param {Boolean} force If `true`, recomputes the size, even if the height and width haven't changed
* *
* *
* @related VirtualRenderer.onResize * @related VirtualRenderer.onResize
**/ **/
this.resize = function(force) { this.resize = function(force) {
@ -308,9 +308,9 @@ var Editor = function(renderer, session) {
this.renderer.setTheme(theme); this.renderer.setTheme(theme);
}; };
/** /**
* {:VirtualRenderer.getTheme} * {:VirtualRenderer.getTheme}
* *
* @returns {String} The set theme * @returns {String} The set theme
* @related VirtualRenderer.getTheme * @related VirtualRenderer.getTheme
**/ **/
@ -322,14 +322,14 @@ var Editor = function(renderer, session) {
* {:VirtualRenderer.setStyle} * {:VirtualRenderer.setStyle}
* @param {String} style A class name * @param {String} style A class name
* *
* *
* @related VirtualRenderer.setStyle * @related VirtualRenderer.setStyle
**/ **/
this.setStyle = function(style) { this.setStyle = function(style) {
this.renderer.setStyle(style); this.renderer.setStyle(style);
}; };
/** /**
* {:VirtualRenderer.unsetStyle} * {:VirtualRenderer.unsetStyle}
* @related VirtualRenderer.unsetStyle * @related VirtualRenderer.unsetStyle
**/ **/
@ -340,8 +340,8 @@ var Editor = function(renderer, session) {
/** /**
* Set a new font size (in pixels) for the editor text. * Set a new font size (in pixels) for the editor text.
* @param {String} size A font size ( _e.g._ "12px") * @param {String} size A font size ( _e.g._ "12px")
* *
* *
**/ **/
this.setFontSize = function(size) { this.setFontSize = function(size) {
if (typeof size == "number") if (typeof size == "number")
@ -375,7 +375,7 @@ var Editor = function(renderer, session) {
}; };
/** /**
* *
* Brings the current `textInput` into focus. * Brings the current `textInput` into focus.
**/ **/
this.focus = function() { this.focus = function() {
@ -398,7 +398,7 @@ var Editor = function(renderer, session) {
}; };
/** /**
* *
* Blurs the current `textInput`. * Blurs the current `textInput`.
**/ **/
this.blur = function() { this.blur = function() {
@ -407,9 +407,9 @@ var Editor = function(renderer, session) {
/** /**
* Emitted once the editor comes into focus. * Emitted once the editor comes into focus.
* @event focus * @event focus
* *
* *
**/ **/
this.onFocus = function() { this.onFocus = function() {
if (this.$isFocused) if (this.$isFocused)
@ -423,8 +423,8 @@ var Editor = function(renderer, session) {
/** /**
* Emitted once the editor has been blurred. * Emitted once the editor has been blurred.
* @event blur * @event blur
* *
* *
**/ **/
this.onBlur = function() { this.onBlur = function() {
if (!this.$isFocused) if (!this.$isFocused)
@ -440,12 +440,12 @@ var Editor = function(renderer, session) {
}; };
/** /**
* Emitted whenever the document is changed. * Emitted whenever the document is changed.
* @event change * @event change
* @param {Object} e Contains a single property, `data`, which has the delta of changes * @param {Object} e Contains a single property, `data`, which has the delta of changes
* *
* *
* *
**/ **/
this.onDocumentChange = function(e) { this.onDocumentChange = function(e) {
var delta = e.data; var delta = e.data;
@ -473,14 +473,14 @@ var Editor = function(renderer, session) {
this.onScrollTopChange = function() { this.onScrollTopChange = function() {
this.renderer.scrollToY(this.session.getScrollTop()); this.renderer.scrollToY(this.session.getScrollTop());
}; };
this.onScrollLeftChange = function() { this.onScrollLeftChange = function() {
this.renderer.scrollToX(this.session.getScrollLeft()); this.renderer.scrollToX(this.session.getScrollLeft());
}; };
/** /**
* Emitted when the selection changes. * Emitted when the selection changes.
* *
**/ **/
this.onCursorChange = function() { this.onCursorChange = function() {
this.$cursorChange(); this.$cursorChange();
@ -508,7 +508,7 @@ var Editor = function(renderer, session) {
session.$highlightLineMarker = null; session.$highlightLineMarker = null;
} else if (!session.$highlightLineMarker && highlight) { } else if (!session.$highlightLineMarker && highlight) {
var range = new Range(highlight.row, highlight.column, highlight.row, Infinity); var range = new Range(highlight.row, highlight.column, highlight.row, Infinity);
range.id = session.addMarker(range, "ace_active-line", "screenLine"); range.id = session.addMarker(range, "ace_active-line", "screenLine");
session.$highlightLineMarker = range; session.$highlightLineMarker = range;
} else if (highlight) { } else if (highlight) {
session.$highlightLineMarker.start.row = highlight.row; session.$highlightLineMarker.start.row = highlight.row;
@ -536,7 +536,7 @@ var Editor = function(renderer, session) {
var re = this.$highlightSelectedWord && this.$getSelectionHighLightRegexp() var re = this.$highlightSelectedWord && this.$getSelectionHighLightRegexp()
this.session.highlight(re); this.session.highlight(re);
this._emit("changeSelection"); this._emit("changeSelection");
}; };
@ -616,10 +616,10 @@ var Editor = function(renderer, session) {
/** /**
* Emitted when text is copied. * Emitted when text is copied.
* @event copy * @event copy
* @param {String} text The copied text * @param {String} text The copied text
* *
* *
**/ **/
/** /**
* *
@ -665,7 +665,7 @@ var Editor = function(renderer, session) {
**/ **/
this.onPaste = function(text) { this.onPaste = function(text) {
// todo this should change when paste becomes a command // todo this should change when paste becomes a command
if (this.$readOnly) if (this.$readOnly)
return; return;
this._emit("paste", text); this._emit("paste", text);
this.insert(text); this.insert(text);
@ -679,8 +679,8 @@ var Editor = function(renderer, session) {
/** /**
* Inserts `text` into wherever the cursor is pointing. * Inserts `text` into wherever the cursor is pointing.
* @param {String} text The new text to add * @param {String} text The new text to add
* *
* *
**/ **/
this.insert = function(text) { this.insert = function(text) {
var session = this.session; var session = this.session;
@ -780,10 +780,10 @@ var Editor = function(renderer, session) {
this.keyBinding.onCommandKey(e, hashId, keyCode); this.keyBinding.onCommandKey(e, hashId, keyCode);
}; };
/** /**
* Pass in `true` to enable overwrites in your session, or `false` to disable. If overwrites is enabled, any text you enter will type over any text after it. If the value of `overwrite` changes, this function also emites the `changeOverwrite` event. * Pass in `true` to enable overwrites in your session, or `false` to disable. If overwrites is enabled, any text you enter will type over any text after it. If the value of `overwrite` changes, this function also emites the `changeOverwrite` event.
* @param {Boolean} overwrite Defines wheter or not to set overwrites * @param {Boolean} overwrite Defines wheter or not to set overwrites
* *
* *
* @related EditSession.setOverwrite * @related EditSession.setOverwrite
**/ **/
@ -791,7 +791,7 @@ var Editor = function(renderer, session) {
this.session.setOverwrite(overwrite); this.session.setOverwrite(overwrite);
}; };
/** /**
* Returns `true` if overwrites are enabled; `false` otherwise. * Returns `true` if overwrites are enabled; `false` otherwise.
* @returns {Boolean} * @returns {Boolean}
* @related EditSession.getOverwrite * @related EditSession.getOverwrite
@ -800,7 +800,7 @@ var Editor = function(renderer, session) {
return this.session.getOverwrite(); return this.session.getOverwrite();
}; };
/** /**
* Sets the value of overwrite to the opposite of whatever it currently is. * Sets the value of overwrite to the opposite of whatever it currently is.
* @related EditSession.toggleOverwrite * @related EditSession.toggleOverwrite
**/ **/
@ -861,7 +861,7 @@ var Editor = function(renderer, session) {
this.getSelectionStyle = function() { this.getSelectionStyle = function() {
return this.getOption("selectionStyle"); return this.getOption("selectionStyle");
}; };
/** /**
* Determines whether or not the current line should be highlighted. * Determines whether or not the current line should be highlighted.
* @param {Boolean} shouldHighlight Set to `true` to highlight the current line * @param {Boolean} shouldHighlight Set to `true` to highlight the current line
@ -911,7 +911,7 @@ var Editor = function(renderer, session) {
/** /**
* If `showInvisibiles` is set to `true`, invisible characters—like spaces or new lines—are show in the editor. * If `showInvisibiles` is set to `true`, invisible characters—like spaces or new lines—are show in the editor.
* @param {Boolean} showInvisibles Specifies whether or not to show invisible characters * @param {Boolean} showInvisibles Specifies whether or not to show invisible characters
* *
**/ **/
this.setShowInvisibles = function(showInvisibles) { this.setShowInvisibles = function(showInvisibles) {
this.renderer.setShowInvisibles(showInvisibles); this.renderer.setShowInvisibles(showInvisibles);
@ -936,7 +936,7 @@ var Editor = function(renderer, session) {
/** /**
* If `showPrintMargin` is set to `true`, the print margin is shown in the editor. * If `showPrintMargin` is set to `true`, the print margin is shown in the editor.
* @param {Boolean} showPrintMargin Specifies whether or not to show the print margin * @param {Boolean} showPrintMargin Specifies whether or not to show the print margin
* *
**/ **/
this.setShowPrintMargin = function(showPrintMargin) { this.setShowPrintMargin = function(showPrintMargin) {
this.renderer.setShowPrintMargin(showPrintMargin); this.renderer.setShowPrintMargin(showPrintMargin);
@ -970,7 +970,7 @@ var Editor = function(renderer, session) {
/** /**
* If `readOnly` is true, then the editor is set to read-only mode, and none of the content can change. * If `readOnly` is true, then the editor is set to read-only mode, and none of the content can change.
* @param {Boolean} readOnly Specifies whether the editor can be modified or not * @param {Boolean} readOnly Specifies whether the editor can be modified or not
* *
**/ **/
this.setReadOnly = function(readOnly) { this.setReadOnly = function(readOnly) {
this.setOption("readOnly", readOnly); this.setOption("readOnly", readOnly);
@ -987,7 +987,7 @@ var Editor = function(renderer, session) {
/** /**
* Specifies whether to use behaviors or not. ["Behaviors" in this case is the auto-pairing of special characters, like quotation marks, parenthesis, or brackets.]{: #BehaviorsDef} * Specifies whether to use behaviors or not. ["Behaviors" in this case is the auto-pairing of special characters, like quotation marks, parenthesis, or brackets.]{: #BehaviorsDef}
* @param {Boolean} enabled Enables or disables behaviors * @param {Boolean} enabled Enables or disables behaviors
* *
**/ **/
this.setBehavioursEnabled = function (enabled) { this.setBehavioursEnabled = function (enabled) {
this.setOption("behavioursEnabled", enabled); this.setOption("behavioursEnabled", enabled);
@ -995,7 +995,7 @@ var Editor = function(renderer, session) {
/** /**
* Returns `true` if the behaviors are currently enabled. {:BehaviorsDef} * Returns `true` if the behaviors are currently enabled. {:BehaviorsDef}
* *
* @returns {Boolean} * @returns {Boolean}
**/ **/
this.getBehavioursEnabled = function () { this.getBehavioursEnabled = function () {
@ -1006,7 +1006,7 @@ var Editor = function(renderer, session) {
* Specifies whether to use wrapping behaviors or not, i.e. automatically wrapping the selection with characters such as brackets * Specifies whether to use wrapping behaviors or not, i.e. automatically wrapping the selection with characters such as brackets
* when such a character is typed in. * when such a character is typed in.
* @param {Boolean} enabled Enables or disables wrapping behaviors * @param {Boolean} enabled Enables or disables wrapping behaviors
* *
**/ **/
this.setWrapBehavioursEnabled = function (enabled) { this.setWrapBehavioursEnabled = function (enabled) {
this.setOption("wrapBehavioursEnabled", enabled); this.setOption("wrapBehavioursEnabled", enabled);
@ -1046,7 +1046,7 @@ var Editor = function(renderer, session) {
/** /**
* Removes words of text from the editor. A "word" is defined as a string of characters bookended by whitespace. * Removes words of text from the editor. A "word" is defined as a string of characters bookended by whitespace.
* @param {String} dir The direction of the deletion to occur, either "left" or "right" * @param {String} dir The direction of the deletion to occur, either "left" or "right"
* *
**/ **/
this.remove = function(dir) { this.remove = function(dir) {
if (this.selection.isEmpty()){ if (this.selection.isEmpty()){
@ -1191,7 +1191,7 @@ var Editor = function(renderer, session) {
/** /**
* Inserts an indentation into the current cursor position or indents the selected lines. * Inserts an indentation into the current cursor position or indents the selected lines.
* *
* @related EditSession.indentRows * @related EditSession.indentRows
**/ **/
this.indent = function() { this.indent = function() {
@ -1261,7 +1261,7 @@ var Editor = function(renderer, session) {
}; };
/** /**
* *
* Given the currently selected range, this function either comments all the lines, or uncomments all of them. * Given the currently selected range, this function either comments all the lines, or uncomments all of them.
**/ **/
this.toggleCommentLines = function() { this.toggleCommentLines = function() {
@ -1293,7 +1293,7 @@ var Editor = function(renderer, session) {
} }
return null; return null;
}; };
/** /**
* If the character before the cursor is a number, this functions changes its value by `amount`. * If the character before the cursor is a number, this functions changes its value by `amount`.
* @param {Number} amount The value to change the numeral by (can be negative to decrease value) * @param {Number} amount The value to change the numeral by (can be negative to decrease value)
@ -1318,14 +1318,14 @@ var Editor = function(renderer, session) {
var t = parseFloat(nr.value); var t = parseFloat(nr.value);
t *= Math.pow(10, decimals); t *= Math.pow(10, decimals);
if(fp !== nr.end && column < fp){ if(fp !== nr.end && column < fp){
amount *= Math.pow(10, nr.end - column - 1); amount *= Math.pow(10, nr.end - column - 1);
} else { } else {
amount *= Math.pow(10, nr.end - column); amount *= Math.pow(10, nr.end - column);
} }
t += amount; t += amount;
t /= Math.pow(10, decimals); t /= Math.pow(10, decimals);
var nnr = t.toFixed(decimals); var nnr = t.toFixed(decimals);
@ -1340,8 +1340,8 @@ var Editor = function(renderer, session) {
} }
} }
}; };
/** /**
* Removes all the lines in the current selection * Removes all the lines in the current selection
* @related EditSession.remove * @related EditSession.remove
**/ **/
@ -1367,17 +1367,16 @@ var Editor = function(renderer, session) {
var row = range.start.row; var row = range.start.row;
doc.duplicateLines(row, row); doc.duplicateLines(row, row);
} else { } else {
var reverse = sel.isBackwards() var point = reverse ? range.start : range.end;
var point = sel.isBackwards() ? range.start : range.end;
var endPoint = doc.insert(point, doc.getTextRange(range), false); var endPoint = doc.insert(point, doc.getTextRange(range), false);
range.start = point; range.start = point;
range.end = endPoint; range.end = endPoint;
sel.setSelectionRange(range, reverse) sel.setSelectionRange(range, reverse)
} }
}; };
/** /**
* Shifts all the selected lines down one row. * Shifts all the selected lines down one row.
* *
* @returns {Number} On success, it returns -1. * @returns {Number} On success, it returns -1.
@ -1389,7 +1388,7 @@ var Editor = function(renderer, session) {
}); });
}; };
/** /**
* Shifts all the selected lines up one row. * Shifts all the selected lines up one row.
* @returns {Number} On success, it returns -1. * @returns {Number} On success, it returns -1.
* @related EditSession.moveLinesDown * @related EditSession.moveLinesDown
@ -1400,28 +1399,25 @@ var Editor = function(renderer, session) {
}); });
}; };
/** /**
* Moves a range of text from the given range to the given position. `toPosition` is an object that looks like this: * Moves a range of text from the given range to the given position. `toPosition` is an object that looks like this:
* ```json * ```json
* { row: newRowLocation, column: newColumnLocation } * { row: newRowLocation, column: newColumnLocation }
* ``` * ```
* @param {Range} fromRange The range of text you want moved within the document * @param {Range} fromRange The range of text you want moved within the document
* @param {Object} toPosition The location (row and column) where you want to move the text to * @param {Object} toPosition The location (row and column) where you want to move the text to
* *
* @returns {Range} The new range where the text was moved to. * @returns {Range} The new range where the text was moved to.
* @related EditSession.moveText * @related EditSession.moveText
**/ **/
this.moveText = function(range, toPosition) { this.moveText = function(range, toPosition) {
if (this.$readOnly)
return null;
return this.session.moveText(range, toPosition); return this.session.moveText(range, toPosition);
}; };
/** /**
* Copies all the selected lines up one row. * Copies all the selected lines up one row.
* @returns {Number} On success, returns 0. * @returns {Number} On success, returns 0.
* *
**/ **/
this.copyLinesUp = function() { this.copyLinesUp = function() {
this.$moveLines(function(firstRow, lastRow) { this.$moveLines(function(firstRow, lastRow) {
@ -1430,7 +1426,7 @@ var Editor = function(renderer, session) {
}); });
}; };
/** /**
* Copies all the selected lines down one row. * Copies all the selected lines down one row.
* @returns {Number} On success, returns the number of new rows added; in other words, `lastRow - firstRow + 1`. * @returns {Number} On success, returns the number of new rows added; in other words, `lastRow - firstRow + 1`.
* @related EditSession.duplicateLines * @related EditSession.duplicateLines
@ -1446,29 +1442,43 @@ var Editor = function(renderer, session) {
/** /**
* Executes a specific function, which can be anything that manipulates selected lines, such as copying them, duplicating them, or shifting them. * Executes a specific function, which can be anything that manipulates selected lines, such as copying them, duplicating them, or shifting them.
* @param {Function} mover A method to call on each selected row * @param {Function} mover A method to call on each selected row
* *
* *
**/ **/
this.$moveLines = function(mover) { this.$moveLines = function(mover) {
var rows = this.$getSelectedRows();
var selection = this.selection; var selection = this.selection;
if (!selection.isMultiLine()) { if (!selection.inMultiSelectMode || this.inVirtualSelectionMode) {
var range = selection.getRange(); var range = selection.toOrientedRange();
var reverse = selection.isBackwards(); var rows = this.$getSelectedRows(range);
} var linesMoved = mover.call(this, rows.first, rows.last);
range.moveBy(linesMoved, 0);
var linesMoved = mover.call(this, rows.first, rows.last); selection.fromOrientedRange(range);
} else {
if (range) { var ranges = selection.rangeList.ranges;
range.start.row += linesMoved; selection.rangeList.detach(this.session);
range.end.row += linesMoved;
selection.setSelectionRange(range, reverse); for (var i = ranges.length; i--; ) {
} var rangeIndex = i;
else { var rows = ranges[i].collapseRows();
selection.setSelectionAnchor(rows.last+linesMoved+1, 0); var last = rows.end.row;
selection.$moveSelection(function() { var first = rows.start.row;
selection.moveCursorTo(rows.first+linesMoved, 0); while (i--) {
}); var rows = ranges[i].collapseRows();
if (first - rows.end.row <= 1)
first = rows.end.row;
else
break;
}
i++;
var linesMoved = mover.call(this, first, last);
while (rangeIndex >= i) {
ranges[rangeIndex].moveBy(linesMoved, 0);
rangeIndex--;
}
}
selection.fromOrientedRange(selection.ranges[0]);
selection.rangeList.attach(this.session);
} }
}; };
@ -1502,7 +1512,7 @@ var Editor = function(renderer, session) {
this.renderer.hideComposition(); this.renderer.hideComposition();
}; };
/** /**
* {:VirtualRenderer.getFirstVisibleRow} * {:VirtualRenderer.getFirstVisibleRow}
* *
* @returns {Number} * @returns {Number}
@ -1512,7 +1522,7 @@ var Editor = function(renderer, session) {
return this.renderer.getFirstVisibleRow(); return this.renderer.getFirstVisibleRow();
}; };
/** /**
* {:VirtualRenderer.getLastVisibleRow} * {:VirtualRenderer.getLastVisibleRow}
* *
* @returns {Number} * @returns {Number}
@ -1525,7 +1535,7 @@ var Editor = function(renderer, session) {
/** /**
* Indicates if the row is currently visible on the screen. * Indicates if the row is currently visible on the screen.
* @param {Number} row The row to check * @param {Number} row The row to check
* *
* @returns {Boolean} * @returns {Boolean}
**/ **/
this.isRowVisible = function(row) { this.isRowVisible = function(row) {
@ -1535,8 +1545,8 @@ var Editor = function(renderer, session) {
/** /**
* Indicates if the entire row is currently visible on the screen. * Indicates if the entire row is currently visible on the screen.
* @param {Number} row The row to check * @param {Number} row The row to check
* *
* *
* @returns {Boolean} * @returns {Boolean}
**/ **/
this.isRowFullyVisible = function(row) { this.isRowFullyVisible = function(row) {
@ -1618,7 +1628,7 @@ var Editor = function(renderer, session) {
this.$moveByPage(-1); this.$moveByPage(-1);
}; };
/** /**
* Moves the editor to the specified row. * Moves the editor to the specified row.
* @related VirtualRenderer.scrollToRow * @related VirtualRenderer.scrollToRow
**/ **/
@ -1626,14 +1636,14 @@ var Editor = function(renderer, session) {
this.renderer.scrollToRow(row); this.renderer.scrollToRow(row);
}; };
/** /**
* Scrolls to a line. If `center` is `true`, it puts the line in middle of screen (or attempts to). * Scrolls to a line. If `center` is `true`, it puts the line in middle of screen (or attempts to).
* @param {Number} line The line to scroll to * @param {Number} line The line to scroll to
* @param {Boolean} center If `true` * @param {Boolean} center If `true`
* @param {Boolean} animate If `true` animates scrolling * @param {Boolean} animate If `true` animates scrolling
* @param {Function} callback Function to be called when the animation has finished * @param {Function} callback Function to be called when the animation has finished
* *
* *
* @related VirtualRenderer.scrollToLine * @related VirtualRenderer.scrollToLine
**/ **/
this.scrollToLine = function(line, center, animate, callback) { this.scrollToLine = function(line, center, animate, callback) {
@ -1652,9 +1662,9 @@ var Editor = function(renderer, session) {
this.renderer.alignCursor(pos, 0.5); this.renderer.alignCursor(pos, 0.5);
}; };
/** /**
* Gets the current position of the cursor. * Gets the current position of the cursor.
* @returns {Object} An object that looks something like this: * @returns {Object} An object that looks something like this:
* *
* ```json * ```json
* { row: currRow, column: currCol } * { row: currRow, column: currCol }
@ -1666,7 +1676,7 @@ var Editor = function(renderer, session) {
return this.selection.getCursor(); return this.selection.getCursor();
}; };
/** /**
* Returns the screen position of the cursor. * Returns the screen position of the cursor.
* @returns {Number} * @returns {Number}
* @related EditSession.documentToScreenPosition * @related EditSession.documentToScreenPosition
@ -1675,7 +1685,7 @@ var Editor = function(renderer, session) {
return this.session.documentToScreenPosition(this.getCursorPosition()); return this.session.documentToScreenPosition(this.getCursorPosition());
}; };
/** /**
* {:Selection.getRange} * {:Selection.getRange}
* @returns {Range} * @returns {Range}
* @related Selection.getRange * @related Selection.getRange
@ -1685,7 +1695,7 @@ var Editor = function(renderer, session) {
}; };
/** /**
* Selects all the text in editor. * Selects all the text in editor.
* @related Selection.selectAll * @related Selection.selectAll
**/ **/
@ -1695,7 +1705,7 @@ var Editor = function(renderer, session) {
this.$blockScrolling -= 1; this.$blockScrolling -= 1;
}; };
/** /**
* {:Selection.clearSelection} * {:Selection.clearSelection}
* @related Selection.clearSelection * @related Selection.clearSelection
**/ **/
@ -1703,7 +1713,7 @@ var Editor = function(renderer, session) {
this.selection.clearSelection(); this.selection.clearSelection();
}; };
/** /**
* Moves the cursor to the specified row and column. Note that this does not de-select the current selection. * Moves the cursor to the specified row and column. Note that this does not de-select the current selection.
* @param {Number} row The new row number * @param {Number} row The new row number
* @param {Number} column The new column number * @param {Number} column The new column number
@ -1715,10 +1725,10 @@ var Editor = function(renderer, session) {
this.selection.moveCursorTo(row, column); this.selection.moveCursorTo(row, column);
}; };
/** /**
* Moves the cursor to the position indicated by `pos.row` and `pos.column`. * Moves the cursor to the position indicated by `pos.row` and `pos.column`.
* @param {Object} pos An object with two properties, row and column * @param {Object} pos An object with two properties, row and column
* *
* *
* @related Selection.moveCursorToPosition * @related Selection.moveCursorToPosition
**/ **/
@ -1726,7 +1736,7 @@ var Editor = function(renderer, session) {
this.selection.moveCursorToPosition(pos); this.selection.moveCursorToPosition(pos);
}; };
/** /**
* Moves the cursor's row and column to the next matching bracket. * Moves the cursor's row and column to the next matching bracket.
* *
**/ **/
@ -1746,7 +1756,7 @@ var Editor = function(renderer, session) {
if (pos.row == cursor.row && Math.abs(pos.column - cursor.column) < 2) if (pos.row == cursor.row && Math.abs(pos.column - cursor.column) < 2)
range = this.session.getBracketRange(pos); range = this.session.getBracketRange(pos);
} }
pos = range && range.cursor || pos; pos = range && range.cursor || pos;
if (pos) { if (pos) {
if (select) { if (select) {
@ -1766,7 +1776,7 @@ var Editor = function(renderer, session) {
* @param {Number} lineNumber The line number to go to * @param {Number} lineNumber The line number to go to
* @param {Number} column A column number to go to * @param {Number} column A column number to go to
* @param {Boolean} animate If `true` animates scolling * @param {Boolean} animate If `true` animates scolling
* *
**/ **/
this.gotoLine = function(lineNumber, column, animate) { this.gotoLine = function(lineNumber, column, animate) {
this.selection.clearSelection(); this.selection.clearSelection();
@ -1780,7 +1790,7 @@ var Editor = function(renderer, session) {
this.scrollToLine(lineNumber - 1, true, animate); this.scrollToLine(lineNumber - 1, true, animate);
}; };
/** /**
* Moves the cursor to the specified row and column. Note that this does de-select the current selection. * Moves the cursor to the specified row and column. Note that this does de-select the current selection.
* @param {Number} row The new row number * @param {Number} row The new row number
* @param {Number} column The new column number * @param {Number} column The new column number
@ -1796,8 +1806,8 @@ var Editor = function(renderer, session) {
/** /**
* Moves the cursor up in the document the specified number of times. Note that this does de-select the current selection. * Moves the cursor up in the document the specified number of times. Note that this does de-select the current selection.
* @param {Number} times The number of times to change navigation * @param {Number} times The number of times to change navigation
* *
* *
**/ **/
this.navigateUp = function(times) { this.navigateUp = function(times) {
this.selection.clearSelection(); this.selection.clearSelection();
@ -1808,8 +1818,8 @@ var Editor = function(renderer, session) {
/** /**
* Moves the cursor down in the document the specified number of times. Note that this does de-select the current selection. * Moves the cursor down in the document the specified number of times. Note that this does de-select the current selection.
* @param {Number} times The number of times to change navigation * @param {Number} times The number of times to change navigation
* *
* *
**/ **/
this.navigateDown = function(times) { this.navigateDown = function(times) {
this.selection.clearSelection(); this.selection.clearSelection();
@ -1820,8 +1830,8 @@ var Editor = function(renderer, session) {
/** /**
* Moves the cursor left in the document the specified number of times. Note that this does de-select the current selection. * Moves the cursor left in the document the specified number of times. Note that this does de-select the current selection.
* @param {Number} times The number of times to change navigation * @param {Number} times The number of times to change navigation
* *
* *
**/ **/
this.navigateLeft = function(times) { this.navigateLeft = function(times) {
if (!this.selection.isEmpty()) { if (!this.selection.isEmpty()) {
@ -1840,8 +1850,8 @@ var Editor = function(renderer, session) {
/** /**
* Moves the cursor right in the document the specified number of times. Note that this does de-select the current selection. * Moves the cursor right in the document the specified number of times. Note that this does de-select the current selection.
* @param {Number} times The number of times to change navigation * @param {Number} times The number of times to change navigation
* *
* *
**/ **/
this.navigateRight = function(times) { this.navigateRight = function(times) {
if (!this.selection.isEmpty()) { if (!this.selection.isEmpty()) {
@ -1858,7 +1868,7 @@ var Editor = function(renderer, session) {
}; };
/** /**
* *
* Moves the cursor to the start of the current line. Note that this does de-select the current selection. * Moves the cursor to the start of the current line. Note that this does de-select the current selection.
**/ **/
this.navigateLineStart = function() { this.navigateLineStart = function() {
@ -1867,7 +1877,7 @@ var Editor = function(renderer, session) {
}; };
/** /**
* *
* Moves the cursor to the end of the current line. Note that this does de-select the current selection. * Moves the cursor to the end of the current line. Note that this does de-select the current selection.
**/ **/
this.navigateLineEnd = function() { this.navigateLineEnd = function() {
@ -1876,7 +1886,7 @@ var Editor = function(renderer, session) {
}; };
/** /**
* *
* Moves the cursor to the end of the current file. Note that this does de-select the current selection. * Moves the cursor to the end of the current file. Note that this does de-select the current selection.
**/ **/
this.navigateFileEnd = function() { this.navigateFileEnd = function() {
@ -1887,7 +1897,7 @@ var Editor = function(renderer, session) {
}; };
/** /**
* *
* Moves the cursor to the start of the current file. Note that this does de-select the current selection. * Moves the cursor to the start of the current file. Note that this does de-select the current selection.
**/ **/
this.navigateFileStart = function() { this.navigateFileStart = function() {
@ -1898,7 +1908,7 @@ var Editor = function(renderer, session) {
}; };
/** /**
* *
* Moves the cursor to the word immediately to the right of the current position. Note that this does de-select the current selection. * Moves the cursor to the word immediately to the right of the current position. Note that this does de-select the current selection.
**/ **/
this.navigateWordRight = function() { this.navigateWordRight = function() {
@ -1907,7 +1917,7 @@ var Editor = function(renderer, session) {
}; };
/** /**
* *
* Moves the cursor to the word immediately to the left of the current position. Note that this does de-select the current selection. * Moves the cursor to the word immediately to the left of the current position. Note that this does de-select the current selection.
**/ **/
this.navigateWordLeft = function() { this.navigateWordLeft = function() {
@ -1988,7 +1998,7 @@ var Editor = function(renderer, session) {
} }
}; };
/** /**
* {:Search.getOptions} For more information on `options`, see [[Search `Search`]]. * {:Search.getOptions} For more information on `options`, see [[Search `Search`]].
* @related Search.getOptions * @related Search.getOptions
* @returns {Object} * @returns {Object}
@ -1997,7 +2007,7 @@ var Editor = function(renderer, session) {
return this.$search.getOptions(); return this.$search.getOptions();
}; };
/** /**
* Attempts to find `needle` within the document. For more information on `options`, see [[Search `Search`]]. * Attempts to find `needle` within the document. For more information on `options`, see [[Search `Search`]].
* @param {String} needle The text to search for (optional) * @param {String} needle The text to search for (optional)
* @param {Object} options An object defining various search properties * @param {Object} options An object defining various search properties
@ -2045,7 +2055,7 @@ var Editor = function(renderer, session) {
this.selection.setRange(range); this.selection.setRange(range);
}; };
/** /**
* Performs another search for `needle` in the document. For more information on `options`, see [[Search `Search`]]. * Performs another search for `needle` in the document. For more information on `options`, see [[Search `Search`]].
* @param {Object} options search options * @param {Object} options search options
* @param {Boolean} animate If `true` animate scrolling * @param {Boolean} animate If `true` animate scrolling
@ -2057,7 +2067,7 @@ var Editor = function(renderer, session) {
this.find({skipCurrent: true, backwards: false}, options, animate); this.find({skipCurrent: true, backwards: false}, options, animate);
}; };
/** /**
* Performs a search for `needle` backwards. For more information on `options`, see [[Search `Search`]]. * Performs a search for `needle` backwards. For more information on `options`, see [[Search `Search`]].
* @param {Object} options search options * @param {Object} options search options
* @param {Boolean} animate If `true` animate scrolling * @param {Boolean} animate If `true` animate scrolling
@ -2081,7 +2091,7 @@ var Editor = function(renderer, session) {
this.renderer.animateScrolling(scrollTop); this.renderer.animateScrolling(scrollTop);
}; };
/** /**
* {:UndoManager.undo} * {:UndoManager.undo}
* @related UndoManager.undo * @related UndoManager.undo
**/ **/
@ -2092,7 +2102,7 @@ var Editor = function(renderer, session) {
this.renderer.scrollCursorIntoView(null, 0.5); this.renderer.scrollCursorIntoView(null, 0.5);
}; };
/** /**
* {:UndoManager.redo} * {:UndoManager.redo}
* @related UndoManager.redo * @related UndoManager.redo
**/ **/
@ -2103,16 +2113,16 @@ var Editor = function(renderer, session) {
this.renderer.scrollCursorIntoView(null, 0.5); this.renderer.scrollCursorIntoView(null, 0.5);
}; };
/** /**
* *
* Cleans up the entire editor. * Cleans up the entire editor.
**/ **/
this.destroy = function() { this.destroy = function() {
this.renderer.destroy(); this.renderer.destroy();
this._emit("destroy", this); this._emit("destroy", this);
}; };
/** /**
* Enables automatic scrolling of the cursor into view when editor itself is inside scrollable element * Enables automatic scrolling of the cursor into view when editor itself is inside scrollable element
* @param {Boolean} enable default true * @param {Boolean} enable default true
**/ **/

View file

@ -482,13 +482,13 @@ var Editor = require("./editor").Editor;
this.inVirtualSelectionMode = true; this.inVirtualSelectionMode = true;
for (var i = rangeList.ranges.length; i--;) { for (var i = rangeList.ranges.length; i--;) {
if ($byLines) { if ($byLines) {
while (i > 0 && rangeList.ranges[i].start.row == rangeList.ranges[i].end.row) while (i > 0 && rangeList.ranges[i].start.row == rangeList.ranges[i - 1].end.row)
i--; 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

@ -516,6 +516,15 @@ var Range = function(startRow, startColumn, endRow, endColumn) {
screenPosEnd.row, screenPosEnd.column screenPosEnd.row, screenPosEnd.column
); );
}; };
/* experimental */
this.moveBy = function(row, column) {
this.start.row += row;
this.start.column += column;
this.end.row += row;
this.end.column += column;
};
}).call(Range.prototype); }).call(Range.prototype);