From 2e6f12725bc01011c545c99c1f1c1da6065063d9 Mon Sep 17 00:00:00 2001 From: aldendaniels Date: Wed, 1 Jan 2014 00:41:45 -0600 Subject: [PATCH 001/476] #1744: Refactor document delta handling Refactor delta handling code to: - Combine the "insertText" and "insertLines" delta types into a single "insert" delta type - Combine the "removeText" and "removeLines" delta types into a single "remove" delta type - Make all document mutations in a single applyDelta function. - Add basic delta validation (more needed . . . see TODOs) - Rework anchor logic to handle new delta types (also simplified) - Rename "insert()" to "insertText()" and "remove()" to "removeText()" - Rename "insertLines()" to "insertFullLines()" and "removeLines()" to "removeFullLines()" See related issue for more information. All tests are passing and the changes appear functional under preliminary testing, but careful review and testing will be necessary. --- lib/ace/anchor.js | 108 +++--- lib/ace/anchor_test.js | 16 +- lib/ace/autocomplete.js | 2 +- lib/ace/background_tokenizer.js | 2 +- lib/ace/background_tokenizer_test.js | 2 +- lib/ace/commands/default_commands.js | 4 +- lib/ace/document.js | 516 +++++++++++++------------ lib/ace/document_test.js | 18 +- lib/ace/edit_session.js | 58 ++- lib/ace/edit_session/folding.js | 2 +- lib/ace/edit_session_test.js | 18 +- lib/ace/editor.js | 39 +- lib/ace/editor_change_document_test.js | 2 +- lib/ace/editor_navigation_test.js | 2 +- lib/ace/ext/chromevox.js | 4 +- lib/ace/ext/elastic_tabstops_lite.js | 2 +- lib/ace/ext/whitespace.js | 2 +- lib/ace/keyboard/vim/commands.js | 10 +- lib/ace/keyboard/vim/maps/motions.js | 6 +- lib/ace/layer/gutter.js | 4 +- lib/ace/line_widgets.js | 2 +- lib/ace/mode/javascript_test.js | 2 +- lib/ace/mode/text.js | 12 +- lib/ace/mouse/dragdrop_handler.js | 2 +- lib/ace/multi_select.js | 10 +- lib/ace/occur_test.js | 12 +- lib/ace/placeholder.js | 12 +- lib/ace/placeholder_test.js | 18 +- 28 files changed, 437 insertions(+), 450 deletions(-) diff --git a/lib/ace/anchor.js b/lib/ace/anchor.js index 3a62e632..91a62eb0 100644 --- a/lib/ace/anchor.js +++ b/lib/ace/anchor.js @@ -100,67 +100,55 @@ var Anchor = exports.Anchor = function(doc, row, column) { * **/ this.onChange = function(e) { - var delta = e.data; - var range = delta.range; - - if (range.start.row == range.end.row && range.start.row != this.row) - return; - - if (range.start.row > this.row) - return; - - if (range.start.row == this.row && range.start.column > this.column) - return; - - var row = this.row; - var column = this.column; - var start = range.start; - var end = range.end; - - if (delta.action === "insertText") { - if (start.row === row && start.column <= column) { - if (start.column === column && this.$insertRight) { - // do nothing - } else if (start.row === end.row) { - column += end.column - start.column; - } else { - column -= start.column; - row += end.row - start.row; - } - } else if (start.row !== end.row && start.row < row) { - row += end.row - start.row; - } - } else if (delta.action === "insertLines") { - if (start.row <= row) { - row += end.row - start.row; - } - } else if (delta.action === "removeText") { - if (start.row === row && start.column < column) { - if (end.column >= column) - column = start.column; - else - column = Math.max(0, column - (end.column - start.column)); - - } else if (start.row !== end.row && start.row < row) { - if (end.row === row) - column = Math.max(0, column - end.column) + start.column; - row -= (end.row - start.row); - } else if (end.row === row) { - row -= end.row - start.row; - column = Math.max(0, column - end.column) + start.column; - } - } else if (delta.action == "removeLines") { - if (start.row <= row) { - if (end.row <= row) - row -= end.row - start.row; - else { - row = start.row; - column = 0; - } - } + + function _pointsInOrder(point1, point2, equalPointsInOrder) + { + var bColIsAfter = equalPointsInOrder ? point1.column <= point2.column : point1.column < point2.column; + return (point1.row < point2.row) || (point1.row == point2.row && bColIsAfter); } - - this.setPosition(row, column, true); + + function getTransformedPoint(delta, point, moveIfEqual) + { + // Get delta info. + var deltaIsInsert = (delta.action == 'insert') + var deltaRowShift = (deltaIsInsert ? 1 : -1) * (delta.range.end.row - delta.range.start.row); + var deltaColShift = (deltaIsInsert ? 1 : -1) * (delta.range.end.column - delta.range.start.column); + var deltaStart = delta.range.start; + var deltaEnd = (deltaIsInsert ? deltaStart : delta.range.end); // Collapse insert range. + + // DELTA AFTER POINT: No change needed. + if (_pointsInOrder(point, deltaStart, moveIfEqual)) + { + return ( + { + row: point.row, + column: point.column + }); + } + + // DELTA BEFORE POINT: Move point by delta shift. + if (_pointsInOrder(deltaEnd, point, !moveIfEqual)) + { + return ( + { + row: point.row + deltaRowShift, + column: point.column + (point.row == deltaEnd.row ? deltaColShift : 0) + }); + } + + // DELTA ENVELOPS POINT (delete only): Move point to delta start. + if (delta.action != 'delete') + throw 'Delete action expected.'; + return ( + { + row: deltaStart.row, + column: deltaStart.column + }); + } + + var delta = e.data; + var point = getTransformedPoint(delta, {row: this.row, column: this.column}, this.$insertRight); + this.setPosition(point.row, point.column, true); }; /** diff --git a/lib/ace/anchor_test.js b/lib/ace/anchor_test.js index 2d7fcb63..0588c9b6 100644 --- a/lib/ace/anchor_test.js +++ b/lib/ace/anchor_test.js @@ -54,7 +54,7 @@ module.exports = { var doc = new Document("juhu\nkinners"); var anchor = new Anchor(doc, 1, 4); - doc.insert({row: 1, column: 1}, "123"); + doc.insertText({row: 1, column: 1}, "123"); assert.position(anchor.getPosition(), 1, 7); }, @@ -62,7 +62,7 @@ module.exports = { var doc = new Document("juhu\nkinners"); var anchor = new Anchor(doc, 1, 4); - doc.insertLines(1, ["123", "456"]); + doc.insertFullLines(1, ["123", "456"]); assert.position(anchor.getPosition(), 3, 4); }, @@ -70,7 +70,7 @@ module.exports = { var doc = new Document("juhu\nkinners"); var anchor = new Anchor(doc, 1, 4); - doc.insertNewLine({row: 0, column: 0}); + doc.insertLines({row: 0, column: 0}, ['', '']); assert.position(anchor.getPosition(), 2, 4); }, @@ -78,7 +78,7 @@ module.exports = { var doc = new Document("juhu\nkinners"); var anchor = new Anchor(doc, 1, 4); - doc.insertNewLine({row: 1, column: 2}); + doc.insertLines({row: 1, column: 2}, ['', '']); assert.position(anchor.getPosition(), 2, 2); }, @@ -110,7 +110,7 @@ module.exports = { var doc = new Document("juhu\n1\n2\nkinners"); var anchor = new Anchor(doc, 3, 4); - doc.removeLines(1, 2); + doc.removeFullLines(1, 2); assert.position(anchor.getPosition(), 1, 4); }, @@ -134,7 +134,7 @@ module.exports = { var doc = new Document("juhu\nkinners\n123"); var anchor = new Anchor(doc, 1, 5); - doc.removeLines(1, 1); + doc.removeFullLines(1, 1); assert.position(anchor.getPosition(), 1, 0); }, @@ -173,9 +173,9 @@ module.exports = { var doc = new Document("juhu\nkinners\n123"); var anchor = new Anchor(doc, 2, 4); - doc.removeLines(0, 3); + doc.removeFullLines(0, 3); assert.position(anchor.getPosition(), 0, 0); - doc.insertLines(0, ["a", "b", "c"]); + doc.insertFullLines(0, ["a", "b", "c"]); assert.position(anchor.getPosition(), 3, 0); assert.equal(doc.getValue(), "a\nb\nc\n"); } diff --git a/lib/ace/autocomplete.js b/lib/ace/autocomplete.js index 50154c01..2344aede 100644 --- a/lib/ace/autocomplete.js +++ b/lib/ace/autocomplete.js @@ -170,7 +170,7 @@ var Autocomplete = function() { "Ctrl-Down|Ctrl-End": function(editor) { editor.completer.goTo("end"); }, "Esc": function(editor) { editor.completer.detach(); }, - "Space": function(editor) { editor.completer.detach(); editor.insert(" ");}, + "Space": function(editor) { editor.completer.detach(); editor.insertText(" ");}, "Return": function(editor) { editor.completer.insertMatch(); }, "Shift-Return": function(editor) { editor.completer.insertMatch(true); }, "Tab": function(editor) { editor.completer.insertMatch(); }, diff --git a/lib/ace/background_tokenizer.js b/lib/ace/background_tokenizer.js index 217be1b3..cb3ffe23 100644 --- a/lib/ace/background_tokenizer.js +++ b/lib/ace/background_tokenizer.js @@ -183,7 +183,7 @@ var BackgroundTokenizer = function(tokenizer, editor) { if (len === 0) { this.lines[startRow] = null; - } else if (delta.action == "removeText" || delta.action == "removeLines") { + } else if (delta.action == "delete") { this.lines.splice(startRow, len + 1, null); this.states.splice(startRow, len + 1, null); } else { diff --git a/lib/ace/background_tokenizer_test.js b/lib/ace/background_tokenizer_test.js index 7a4cc78c..16c63132 100644 --- a/lib/ace/background_tokenizer_test.js +++ b/lib/ace/background_tokenizer_test.js @@ -70,7 +70,7 @@ module.exports = { forceTokenize(doc) testStates(doc, ["comment_regex_allowed", "comment_regex_allowed"]) - doc.insert({row:0, column:2}, "\n*/") + doc.insertText({row:0, column:2}, "\n*/") testStates(doc, [undefined, undefined, "comment_regex_allowed"]) forceTokenize(doc) diff --git a/lib/ace/commands/default_commands.js b/lib/ace/commands/default_commands.js index e4a8212e..0fd9686c 100644 --- a/lib/ace/commands/default_commands.js +++ b/lib/ace/commands/default_commands.js @@ -508,13 +508,13 @@ exports.commands = [{ scrollIntoView: "selectionPart" }, { name: "insertstring", - exec: function(editor, str) { editor.insert(str); }, + exec: function(editor, str) { editor.insertText(str); }, multiSelectAction: "forEach", scrollIntoView: "cursor" }, { name: "inserttext", exec: function(editor, args) { - editor.insert(lang.stringRepeat(args.text || "", args.times || 1)); + editor.insertText(lang.stringRepeat(args.text || "", args.times || 1)); }, multiSelectAction: "forEach" }, { diff --git a/lib/ace/document.js b/lib/ace/document.js index 75a7920d..ace69e32 100644 --- a/lib/ace/document.js +++ b/lib/ace/document.js @@ -51,17 +51,17 @@ var Anchor = require("./anchor").Anchor; * @constructor **/ -var Document = function(text) { - this.$lines = []; +var Document = function(textOrLines) { + this.$lines = ['']; // There has to be one line at least in the document. If you pass an empty // string to the insert function, nothing will happen. Workaround. - if (text.length == 0) { + if (textOrLines.length == 0) { this.$lines = [""]; - } else if (Array.isArray(text)) { - this._insertLines(0, text); + } else if (Array.isArray(textOrLines)) { + this.insertLines({row: 0, column: 0}, textOrLines); } else { - this.insert({row: 0, column:0}, text); + this.insertText({row: 0, column:0}, textOrLines); } }; @@ -77,7 +77,7 @@ var Document = function(text) { this.setValue = function(text) { var len = this.getLength(); this.remove(new Range(0, 0, len, this.getLine(len-1).length)); - this.insert({row: 0, column:0}, text); + this.insertText({row: 0, column:0}, text); }; /** @@ -211,27 +211,85 @@ var Document = function(text) { * @returns {String} **/ this.getTextRange = function(range) { + return this._getLinesForRange(range).join(this.getNewLineCharacter()); + }; + + this._getLinesForRange = function(range) { if (range.start.row == range.end.row) { - return this.getLine(range.start.row) - .substring(range.start.column, range.end.column); + return [this.getLine(range.start.row) + .substring(range.start.column, range.end.column)]; } var lines = this.getLines(range.start.row, range.end.row); lines[0] = (lines[0] || "").substring(range.start.column); var l = lines.length - 1; if (range.end.row - range.start.row == l) lines[l] = lines[l].substring(0, range.end.column); - return lines.join(this.getNewLineCharacter()); + return lines; }; this.$clipPosition = function(position) { + var length = this.getLength(); - if (position.row >= length) { + if (position.row >= length) + { position.row = Math.max(0, length - 1); - position.column = this.getLine(length-1).length; - } else if (position.row < 0) - position.row = 0; + position.column = this.getLine(length - 1).length; + } + else + { + position.row = Math.max(0, position.row); + position.column = Math.min(Math.max(position.column, 0), this.getLine(position.row).length); + } return position; }; + + this.$getClippedRange = function(range) + { + // Get Range object. + if (!range instanceof Range) + range = Range.fromPoints(range.start, range.end); + + // Return clipped range. + this.$clipPosition(range.start); + this.$clipPosition(range.end); + return range; + } + + this.$validateDelta = function(delta) + { + function fnThrow(errorText) + { + errorText = 'Invalid Delta: ' + errorText; + console.log(errorText, delta); + throw errorText; + } + + if (!delta.lines instanceof Array) + { + fnThrow('Delta object lines must be an array'); + } + + if (!delta.range instanceof Range) + { + fnThrow('Range object is not an instance of the Range class'); + } + + var start = delta.range.start; + if (Math.min(Math.max(start.row, 0), this.getLength() - 1 ) != start.row || + Math.min(Math.max(start.column, 0), this.$lines[start.row].length) != start.column) + { + fnThrow('Range start point not contained in document'); + } + + if (delta.lines.length - 1 != delta.range.end.row - delta.range.start.row) + { + fnThrow('Range row offsets does not match delta lines'); + } + + // TODO: Validate that the ending column offset matches the lines. + // TODO: Validate for deletions that the lines deleted match the lines + // in the document. + }, /** * Inserts a block of `text` at the indicated `position`. @@ -240,54 +298,34 @@ var Document = function(text) { * @returns {Object} The position ({row, column}) of the last line of `text`. If the length of `text` is 0, this function simply returns `position`. * **/ - this.insert = function(position, text) { - if (!text || text.length === 0) - return position; - - position = this.$clipPosition(position); - - // only detect new lines if the document has no line break yet + this.insertText = function(position, text) { + + // Only detect new lines if the document has no line break yet. if (this.getLength() <= 1) this.$detectNewLine(text); - - var lines = this.$split(text); - var firstLine = lines.splice(0, 1)[0]; - var lastLine = lines.length == 0 ? null : lines.splice(lines.length - 1, 1)[0]; - - position = this.insertInLine(position, firstLine); - if (lastLine !== null) { - position = this.insertNewLine(position); // terminate first line - position = this._insertLines(position.row, lines); - position = this.insertInLine(position, lastLine || ""); - } - return position; + + return this.insertLines(position, this.$split(text)); }; /** - * Fires whenever the document changes. - * - * Several methods trigger different `"change"` events. Below is a list of each action type, followed by each property that's also available: - * - * * `"insertLines"` (emitted by [[Document.insertLines]]) - * * `range`: the [[Range]] of the change within the document - * * `lines`: the lines in the document that are changing - * * `"insertText"` (emitted by [[Document.insertNewLine]]) - * * `range`: the [[Range]] of the change within the document - * * `text`: the text that's being added - * * `"removeLines"` (emitted by [[Document.insertLines]]) - * * `range`: the [[Range]] of the change within the document - * * `lines`: the lines in the document that were removed - * * `nl`: the new line character (as defined by [[Document.getNewLineCharacter]]) - * * `"removeText"` (emitted by [[Document.removeInLine]] and [[Document.removeNewLine]]) - * * `range`: the [[Range]] of the change within the document - * * `text`: the text that's being removed - * - * @event change - * @param {Object} e Contains at least one property called `"action"`. `"action"` indicates the action that triggered the change. Each action also has a set of additional properties. - * - **/ + * Fires whenever the document changes. + * + * Several methods trigger different `"change"` events. Below is a list of each action type, followed by each property that's also available: + * + * * `"insert"` + * * `range`: the [[Range]] of the change within the document + * * `lines`: the lines being added + * * `"delete"` + * * `range`: the [[Range]] of the change within the document + * * `lines`: the lines being removed + * + * @event change + * @param {Object} e Contains at least one property called `"action"`. `"action"` indicates the action that triggered the change. Each action also has a set of additional properties. + * + **/ + /** - * Inserts the elements in `lines` into the document, starting at the row index given by `row`. This method also triggers the `'change'` event. + * Inserts the elements in `lines` into the document as full lines (does not merge with existing line), starting at the row index given by `row`. This method also triggers the `'change'` event. * @param {Number} row The index of the row to insert at * @param {Array} lines An array of strings * @returns {Object} Contains the final row and column, like this: @@ -300,100 +338,62 @@ var Document = function(text) { * ``` * **/ - this.insertLines = function(row, lines) { - if (row >= this.getLength()) - return this.insert({row: row, column: 0}, "\n" + lines.join("\n")); - return this._insertLines(Math.max(row, 0), lines); - }; - this._insertLines = function(row, lines) { - if (lines.length == 0) - return {row: row, column: 0}; - - // apply doesn't work for big arrays (smallest threshold is on safari 0xFFFF) - // to circumvent that we have to break huge inserts into smaller chunks here - if (lines.length > 0xFFFF) { - var end = this._insertLines(row, lines.slice(0xFFFF)); - lines = lines.slice(0, 0xFFFF); + this.insertFullLines = function(row, lines) + { + // Clip to document. + // Allow one past the document end. + row = Math.min(Math.max(row, 0), this.getLength()); + + // Calculate insertion point. + var column = 0; + if (row < this.getLength()) // Insert before the specified row. + { + lines = lines.concat(['']); + column = 0; } + else // Insert after the last row in the document. + { + lines = [''].concat(lines); + row--; + var column = this.$lines[row].length; + } + + // Insert. + this.insertLines({row: row, column: column}, lines); + }, - var args = [row, 0]; - args.push.apply(args, lines); - this.$lines.splice.apply(this.$lines, args); + /** + * Inserts the elements in `lines` into the document, starting at the position index given by `row`. This method also triggers the `'change'` event. + * @param {Number} row The index of the row to insert at + * @param {Array} lines An array of strings + * @returns {Object} Contains the final row and column, like this: + * ``` + * {row: endRow, column: 0} + * ``` + * If `lines` is empty, this function returns an object containing the current row, and column, like this: + * ``` + * {row: row, column: 0} + * ``` + * + **/ + this.insertLines = function(position, lines){ - var range = new Range(row, 0, row + lines.length, 0); - var delta = { - action: "insertLines", - range: range, + // Calculate insertion range end point. + this.$clipPosition(position); + var endPoint = { + row : position.row + lines.length - 1, + column : (lines.length == 1 ? position.column : 0) + lines[lines.length - 1].length + }; + + // Apply delta (emits change). + this.applyDelta({ + action: "insert", + range: Range.fromPoints(position, endPoint), lines: lines - }; - this._emit("change", { data: delta }); - return end || range.end; - }; - - /** - * Inserts a new line into the document at the current row's `position`. This method also triggers the `'change'` event. - * @param {Object} position The position to insert at - * @returns {Object} Returns an object containing the final row and column, like this:
- * ``` - * {row: endRow, column: 0} - * ``` - * - **/ - this.insertNewLine = function(position) { - position = this.$clipPosition(position); - var line = this.$lines[position.row] || ""; - - this.$lines[position.row] = line.substring(0, position.column); - this.$lines.splice(position.row + 1, 0, line.substring(position.column, line.length)); - - var end = { - row : position.row + 1, - column : 0 - }; - - var delta = { - action: "insertText", - range: Range.fromPoints(position, end), - text: this.getNewLineCharacter() - }; - this._emit("change", { data: delta }); - - return end; - }; - - /** - * Inserts `text` into the `position` at the current row. This method also triggers the `'change'` event. - * @param {Object} position The position to insert at; it's an object that looks like `{ row: row, column: column}` - * @param {String} text A chunk of text - * @returns {Object} Returns an object containing the final row and column, like this: - * ``` - * {row: endRow, column: 0} - * ``` - * - **/ - this.insertInLine = function(position, text) { - if (text.length == 0) - return position; - - var line = this.$lines[position.row] || ""; - - this.$lines[position.row] = line.substring(0, position.column) + text - + line.substring(position.column); - - var end = { - row : position.row, - column : position.column + text.length - }; - - var delta = { - action: "insertText", - range: Range.fromPoints(position, end), - text: text - }; - this._emit("change", { data: delta }); - - return end; - }; + }); + + return endPoint; + } /** * Removes the `range` from the document. @@ -402,37 +402,16 @@ var Document = function(text) { * **/ this.remove = function(range) { - if (!range instanceof Range) - range = Range.fromPoints(range.start, range.end); - // clip to document - range.start = this.$clipPosition(range.start); - range.end = this.$clipPosition(range.end); - - if (range.isEmpty()) - return range.start; - - var firstRow = range.start.row; - var lastRow = range.end.row; - - if (range.isMultiLine()) { - var firstFullRow = range.start.column == 0 ? firstRow : firstRow + 1; - var lastFullRow = lastRow - 1; - - if (range.end.column > 0) - this.removeInLine(lastRow, 0, range.end.column); - - if (lastFullRow >= firstFullRow) - this._removeLines(firstFullRow, lastFullRow); - - if (firstFullRow != firstRow) { - this.removeInLine(firstRow, range.start.column, this.getLine(firstRow).length); - this.removeNewLine(range.start.row); - } - } - else { - this.removeInLine(firstRow, range.start.column, range.end.column); - } - return range.start; + + // Apply delta (emits change). + range = this.$getClippedRange(range); + this.applyDelta( + { + action: 'delete', + range: range, + lines: this._getLinesForRange(range), + }); + return range.start; }; /** @@ -444,21 +423,18 @@ var Document = function(text) { * **/ this.removeInLine = function(row, startColumn, endColumn) { - if (startColumn == endColumn) - return; - + + // Calculate deleteion range. var range = new Range(row, startColumn, row, endColumn); - var line = this.getLine(row); - var removed = line.substring(startColumn, endColumn); - var newLine = line.substring(0, startColumn) + line.substring(endColumn, line.length); - this.$lines.splice(row, 1, newLine); - - var delta = { - action: "removeText", + range = this.$getClippedRange(range); + + // Apply delta (emits change). + this.applyDelta({ + action: "delete", range: range, - text: removed - }; - this._emit("change", { data: delta }); + lines: this._getLinesForRange(range) + }); + return range.start; }; @@ -469,24 +445,35 @@ var Document = function(text) { * @returns {[String]} Returns all the removed lines. * **/ - this.removeLines = function(firstRow, lastRow) { - if (firstRow < 0 || lastRow >= this.getLength()) - return this.remove(new Range(firstRow, 0, lastRow + 1, 0)); - return this._removeLines(firstRow, lastRow); - }; - - this._removeLines = function(firstRow, lastRow) { - var range = new Range(firstRow, 0, lastRow + 1, 0); - var removed = this.$lines.splice(firstRow, lastRow - firstRow + 1); - - var delta = { - action: "removeLines", + this.removeFullLines = function(firstRow, lastRow) { + + // Clip to document. + firstRow = Math.min(Math.max(0, firstRow), this.getLength() - 1); + lastRow = Math.min(Math.max(0, lastRow ), this.getLength() - 1); + + // Calculate deletion range. + // Delete the ending new line unless we're at the end of the document. + // If we're at the end of the document, delete the starting new line. + var deleteFirstNewLine = lastRow == this.getLength() - 1 && firstRow > 0; + var deleteLastNewLine = lastRow < this.getLength() - 1; + var startRow = ( deleteFirstNewLine ? firstRow - 1 : firstRow ); + var startCol = ( deleteFirstNewLine ? this.getLine(startRow).length : 0 ); + var endRow = ( deleteLastNewLine ? lastRow + 1 : lastRow ); + var endCol = ( deleteLastNewLine ? 0 : this.getLine(endRow).length ); + var range = new Range(startRow, startCol, endRow, endCol); + + // Store delelted lines with bounding newlines ommitted (maintains previous behavior). + var deletedLines = this.$lines.slice(firstRow, lastRow + 1); + + // Apply delta (emits change). + this.applyDelta({ + action: "delete", range: range, - nl: this.getNewLineCharacter(), - lines: removed - }; - this._emit("change", { data: delta }); - return removed; + lines: this._getLinesForRange(range) + }); + + // Return the deleted lines. + return deletedLines; }; /** @@ -495,20 +482,16 @@ var Document = function(text) { * **/ this.removeNewLine = function(row) { - var firstLine = this.getLine(row); - var secondLine = this.getLine(row+1); - - var range = new Range(row, firstLine.length, row+1, 0); - var line = firstLine + secondLine; - - this.$lines.splice(row, 2, line); - - var delta = { - action: "removeText", - range: range, - text: this.getNewLineCharacter() - }; - this._emit("change", { data: delta }); + + if (row < this.getLength() - 1 && row >= 0) + { + // Apply delta (emits change). + this.applyDelta({ + action: "delete", + range: new Range(row, this.getLine(row).length, row + 1, 0), + lines: ['', ''] + }); + } }; /** @@ -534,53 +517,90 @@ var Document = function(text) { this.remove(range); if (text) { - var end = this.insert(range.start, text); + var end = this.insertText(range.start, text); } else { end = range.start; } - + return end; }; /** - * Applies all the changes previously accumulated. These can be either `'includeText'`, `'insertLines'`, `'removeText'`, and `'removeLines'`. + * Applies all the changes previously accumulated. These can be either `'insert'` or `'delete'`. **/ this.applyDeltas = function(deltas) { for (var i=0; i=0; i--) { - var delta = deltas[i]; - - var range = Range.fromPoints(delta.range.start, delta.range.end); - - if (delta.action == "insertLines") - this._removeLines(range.start.row, range.end.row - 1); - else if (delta.action == "insertText") - this.remove(range); - else if (delta.action == "removeLines") - this._insertLines(range.start.row, delta.lines); - else if (delta.action == "removeText") - this.insert(range.start, delta.text); + this.revertDelta(deltas[i]); } }; + + this.revertDelta = function(delta) + { + this.applyDelta({ + action: (delta.action == 'insert' ? 'delete' : 'insert'), + range: delta.range.clone(), + lines: delta.lines.slice() + }); + }, /** * Converts an index position in a document to a `{row, column}` object. diff --git a/lib/ace/document_test.js b/lib/ace/document_test.js index 5c324db0..50bd8b49 100644 --- a/lib/ace/document_test.js +++ b/lib/ace/document_test.js @@ -48,7 +48,7 @@ module.exports = { var deltas = []; doc.on("change", function(e) { deltas.push(e.data); }); - doc.insert({row: 0, column: 1}, "juhu"); + doc.insertText({row: 0, column: 1}, "juhu"); assert.equal(doc.getValue(), ["1juhu2", "34"].join("\n")); var d = deltas.concat(); @@ -65,7 +65,7 @@ module.exports = { var deltas = []; doc.on("change", function(e) { deltas.push(e.data); }); - doc.insertNewLine({row: 0, column: 1}); + doc.insertLines({row: 0, column: 1}, ['', '']); assert.equal(doc.getValue(), ["1", "2", "34"].join("\n")); var d = deltas.concat(); @@ -82,7 +82,7 @@ module.exports = { var deltas = []; doc.on("change", function(e) { deltas.push(e.data); }); - doc.insertLines(0, ["aa", "bb"]); + doc.insertFullLines(0, ["aa", "bb"]); assert.equal(doc.getValue(), ["aa", "bb", "12", "34"].join("\n")); var d = deltas.concat(); @@ -99,7 +99,7 @@ module.exports = { var deltas = []; doc.on("change", function(e) { deltas.push(e.data); }); - doc.insertLines(2, ["aa", "bb"]); + doc.insertFullLines(2, ["aa", "bb"]); assert.equal(doc.getValue(), ["12", "34", "aa", "bb"].join("\n")); }, @@ -109,7 +109,7 @@ module.exports = { var deltas = []; doc.on("change", function(e) { deltas.push(e.data); }); - doc.insertLines(1, ["aa", "bb"]); + doc.insertFullLines(1, ["aa", "bb"]); assert.equal(doc.getValue(), ["12", "aa", "bb", "34"].join("\n")); var d = deltas.concat(); @@ -126,7 +126,7 @@ module.exports = { var deltas = []; doc.on("change", function(e) { deltas.push(e.data); }); - doc.insert({row: 0, column: 0}, "aa\nbb\ncc"); + doc.insertText({row: 0, column: 0}, "aa\nbb\ncc"); assert.equal(doc.getValue(), ["aa", "bb", "cc12", "34"].join("\n")); var d = deltas.concat(); @@ -143,7 +143,7 @@ module.exports = { var deltas = []; doc.on("change", function(e) { deltas.push(e.data); }); - doc.insert({row: 2, column: 0}, "aa\nbb\ncc"); + doc.insertText({row: 1, column: 2}, "aa\nbb\ncc"); assert.equal(doc.getValue(), ["12", "34aa", "bb", "cc"].join("\n")); var d = deltas.concat(); @@ -160,7 +160,7 @@ module.exports = { var deltas = []; doc.on("change", function(e) { deltas.push(e.data); }); - doc.insert({row: 0, column: 1}, "aa\nbb\ncc"); + doc.insertText({row: 0, column: 1}, "aa\nbb\ncc"); assert.equal(doc.getValue(), ["1aa", "bb", "cc2", "34"].join("\n")); var d = deltas.concat(); @@ -235,7 +235,7 @@ module.exports = { "test: remove lines should return the removed lines" : function() { var doc = new Document(["1234", "5678", "abcd"]); - var removed = doc.removeLines(1, 2); + var removed = doc.removeFullLines(1, 2); assert.equal(removed.join("\n"), ["5678", "abcd"].join("\n")); }, diff --git a/lib/ace/edit_session.js b/lib/ace/edit_session.js index a63ceeef..98c255a4 100644 --- a/lib/ace/edit_session.js +++ b/lib/ace/edit_session.js @@ -1136,8 +1136,8 @@ var EditSession = function(text, mode) { * * **/ - this.insert = function(position, text) { - return this.doc.insert(position, text); + this.insertText = function(position, text) { + return this.doc.insertText(position, text); }; /** @@ -1151,6 +1151,19 @@ var EditSession = function(text, mode) { this.remove = function(range) { return this.doc.remove(range); }; + + /** + * Removes a range of full lines. This method also triggers the `'change'` event. + * @param {Number} firstRow The first row to be removed + * @param {Number} lastRow The last row to be removed + * @returns {[String]} Returns all the removed lines. + * + * @related Document.removeFullLines + * + **/ + this.removeFullLines = function(firstRow, lastRow){ + return this.doc.removeFullLines(firstRow, lastRow) + } /** * Reverts previous changes to your document. @@ -1227,9 +1240,7 @@ var EditSession = function(text, mode) { this.$getUndoSelection = function(deltas, isUndo, lastUndoRange) { function isInsert(delta) { - var insert = - delta.action === "insertText" || delta.action === "insertLines"; - return isUndo ? !insert : insert; + return isUndo ? delta.action !== "insert" : delta.action === "insert"; } var delta = deltas[0]; @@ -1339,7 +1350,7 @@ var EditSession = function(text, mode) { } } - toRange.end = this.insert(toRange.start, text); + toRange.end = this.insertText(toRange.start, text); if (folds.length) { var oldStart = fromRange.start; var newStart = toRange.start; @@ -1373,7 +1384,7 @@ var EditSession = function(text, mode) { this.indentRows = function(startRow, endRow, indentString) { indentString = indentString.replace(/\t/g, this.getTabString()); for (var row=startRow; row<=endRow; row++) - this.insert({row: row, column:0}, indentString); + this.insertText({row: row, column:0}, indentString); }; /** @@ -1430,11 +1441,11 @@ var EditSession = function(text, mode) { x.end.row += diff; return x; }); - + var lines = dir == 0 ? this.doc.getLines(firstRow, lastRow) - : this.doc.removeLines(firstRow, lastRow); - this.doc.insertLines(firstRow+diff, lines); + : this.doc.removeFullLines(firstRow, lastRow); + this.doc.insertFullLines(firstRow+diff, lines); folds.length && this.addFolds(folds); return diff; }; @@ -1444,8 +1455,6 @@ var EditSession = function(text, mode) { * @param {Number} lastRow The final row to move up * @returns {Number} If `firstRow` is less-than or equal to 0, this function returns 0. Otherwise, on success, it returns -1. * - * @related Document.insertLines - * **/ this.moveLinesUp = function(firstRow, lastRow) { return this.$moveLines(firstRow, lastRow, -1); @@ -1456,8 +1465,6 @@ var EditSession = function(text, mode) { * @param {Number} firstRow The starting row to move down * @param {Number} lastRow The final row to move down * @returns {Number} If `firstRow` is less-than or equal to 0, this function returns 0. Otherwise, on success, it returns -1. - * - * @related Document.insertLines **/ this.moveLinesDown = function(firstRow, lastRow) { return this.$moveLines(firstRow, lastRow, 1); @@ -1668,28 +1675,17 @@ var EditSession = function(text, mode) { this.$updateInternalDataOnChange = function(e) { var useWrapMode = this.$useWrapMode; - var len; var action = e.data.action; - var firstRow = e.data.range.start.row; - var lastRow = e.data.range.end.row; var start = e.data.range.start; var end = e.data.range.end; + var firstRow = start.row; + var lastRow = end.row; + var len = lastRow - firstRow; var removedFolds = null; - - if (action.indexOf("Lines") != -1) { - if (action == "insertLines") { - lastRow = firstRow + (e.data.lines.length); - } else { - lastRow = firstRow; - } - len = e.data.lines ? e.data.lines.length : lastRow - firstRow; - } else { - len = lastRow - firstRow; - } - + this.$updating = true; if (len != 0) { - if (action.indexOf("remove") != -1) { + if (action == "delete") { this[useWrapMode ? "$wrapData" : "$rowLengthCache"].splice(firstRow, len); var foldLines = this.$foldData; @@ -1764,7 +1760,7 @@ var EditSession = function(text, mode) { // Realign folds. E.g. if you add some new chars before a fold, the // fold should "move" to the right. len = Math.abs(e.data.range.start.column - e.data.range.end.column); - if (action.indexOf("remove") != -1) { + if (action == "delete") { // Get all the folds in the change range and remove them. removedFolds = this.getFoldsInRange(e.data.range); this.removeFolds(removedFolds); diff --git a/lib/ace/edit_session/folding.js b/lib/ace/edit_session/folding.js index 474818b2..9df35593 100644 --- a/lib/ace/edit_session/folding.js +++ b/lib/ace/edit_session/folding.js @@ -833,7 +833,7 @@ function Folding() { if (len === 0) { this.foldWidgets[firstRow] = null; - } else if (delta.action == "removeText" || delta.action == "removeLines") { + } else if (delta.action == "delete") { this.foldWidgets.splice(firstRow, len + 1, null); } else { var args = Array(len + 1); diff --git a/lib/ace/edit_session_test.js b/lib/ace/edit_session_test.js index 87cc9567..855da4d1 100644 --- a/lib/ace/edit_session_test.js +++ b/lib/ace/edit_session_test.js @@ -430,12 +430,12 @@ module.exports = { session.setTabSize(4); assert.equal(session.getScreenWidth(), 2); - session.doc.insertNewLine({row: 0, column: Infinity}); - session.doc.insertLines(1, ["123"]); + session.doc.insertLines({row: 0, column: Infinity}, ['', '']); + session.doc.insertFullLines(1, ["123"]); assert.equal(session.getScreenWidth(), 3); - session.doc.insertNewLine({row: 0, column: Infinity}); - session.doc.insertLines(1, ["\t\t"]); + session.doc.insertLines({row: 0, column: Infinity}, ['', '']); + session.doc.insertFullLines(1, ["\t\t"]); assert.equal(session.getScreenWidth(), 8); @@ -459,9 +459,9 @@ module.exports = { session.setUseWrapMode(true); - document.insertLines(0, ["a", "b"]); - document.insertLines(2, ["c", "d"]); - document.removeLines(1, 2); + document.insertFullLines(0, ["a", "b"]); + document.insertFullLines(2, ["c", "d"]); + document.removeFullLines(1, 2); }, "test wrapMode init has to create wrapData array": function() { @@ -649,7 +649,7 @@ module.exports = { var foldLines = session.$foldData; function insert(row, column, text) { - session.insert({row: row, column: column}, text); + session.insertText({row: row, column: column}, text); // Force the session to store all changes made to the document NOW // on the undoManager's queue. Otherwise we can't undo in separate @@ -748,7 +748,7 @@ module.exports = { undoManager = session.getUndoManager(), foldLines = session.$foldData; function insert(row, column, text) { - session.insert({row: row, column: column}, text); + session.insertText({row: row, column: column}, text); // Force the session to store all changes made to the document NOW // on the undoManager's queue. Otherwise we can't undo in separate // steps later. diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 6486a881..3926fc81 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -601,19 +601,10 @@ var Editor = function(renderer, session) { * **/ this.onDocumentChange = function(e) { - var delta = e.data; - var range = delta.range; - var lastRow; - - if (range.start.row == range.end.row && delta.action != "insertLines" && delta.action != "removeLines") - lastRow = range.end.row; - else - lastRow = Infinity; - this.renderer.updateLines(range.start.row, lastRow); - + this.renderer.updateLines(e.data.range.start.row, e.data.range.end.row); this._emit("change", e); - - // update cursor because tab characters can influence the cursor position + + // update cursor because tab characters can influence the cursor position. this.$cursorChange(); }; @@ -827,7 +818,7 @@ var Editor = function(renderer, session) { if (this.$readOnly) return; this._emit("paste", text); - this.insert(text); + this.insertText(text); }; @@ -841,7 +832,7 @@ var Editor = function(renderer, session) { * * **/ - this.insert = function(text) { + this.insertText = function(text) { var session = this.session; var mode = session.getMode(); var cursor = this.getCursorPosition(); @@ -887,7 +878,7 @@ var Editor = function(renderer, session) { var lineState = session.getState(cursor.row); var line = session.getLine(cursor.row); var shouldOutdent = mode.checkOutdent(lineState, line, text); - var end = session.insert(cursor, text); + var end = session.insertText(cursor, text); if (transform && transform.selection) { if (transform.selection.length == 2) { // Transform relative to the current column @@ -906,7 +897,7 @@ var Editor = function(renderer, session) { if (session.getDocument().isNewLine(text)) { var lineIndent = mode.getNextLineIndent(lineState, line.slice(0, cursor.column), session.getTabString()); - session.insert({row: cursor.row+1, column: 0}, lineIndent); + session.insertText({row: cursor.row+1, column: 0}, lineIndent); } if (shouldOutdent) mode.autoOutdent(lineState, session, cursor.row); @@ -1279,7 +1270,7 @@ var Editor = function(renderer, session) { } var cursor = this.getCursorPosition(); - this.insert("\n"); + this.insertText("\n"); this.moveCursorToPosition(cursor); }; @@ -1378,7 +1369,7 @@ var Editor = function(renderer, session) { this.selection.setSelectionRange(range); indentString = "\t"; } - return this.insert(indentString); + return this.insertText(indentString); }; /** @@ -1516,15 +1507,7 @@ var Editor = function(renderer, session) { **/ this.removeLines = function() { var rows = this.$getSelectedRows(); - var range; - if (rows.first === 0 || rows.last+1 < this.session.getLength()) - range = new Range(rows.first, 0, rows.last+1, 0); - else - range = new Range( - rows.first-1, this.session.getLine(rows.first-1).length, - rows.last, this.session.getLine(rows.last).length - ); - this.session.remove(range); + this.session.removeFullLines(rows.first, rows.last); this.clearSelection(); }; @@ -1538,7 +1521,7 @@ var Editor = function(renderer, session) { doc.duplicateLines(row, row); } else { var point = reverse ? range.start : range.end; - var endPoint = doc.insert(point, doc.getTextRange(range), false); + var endPoint = doc.insertText(point, doc.getTextRange(range), false); range.start = point; range.end = endPoint; diff --git a/lib/ace/editor_change_document_test.js b/lib/ace/editor_change_document_test.js index a1fdaad9..83fffeb3 100644 --- a/lib/ace/editor_change_document_test.js +++ b/lib/ace/editor_change_document_test.js @@ -172,7 +172,7 @@ module.exports = { self.session1.setMode(new HtmlMode()); // 5. Try to type valid HTML - self.session1.insert({row: 0, column: 0}, ""); + self.session1.insertText({row: 0, column: 0}, ""); setTimeout(function() { assert.equal(Object.keys(self.session1.getAnnotations()).length, 0); diff --git a/lib/ace/editor_navigation_test.js b/lib/ace/editor_navigation_test.js index ab348241..b60d391d 100644 --- a/lib/ace/editor_navigation_test.js +++ b/lib/ace/editor_navigation_test.js @@ -150,7 +150,7 @@ module.exports = { var editor = new Editor(new MockRenderer(), new EditSession(["1234", "1234567890"])); editor.navigateTo(0, 3); - editor.insert("juhu"); + editor.insertText("juhu"); editor.navigateDown(); assert.position(editor.getCursorPosition(), 1, 7); diff --git a/lib/ace/ext/chromevox.js b/lib/ace/ext/chromevox.js index 9f7a7996..20bbb6c7 100644 --- a/lib/ace/ext/chromevox.js +++ b/lib/ace/ext/chromevox.js @@ -581,12 +581,12 @@ var onSelectionChange = function(evt) { var onChange = function(evt) { var data = evt.data; switch (data.action) { - case 'removeText': + case 'delete': cvox.Api.speak(data.text, 0, DELETED_PROP); /* Let the future cursor change event know it's from text change. */ changed = true; break; - case 'insertText': + case 'insert': cvox.Api.speak(data.text, 0); /* Let the future cursor change event know it's from text change. */ changed = true; diff --git a/lib/ace/ext/elastic_tabstops_lite.js b/lib/ace/ext/elastic_tabstops_lite.js index 9901c5df..4d3a40d4 100644 --- a/lib/ace/ext/elastic_tabstops_lite.js +++ b/lib/ace/ext/elastic_tabstops_lite.js @@ -236,7 +236,7 @@ var ElasticTabstopsLite = function(editor) { if (difference > 0) { // put the spaces after the tab and then delete the tab, so any insertion // points behave as expected - this.$editor.session.getDocument().insertInLine({row: row, column: it + 1}, Array(difference + 1).join(" ") + "\t"); + this.$editor.session.getDocument().insertText({row: row, column: it + 1}, Array(difference + 1).join(" ") + "\t"); this.$editor.session.getDocument().removeInLine(row, it, it + 1); bias += difference; diff --git a/lib/ace/ext/whitespace.js b/lib/ace/ext/whitespace.js index 83486fb0..736e991d 100644 --- a/lib/ace/ext/whitespace.js +++ b/lib/ace/ext/whitespace.js @@ -153,7 +153,7 @@ exports.convertIndentation = function(session, ch, len) { if (toInsert != match) { doc.removeInLine(i, 0, match.length); - doc.insertInLine({row: i, column: 0}, toInsert); + doc.insertText({row: i, column: 0}, toInsert); } } } diff --git a/lib/ace/keyboard/vim/commands.js b/lib/ace/keyboard/vim/commands.js index dd3357d6..defca037 100644 --- a/lib/ace/keyboard/vim/commands.js +++ b/lib/ace/keyboard/vim/commands.js @@ -104,7 +104,7 @@ var actions = exports.actions = { if (param && param.length) { if (param.length > 1) param = param == "return" ? "\n" : param == "tab" ? "\t" : param; - repeat(function() { editor.insert(param); }, count || 1); + repeat(function() { editor.insertText(param); }, count || 1); editor.navigateLeft(); } } @@ -224,12 +224,12 @@ var actions = exports.actions = { var pos = editor.getCursorPosition(); pos.column = editor.session.getLine(pos.row).length; var text = lang.stringRepeat("\n" + defaultReg.text, count || 1); - editor.session.insert(pos, text); + editor.session.insertText(pos, text); editor.moveCursorTo(pos.row + 1, 0); } else { editor.navigateRight(); - editor.insert(lang.stringRepeat(defaultReg.text, count || 1)); + editor.insertText(lang.stringRepeat(defaultReg.text, count || 1)); editor.navigateLeft(); } editor.setOverwrite(true); @@ -245,11 +245,11 @@ var actions = exports.actions = { var pos = editor.getCursorPosition(); pos.column = 0; var text = lang.stringRepeat(defaultReg.text + "\n", count || 1); - editor.session.insert(pos, text); + editor.session.insertText(pos, text); editor.moveCursorToPosition(pos); } else { - editor.insert(lang.stringRepeat(defaultReg.text, count || 1)); + editor.insertText(lang.stringRepeat(defaultReg.text, count || 1)); } editor.setOverwrite(true); editor.selection.clearSelection(); diff --git a/lib/ace/keyboard/vim/maps/motions.js b/lib/ace/keyboard/vim/maps/motions.js index 91c8b8af..2bf580b0 100644 --- a/lib/ace/keyboard/vim/maps/motions.js +++ b/lib/ace/keyboard/vim/maps/motions.js @@ -545,7 +545,7 @@ module.exports = { if (content.length) { editor.navigateLineEnd() - editor.insert(content); + editor.insertText(content); util.insertMode(editor); } } @@ -562,9 +562,9 @@ module.exports = { if(row > 0) { editor.navigateUp(); editor.navigateLineEnd() - editor.insert(content); + editor.insertText(content); } else { - editor.session.insert({row: 0, column: 0}, content); + editor.session.insertText({row: 0, column: 0}, content); editor.navigateUp(); } util.insertMode(editor); diff --git a/lib/ace/layer/gutter.js b/lib/ace/layer/gutter.js index 5a535094..9d813d2a 100644 --- a/lib/ace/layer/gutter.js +++ b/lib/ace/layer/gutter.js @@ -108,8 +108,8 @@ var Gutter = function(parentEl) { var len = range.end.row - firstRow; if (len === 0) { // do nothing - } else if (delta.action == "removeText" || delta.action == "removeLines") { - this.$annotations.splice(firstRow, len + 1, null); + } else if (delta.action == "delete") { + this.$annotations.splice(firstRow, len, null); } else { var args = new Array(len + 1); args.unshift(firstRow, 1); diff --git a/lib/ace/line_widgets.js b/lib/ace/line_widgets.js index 597b3ca2..4023bd55 100644 --- a/lib/ace/line_widgets.js +++ b/lib/ace/line_widgets.js @@ -123,7 +123,7 @@ function LineWidgets(session) { if (len === 0) { // return - } else if (delta.action == "removeText" || delta.action == "removeLines") { + } else if (delta.action == "delete") { var removed = cells.splice(startRow + 1, len); removed.forEach(function(w) { w && this.removeLineWidget(w); diff --git a/lib/ace/mode/javascript_test.js b/lib/ace/mode/javascript_test.js index b413765a..cc2b22d7 100644 --- a/lib/ace/mode/javascript_test.js +++ b/lib/ace/mode/javascript_test.js @@ -131,7 +131,7 @@ module.exports = { this.mode.toggleCommentLines("start", session, 0, 2); assert.equal([" abc", " cde", " fg"].join("\n"), session.toString()); - session.insert({row: 0, column: 0}, " "); + session.insertText({row: 0, column: 0}, " "); this.mode.toggleCommentLines("start", session, 0, 2); assert.equal(["// abc", "// cde", "// fg"].join("\n"), session.toString()); }, diff --git a/lib/ace/mode/text.js b/lib/ace/mode/text.js index d04fed78..534ac03c 100644 --- a/lib/ace/mode/text.js +++ b/lib/ace/mode/text.js @@ -92,8 +92,8 @@ var Mode = function() { if (testRemove(line, i)) return; if (!ignoreBlankLines || /\S/.test(line)) { - doc.insertInLine({row: i, column: line.length}, lineCommentEnd); - doc.insertInLine({row: i, column: minIndent}, lineCommentStart); + doc.insertText({row: i, column: line.length}, lineCommentEnd); + doc.insertText({row: i, column: minIndent}, lineCommentStart); } }; @@ -138,9 +138,9 @@ var Mode = function() { var comment = function(line, i) { if (!ignoreBlankLines || /\S/.test(line)) { if (shouldInsertSpace(line, minIndent, minIndent)) - doc.insertInLine({row: i, column: minIndent}, commentWithSpace); + doc.insertText({row: i, column: minIndent}, commentWithSpace); else - doc.insertInLine({row: i, column: minIndent}, lineCommentStart); + doc.insertText({row: i, column: minIndent}, lineCommentStart); } }; var testRemove = function(line, i) { @@ -244,8 +244,8 @@ var Mode = function() { } else { colDiff = comment.start.length startRow = range.start.row; - session.insert(range.end, comment.end); - session.insert(range.start, comment.start); + session.insertText(range.end, comment.end); + session.insertText(range.start, comment.start); } // todo: selection should have ended up in the right place automatically! if (initialRange.start.row == startRow) diff --git a/lib/ace/mouse/dragdrop_handler.js b/lib/ace/mouse/dragdrop_handler.js index 3a4bc188..3ad54bb8 100644 --- a/lib/ace/mouse/dragdrop_handler.js +++ b/lib/ace/mouse/dragdrop_handler.js @@ -176,7 +176,7 @@ function DragdropHandler(mouseHandler) { var dropData = dataTransfer.getData('Text'); range = { start: dragCursor, - end: editor.session.insert(dragCursor, dropData) + end: editor.session.insertText(dragCursor, dropData) }; editor.focus(); dragOperation = null; diff --git a/lib/ace/multi_select.js b/lib/ace/multi_select.js index 7f4c5355..883d9a7c 100644 --- a/lib/ace/multi_select.js +++ b/lib/ace/multi_select.js @@ -544,7 +544,7 @@ var Editor = require("./editor").Editor; this._signal("paste", text); if (!this.inMultiSelectMode || this.inVirtualSelectionMode) - return this.insert(text); + return this.insertText(text); var lines = text.split(/\r\n|\r|\n/); var ranges = this.selection.rangeList.ranges; @@ -557,7 +557,7 @@ var Editor = require("./editor").Editor; if (!range.isEmpty()) this.session.remove(range); - this.session.insert(range.start, lines[i]); + this.session.insertText(range.start, lines[i]); } }; @@ -740,9 +740,9 @@ var Editor = require("./editor").Editor; if (fr < 0) fr = 0; if (lr >= max) lr = max - 1; } - var lines = this.session.doc.removeLines(fr, lr); + var lines = this.session.removeFullLines(fr, lr); lines = this.$reAlignText(lines, guessRange); - this.session.doc.insert({row: fr, column: 0}, lines.join("\n") + "\n"); + this.session.insertText({row: fr, column: 0}, lines.join("\n") + "\n"); if (!guessRange) { range.start.column = 0; range.end.column = lines[lines.length - 1].length; @@ -778,7 +778,7 @@ var Editor = require("./editor").Editor; var l = maxCol - p.column; var d = spaceOffsets[i] - minSpace; if (l > d) - session.insert(p, lang.stringRepeat(" ", l - d)); + session.insertText(p, lang.stringRepeat(" ", l - d)); else session.remove(new Range(p.row, p.column, p.row, p.column - l + d)); diff --git a/lib/ace/occur_test.js b/lib/ace/occur_test.js index 59fb8548..a9b2c894 100644 --- a/lib/ace/occur_test.js +++ b/lib/ace/occur_test.js @@ -55,7 +55,7 @@ module.exports = { }, "test: find lines matching" : function() { - editor.session.insert({row: 0, column: 0}, 'abc\ndef\nxyz\nbcxbc'); + editor.session.insertText({row: 0, column: 0}, 'abc\ndef\nxyz\nbcxbc'); var result = occur.matchingLines(editor.session, {needle: 'bc'}), expected = [{row: 0, content: 'abc'}, {row: 3, content: 'bcxbc'}]; assert.deepEqual(result, expected); @@ -63,7 +63,7 @@ module.exports = { "test: display occurrences" : function() { var text = 'abc\ndef\nxyz\nbcx\n'; - editor.session.insert({row: 0, column: 0}, text); + editor.session.insertText({row: 0, column: 0}, text); occur.displayOccurContent(editor, {needle: 'bc'}); assert.equal(editor.getValue(), 'abc\nbcx'); occur.displayOriginalContent(editor); @@ -72,7 +72,7 @@ module.exports = { "test: original position from occur doc" : function() { var text = 'abc\ndef\nxyz\nbcx\n'; - editor.session.insert({row: 0, column: 0}, text); + editor.session.insertText({row: 0, column: 0}, text); occur.displayOccurContent(editor, {needle: 'bc'}); assert.equal(editor.getValue(), 'abc\nbcx'); var pos = occur.occurToOriginalPosition(editor.session, {row: 1, column: 2}); @@ -82,7 +82,7 @@ module.exports = { "test: occur command" : function() { // setup var text = 'hel\nlo\n\nwo\nrld\n'; - editor.session.insert({row: 0, column: 0}, text); + editor.session.insertText({row: 0, column: 0}, text); editor.commands.addCommand(occurStartCommand); // run occur for lines including 'o' @@ -106,7 +106,7 @@ module.exports = { "test: occur navigation" : function() { // setup var text = 'hel\nlo\n\nwo\nrld\n'; - editor.session.insert({row: 0, column: 0}, text); + editor.session.insertText({row: 0, column: 0}, text); editor.commands.addCommand(occurStartCommand); editor.moveCursorToPosition({row: 1, column: 1}); @@ -125,7 +125,7 @@ module.exports = { "test: recursive occur" : function() { // setup var text = 'x\nabc1\nx\nabc2\n'; - editor.session.insert({row: 0, column: 0}, text); + editor.session.insertText({row: 0, column: 0}, text); editor.commands.addCommand(occurStartCommand); // orig -> occur1 diff --git a/lib/ace/placeholder.js b/lib/ace/placeholder.js index bcb44ce2..9d0a3c3c 100644 --- a/lib/ace/placeholder.js +++ b/lib/ace/placeholder.js @@ -155,21 +155,21 @@ var PlaceHolder = function(session, length, pos, others, mainClass, othersClass) if(range.start.row !== this.pos.row) return; if (this.$updating) return; this.$updating = true; - var lengthDiff = delta.action === "insertText" ? range.end.column - range.start.column : range.start.column - range.end.column; + var lengthDiff = delta.action === "insert" ? range.end.column - range.start.column : range.start.column - range.end.column; if(range.start.column >= this.pos.column && range.start.column <= this.pos.column + this.length + 1) { var distanceFromStart = range.start.column - this.pos.column; this.length += lengthDiff; if(!this.session.$fromUndo) { - if(delta.action === "insertText") { + if(delta.action === "insert") { for (var i = this.others.length - 1; i >= 0; i--) { var otherPos = this.others[i]; var newPos = {row: otherPos.row, column: otherPos.column + distanceFromStart}; if(otherPos.row === range.start.row && range.start.column < otherPos.column) newPos.column += lengthDiff; - this.doc.insert(newPos, delta.text); + this.doc.insertLines(newPos, delta.lines); } - } else if(delta.action === "removeText") { + } else if(delta.action === "delete") { for (var i = this.others.length - 1; i >= 0; i--) { var otherPos = this.others[i]; var newPos = {row: otherPos.row, column: otherPos.column + distanceFromStart}; @@ -179,7 +179,7 @@ var PlaceHolder = function(session, length, pos, others, mainClass, othersClass) } } // Special case: insert in beginning - if(range.start.column === this.pos.column && delta.action === "insertText") { + if(range.start.column === this.pos.column && delta.action === "insert") { setTimeout(function() { this.pos.setPosition(this.pos.row, this.pos.column - lengthDiff); for (var i = 0; i < this.others.length; i++) { @@ -191,7 +191,7 @@ var PlaceHolder = function(session, length, pos, others, mainClass, othersClass) } }.bind(this), 0); } - else if(range.start.column === this.pos.column && delta.action === "removeText") { + else if(range.start.column === this.pos.column && delta.action === "delete") { setTimeout(function() { for (var i = 0; i < this.others.length; i++) { var other = this.others[i]; diff --git a/lib/ace/placeholder_test.js b/lib/ace/placeholder_test.js index 97e561fd..dc7e4b23 100644 --- a/lib/ace/placeholder_test.js +++ b/lib/ace/placeholder_test.js @@ -53,9 +53,9 @@ module.exports = { new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]); editor.moveCursorTo(0, 5); - editor.insert('b'); + editor.insertText('b'); assert.equal(session.doc.getValue(), "var ab = 10;\nconsole.log(ab, ab);"); - editor.insert('cd'); + editor.insertText('cd'); assert.equal(session.doc.getValue(), "var abcd = 10;\nconsole.log(abcd, abcd);"); editor.remove('left'); editor.remove('left'); @@ -70,7 +70,7 @@ module.exports = { new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]); editor.moveCursorTo(2, 0); - editor.insert('b'); + editor.insertText('b'); assert.equal(session.doc.getValue(), "var a = 10;\nconsole.log(a, a);\nb"); }, @@ -81,12 +81,12 @@ module.exports = { var p = new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]); editor.moveCursorTo(0, 4); - editor.insert('$'); + editor.insertText('$'); assert.equal(session.doc.getValue(), "var $a = 10;\nconsole.log($a, $a);"); editor.moveCursorTo(0, 4); // Have to put this in a setTimeout because the anchor is only fixed later. setTimeout(function() { - editor.insert('v'); + editor.insertText('v'); assert.equal(session.doc.getValue(), "var v$a = 10;\nconsole.log(v$a, v$a);"); next(); }, 10); @@ -99,10 +99,10 @@ module.exports = { var p = new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]); editor.moveCursorTo(0, 5); - editor.insert('b'); + editor.insertText('b'); assert.equal(session.doc.getValue(), "var ab = 10;\nconsole.log(ab, ab);"); p.detach(); - editor.insert('cd'); + editor.insertText('cd'); assert.equal(session.doc.getValue(), "var abcd = 10;\nconsole.log(ab, ab);"); }, @@ -136,8 +136,8 @@ module.exports = { var p = new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]); editor.moveCursorTo(0, 5); - editor.insert('b'); - editor.insert('cd'); + editor.insertText('b'); + editor.insertText('cd'); editor.remove('left'); assert.equal(session.doc.getValue(), "var abc = 10;\nconsole.log(abc, abc);"); // Wait a little for the changes to enter the undo stack From 27768230c86eadb6f3a65f6c212af6c0f6a2972b Mon Sep 17 00:00:00 2001 From: aldendaniels Date: Wed, 1 Jan 2014 11:05:27 -0600 Subject: [PATCH 002/476] Maintain public API and update coding convention This seeks to keep the public API in-tact while improving method names within ace by keeping the old methods as wrappers around the new better-named methods. For example, document.insert() now simply calls document.insertText() and warns the caller via a console.log() that they are using a deprecated method. I've also updated the coding style of my changes (where I noticed discrepancies) to match the rest of Ace. --- lib/ace/anchor.js | 22 ++++------ lib/ace/anchor_test.js | 4 +- lib/ace/document.js | 80 +++++++++++++++++++++--------------- lib/ace/document_test.js | 2 +- lib/ace/edit_session.js | 6 +++ lib/ace/edit_session_test.js | 4 +- lib/ace/editor.js | 6 +++ lib/ace/placeholder.js | 2 +- 8 files changed, 73 insertions(+), 53 deletions(-) diff --git a/lib/ace/anchor.js b/lib/ace/anchor.js index 91a62eb0..affb4f19 100644 --- a/lib/ace/anchor.js +++ b/lib/ace/anchor.js @@ -117,33 +117,29 @@ var Anchor = exports.Anchor = function(doc, row, column) { var deltaEnd = (deltaIsInsert ? deltaStart : delta.range.end); // Collapse insert range. // DELTA AFTER POINT: No change needed. - if (_pointsInOrder(point, deltaStart, moveIfEqual)) - { - return ( - { + if (_pointsInOrder(point, deltaStart, moveIfEqual)) { + return { row: point.row, column: point.column - }); + }; } // DELTA BEFORE POINT: Move point by delta shift. - if (_pointsInOrder(deltaEnd, point, !moveIfEqual)) - { - return ( - { + if (_pointsInOrder(deltaEnd, point, !moveIfEqual)) { + return { row: point.row + deltaRowShift, column: point.column + (point.row == deltaEnd.row ? deltaColShift : 0) - }); + }; } // DELTA ENVELOPS POINT (delete only): Move point to delta start. if (delta.action != 'delete') throw 'Delete action expected.'; - return ( - { + + return { row: deltaStart.row, column: deltaStart.column - }); + }; } var delta = e.data; diff --git a/lib/ace/anchor_test.js b/lib/ace/anchor_test.js index 0588c9b6..0661765b 100644 --- a/lib/ace/anchor_test.js +++ b/lib/ace/anchor_test.js @@ -70,7 +70,7 @@ module.exports = { var doc = new Document("juhu\nkinners"); var anchor = new Anchor(doc, 1, 4); - doc.insertLines({row: 0, column: 0}, ['', '']); + doc.insertMergedLines({row: 0, column: 0}, ['', '']); assert.position(anchor.getPosition(), 2, 4); }, @@ -78,7 +78,7 @@ module.exports = { var doc = new Document("juhu\nkinners"); var anchor = new Anchor(doc, 1, 4); - doc.insertLines({row: 1, column: 2}, ['', '']); + doc.insertMergedLines({row: 1, column: 2}, ['', '']); assert.position(anchor.getPosition(), 2, 2); }, diff --git a/lib/ace/document.js b/lib/ace/document.js index ace69e32..07726de7 100644 --- a/lib/ace/document.js +++ b/lib/ace/document.js @@ -59,7 +59,7 @@ var Document = function(textOrLines) { if (textOrLines.length == 0) { this.$lines = [""]; } else if (Array.isArray(textOrLines)) { - this.insertLines({row: 0, column: 0}, textOrLines); + this.insertMergedLines({row: 0, column: 0}, textOrLines); } else { this.insertText({row: 0, column:0}, textOrLines); } @@ -230,13 +230,10 @@ var Document = function(textOrLines) { this.$clipPosition = function(position) { var length = this.getLength(); - if (position.row >= length) - { + if (position.row >= length) { position.row = Math.max(0, length - 1); position.column = this.getLine(length - 1).length; - } - else - { + } else { position.row = Math.max(0, position.row); position.column = Math.min(Math.max(position.column, 0), this.getLine(position.row).length); } @@ -264,16 +261,15 @@ var Document = function(textOrLines) { throw errorText; } + // Validate lines. if (!delta.lines instanceof Array) - { fnThrow('Delta object lines must be an array'); - } + // Validate range type. if (!delta.range instanceof Range) - { fnThrow('Range object is not an instance of the Range class'); - } + // Validate start point. var start = delta.range.start; if (Math.min(Math.max(start.row, 0), this.getLength() - 1 ) != start.row || Math.min(Math.max(start.column, 0), this.$lines[start.row].length) != start.column) @@ -281,16 +277,37 @@ var Document = function(textOrLines) { fnThrow('Range start point not contained in document'); } + // Validate ending row offset. if (delta.lines.length - 1 != delta.range.end.row - delta.range.start.row) - { fnThrow('Range row offsets does not match delta lines'); - } - // TODO: Validate that the ending column offset matches the lines. - // TODO: Validate for deletions that the lines deleted match the lines - // in the document. + // TODO: + // - Validate that the ending column offset matches the lines. + // - Validate the deleted lines match the lines in the document. }, + // Deprecated methods retained for backwards compatibility. + this.insert = function(position, text){ + console.log('Warning: document.insert is deprecated. Use the insertText method instead.'); + return this.insertText(position, text); + } + this.insertLines = function(row, lines) { + console.log('Warning: document.insertLines is deprecated. Use the insertFullLines method instead.'); + return this.insertFullLines(row, lines); + } + this.removeLines = function(firstRow, lastRow) { + console.log('Warning: document.removeLines is deprecated. Use the removeFullLines method instead.'); + return this.removeFullLines(firstRow, lastRow); + } + this.insertNewLine = function(position) { + console.log('Warning: document.insertNewLine is deprecated. Use insertMergedLines(position, [\'\', \'\']) instead.'); + return this.insertMergedLines(position, ['', '']); + } + this.insertInLine = function(position, text) { + console.log('Warning: document.insertInLine is deprecated. Use insertText instead.'); + return this.insertText(position, text); + } + /** * Inserts a block of `text` at the indicated `position`. * @param {Object} position The position to start inserting at; it's an object that looks like `{ row: row, column: column}` @@ -304,9 +321,9 @@ var Document = function(textOrLines) { if (this.getLength() <= 1) this.$detectNewLine(text); - return this.insertLines(position, this.$split(text)); + return this.insertMergedLines(position, this.$split(text)); }; - + /** * Fires whenever the document changes. * @@ -338,28 +355,26 @@ var Document = function(textOrLines) { * ``` * **/ - this.insertFullLines = function(row, lines) - { + this.insertFullLines = function(row, lines) { // Clip to document. // Allow one past the document end. row = Math.min(Math.max(row, 0), this.getLength()); // Calculate insertion point. var column = 0; - if (row < this.getLength()) // Insert before the specified row. - { + if (row < this.getLength()) { + // Insert before the specified row. lines = lines.concat(['']); column = 0; - } - else // Insert after the last row in the document. - { + } else { + // Insert after the last row in the document. lines = [''].concat(lines); row--; var column = this.$lines[row].length; } // Insert. - this.insertLines({row: row, column: column}, lines); + this.insertMergedLines({row: row, column: column}, lines); }, /** @@ -376,7 +391,7 @@ var Document = function(textOrLines) { * ``` * **/ - this.insertLines = function(position, lines){ + this.insertMergedLines = function(position, lines) { // Calculate insertion range end point. this.$clipPosition(position); @@ -405,8 +420,7 @@ var Document = function(textOrLines) { // Apply delta (emits change). range = this.$getClippedRange(range); - this.applyDelta( - { + this.applyDelta({ action: 'delete', range: range, lines: this._getLinesForRange(range), @@ -483,14 +497,13 @@ var Document = function(textOrLines) { **/ this.removeNewLine = function(row) { - if (row < this.getLength() - 1 && row >= 0) - { + if (row < this.getLength() - 1 && row >= 0) { // Apply delta (emits change). this.applyDelta({ action: "delete", range: new Range(row, this.getLine(row).length, row + 1, 0), lines: ['', ''] - }); + }); } }; @@ -558,8 +571,7 @@ var Document = function(textOrLines) { { case 'insert': splitLine(this.$lines, delta.range.start); - for (var i = 0; i < delta.lines.length; i++) - { + for (var i = 0; i < delta.lines.length; i++) { var row = delta.range.start.row + 1 + i; this.$lines.splice(row, 0, delta.lines[i]); } @@ -571,7 +583,7 @@ var Document = function(textOrLines) { splitLine(this.$lines, delta.range.end); splitLine(this.$lines, delta.range.start); this.$lines.splice( - delta.range.start.row + 1, // Where to start deleting + delta.range.start.row + 1, // Where to start deleting delta.range.end.row - delta.range.start.row + 1 // Num lines to delete. ); joinLineWithNext(this.$lines, delta.range.start.row); diff --git a/lib/ace/document_test.js b/lib/ace/document_test.js index 50bd8b49..c29d6133 100644 --- a/lib/ace/document_test.js +++ b/lib/ace/document_test.js @@ -65,7 +65,7 @@ module.exports = { var deltas = []; doc.on("change", function(e) { deltas.push(e.data); }); - doc.insertLines({row: 0, column: 1}, ['', '']); + doc.insertMergedLines({row: 0, column: 1}, ['', '']); assert.equal(doc.getValue(), ["1", "2", "34"].join("\n")); var d = deltas.concat(); diff --git a/lib/ace/edit_session.js b/lib/ace/edit_session.js index 98c255a4..e48bdc37 100644 --- a/lib/ace/edit_session.js +++ b/lib/ace/edit_session.js @@ -1127,6 +1127,12 @@ var EditSession = function(text, mode) { this.getTextRange = function(range) { return this.doc.getTextRange(range || this.selection.getRange()); }; + + // Deprecated method retained for backwards compatibility. + this.insert = function(position, text){ + console.log('Warning: editsession.insert is deprecated. Use the insertText method instead.'); + return this.insertText(position, text) + } /** * Inserts a block of `text` and the indicated `position`. diff --git a/lib/ace/edit_session_test.js b/lib/ace/edit_session_test.js index 855da4d1..fd20259c 100644 --- a/lib/ace/edit_session_test.js +++ b/lib/ace/edit_session_test.js @@ -430,11 +430,11 @@ module.exports = { session.setTabSize(4); assert.equal(session.getScreenWidth(), 2); - session.doc.insertLines({row: 0, column: Infinity}, ['', '']); + session.doc.insertMergedLines({row: 0, column: Infinity}, ['', '']); session.doc.insertFullLines(1, ["123"]); assert.equal(session.getScreenWidth(), 3); - session.doc.insertLines({row: 0, column: Infinity}, ['', '']); + session.doc.insertMergedLines({row: 0, column: Infinity}, ['', '']); session.doc.insertFullLines(1, ["\t\t"]); assert.equal(session.getScreenWidth(), 8); diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 3926fc81..2854eaaf 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -825,6 +825,12 @@ var Editor = function(renderer, session) { this.execCommand = function(command, args) { this.commands.exec(command, this, args); }; + + // Deprecated method retained for backwards compatibility. + this.insert = function(text){ + console.log('Warning: editor.insert is deprecated. Use the insertText method instead.'); + return this.insertText(text) + } /** * Inserts `text` into wherever the cursor is pointing. diff --git a/lib/ace/placeholder.js b/lib/ace/placeholder.js index 9d0a3c3c..c9a44e50 100644 --- a/lib/ace/placeholder.js +++ b/lib/ace/placeholder.js @@ -167,7 +167,7 @@ var PlaceHolder = function(session, length, pos, others, mainClass, othersClass) var newPos = {row: otherPos.row, column: otherPos.column + distanceFromStart}; if(otherPos.row === range.start.row && range.start.column < otherPos.column) newPos.column += lengthDiff; - this.doc.insertLines(newPos, delta.lines); + this.doc.insertMergedLines(newPos, delta.lines); } } else if(delta.action === "delete") { for (var i = this.others.length - 1; i >= 0; i--) { From 026af74016a2829003a7dfa2ccde906309abd40b Mon Sep 17 00:00:00 2001 From: aldendaniels Date: Wed, 1 Jan 2014 12:04:12 -0600 Subject: [PATCH 003/476] Fix render bugs Both were introduced in 2e6f12725bc01011c545c99c1f1c1da6065063d9. --- lib/ace/editor.js | 8 ++++++-- lib/ace/layer/gutter.js | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 2854eaaf..73873b4a 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -601,10 +601,14 @@ var Editor = function(renderer, session) { * **/ this.onDocumentChange = function(e) { - this.renderer.updateLines(e.data.range.start.row, e.data.range.end.row); + + // Rerender and emit "change" event. + var range = e.data.range; + var lastRow = (range.start.row == range.end.row ? range.end.row : Infinity); + this.renderer.updateLines(range.start.row, lastRow); this._emit("change", e); - // update cursor because tab characters can influence the cursor position. + // Update cursor because tab characters can influence the cursor position. this.$cursorChange(); }; diff --git a/lib/ace/layer/gutter.js b/lib/ace/layer/gutter.js index 9d813d2a..82dcf245 100644 --- a/lib/ace/layer/gutter.js +++ b/lib/ace/layer/gutter.js @@ -109,7 +109,7 @@ var Gutter = function(parentEl) { if (len === 0) { // do nothing } else if (delta.action == "delete") { - this.$annotations.splice(firstRow, len, null); + this.$annotations.splice(firstRow, len + 1, null); } else { var args = new Array(len + 1); args.unshift(firstRow, 1); From 810e196cc6f1d49da208c8c4ed98045e6c29ebbb Mon Sep 17 00:00:00 2001 From: aldendaniels Date: Wed, 1 Jan 2014 12:06:19 -0600 Subject: [PATCH 004/476] Use console.warn for deprecated methods console.warn makes better sense than console.log and matches similar warnings in ace (see gutter.js for example). --- lib/ace/document.js | 10 +++++----- lib/ace/edit_session.js | 2 +- lib/ace/editor.js | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/ace/document.js b/lib/ace/document.js index 07726de7..c689553c 100644 --- a/lib/ace/document.js +++ b/lib/ace/document.js @@ -288,23 +288,23 @@ var Document = function(textOrLines) { // Deprecated methods retained for backwards compatibility. this.insert = function(position, text){ - console.log('Warning: document.insert is deprecated. Use the insertText method instead.'); + console.warn('Use of document.insert is deprecated. Use the insertText method instead.'); return this.insertText(position, text); } this.insertLines = function(row, lines) { - console.log('Warning: document.insertLines is deprecated. Use the insertFullLines method instead.'); + console.warn('Use of document.insertLines is deprecated. Use the insertFullLines method instead.'); return this.insertFullLines(row, lines); } this.removeLines = function(firstRow, lastRow) { - console.log('Warning: document.removeLines is deprecated. Use the removeFullLines method instead.'); + console.warn('Use of document.removeLines is deprecated. Use the removeFullLines method instead.'); return this.removeFullLines(firstRow, lastRow); } this.insertNewLine = function(position) { - console.log('Warning: document.insertNewLine is deprecated. Use insertMergedLines(position, [\'\', \'\']) instead.'); + console.warn('Use of document.insertNewLine is deprecated. Use insertMergedLines(position, [\'\', \'\']) instead.'); return this.insertMergedLines(position, ['', '']); } this.insertInLine = function(position, text) { - console.log('Warning: document.insertInLine is deprecated. Use insertText instead.'); + console.warn('Use of document.insertInLine is deprecated. Use insertText instead.'); return this.insertText(position, text); } diff --git a/lib/ace/edit_session.js b/lib/ace/edit_session.js index e48bdc37..0845418b 100644 --- a/lib/ace/edit_session.js +++ b/lib/ace/edit_session.js @@ -1130,7 +1130,7 @@ var EditSession = function(text, mode) { // Deprecated method retained for backwards compatibility. this.insert = function(position, text){ - console.log('Warning: editsession.insert is deprecated. Use the insertText method instead.'); + console.warn('Use of editsession.insert is deprecated. Use the insertText method instead.'); return this.insertText(position, text) } diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 73873b4a..6f5980a1 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -832,7 +832,7 @@ var Editor = function(renderer, session) { // Deprecated method retained for backwards compatibility. this.insert = function(text){ - console.log('Warning: editor.insert is deprecated. Use the insertText method instead.'); + console.warn('Use of editor.insert is deprecated. Use the insertText method instead.'); return this.insertText(text) } From 612478e39f26e32c985ca8e66b8a4a37ca3834a5 Mon Sep 17 00:00:00 2001 From: aldendaniels Date: Wed, 1 Jan 2014 14:45:54 -0600 Subject: [PATCH 005/476] Speed up single-line deltas 2e6f12725bc01011c545c99c1f1c1da6065063d9 slowed down the application of deltas that only affect a single line. The slow-down, though trivial for a single line, is significant for operations than separately modify thousands of rows (such as indenting a large document). This commit speeds up single-line deltas by avoiding unnecessary calls to splitLine() and joinLineWithNext(). --- lib/ace/document.js | 70 ++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/lib/ace/document.js b/lib/ace/document.js index c689553c..9d6aaac3 100644 --- a/lib/ace/document.js +++ b/lib/ace/document.js @@ -261,9 +261,13 @@ var Document = function(textOrLines) { throw errorText; } + // Validate action. + if (delta.action != 'insert' && delta.action != 'delete') + fnThrow('Delta action must be "insert" or "delete".'); + // Validate lines. if (!delta.lines instanceof Array) - fnThrow('Delta object lines must be an array'); + fnThrow('Delta lines must be an array'); // Validate range type. if (!delta.range instanceof Range) @@ -567,30 +571,50 @@ var Document = function(textOrLines) { this.$validateDelta(delta); // Apply delta. - switch (delta.action) + if (delta.range.start.row == delta.range.end.row) { - case 'insert': - splitLine(this.$lines, delta.range.start); - for (var i = 0; i < delta.lines.length; i++) { - var row = delta.range.start.row + 1 + i; - this.$lines.splice(row, 0, delta.lines[i]); - } - joinLineWithNext(this.$lines, delta.range.start.row); - joinLineWithNext(this.$lines, delta.range.end.row); - break; + // Apply single-line delta. + // Note: The multi-line code below correctly handle single-line + // deltas too, but we need to short-circuit for speed. + var row = delta.range.start.row; + var startColumn = delta.range.start.column; + var endColumn = delta.range.end.column; + var line = this.$lines[row]; + switch (delta.action) { + + case 'insert': + this.$lines[row] = line.substring(0, startColumn) + delta.lines[0] + line.substring(startColumn); + break; + + case 'delete': + this.$lines[row] = line.substring(0, startColumn) + line.substring(endColumn); + break; + } + } else { - case 'delete': - splitLine(this.$lines, delta.range.end); - splitLine(this.$lines, delta.range.start); - this.$lines.splice( - delta.range.start.row + 1, // Where to start deleting - delta.range.end.row - delta.range.start.row + 1 // Num lines to delete. - ); - joinLineWithNext(this.$lines, delta.range.start.row); - break; - - default: - throw 'Invalid delta type: ' + delta.action + // Apply multi-line delta. + switch (delta.action) { + + case 'insert': + splitLine(this.$lines, delta.range.start); + for (var i = 0; i < delta.lines.length; i++) { + var row = delta.range.start.row + 1 + i; + this.$lines.splice(row, 0, delta.lines[i]); + } + joinLineWithNext(this.$lines, delta.range.start.row); + joinLineWithNext(this.$lines, delta.range.end.row); + break; + + case 'delete': + splitLine(this.$lines, delta.range.end); + splitLine(this.$lines, delta.range.start); + this.$lines.splice( + delta.range.start.row + 1, // Where to start deleting + delta.range.end.row - delta.range.start.row + 1 // Num lines to delete. + ); + joinLineWithNext(this.$lines, delta.range.start.row); + break; + } } this._emit("change", { data: delta }); From b503e65e0370cb01a251a34719bde5ddb508e2c4 Mon Sep 17 00:00:00 2001 From: aldendaniels Date: Fri, 3 Jan 2014 14:59:22 -0600 Subject: [PATCH 006/476] Break on applyDelta into its own module This makes it possible to break out helper functions without exposing them to the rest of the document class. Also, long term, we may want to have a stand-alone test suite for applyDelta, so it makes sense in its own file. All other changes involve syntax corrections (some syntax issues were mine, others pre-existed) to make the documentation compilation work. --- lib/ace/apply_delta.js | 137 +++++++++++++++++++++++++++ lib/ace/document.js | 204 +++++++++++++--------------------------- lib/ace/edit_session.js | 14 +-- lib/ace/editor.js | 8 +- lib/ace/scrollbar.js | 8 +- 5 files changed, 215 insertions(+), 156 deletions(-) create mode 100644 lib/ace/apply_delta.js diff --git a/lib/ace/apply_delta.js b/lib/ace/apply_delta.js new file mode 100644 index 00000000..02c0a498 --- /dev/null +++ b/lib/ace/apply_delta.js @@ -0,0 +1,137 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Distributed under the BSD license: + * + * Copyright (c) 2010, Ajax.org B.V. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Ajax.org B.V. nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 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 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * ***** END LICENSE BLOCK ***** */ + +define(function(require, exports, module) { +"use strict"; + +var Range = require("./range").Range; + +function splitLine (lines, point) { + var text = lines[point.row]; + lines[point.row] = text.slice(0, point.column); + lines.splice(point.row + 1, 0, text.slice(point.column)); +} + +function joinLineWithNext(lines, row) { + lines[row] += lines[row + 1]; + lines.splice(row + 1, 1); +} + +function throwDeltaError(delta, errorText){ + errorText = 'Invalid Delta: ' + errorText; + console.log(errorText, delta); + throw errorText; +} + +function validateDelta(lines, delta) { + + // Validate action. + if (delta.action != 'insert' && delta.action != 'delete') + fnThrow('Delta action must be "insert" or "delete".'); + + // Validate lines. + if (!delta.lines instanceof Array) + fnThrow('Delta lines must be an array'); + + // Validate range type. + if (!delta.range instanceof Range) + fnThrow('Range object is not an instance of the Range class'); + + // Validate start point. + var start = delta.range.start; + if (Math.min(Math.max(start.row, 0), lines.length - 1 ) != start.row || + Math.min(Math.max(start.column, 0), lines[start.row].length) != start.column) + { + fnThrow('Range start point not contained in document'); + } + + // Validate ending row offset. + if (delta.lines.length - 1 != delta.range.end.row - delta.range.start.row) + fnThrow('Range row offsets does not match delta lines'); + + // TODO: + // - Validate that the ending column offset matches the lines. + // - Validate the deleted lines match the lines in the document. +} + + +exports.applyDelta = function(lines, delta) { + + // Validate delta. + validateDelta(lines, delta); + + // Apply delta. + if (delta.range.start.row == delta.range.end.row) + { + // Apply single-line delta. + // Note: The multi-line code below correctly handle single-line + // deltas too, but we need to short-circuit for speed. + var row = delta.range.start.row; + var startColumn = delta.range.start.column; + var endColumn = delta.range.end.column; + var line = lines[row]; + switch (delta.action) { + + case 'insert': + lines[row] = line.substring(0, startColumn) + delta.lines[0] + line.substring(startColumn); + break; + + case 'delete': + lines[row] = line.substring(0, startColumn) + line.substring(endColumn); + break; + } + } else { + + // Apply multi-line delta. + switch (delta.action) { + + case 'insert': + splitLine(lines, delta.range.start); + for (var i = 0; i < delta.lines.length; i++) { + var row = delta.range.start.row + 1 + i; + lines.splice(row, 0, delta.lines[i]); + } + joinLineWithNext(lines, delta.range.start.row); + joinLineWithNext(lines, delta.range.end.row); + break; + + case 'delete': + splitLine(lines, delta.range.end); + splitLine(lines, delta.range.start); + lines.splice( + delta.range.start.row + 1, // Where to start deleting + delta.range.end.row - delta.range.start.row + 1 // Num lines to delete. + ); + joinLineWithNext(lines, delta.range.start.row); + break; + } + } +} +}); diff --git a/lib/ace/document.js b/lib/ace/document.js index 9d6aaac3..768182a4 100644 --- a/lib/ace/document.js +++ b/lib/ace/document.js @@ -32,6 +32,7 @@ define(function(require, exports, module) { "use strict"; var oop = require("./lib/oop"); +var applyDelta = require("./apply_delta").applyDelta; var EventEmitter = require("./lib/event_emitter").EventEmitter; var Range = require("./range").Range; var Anchor = require("./anchor").Anchor; @@ -56,7 +57,7 @@ var Document = function(textOrLines) { // There has to be one line at least in the document. If you pass an empty // string to the insert function, nothing will happen. Workaround. - if (textOrLines.length == 0) { + if (textOrLines.length === 0) { this.$lines = [""]; } else if (Array.isArray(textOrLines)) { this.insertMergedLines({row: 0, column: 0}, textOrLines); @@ -107,10 +108,10 @@ var Document = function(textOrLines) { **/ // check for IE split bug - if ("aaa".split(/a/).length == 0) + if ("aaa".split(/a/).length === 0) this.$split = function(text) { return text.replace(/\r\n|\r/g, "\n").split("\n"); - } + }; else this.$split = function(text) { return text.split(/\r\n|\r|\n/); @@ -205,30 +206,41 @@ var Document = function(textOrLines) { }; /** - * [Given a range within the document, this function returns all the text within that range as a single string.]{: #Document.getTextRange.desc} - * @param {Range} range The range to work with + * Returns all the text within `range` as a single string. + * @param {Range} range The range to work with. * * @returns {String} **/ this.getTextRange = function(range) { - return this._getLinesForRange(range).join(this.getNewLineCharacter()); + return this.getLinesForRange(range).join(this.getNewLineCharacter()); }; - this._getLinesForRange = function(range) { + /** + * Returns all the text within `range` as an array of lines. + * @param {Range} range The range to work with. + * + * @returns {Array} + **/ + this.getLinesForRange = function(range) { + var lines; if (range.start.row == range.end.row) { - return [this.getLine(range.start.row) - .substring(range.start.column, range.end.column)]; + + // Handle a single-line range. + lines = [this.getLine(range.start.row).substring(range.start.column, range.end.column)]; + + } else { + + // Handle a multi-line range. + lines = this.getLines(range.start.row, range.end.row); + lines[0] = (lines[0] || "").substring(range.start.column); + var l = lines.length - 1; + if (range.end.row - range.start.row == l) + lines[l] = lines[l].substring(0, range.end.column); } - var lines = this.getLines(range.start.row, range.end.row); - lines[0] = (lines[0] || "").substring(range.start.column); - var l = lines.length - 1; - if (range.end.row - range.start.row == l) - lines[l] = lines[l].substring(0, range.end.column); return lines; }; this.$clipPosition = function(position) { - var length = this.getLength(); if (position.row >= length) { position.row = Math.max(0, length - 1); @@ -240,8 +252,8 @@ var Document = function(textOrLines) { return position; }; - this.$getClippedRange = function(range) - { + this.$getClippedRange = function(range) { + // Get Range object. if (!range instanceof Range) range = Range.fromPoints(range.start, range.end); @@ -250,67 +262,29 @@ var Document = function(textOrLines) { this.$clipPosition(range.start); this.$clipPosition(range.end); return range; - } + }; - this.$validateDelta = function(delta) - { - function fnThrow(errorText) - { - errorText = 'Invalid Delta: ' + errorText; - console.log(errorText, delta); - throw errorText; - } - - // Validate action. - if (delta.action != 'insert' && delta.action != 'delete') - fnThrow('Delta action must be "insert" or "delete".'); - - // Validate lines. - if (!delta.lines instanceof Array) - fnThrow('Delta lines must be an array'); - - // Validate range type. - if (!delta.range instanceof Range) - fnThrow('Range object is not an instance of the Range class'); - - // Validate start point. - var start = delta.range.start; - if (Math.min(Math.max(start.row, 0), this.getLength() - 1 ) != start.row || - Math.min(Math.max(start.column, 0), this.$lines[start.row].length) != start.column) - { - fnThrow('Range start point not contained in document'); - } - - // Validate ending row offset. - if (delta.lines.length - 1 != delta.range.end.row - delta.range.start.row) - fnThrow('Range row offsets does not match delta lines'); - - // TODO: - // - Validate that the ending column offset matches the lines. - // - Validate the deleted lines match the lines in the document. - }, - // Deprecated methods retained for backwards compatibility. this.insert = function(position, text){ console.warn('Use of document.insert is deprecated. Use the insertText method instead.'); return this.insertText(position, text); - } + }; this.insertLines = function(row, lines) { console.warn('Use of document.insertLines is deprecated. Use the insertFullLines method instead.'); return this.insertFullLines(row, lines); - } + }; this.removeLines = function(firstRow, lastRow) { console.warn('Use of document.removeLines is deprecated. Use the removeFullLines method instead.'); return this.removeFullLines(firstRow, lastRow); - } + }; this.insertNewLine = function(position) { console.warn('Use of document.insertNewLine is deprecated. Use insertMergedLines(position, [\'\', \'\']) instead.'); return this.insertMergedLines(position, ['', '']); - } + }; this.insertInLine = function(position, text) { console.warn('Use of document.insertInLine is deprecated. Use insertText instead.'); return this.insertText(position, text); - } + }; /** * Inserts a block of `text` at the indicated `position`. @@ -374,12 +348,12 @@ var Document = function(textOrLines) { // Insert after the last row in the document. lines = [''].concat(lines); row--; - var column = this.$lines[row].length; + column = this.$lines[row].length; } // Insert. this.insertMergedLines({row: row, column: column}, lines); - }, + }; /** * Inserts the elements in `lines` into the document, starting at the position index given by `row`. This method also triggers the `'change'` event. @@ -412,7 +386,7 @@ var Document = function(textOrLines) { }); return endPoint; - } + }; /** * Removes the `range` from the document. @@ -427,7 +401,7 @@ var Document = function(textOrLines) { this.applyDelta({ action: 'delete', range: range, - lines: this._getLinesForRange(range), + lines: this.getLinesForRange(range), }); return range.start; }; @@ -450,7 +424,7 @@ var Document = function(textOrLines) { this.applyDelta({ action: "delete", range: range, - lines: this._getLinesForRange(range) + lines: this.getLinesForRange(range) }); return range.start; @@ -487,7 +461,7 @@ var Document = function(textOrLines) { this.applyDelta({ action: "delete", range: range, - lines: this._getLinesForRange(range) + lines: this.getLinesForRange(range) }); // Return the deleted lines. @@ -524,7 +498,7 @@ var Document = function(textOrLines) { this.replace = function(range, text) { if (!range instanceof Range) range = Range.fromPoints(range.start, range.end); - if (text.length == 0 && range.isEmpty()) + if (text.length === 0 && range.isEmpty()) return range.start; // Shortcut: If the text we want to insert is the same as it is already @@ -533,8 +507,9 @@ var Document = function(textOrLines) { return range.end; this.remove(range); + var end; if (text) { - var end = this.insertText(range.start, text); + end = this.insertText(range.start, text); } else { end = range.start; @@ -544,7 +519,8 @@ var Document = function(textOrLines) { }; /** - * Applies all the changes previously accumulated. These can be either `'insert'` or `'delete'`. + * Applies all changes in `deltas` to the document. + * @param {Array} deltas An array of delta objects (can include 'insert' and 'delete' actions) **/ this.applyDeltas = function(deltas) { for (var i=0; i=0; i--) { @@ -629,6 +538,19 @@ var Document = function(textOrLines) { } }; + /** + * Applies `delta` to the document. + * @param {Object} delta A delta object (can include 'insert' and 'delete' actions) + **/ + this.applyDelta = function(delta) { + applyDelta(this.$lines, delta); + this._emit("change", { data: delta }); + }; + + /** + * Reverts `delta` from the document. + * @param {Object} delta A delta object (can include 'insert' and 'delete' actions) + **/ this.revertDelta = function(delta) { this.applyDelta({ @@ -636,8 +558,8 @@ var Document = function(textOrLines) { range: delta.range.clone(), lines: delta.lines.slice() }); - }, - + }; + /** * Converts an index position in a document to a `{row, column}` object. * diff --git a/lib/ace/edit_session.js b/lib/ace/edit_session.js index 0845418b..0d4df080 100644 --- a/lib/ace/edit_session.js +++ b/lib/ace/edit_session.js @@ -1131,8 +1131,8 @@ var EditSession = function(text, mode) { // Deprecated method retained for backwards compatibility. this.insert = function(position, text){ console.warn('Use of editsession.insert is deprecated. Use the insertText method instead.'); - return this.insertText(position, text) - } + return this.insertText(position, text); + }; /** * Inserts a block of `text` and the indicated `position`. @@ -1168,8 +1168,8 @@ var EditSession = function(text, mode) { * **/ this.removeFullLines = function(firstRow, lastRow){ - return this.doc.removeFullLines(firstRow, lastRow) - } + return this.doc.removeFullLines(firstRow, lastRow); + }; /** * Reverts previous changes to your document. @@ -1619,7 +1619,7 @@ var EditSession = function(text, mode) { * @private **/ this.adjustWrapLimit = function(desiredLimit, $printMargin) { - var limits = this.$wrapLimitRange + var limits = this.$wrapLimitRange; if (limits.max < 0) limits = {min: $printMargin, max: $printMargin}; var wrapLimit = this.$constrainWrapLimit(desiredLimit, limits.min, limits.max); @@ -1738,7 +1738,7 @@ var EditSession = function(text, mode) { var foldLine = this.getFoldLine(firstRow); var idx = 0; if (foldLine) { - var cmp = foldLine.range.compareInside(start.row, start.column) + var cmp = foldLine.range.compareInside(start.row, start.column); // Inside of the foldLine range. Need to split stuff up. if (cmp == 0) { foldLine = foldLine.split(start.row, start.column); @@ -2215,7 +2215,7 @@ var EditSession = function(text, mode) { return { row: maxRow, column: this.getLine(maxRow).length - } + }; } else { line = this.getLine(docRow); foldLine = null; diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 6f5980a1..79f59e68 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -833,8 +833,8 @@ var Editor = function(renderer, session) { // Deprecated method retained for backwards compatibility. this.insert = function(text){ console.warn('Use of editor.insert is deprecated. Use the insertText method instead.'); - return this.insertText(text) - } + return this.insertText(text); + }; /** * Inserts `text` into wherever the cursor is pointing. @@ -876,7 +876,7 @@ var Editor = function(renderer, session) { } if (text == "\n" || text == "\r\n") { - var line = session.getLine(cursor.row) + var line = session.getLine(cursor.row); if (cursor.column > line.search(/\S|$/)) { var d = line.substr(cursor.column).search(/\S|$/); session.doc.removeInLine(cursor.row, cursor.column, cursor.column + d); @@ -1362,7 +1362,7 @@ var Editor = function(renderer, session) { } } - var line = session.getLine(range.start.row) + var line = session.getLine(range.start.row); var position = range.start; var size = session.getTabSize(); var column = session.documentToScreenColumn(position.row, position.column); diff --git a/lib/ace/scrollbar.js b/lib/ace/scrollbar.js index 28f41fe4..466d1af1 100644 --- a/lib/ace/scrollbar.js +++ b/lib/ace/scrollbar.js @@ -156,9 +156,9 @@ oop.inherits(VScrollBar, ScrollBar); * Sets the scroll top of the scroll bar. * @param {Number} scrollTop The new scroll top **/ - // on chrome 17+ for small zoom levels after calling this function - // this.element.scrollTop != scrollTop which makes page to scroll up. this.setScrollTop = function(scrollTop) { + // on chrome 17+ for small zoom levels after calling this function + // this.element.scrollTop != scrollTop which makes page to scroll up. if (this.scrollTop != scrollTop) { this.skipEvent = true; this.scrollTop = this.element.scrollTop = scrollTop; @@ -249,9 +249,9 @@ oop.inherits(HScrollBar, ScrollBar); * Sets the scroll left of the scroll bar. * @param {Number} scrollTop The new scroll left **/ - // on chrome 17+ for small zoom levels after calling this function - // this.element.scrollTop != scrollTop which makes page to scroll up. this.setScrollLeft = function(scrollLeft) { + // on chrome 17+ for small zoom levels after calling this function + // this.element.scrollTop != scrollTop which makes page to scroll up. if (this.scrollLeft != scrollLeft) { this.skipEvent = true; this.scrollLeft = this.element.scrollLeft = scrollLeft; From 8624ab8dcb9bac917488feb8856e8b709418adb7 Mon Sep 17 00:00:00 2001 From: aldendaniels Date: Sat, 4 Jan 2014 01:59:30 -0600 Subject: [PATCH 007/476] Stop using splice.appy() in ace Since .apply() can't handle more than 65535 parameters, splice.apply() is brittle. It's also hard to read. This replaces splice.apply() calls throughout ace code with lang.spliceIntoArray(). --- lib/ace/background_tokenizer.js | 14 ++++++++------ lib/ace/edit_session.js | 14 +++++++------- lib/ace/edit_session/folding.js | 6 +++--- lib/ace/layer/gutter.js | 5 ++--- lib/ace/lib/lang.js | 14 ++++++++++++++ lib/ace/line_widgets.js | 5 ++--- lib/ace/mode/asciidoc_highlight_rules.js | 8 +++++--- lib/ace/mode/text_highlight_rules.js | 6 +++--- lib/ace/snippets.js | 18 +++++++++--------- 9 files changed, 53 insertions(+), 37 deletions(-) diff --git a/lib/ace/background_tokenizer.js b/lib/ace/background_tokenizer.js index cb3ffe23..4b82f19a 100644 --- a/lib/ace/background_tokenizer.js +++ b/lib/ace/background_tokenizer.js @@ -32,6 +32,7 @@ define(function(require, exports, module) { "use strict"; var oop = require("./lib/oop"); +var lang = require("./lib/lang"); var EventEmitter = require("./lib/event_emitter").EventEmitter; @@ -187,14 +188,15 @@ var BackgroundTokenizer = function(tokenizer, editor) { this.lines.splice(startRow, len + 1, null); this.states.splice(startRow, len + 1, null); } else { - var args = Array(len + 1); - args.unshift(startRow, 1); - this.lines.splice.apply(this.lines, args); - this.states.splice.apply(this.states, args); + this.lines.splice( startRow, 1); + this.states.splice(startRow, 1); + var toInsert = Array(len + 1); + lang.spliceIntoArray(this.lines, startRow, toInsert); + lang.spliceIntoArray(this.states, startRow, toInsert); } - + this.currentLine = Math.min(startRow, this.currentLine, this.doc.getLength()); - + this.stop(); }; diff --git a/lib/ace/edit_session.js b/lib/ace/edit_session.js index 0d4df080..cd14c1a5 100644 --- a/lib/ace/edit_session.js +++ b/lib/ace/edit_session.js @@ -1723,15 +1723,15 @@ var EditSession = function(text, mode) { } else { var args; if (useWrapMode) { - args = [firstRow, 0]; - for (var i = 0; i < len; i++) args.push([]); - this.$wrapData.splice.apply(this.$wrapData, args); + var toInsert = []; + for (var i = 0; i < len; i++) + toInsert.push([]); + lang.spliceIntoArray(this.$wrapData, firstRow, toInsert); } else { - args = Array(len); - args.unshift(firstRow, 0); - this.$rowLengthCache.splice.apply(this.$rowLengthCache, args); + var toInsert = Array(len); + lang.spliceIntoArray(this.$rowLengthCache, firstRow, toInsert); } - + // If some new line is added inside of a foldLine, then split // the fold line up. var foldLines = this.$foldData; diff --git a/lib/ace/edit_session/folding.js b/lib/ace/edit_session/folding.js index 9df35593..1563bd67 100644 --- a/lib/ace/edit_session/folding.js +++ b/lib/ace/edit_session/folding.js @@ -31,6 +31,7 @@ define(function(require, exports, module) { "use strict"; +var lang = require("../lib/lang"); var Range = require("../range").Range; var FoldLine = require("./fold_line").FoldLine; var Fold = require("./fold").Fold; @@ -836,9 +837,8 @@ function Folding() { } else if (delta.action == "delete") { this.foldWidgets.splice(firstRow, len + 1, null); } else { - var args = Array(len + 1); - args.unshift(firstRow, 1); - this.foldWidgets.splice.apply(this.foldWidgets, args); + this.foldWidgets.splice(firstRow, 1); + lang.spliceIntoArray(this.foldWidgets, firstRow, Array(len + 1)); } }; diff --git a/lib/ace/layer/gutter.js b/lib/ace/layer/gutter.js index 82dcf245..24f9c430 100644 --- a/lib/ace/layer/gutter.js +++ b/lib/ace/layer/gutter.js @@ -111,9 +111,8 @@ var Gutter = function(parentEl) { } else if (delta.action == "delete") { this.$annotations.splice(firstRow, len + 1, null); } else { - var args = new Array(len + 1); - args.unshift(firstRow, 1); - this.$annotations.splice.apply(this.$annotations, args); + this.$annotations.splice(firstRow, 1); + lang.spliceIntoArray(this.$annotations, firstRow, new Array(len + 1)); } }; diff --git a/lib/ace/lib/lang.js b/lib/ace/lib/lang.js index 4405b5c2..e0e9d826 100644 --- a/lib/ace/lib/lang.js +++ b/lib/ace/lib/lang.js @@ -123,6 +123,20 @@ exports.arrayRemove = function(array, value) { } }; +/* + * Splice the items in `a2` into `a1` at position `offset`. + */ +exports.spliceIntoArray = function(a1, offset, a2) { + + // Handle negative offsets. + if (offset < 0) + offset = Math.max(a1.length + offset, 0); + + // Splice. + for (var i = 0; i < a2.length; i++) + a1.splice(i + offset, 0, a2[i]); +} + exports.escapeRegExp = function(str) { return str.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1'); }; diff --git a/lib/ace/line_widgets.js b/lib/ace/line_widgets.js index 4023bd55..7cbe9809 100644 --- a/lib/ace/line_widgets.js +++ b/lib/ace/line_widgets.js @@ -32,6 +32,7 @@ define(function(require, exports, module) { "use strict"; var oop = require("./lib/oop"); +var lang = require("./lib/lang"); var dom = require("./lib/dom"); var Range = require("./range").Range; @@ -130,9 +131,7 @@ function LineWidgets(session) { }, this); this.$updateRows(); } else { - var args = Array(len); - args.unshift(startRow, 0); - cells.splice.apply(cells, args); + lang.spliceIntoArray(cells, startRow, new Array(len)); this.$updateRows(); } }; diff --git a/lib/ace/mode/asciidoc_highlight_rules.js b/lib/ace/mode/asciidoc_highlight_rules.js index c0d1a305..308fc8e4 100644 --- a/lib/ace/mode/asciidoc_highlight_rules.js +++ b/lib/ace/mode/asciidoc_highlight_rules.js @@ -32,6 +32,7 @@ define(function(require, exports, module) { "use strict"; var oop = require("../lib/oop"); +var lang = require("../lib/lang"); var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules; var AsciidocHighlightRules = function() { @@ -215,13 +216,14 @@ var AsciidocHighlightRules = function() { for (var i = stateRules.length; i--; ) { var rule = stateRules[i]; if (rule.include || typeof rule == "string") { - var args = [i, 1].concat(this.$rules[rule.include || rule]); + var toInsert = this.$rules[rule.include || rule]; if (rule.noEscape) { - args = args.filter(function(x) { + toInsert = toInsert.filter(function(x) { return !x.next; }); } - stateRules.splice.apply(stateRules, args); + stateRules.splice(i, 1); + lang.spliceIntoArray(stateRules, i, toInsert); } else if (rule.token in tokenMap) { rule.token = tokenMap[rule.token]; } diff --git a/lib/ace/mode/text_highlight_rules.js b/lib/ace/mode/text_highlight_rules.js index 48e016b0..9e304a53 100644 --- a/lib/ace/mode/text_highlight_rules.js +++ b/lib/ace/mode/text_highlight_rules.js @@ -181,10 +181,10 @@ var TextHighlightRules = function() { toInsert = rule; if (toInsert) { - var args = [i, 1].concat(toInsert); if (rule.noEscape) - args = args.filter(function(x) {return !x.next;}); - state.splice.apply(state, args); + toInsert = toInsert.filter(function(x) {return !x.next;}); + state.splice(i, 1); + lang.spliceIntoArray(state, i, toInsert); // skip included rules since they are already processed //i += args.length - 3; i--; diff --git a/lib/ace/snippets.js b/lib/ace/snippets.js index ee1a5498..e5d585bb 100644 --- a/lib/ace/snippets.js +++ b/lib/ace/snippets.js @@ -335,12 +335,12 @@ var SnippetManager = function() { } var ts = tabstops[id]; - var arg = typeof ts.value == "string" ? [ts.value] : copyValue(ts.value); - arg.unshift(i + 1, Math.max(0, i1 - i)); - arg.push(p); + var toInsert = typeof ts.value == "string" ? [ts.value] : copyValue(ts.value); + toInsert.push(p); expanding[id] = p; - tokens.splice.apply(tokens, arg); - + tokens.splice(i + 1, Math.max(0, i1 - i)); + lang.spliceIntoArray(tokens, i + 1, toInsert); + if (ts.indexOf(p) === -1) ts.push(p); }; @@ -755,7 +755,7 @@ var TabstopManager = function(editor) { } var i = this.index; - var arg = [i, 0]; + var toInsert = []; var ranges = this.ranges; var editor = this.editor; tabstops.forEach(function(ts) { @@ -776,12 +776,12 @@ var TabstopManager = function(editor) { } if (!ts.firstNonLinked) ts.hasLinkedRanges = false; - arg.push(ts); + toInsert.push(ts); this.addTabstopMarkers(ts); }, this); // tabstop 0 is the last one - arg.push(arg.splice(2, 1)[0]); - this.tabstops.splice.apply(this.tabstops, arg); + toInsert.push(toInsert.splice(0, 1)[0]); + lang.spliceIntoArray(this.tabstops, this.index, toInsert); }; this.addTabstopMarkers = function(ts) { From 91df7cd6635110dca0a97e7419150739f8d9ffd6 Mon Sep 17 00:00:00 2001 From: aldendaniels Date: Wed, 8 Jan 2014 21:39:30 -0600 Subject: [PATCH 008/476] Revert "Stop using splice.appy() in ace" This reverts commit 8624ab8dcb9bac917488feb8856e8b709418adb7. --- lib/ace/background_tokenizer.js | 14 ++++++-------- lib/ace/edit_session.js | 14 +++++++------- lib/ace/edit_session/folding.js | 6 +++--- lib/ace/layer/gutter.js | 5 +++-- lib/ace/lib/lang.js | 14 -------------- lib/ace/line_widgets.js | 5 +++-- lib/ace/mode/asciidoc_highlight_rules.js | 8 +++----- lib/ace/mode/text_highlight_rules.js | 6 +++--- lib/ace/snippets.js | 18 +++++++++--------- 9 files changed, 37 insertions(+), 53 deletions(-) diff --git a/lib/ace/background_tokenizer.js b/lib/ace/background_tokenizer.js index 4b82f19a..cb3ffe23 100644 --- a/lib/ace/background_tokenizer.js +++ b/lib/ace/background_tokenizer.js @@ -32,7 +32,6 @@ define(function(require, exports, module) { "use strict"; var oop = require("./lib/oop"); -var lang = require("./lib/lang"); var EventEmitter = require("./lib/event_emitter").EventEmitter; @@ -188,15 +187,14 @@ var BackgroundTokenizer = function(tokenizer, editor) { this.lines.splice(startRow, len + 1, null); this.states.splice(startRow, len + 1, null); } else { - this.lines.splice( startRow, 1); - this.states.splice(startRow, 1); - var toInsert = Array(len + 1); - lang.spliceIntoArray(this.lines, startRow, toInsert); - lang.spliceIntoArray(this.states, startRow, toInsert); + var args = Array(len + 1); + args.unshift(startRow, 1); + this.lines.splice.apply(this.lines, args); + this.states.splice.apply(this.states, args); } - + this.currentLine = Math.min(startRow, this.currentLine, this.doc.getLength()); - + this.stop(); }; diff --git a/lib/ace/edit_session.js b/lib/ace/edit_session.js index cd14c1a5..0d4df080 100644 --- a/lib/ace/edit_session.js +++ b/lib/ace/edit_session.js @@ -1723,15 +1723,15 @@ var EditSession = function(text, mode) { } else { var args; if (useWrapMode) { - var toInsert = []; - for (var i = 0; i < len; i++) - toInsert.push([]); - lang.spliceIntoArray(this.$wrapData, firstRow, toInsert); + args = [firstRow, 0]; + for (var i = 0; i < len; i++) args.push([]); + this.$wrapData.splice.apply(this.$wrapData, args); } else { - var toInsert = Array(len); - lang.spliceIntoArray(this.$rowLengthCache, firstRow, toInsert); + args = Array(len); + args.unshift(firstRow, 0); + this.$rowLengthCache.splice.apply(this.$rowLengthCache, args); } - + // If some new line is added inside of a foldLine, then split // the fold line up. var foldLines = this.$foldData; diff --git a/lib/ace/edit_session/folding.js b/lib/ace/edit_session/folding.js index 1563bd67..9df35593 100644 --- a/lib/ace/edit_session/folding.js +++ b/lib/ace/edit_session/folding.js @@ -31,7 +31,6 @@ define(function(require, exports, module) { "use strict"; -var lang = require("../lib/lang"); var Range = require("../range").Range; var FoldLine = require("./fold_line").FoldLine; var Fold = require("./fold").Fold; @@ -837,8 +836,9 @@ function Folding() { } else if (delta.action == "delete") { this.foldWidgets.splice(firstRow, len + 1, null); } else { - this.foldWidgets.splice(firstRow, 1); - lang.spliceIntoArray(this.foldWidgets, firstRow, Array(len + 1)); + var args = Array(len + 1); + args.unshift(firstRow, 1); + this.foldWidgets.splice.apply(this.foldWidgets, args); } }; diff --git a/lib/ace/layer/gutter.js b/lib/ace/layer/gutter.js index 24f9c430..82dcf245 100644 --- a/lib/ace/layer/gutter.js +++ b/lib/ace/layer/gutter.js @@ -111,8 +111,9 @@ var Gutter = function(parentEl) { } else if (delta.action == "delete") { this.$annotations.splice(firstRow, len + 1, null); } else { - this.$annotations.splice(firstRow, 1); - lang.spliceIntoArray(this.$annotations, firstRow, new Array(len + 1)); + var args = new Array(len + 1); + args.unshift(firstRow, 1); + this.$annotations.splice.apply(this.$annotations, args); } }; diff --git a/lib/ace/lib/lang.js b/lib/ace/lib/lang.js index e0e9d826..4405b5c2 100644 --- a/lib/ace/lib/lang.js +++ b/lib/ace/lib/lang.js @@ -123,20 +123,6 @@ exports.arrayRemove = function(array, value) { } }; -/* - * Splice the items in `a2` into `a1` at position `offset`. - */ -exports.spliceIntoArray = function(a1, offset, a2) { - - // Handle negative offsets. - if (offset < 0) - offset = Math.max(a1.length + offset, 0); - - // Splice. - for (var i = 0; i < a2.length; i++) - a1.splice(i + offset, 0, a2[i]); -} - exports.escapeRegExp = function(str) { return str.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1'); }; diff --git a/lib/ace/line_widgets.js b/lib/ace/line_widgets.js index 7cbe9809..4023bd55 100644 --- a/lib/ace/line_widgets.js +++ b/lib/ace/line_widgets.js @@ -32,7 +32,6 @@ define(function(require, exports, module) { "use strict"; var oop = require("./lib/oop"); -var lang = require("./lib/lang"); var dom = require("./lib/dom"); var Range = require("./range").Range; @@ -131,7 +130,9 @@ function LineWidgets(session) { }, this); this.$updateRows(); } else { - lang.spliceIntoArray(cells, startRow, new Array(len)); + var args = Array(len); + args.unshift(startRow, 0); + cells.splice.apply(cells, args); this.$updateRows(); } }; diff --git a/lib/ace/mode/asciidoc_highlight_rules.js b/lib/ace/mode/asciidoc_highlight_rules.js index 308fc8e4..c0d1a305 100644 --- a/lib/ace/mode/asciidoc_highlight_rules.js +++ b/lib/ace/mode/asciidoc_highlight_rules.js @@ -32,7 +32,6 @@ define(function(require, exports, module) { "use strict"; var oop = require("../lib/oop"); -var lang = require("../lib/lang"); var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules; var AsciidocHighlightRules = function() { @@ -216,14 +215,13 @@ var AsciidocHighlightRules = function() { for (var i = stateRules.length; i--; ) { var rule = stateRules[i]; if (rule.include || typeof rule == "string") { - var toInsert = this.$rules[rule.include || rule]; + var args = [i, 1].concat(this.$rules[rule.include || rule]); if (rule.noEscape) { - toInsert = toInsert.filter(function(x) { + args = args.filter(function(x) { return !x.next; }); } - stateRules.splice(i, 1); - lang.spliceIntoArray(stateRules, i, toInsert); + stateRules.splice.apply(stateRules, args); } else if (rule.token in tokenMap) { rule.token = tokenMap[rule.token]; } diff --git a/lib/ace/mode/text_highlight_rules.js b/lib/ace/mode/text_highlight_rules.js index 9e304a53..48e016b0 100644 --- a/lib/ace/mode/text_highlight_rules.js +++ b/lib/ace/mode/text_highlight_rules.js @@ -181,10 +181,10 @@ var TextHighlightRules = function() { toInsert = rule; if (toInsert) { + var args = [i, 1].concat(toInsert); if (rule.noEscape) - toInsert = toInsert.filter(function(x) {return !x.next;}); - state.splice(i, 1); - lang.spliceIntoArray(state, i, toInsert); + args = args.filter(function(x) {return !x.next;}); + state.splice.apply(state, args); // skip included rules since they are already processed //i += args.length - 3; i--; diff --git a/lib/ace/snippets.js b/lib/ace/snippets.js index e5d585bb..ee1a5498 100644 --- a/lib/ace/snippets.js +++ b/lib/ace/snippets.js @@ -335,12 +335,12 @@ var SnippetManager = function() { } var ts = tabstops[id]; - var toInsert = typeof ts.value == "string" ? [ts.value] : copyValue(ts.value); - toInsert.push(p); + var arg = typeof ts.value == "string" ? [ts.value] : copyValue(ts.value); + arg.unshift(i + 1, Math.max(0, i1 - i)); + arg.push(p); expanding[id] = p; - tokens.splice(i + 1, Math.max(0, i1 - i)); - lang.spliceIntoArray(tokens, i + 1, toInsert); - + tokens.splice.apply(tokens, arg); + if (ts.indexOf(p) === -1) ts.push(p); }; @@ -755,7 +755,7 @@ var TabstopManager = function(editor) { } var i = this.index; - var toInsert = []; + var arg = [i, 0]; var ranges = this.ranges; var editor = this.editor; tabstops.forEach(function(ts) { @@ -776,12 +776,12 @@ var TabstopManager = function(editor) { } if (!ts.firstNonLinked) ts.hasLinkedRanges = false; - toInsert.push(ts); + arg.push(ts); this.addTabstopMarkers(ts); }, this); // tabstop 0 is the last one - toInsert.push(toInsert.splice(0, 1)[0]); - lang.spliceIntoArray(this.tabstops, this.index, toInsert); + arg.push(arg.splice(2, 1)[0]); + this.tabstops.splice.apply(this.tabstops, arg); }; this.addTabstopMarkers = function(ts) { From 6fe381f6335790f7a0ee6646cdcb9ab442893df5 Mon Sep 17 00:00:00 2001 From: aldendaniels Date: Wed, 8 Jan 2014 22:00:11 -0600 Subject: [PATCH 009/476] Rename insertText back to insert --- lib/ace/anchor_test.js | 2 +- lib/ace/autocomplete.js | 2 +- lib/ace/background_tokenizer_test.js | 2 +- lib/ace/commands/default_commands.js | 4 ++-- lib/ace/document.js | 16 ++++++---------- lib/ace/document_test.js | 8 ++++---- lib/ace/edit_session.js | 14 ++++---------- lib/ace/edit_session_test.js | 4 ++-- lib/ace/editor.js | 20 +++++++------------- lib/ace/editor_change_document_test.js | 2 +- lib/ace/editor_navigation_test.js | 2 +- lib/ace/ext/elastic_tabstops_lite.js | 2 +- lib/ace/ext/whitespace.js | 2 +- lib/ace/keyboard/vim/commands.js | 10 +++++----- lib/ace/keyboard/vim/maps/motions.js | 6 +++--- lib/ace/mode/javascript_test.js | 2 +- lib/ace/mode/text.js | 12 ++++++------ lib/ace/mouse/dragdrop_handler.js | 2 +- lib/ace/multi_select.js | 8 ++++---- lib/ace/occur_test.js | 12 ++++++------ lib/ace/placeholder_test.js | 18 +++++++++--------- 21 files changed, 67 insertions(+), 83 deletions(-) diff --git a/lib/ace/anchor_test.js b/lib/ace/anchor_test.js index 0661765b..d40d459b 100644 --- a/lib/ace/anchor_test.js +++ b/lib/ace/anchor_test.js @@ -54,7 +54,7 @@ module.exports = { var doc = new Document("juhu\nkinners"); var anchor = new Anchor(doc, 1, 4); - doc.insertText({row: 1, column: 1}, "123"); + doc.insert({row: 1, column: 1}, "123"); assert.position(anchor.getPosition(), 1, 7); }, diff --git a/lib/ace/autocomplete.js b/lib/ace/autocomplete.js index 2344aede..50154c01 100644 --- a/lib/ace/autocomplete.js +++ b/lib/ace/autocomplete.js @@ -170,7 +170,7 @@ var Autocomplete = function() { "Ctrl-Down|Ctrl-End": function(editor) { editor.completer.goTo("end"); }, "Esc": function(editor) { editor.completer.detach(); }, - "Space": function(editor) { editor.completer.detach(); editor.insertText(" ");}, + "Space": function(editor) { editor.completer.detach(); editor.insert(" ");}, "Return": function(editor) { editor.completer.insertMatch(); }, "Shift-Return": function(editor) { editor.completer.insertMatch(true); }, "Tab": function(editor) { editor.completer.insertMatch(); }, diff --git a/lib/ace/background_tokenizer_test.js b/lib/ace/background_tokenizer_test.js index 16c63132..7a4cc78c 100644 --- a/lib/ace/background_tokenizer_test.js +++ b/lib/ace/background_tokenizer_test.js @@ -70,7 +70,7 @@ module.exports = { forceTokenize(doc) testStates(doc, ["comment_regex_allowed", "comment_regex_allowed"]) - doc.insertText({row:0, column:2}, "\n*/") + doc.insert({row:0, column:2}, "\n*/") testStates(doc, [undefined, undefined, "comment_regex_allowed"]) forceTokenize(doc) diff --git a/lib/ace/commands/default_commands.js b/lib/ace/commands/default_commands.js index 0fd9686c..e4a8212e 100644 --- a/lib/ace/commands/default_commands.js +++ b/lib/ace/commands/default_commands.js @@ -508,13 +508,13 @@ exports.commands = [{ scrollIntoView: "selectionPart" }, { name: "insertstring", - exec: function(editor, str) { editor.insertText(str); }, + exec: function(editor, str) { editor.insert(str); }, multiSelectAction: "forEach", scrollIntoView: "cursor" }, { name: "inserttext", exec: function(editor, args) { - editor.insertText(lang.stringRepeat(args.text || "", args.times || 1)); + editor.insert(lang.stringRepeat(args.text || "", args.times || 1)); }, multiSelectAction: "forEach" }, { diff --git a/lib/ace/document.js b/lib/ace/document.js index 768182a4..a84d3bcc 100644 --- a/lib/ace/document.js +++ b/lib/ace/document.js @@ -62,7 +62,7 @@ var Document = function(textOrLines) { } else if (Array.isArray(textOrLines)) { this.insertMergedLines({row: 0, column: 0}, textOrLines); } else { - this.insertText({row: 0, column:0}, textOrLines); + this.insert({row: 0, column:0}, textOrLines); } }; @@ -78,7 +78,7 @@ var Document = function(textOrLines) { this.setValue = function(text) { var len = this.getLength(); this.remove(new Range(0, 0, len, this.getLine(len-1).length)); - this.insertText({row: 0, column:0}, text); + this.insert({row: 0, column:0}, text); }; /** @@ -265,10 +265,6 @@ var Document = function(textOrLines) { }; // Deprecated methods retained for backwards compatibility. - this.insert = function(position, text){ - console.warn('Use of document.insert is deprecated. Use the insertText method instead.'); - return this.insertText(position, text); - }; this.insertLines = function(row, lines) { console.warn('Use of document.insertLines is deprecated. Use the insertFullLines method instead.'); return this.insertFullLines(row, lines); @@ -282,8 +278,8 @@ var Document = function(textOrLines) { return this.insertMergedLines(position, ['', '']); }; this.insertInLine = function(position, text) { - console.warn('Use of document.insertInLine is deprecated. Use insertText instead.'); - return this.insertText(position, text); + console.warn('Use of document.insertInLine is deprecated. Use insert instead.'); + return this.insert(position, text); }; /** @@ -293,7 +289,7 @@ var Document = function(textOrLines) { * @returns {Object} The position ({row, column}) of the last line of `text`. If the length of `text` is 0, this function simply returns `position`. * **/ - this.insertText = function(position, text) { + this.insert = function(position, text) { // Only detect new lines if the document has no line break yet. if (this.getLength() <= 1) @@ -509,7 +505,7 @@ var Document = function(textOrLines) { this.remove(range); var end; if (text) { - end = this.insertText(range.start, text); + end = this.insert(range.start, text); } else { end = range.start; diff --git a/lib/ace/document_test.js b/lib/ace/document_test.js index c29d6133..a91fccf2 100644 --- a/lib/ace/document_test.js +++ b/lib/ace/document_test.js @@ -48,7 +48,7 @@ module.exports = { var deltas = []; doc.on("change", function(e) { deltas.push(e.data); }); - doc.insertText({row: 0, column: 1}, "juhu"); + doc.insert({row: 0, column: 1}, "juhu"); assert.equal(doc.getValue(), ["1juhu2", "34"].join("\n")); var d = deltas.concat(); @@ -126,7 +126,7 @@ module.exports = { var deltas = []; doc.on("change", function(e) { deltas.push(e.data); }); - doc.insertText({row: 0, column: 0}, "aa\nbb\ncc"); + doc.insert({row: 0, column: 0}, "aa\nbb\ncc"); assert.equal(doc.getValue(), ["aa", "bb", "cc12", "34"].join("\n")); var d = deltas.concat(); @@ -143,7 +143,7 @@ module.exports = { var deltas = []; doc.on("change", function(e) { deltas.push(e.data); }); - doc.insertText({row: 1, column: 2}, "aa\nbb\ncc"); + doc.insert({row: 1, column: 2}, "aa\nbb\ncc"); assert.equal(doc.getValue(), ["12", "34aa", "bb", "cc"].join("\n")); var d = deltas.concat(); @@ -160,7 +160,7 @@ module.exports = { var deltas = []; doc.on("change", function(e) { deltas.push(e.data); }); - doc.insertText({row: 0, column: 1}, "aa\nbb\ncc"); + doc.insert({row: 0, column: 1}, "aa\nbb\ncc"); assert.equal(doc.getValue(), ["1aa", "bb", "cc2", "34"].join("\n")); var d = deltas.concat(); diff --git a/lib/ace/edit_session.js b/lib/ace/edit_session.js index 0d4df080..f140bdac 100644 --- a/lib/ace/edit_session.js +++ b/lib/ace/edit_session.js @@ -1127,12 +1127,6 @@ var EditSession = function(text, mode) { this.getTextRange = function(range) { return this.doc.getTextRange(range || this.selection.getRange()); }; - - // Deprecated method retained for backwards compatibility. - this.insert = function(position, text){ - console.warn('Use of editsession.insert is deprecated. Use the insertText method instead.'); - return this.insertText(position, text); - }; /** * Inserts a block of `text` and the indicated `position`. @@ -1142,8 +1136,8 @@ var EditSession = function(text, mode) { * * **/ - this.insertText = function(position, text) { - return this.doc.insertText(position, text); + this.insert = function(position, text) { + return this.doc.insert(position, text); }; /** @@ -1356,7 +1350,7 @@ var EditSession = function(text, mode) { } } - toRange.end = this.insertText(toRange.start, text); + toRange.end = this.insert(toRange.start, text); if (folds.length) { var oldStart = fromRange.start; var newStart = toRange.start; @@ -1390,7 +1384,7 @@ var EditSession = function(text, mode) { this.indentRows = function(startRow, endRow, indentString) { indentString = indentString.replace(/\t/g, this.getTabString()); for (var row=startRow; row<=endRow; row++) - this.insertText({row: row, column:0}, indentString); + this.insert({row: row, column:0}, indentString); }; /** diff --git a/lib/ace/edit_session_test.js b/lib/ace/edit_session_test.js index fd20259c..75abf468 100644 --- a/lib/ace/edit_session_test.js +++ b/lib/ace/edit_session_test.js @@ -649,7 +649,7 @@ module.exports = { var foldLines = session.$foldData; function insert(row, column, text) { - session.insertText({row: row, column: column}, text); + session.insert({row: row, column: column}, text); // Force the session to store all changes made to the document NOW // on the undoManager's queue. Otherwise we can't undo in separate @@ -748,7 +748,7 @@ module.exports = { undoManager = session.getUndoManager(), foldLines = session.$foldData; function insert(row, column, text) { - session.insertText({row: row, column: column}, text); + session.insert({row: row, column: column}, text); // Force the session to store all changes made to the document NOW // on the undoManager's queue. Otherwise we can't undo in separate // steps later. diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 79f59e68..af991ef1 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -822,19 +822,13 @@ var Editor = function(renderer, session) { if (this.$readOnly) return; this._emit("paste", text); - this.insertText(text); + this.insert(text); }; this.execCommand = function(command, args) { this.commands.exec(command, this, args); }; - - // Deprecated method retained for backwards compatibility. - this.insert = function(text){ - console.warn('Use of editor.insert is deprecated. Use the insertText method instead.'); - return this.insertText(text); - }; /** * Inserts `text` into wherever the cursor is pointing. @@ -842,7 +836,7 @@ var Editor = function(renderer, session) { * * **/ - this.insertText = function(text) { + this.insert = function(text) { var session = this.session; var mode = session.getMode(); var cursor = this.getCursorPosition(); @@ -888,7 +882,7 @@ var Editor = function(renderer, session) { var lineState = session.getState(cursor.row); var line = session.getLine(cursor.row); var shouldOutdent = mode.checkOutdent(lineState, line, text); - var end = session.insertText(cursor, text); + var end = session.insert(cursor, text); if (transform && transform.selection) { if (transform.selection.length == 2) { // Transform relative to the current column @@ -907,7 +901,7 @@ var Editor = function(renderer, session) { if (session.getDocument().isNewLine(text)) { var lineIndent = mode.getNextLineIndent(lineState, line.slice(0, cursor.column), session.getTabString()); - session.insertText({row: cursor.row+1, column: 0}, lineIndent); + session.insert({row: cursor.row+1, column: 0}, lineIndent); } if (shouldOutdent) mode.autoOutdent(lineState, session, cursor.row); @@ -1280,7 +1274,7 @@ var Editor = function(renderer, session) { } var cursor = this.getCursorPosition(); - this.insertText("\n"); + this.insert("\n"); this.moveCursorToPosition(cursor); }; @@ -1379,7 +1373,7 @@ var Editor = function(renderer, session) { this.selection.setSelectionRange(range); indentString = "\t"; } - return this.insertText(indentString); + return this.insert(indentString); }; /** @@ -1531,7 +1525,7 @@ var Editor = function(renderer, session) { doc.duplicateLines(row, row); } else { var point = reverse ? range.start : range.end; - var endPoint = doc.insertText(point, doc.getTextRange(range), false); + var endPoint = doc.insert(point, doc.getTextRange(range), false); range.start = point; range.end = endPoint; diff --git a/lib/ace/editor_change_document_test.js b/lib/ace/editor_change_document_test.js index 83fffeb3..a1fdaad9 100644 --- a/lib/ace/editor_change_document_test.js +++ b/lib/ace/editor_change_document_test.js @@ -172,7 +172,7 @@ module.exports = { self.session1.setMode(new HtmlMode()); // 5. Try to type valid HTML - self.session1.insertText({row: 0, column: 0}, ""); + self.session1.insert({row: 0, column: 0}, ""); setTimeout(function() { assert.equal(Object.keys(self.session1.getAnnotations()).length, 0); diff --git a/lib/ace/editor_navigation_test.js b/lib/ace/editor_navigation_test.js index b60d391d..ab348241 100644 --- a/lib/ace/editor_navigation_test.js +++ b/lib/ace/editor_navigation_test.js @@ -150,7 +150,7 @@ module.exports = { var editor = new Editor(new MockRenderer(), new EditSession(["1234", "1234567890"])); editor.navigateTo(0, 3); - editor.insertText("juhu"); + editor.insert("juhu"); editor.navigateDown(); assert.position(editor.getCursorPosition(), 1, 7); diff --git a/lib/ace/ext/elastic_tabstops_lite.js b/lib/ace/ext/elastic_tabstops_lite.js index 4d3a40d4..a8c3570c 100644 --- a/lib/ace/ext/elastic_tabstops_lite.js +++ b/lib/ace/ext/elastic_tabstops_lite.js @@ -236,7 +236,7 @@ var ElasticTabstopsLite = function(editor) { if (difference > 0) { // put the spaces after the tab and then delete the tab, so any insertion // points behave as expected - this.$editor.session.getDocument().insertText({row: row, column: it + 1}, Array(difference + 1).join(" ") + "\t"); + this.$editor.session.getDocument().insert({row: row, column: it + 1}, Array(difference + 1).join(" ") + "\t"); this.$editor.session.getDocument().removeInLine(row, it, it + 1); bias += difference; diff --git a/lib/ace/ext/whitespace.js b/lib/ace/ext/whitespace.js index 736e991d..be0cde28 100644 --- a/lib/ace/ext/whitespace.js +++ b/lib/ace/ext/whitespace.js @@ -153,7 +153,7 @@ exports.convertIndentation = function(session, ch, len) { if (toInsert != match) { doc.removeInLine(i, 0, match.length); - doc.insertText({row: i, column: 0}, toInsert); + doc.insert({row: i, column: 0}, toInsert); } } } diff --git a/lib/ace/keyboard/vim/commands.js b/lib/ace/keyboard/vim/commands.js index defca037..dd3357d6 100644 --- a/lib/ace/keyboard/vim/commands.js +++ b/lib/ace/keyboard/vim/commands.js @@ -104,7 +104,7 @@ var actions = exports.actions = { if (param && param.length) { if (param.length > 1) param = param == "return" ? "\n" : param == "tab" ? "\t" : param; - repeat(function() { editor.insertText(param); }, count || 1); + repeat(function() { editor.insert(param); }, count || 1); editor.navigateLeft(); } } @@ -224,12 +224,12 @@ var actions = exports.actions = { var pos = editor.getCursorPosition(); pos.column = editor.session.getLine(pos.row).length; var text = lang.stringRepeat("\n" + defaultReg.text, count || 1); - editor.session.insertText(pos, text); + editor.session.insert(pos, text); editor.moveCursorTo(pos.row + 1, 0); } else { editor.navigateRight(); - editor.insertText(lang.stringRepeat(defaultReg.text, count || 1)); + editor.insert(lang.stringRepeat(defaultReg.text, count || 1)); editor.navigateLeft(); } editor.setOverwrite(true); @@ -245,11 +245,11 @@ var actions = exports.actions = { var pos = editor.getCursorPosition(); pos.column = 0; var text = lang.stringRepeat(defaultReg.text + "\n", count || 1); - editor.session.insertText(pos, text); + editor.session.insert(pos, text); editor.moveCursorToPosition(pos); } else { - editor.insertText(lang.stringRepeat(defaultReg.text, count || 1)); + editor.insert(lang.stringRepeat(defaultReg.text, count || 1)); } editor.setOverwrite(true); editor.selection.clearSelection(); diff --git a/lib/ace/keyboard/vim/maps/motions.js b/lib/ace/keyboard/vim/maps/motions.js index 2bf580b0..91c8b8af 100644 --- a/lib/ace/keyboard/vim/maps/motions.js +++ b/lib/ace/keyboard/vim/maps/motions.js @@ -545,7 +545,7 @@ module.exports = { if (content.length) { editor.navigateLineEnd() - editor.insertText(content); + editor.insert(content); util.insertMode(editor); } } @@ -562,9 +562,9 @@ module.exports = { if(row > 0) { editor.navigateUp(); editor.navigateLineEnd() - editor.insertText(content); + editor.insert(content); } else { - editor.session.insertText({row: 0, column: 0}, content); + editor.session.insert({row: 0, column: 0}, content); editor.navigateUp(); } util.insertMode(editor); diff --git a/lib/ace/mode/javascript_test.js b/lib/ace/mode/javascript_test.js index cc2b22d7..b413765a 100644 --- a/lib/ace/mode/javascript_test.js +++ b/lib/ace/mode/javascript_test.js @@ -131,7 +131,7 @@ module.exports = { this.mode.toggleCommentLines("start", session, 0, 2); assert.equal([" abc", " cde", " fg"].join("\n"), session.toString()); - session.insertText({row: 0, column: 0}, " "); + session.insert({row: 0, column: 0}, " "); this.mode.toggleCommentLines("start", session, 0, 2); assert.equal(["// abc", "// cde", "// fg"].join("\n"), session.toString()); }, diff --git a/lib/ace/mode/text.js b/lib/ace/mode/text.js index 534ac03c..76ba15b5 100644 --- a/lib/ace/mode/text.js +++ b/lib/ace/mode/text.js @@ -92,8 +92,8 @@ var Mode = function() { if (testRemove(line, i)) return; if (!ignoreBlankLines || /\S/.test(line)) { - doc.insertText({row: i, column: line.length}, lineCommentEnd); - doc.insertText({row: i, column: minIndent}, lineCommentStart); + doc.insert({row: i, column: line.length}, lineCommentEnd); + doc.insert({row: i, column: minIndent}, lineCommentStart); } }; @@ -138,9 +138,9 @@ var Mode = function() { var comment = function(line, i) { if (!ignoreBlankLines || /\S/.test(line)) { if (shouldInsertSpace(line, minIndent, minIndent)) - doc.insertText({row: i, column: minIndent}, commentWithSpace); + doc.insert({row: i, column: minIndent}, commentWithSpace); else - doc.insertText({row: i, column: minIndent}, lineCommentStart); + doc.insert({row: i, column: minIndent}, lineCommentStart); } }; var testRemove = function(line, i) { @@ -244,8 +244,8 @@ var Mode = function() { } else { colDiff = comment.start.length startRow = range.start.row; - session.insertText(range.end, comment.end); - session.insertText(range.start, comment.start); + session.insert(range.end, comment.end); + session.insert(range.start, comment.start); } // todo: selection should have ended up in the right place automatically! if (initialRange.start.row == startRow) diff --git a/lib/ace/mouse/dragdrop_handler.js b/lib/ace/mouse/dragdrop_handler.js index 3ad54bb8..3a4bc188 100644 --- a/lib/ace/mouse/dragdrop_handler.js +++ b/lib/ace/mouse/dragdrop_handler.js @@ -176,7 +176,7 @@ function DragdropHandler(mouseHandler) { var dropData = dataTransfer.getData('Text'); range = { start: dragCursor, - end: editor.session.insertText(dragCursor, dropData) + end: editor.session.insert(dragCursor, dropData) }; editor.focus(); dragOperation = null; diff --git a/lib/ace/multi_select.js b/lib/ace/multi_select.js index 883d9a7c..965cd961 100644 --- a/lib/ace/multi_select.js +++ b/lib/ace/multi_select.js @@ -544,7 +544,7 @@ var Editor = require("./editor").Editor; this._signal("paste", text); if (!this.inMultiSelectMode || this.inVirtualSelectionMode) - return this.insertText(text); + return this.insert(text); var lines = text.split(/\r\n|\r|\n/); var ranges = this.selection.rangeList.ranges; @@ -557,7 +557,7 @@ var Editor = require("./editor").Editor; if (!range.isEmpty()) this.session.remove(range); - this.session.insertText(range.start, lines[i]); + this.session.insert(range.start, lines[i]); } }; @@ -742,7 +742,7 @@ var Editor = require("./editor").Editor; } var lines = this.session.removeFullLines(fr, lr); lines = this.$reAlignText(lines, guessRange); - this.session.insertText({row: fr, column: 0}, lines.join("\n") + "\n"); + this.session.insert({row: fr, column: 0}, lines.join("\n") + "\n"); if (!guessRange) { range.start.column = 0; range.end.column = lines[lines.length - 1].length; @@ -778,7 +778,7 @@ var Editor = require("./editor").Editor; var l = maxCol - p.column; var d = spaceOffsets[i] - minSpace; if (l > d) - session.insertText(p, lang.stringRepeat(" ", l - d)); + session.insert(p, lang.stringRepeat(" ", l - d)); else session.remove(new Range(p.row, p.column, p.row, p.column - l + d)); diff --git a/lib/ace/occur_test.js b/lib/ace/occur_test.js index a9b2c894..59fb8548 100644 --- a/lib/ace/occur_test.js +++ b/lib/ace/occur_test.js @@ -55,7 +55,7 @@ module.exports = { }, "test: find lines matching" : function() { - editor.session.insertText({row: 0, column: 0}, 'abc\ndef\nxyz\nbcxbc'); + editor.session.insert({row: 0, column: 0}, 'abc\ndef\nxyz\nbcxbc'); var result = occur.matchingLines(editor.session, {needle: 'bc'}), expected = [{row: 0, content: 'abc'}, {row: 3, content: 'bcxbc'}]; assert.deepEqual(result, expected); @@ -63,7 +63,7 @@ module.exports = { "test: display occurrences" : function() { var text = 'abc\ndef\nxyz\nbcx\n'; - editor.session.insertText({row: 0, column: 0}, text); + editor.session.insert({row: 0, column: 0}, text); occur.displayOccurContent(editor, {needle: 'bc'}); assert.equal(editor.getValue(), 'abc\nbcx'); occur.displayOriginalContent(editor); @@ -72,7 +72,7 @@ module.exports = { "test: original position from occur doc" : function() { var text = 'abc\ndef\nxyz\nbcx\n'; - editor.session.insertText({row: 0, column: 0}, text); + editor.session.insert({row: 0, column: 0}, text); occur.displayOccurContent(editor, {needle: 'bc'}); assert.equal(editor.getValue(), 'abc\nbcx'); var pos = occur.occurToOriginalPosition(editor.session, {row: 1, column: 2}); @@ -82,7 +82,7 @@ module.exports = { "test: occur command" : function() { // setup var text = 'hel\nlo\n\nwo\nrld\n'; - editor.session.insertText({row: 0, column: 0}, text); + editor.session.insert({row: 0, column: 0}, text); editor.commands.addCommand(occurStartCommand); // run occur for lines including 'o' @@ -106,7 +106,7 @@ module.exports = { "test: occur navigation" : function() { // setup var text = 'hel\nlo\n\nwo\nrld\n'; - editor.session.insertText({row: 0, column: 0}, text); + editor.session.insert({row: 0, column: 0}, text); editor.commands.addCommand(occurStartCommand); editor.moveCursorToPosition({row: 1, column: 1}); @@ -125,7 +125,7 @@ module.exports = { "test: recursive occur" : function() { // setup var text = 'x\nabc1\nx\nabc2\n'; - editor.session.insertText({row: 0, column: 0}, text); + editor.session.insert({row: 0, column: 0}, text); editor.commands.addCommand(occurStartCommand); // orig -> occur1 diff --git a/lib/ace/placeholder_test.js b/lib/ace/placeholder_test.js index dc7e4b23..97e561fd 100644 --- a/lib/ace/placeholder_test.js +++ b/lib/ace/placeholder_test.js @@ -53,9 +53,9 @@ module.exports = { new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]); editor.moveCursorTo(0, 5); - editor.insertText('b'); + editor.insert('b'); assert.equal(session.doc.getValue(), "var ab = 10;\nconsole.log(ab, ab);"); - editor.insertText('cd'); + editor.insert('cd'); assert.equal(session.doc.getValue(), "var abcd = 10;\nconsole.log(abcd, abcd);"); editor.remove('left'); editor.remove('left'); @@ -70,7 +70,7 @@ module.exports = { new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]); editor.moveCursorTo(2, 0); - editor.insertText('b'); + editor.insert('b'); assert.equal(session.doc.getValue(), "var a = 10;\nconsole.log(a, a);\nb"); }, @@ -81,12 +81,12 @@ module.exports = { var p = new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]); editor.moveCursorTo(0, 4); - editor.insertText('$'); + editor.insert('$'); assert.equal(session.doc.getValue(), "var $a = 10;\nconsole.log($a, $a);"); editor.moveCursorTo(0, 4); // Have to put this in a setTimeout because the anchor is only fixed later. setTimeout(function() { - editor.insertText('v'); + editor.insert('v'); assert.equal(session.doc.getValue(), "var v$a = 10;\nconsole.log(v$a, v$a);"); next(); }, 10); @@ -99,10 +99,10 @@ module.exports = { var p = new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]); editor.moveCursorTo(0, 5); - editor.insertText('b'); + editor.insert('b'); assert.equal(session.doc.getValue(), "var ab = 10;\nconsole.log(ab, ab);"); p.detach(); - editor.insertText('cd'); + editor.insert('cd'); assert.equal(session.doc.getValue(), "var abcd = 10;\nconsole.log(ab, ab);"); }, @@ -136,8 +136,8 @@ module.exports = { var p = new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]); editor.moveCursorTo(0, 5); - editor.insertText('b'); - editor.insertText('cd'); + editor.insert('b'); + editor.insert('cd'); editor.remove('left'); assert.equal(session.doc.getValue(), "var abc = 10;\nconsole.log(abc, abc);"); // Wait a little for the changes to enter the undo stack From f2a2e4e1a8ce7870824beea9c956ae2671aadc86 Mon Sep 17 00:00:00 2001 From: aldendaniels Date: Wed, 8 Jan 2014 22:18:05 -0600 Subject: [PATCH 010/476] Rename the 'delete' delta action to 'remove' Matches previous naming convention. --- lib/ace/anchor.js | 2 +- lib/ace/apply_delta.js | 8 ++++---- lib/ace/background_tokenizer.js | 2 +- lib/ace/document.js | 20 ++++++++++---------- lib/ace/edit_session.js | 4 ++-- lib/ace/edit_session/folding.js | 2 +- lib/ace/ext/chromevox.js | 2 +- lib/ace/layer/gutter.js | 2 +- lib/ace/line_widgets.js | 2 +- lib/ace/placeholder.js | 4 ++-- 10 files changed, 24 insertions(+), 24 deletions(-) diff --git a/lib/ace/anchor.js b/lib/ace/anchor.js index affb4f19..fee42bb5 100644 --- a/lib/ace/anchor.js +++ b/lib/ace/anchor.js @@ -133,7 +133,7 @@ var Anchor = exports.Anchor = function(doc, row, column) { } // DELTA ENVELOPS POINT (delete only): Move point to delta start. - if (delta.action != 'delete') + if (delta.action != 'remove') throw 'Delete action expected.'; return { diff --git a/lib/ace/apply_delta.js b/lib/ace/apply_delta.js index 02c0a498..142d5e1c 100644 --- a/lib/ace/apply_delta.js +++ b/lib/ace/apply_delta.js @@ -53,8 +53,8 @@ function throwDeltaError(delta, errorText){ function validateDelta(lines, delta) { // Validate action. - if (delta.action != 'insert' && delta.action != 'delete') - fnThrow('Delta action must be "insert" or "delete".'); + if (delta.action != 'insert' && delta.action != 'remove') + fnThrow('Delta action must be "insert" or "remove".'); // Validate lines. if (!delta.lines instanceof Array) @@ -103,7 +103,7 @@ exports.applyDelta = function(lines, delta) { lines[row] = line.substring(0, startColumn) + delta.lines[0] + line.substring(startColumn); break; - case 'delete': + case 'remove': lines[row] = line.substring(0, startColumn) + line.substring(endColumn); break; } @@ -122,7 +122,7 @@ exports.applyDelta = function(lines, delta) { joinLineWithNext(lines, delta.range.end.row); break; - case 'delete': + case 'remove': splitLine(lines, delta.range.end); splitLine(lines, delta.range.start); lines.splice( diff --git a/lib/ace/background_tokenizer.js b/lib/ace/background_tokenizer.js index cb3ffe23..4d3d7034 100644 --- a/lib/ace/background_tokenizer.js +++ b/lib/ace/background_tokenizer.js @@ -183,7 +183,7 @@ var BackgroundTokenizer = function(tokenizer, editor) { if (len === 0) { this.lines[startRow] = null; - } else if (delta.action == "delete") { + } else if (delta.action == 'remove') { this.lines.splice(startRow, len + 1, null); this.states.splice(startRow, len + 1, null); } else { diff --git a/lib/ace/document.js b/lib/ace/document.js index a84d3bcc..de77582e 100644 --- a/lib/ace/document.js +++ b/lib/ace/document.js @@ -306,7 +306,7 @@ var Document = function(textOrLines) { * * `"insert"` * * `range`: the [[Range]] of the change within the document * * `lines`: the lines being added - * * `"delete"` + * * `"remove"` * * `range`: the [[Range]] of the change within the document * * `lines`: the lines being removed * @@ -395,7 +395,7 @@ var Document = function(textOrLines) { // Apply delta (emits change). range = this.$getClippedRange(range); this.applyDelta({ - action: 'delete', + action: 'remove', range: range, lines: this.getLinesForRange(range), }); @@ -418,7 +418,7 @@ var Document = function(textOrLines) { // Apply delta (emits change). this.applyDelta({ - action: "delete", + action: 'remove', range: range, lines: this.getLinesForRange(range) }); @@ -455,7 +455,7 @@ var Document = function(textOrLines) { // Apply delta (emits change). this.applyDelta({ - action: "delete", + action: 'remove', range: range, lines: this.getLinesForRange(range) }); @@ -474,7 +474,7 @@ var Document = function(textOrLines) { if (row < this.getLength() - 1 && row >= 0) { // Apply delta (emits change). this.applyDelta({ - action: "delete", + action: 'remove', range: new Range(row, this.getLine(row).length, row + 1, 0), lines: ['', ''] }); @@ -516,7 +516,7 @@ var Document = function(textOrLines) { /** * Applies all changes in `deltas` to the document. - * @param {Array} deltas An array of delta objects (can include 'insert' and 'delete' actions) + * @param {Array} deltas An array of delta objects (can include 'insert' and 'remove' actions) **/ this.applyDeltas = function(deltas) { for (var i=0; i=0; i--) { @@ -536,7 +536,7 @@ var Document = function(textOrLines) { /** * Applies `delta` to the document. - * @param {Object} delta A delta object (can include 'insert' and 'delete' actions) + * @param {Object} delta A delta object (can include 'insert' and 'remove' actions) **/ this.applyDelta = function(delta) { applyDelta(this.$lines, delta); @@ -545,12 +545,12 @@ var Document = function(textOrLines) { /** * Reverts `delta` from the document. - * @param {Object} delta A delta object (can include 'insert' and 'delete' actions) + * @param {Object} delta A delta object (can include 'insert' and 'remove' actions) **/ this.revertDelta = function(delta) { this.applyDelta({ - action: (delta.action == 'insert' ? 'delete' : 'insert'), + action: (delta.action == 'insert' ? 'remove' : 'insert'), range: delta.range.clone(), lines: delta.lines.slice() }); diff --git a/lib/ace/edit_session.js b/lib/ace/edit_session.js index f140bdac..7312f23b 100644 --- a/lib/ace/edit_session.js +++ b/lib/ace/edit_session.js @@ -1685,7 +1685,7 @@ var EditSession = function(text, mode) { this.$updating = true; if (len != 0) { - if (action == "delete") { + if (action == 'remove') { this[useWrapMode ? "$wrapData" : "$rowLengthCache"].splice(firstRow, len); var foldLines = this.$foldData; @@ -1760,7 +1760,7 @@ var EditSession = function(text, mode) { // Realign folds. E.g. if you add some new chars before a fold, the // fold should "move" to the right. len = Math.abs(e.data.range.start.column - e.data.range.end.column); - if (action == "delete") { + if (action == 'remove') { // Get all the folds in the change range and remove them. removedFolds = this.getFoldsInRange(e.data.range); this.removeFolds(removedFolds); diff --git a/lib/ace/edit_session/folding.js b/lib/ace/edit_session/folding.js index 9df35593..583dba0d 100644 --- a/lib/ace/edit_session/folding.js +++ b/lib/ace/edit_session/folding.js @@ -833,7 +833,7 @@ function Folding() { if (len === 0) { this.foldWidgets[firstRow] = null; - } else if (delta.action == "delete") { + } else if (delta.action == 'remove') { this.foldWidgets.splice(firstRow, len + 1, null); } else { var args = Array(len + 1); diff --git a/lib/ace/ext/chromevox.js b/lib/ace/ext/chromevox.js index 20bbb6c7..4abb8278 100644 --- a/lib/ace/ext/chromevox.js +++ b/lib/ace/ext/chromevox.js @@ -581,7 +581,7 @@ var onSelectionChange = function(evt) { var onChange = function(evt) { var data = evt.data; switch (data.action) { - case 'delete': + case 'remove': cvox.Api.speak(data.text, 0, DELETED_PROP); /* Let the future cursor change event know it's from text change. */ changed = true; diff --git a/lib/ace/layer/gutter.js b/lib/ace/layer/gutter.js index 82dcf245..235425ab 100644 --- a/lib/ace/layer/gutter.js +++ b/lib/ace/layer/gutter.js @@ -108,7 +108,7 @@ var Gutter = function(parentEl) { var len = range.end.row - firstRow; if (len === 0) { // do nothing - } else if (delta.action == "delete") { + } else if (delta.action == 'remove') { this.$annotations.splice(firstRow, len + 1, null); } else { var args = new Array(len + 1); diff --git a/lib/ace/line_widgets.js b/lib/ace/line_widgets.js index 4023bd55..d0c7581e 100644 --- a/lib/ace/line_widgets.js +++ b/lib/ace/line_widgets.js @@ -123,7 +123,7 @@ function LineWidgets(session) { if (len === 0) { // return - } else if (delta.action == "delete") { + } else if (delta.action == 'remove') { var removed = cells.splice(startRow + 1, len); removed.forEach(function(w) { w && this.removeLineWidget(w); diff --git a/lib/ace/placeholder.js b/lib/ace/placeholder.js index c9a44e50..df0bfa9e 100644 --- a/lib/ace/placeholder.js +++ b/lib/ace/placeholder.js @@ -169,7 +169,7 @@ var PlaceHolder = function(session, length, pos, others, mainClass, othersClass) newPos.column += lengthDiff; this.doc.insertMergedLines(newPos, delta.lines); } - } else if(delta.action === "delete") { + } else if(delta.action === 'remove') { for (var i = this.others.length - 1; i >= 0; i--) { var otherPos = this.others[i]; var newPos = {row: otherPos.row, column: otherPos.column + distanceFromStart}; @@ -191,7 +191,7 @@ var PlaceHolder = function(session, length, pos, others, mainClass, othersClass) } }.bind(this), 0); } - else if(range.start.column === this.pos.column && delta.action === "delete") { + else if(range.start.column === this.pos.column && delta.action === 'remove') { setTimeout(function() { for (var i = 0; i < this.others.length; i++) { var other = this.others[i]; From 3a048cdf611b86151eb28a01af7db666bce39aca Mon Sep 17 00:00:00 2001 From: aldendaniels Date: Wed, 8 Jan 2014 22:51:39 -0600 Subject: [PATCH 011/476] Bring back insertInLine Avoids an extra $split call. --- lib/ace/document.js | 37 +++++++++++++++++++++++++--- lib/ace/ext/elastic_tabstops_lite.js | 2 +- lib/ace/ext/whitespace.js | 2 +- lib/ace/mode/text.js | 4 +-- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/lib/ace/document.js b/lib/ace/document.js index de77582e..6968e8f0 100644 --- a/lib/ace/document.js +++ b/lib/ace/document.js @@ -277,10 +277,6 @@ var Document = function(textOrLines) { console.warn('Use of document.insertNewLine is deprecated. Use insertMergedLines(position, [\'\', \'\']) instead.'); return this.insertMergedLines(position, ['', '']); }; - this.insertInLine = function(position, text) { - console.warn('Use of document.insertInLine is deprecated. Use insert instead.'); - return this.insert(position, text); - }; /** * Inserts a block of `text` at the indicated `position`. @@ -298,6 +294,39 @@ var Document = function(textOrLines) { return this.insertMergedLines(position, this.$split(text)); }; + /** + * Inserts `text` into the `position` at the current row. This method also triggers the `'change'` event. + * + * This differs from the `insert` method in two ways: + * 1. This does NOT handle newline characters (single-line text only). + * 2. This is faster than the `insert` method for single-line text insertions. + * + * @param {Object} position The position to insert at; it's an object that looks like `{ row: row, column: column}` + * @param {String} text A chunk of text + * @returns {Object} Returns an object containing the final row and column, like this: + * ``` + * {row: endRow, column: 0} + * ``` + **/ + this.insertInLine = function(position, text) { + + // Calculate insertion range end point. + this.$clipPosition(position); + var endPoint = { + row : position.row, + column : position.column + text.length + }; + + // Apply delta (emits change). + this.applyDelta({ + action: "insert", + range: Range.fromPoints(position, endPoint), + lines: [text] + }); + + return endPoint; + }; + /** * Fires whenever the document changes. * diff --git a/lib/ace/ext/elastic_tabstops_lite.js b/lib/ace/ext/elastic_tabstops_lite.js index a8c3570c..9901c5df 100644 --- a/lib/ace/ext/elastic_tabstops_lite.js +++ b/lib/ace/ext/elastic_tabstops_lite.js @@ -236,7 +236,7 @@ var ElasticTabstopsLite = function(editor) { if (difference > 0) { // put the spaces after the tab and then delete the tab, so any insertion // points behave as expected - this.$editor.session.getDocument().insert({row: row, column: it + 1}, Array(difference + 1).join(" ") + "\t"); + this.$editor.session.getDocument().insertInLine({row: row, column: it + 1}, Array(difference + 1).join(" ") + "\t"); this.$editor.session.getDocument().removeInLine(row, it, it + 1); bias += difference; diff --git a/lib/ace/ext/whitespace.js b/lib/ace/ext/whitespace.js index be0cde28..83486fb0 100644 --- a/lib/ace/ext/whitespace.js +++ b/lib/ace/ext/whitespace.js @@ -153,7 +153,7 @@ exports.convertIndentation = function(session, ch, len) { if (toInsert != match) { doc.removeInLine(i, 0, match.length); - doc.insert({row: i, column: 0}, toInsert); + doc.insertInLine({row: i, column: 0}, toInsert); } } } diff --git a/lib/ace/mode/text.js b/lib/ace/mode/text.js index 76ba15b5..343fc271 100644 --- a/lib/ace/mode/text.js +++ b/lib/ace/mode/text.js @@ -92,8 +92,8 @@ var Mode = function() { if (testRemove(line, i)) return; if (!ignoreBlankLines || /\S/.test(line)) { - doc.insert({row: i, column: line.length}, lineCommentEnd); - doc.insert({row: i, column: minIndent}, lineCommentStart); + doc.insertInLine({row: i, column: line.length}, lineCommentEnd); + doc.insertInLine({row: i, column: minIndent}, lineCommentStart); } }; From 08edcdfc988fe8eacc40af4da464fb8ebde8c61c Mon Sep 17 00:00:00 2001 From: aldendaniels Date: Fri, 10 Jan 2014 22:49:17 -0600 Subject: [PATCH 012/476] Stop doing a line-by-line splicing in applyDelta Also brings back the functionality where large deltas are split into smaller deltas so that .splice.apply() calls will work. --- lib/ace/apply_delta.js | 7 ++----- lib/ace/document.js | 30 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/lib/ace/apply_delta.js b/lib/ace/apply_delta.js index 142d5e1c..380c3289 100644 --- a/lib/ace/apply_delta.js +++ b/lib/ace/apply_delta.js @@ -79,9 +79,9 @@ function validateDelta(lines, delta) { // TODO: // - Validate that the ending column offset matches the lines. // - Validate the deleted lines match the lines in the document. + // - Vaiidate that an insert delta does not contain more than 65001 entries. } - exports.applyDelta = function(lines, delta) { // Validate delta. @@ -114,10 +114,7 @@ exports.applyDelta = function(lines, delta) { case 'insert': splitLine(lines, delta.range.start); - for (var i = 0; i < delta.lines.length; i++) { - var row = delta.range.start.row + 1 + i; - lines.splice(row, 0, delta.lines[i]); - } + lines.splice.apply(lines, [delta.range.start.row + 1, 0].concat(delta.lines)); joinLineWithNext(lines, delta.range.start.row); joinLineWithNext(lines, delta.range.end.row); break; diff --git a/lib/ace/document.js b/lib/ace/document.js index 6968e8f0..cefaa136 100644 --- a/lib/ace/document.js +++ b/lib/ace/document.js @@ -568,8 +568,36 @@ var Document = function(textOrLines) { * @param {Object} delta A delta object (can include 'insert' and 'remove' actions) **/ this.applyDelta = function(delta) { + + // Split large insert deltas. This is necessary because: + // 1. We need to support splicing delta lines into the document via $lines.splice.apply(...) + // 2. fn.apply() doesn't work for a large number of params. The mallest threshold is on safari 0xFFFF. + // + // To Do: Ideally we'd be consistent and also split 'delete' deltas. We don't do this now, because delete + // delta handling is too slow. If we make delete delta handling faster we can split all large deltas + // as shown in https://gist.github.com/aldendaniels/8367109#file-document-snippet-js + // If we do this, update validateDelta() to limit the number of lines in a delete delta. + var bIsInsert = delta.action == 'insert'; + while (bIsInsert && delta.lines.length > 65001) + { + // Get split deltas. + var lines = delta.lines.splice(0, 65000); + lines.push(''); + this.applyDelta({ + action: delta.action, + lines: lines, + range: new Range(delta.range.start.row, delta.range.start.column, + delta.range.start.row + 65000, 0) + }); + + // Update remaining delta. + delta.range.start.row += 65000; + delta.range.start.column = 0; + } + + // Apply. applyDelta(this.$lines, delta); - this._emit("change", { data: delta }); + this._emit("change", { data: delta }); }; /** From ef0e8da5223884d4ff041ff59b1711488ca76a4a Mon Sep 17 00:00:00 2001 From: aldendaniels Date: Sat, 11 Jan 2014 11:20:34 -0600 Subject: [PATCH 013/476] Fix / complete validateDelta This uncovered the fact that until now delta.range had not always been a Range object. This inconsistency has been resolved by my changes in mirror.js. --- lib/ace/apply_delta.js | 96 +++++++++++++++++++++------------------- lib/ace/worker/mirror.js | 13 +++++- 2 files changed, 62 insertions(+), 47 deletions(-) diff --git a/lib/ace/apply_delta.js b/lib/ace/apply_delta.js index 380c3289..3d40d614 100644 --- a/lib/ace/apply_delta.js +++ b/lib/ace/apply_delta.js @@ -33,59 +33,63 @@ define(function(require, exports, module) { var Range = require("./range").Range; -function splitLine (lines, point) { - var text = lines[point.row]; - lines[point.row] = text.slice(0, point.column); - lines.splice(point.row + 1, 0, text.slice(point.column)); +function splitLine (docLines, position) { + var text = docLines[position.row]; + docLines[position.row] = text.slice(0, position.column); + docLines.splice(position.row + 1, 0, text.slice(position.column)); } -function joinLineWithNext(lines, row) { - lines[row] += lines[row + 1]; - lines.splice(row + 1, 1); +function joinLineWithNext(docLines, row) { + docLines[row] += docLines[row + 1]; + docLines.splice(row + 1, 1); } function throwDeltaError(delta, errorText){ - errorText = 'Invalid Delta: ' + errorText; - console.log(errorText, delta); - throw errorText; + console.log('Invalid Delta:', delta); + throw 'Invalid Delta: ' + errorText; } -function validateDelta(lines, delta) { +function positionInDocument(docLines, position) +{ + return position.row >= 0 && position.row < docLines.length && + position.column >= 0 && position.column <= docLines[position.row].length; +} - // Validate action. +function validateDelta(docLines, delta) { + + // Validate action string. if (delta.action != 'insert' && delta.action != 'remove') - fnThrow('Delta action must be "insert" or "remove".'); - - // Validate lines. - if (!delta.lines instanceof Array) - fnThrow('Delta lines must be an array'); + throwDeltaError(delta, 'delta.action must be "insert" or "remove"'); + // Validate lines type. + if (!(delta.lines instanceof Array)) + throwDeltaError(delta, 'delta.lines must be an Array'); + // Validate range type. - if (!delta.range instanceof Range) - fnThrow('Range object is not an instance of the Range class'); - - // Validate start point. + if (!(delta.range instanceof Range)) + throwDeltaError(delta, 'delta.range must be an instance of the Range class'); + + // Validate that the start point is contained in the document. var start = delta.range.start; - if (Math.min(Math.max(start.row, 0), lines.length - 1 ) != start.row || - Math.min(Math.max(start.column, 0), lines[start.row].length) != start.column) - { - fnThrow('Range start point not contained in document'); - } + if (!positionInDocument(docLines, delta.range.start)) + throwDeltaError(delta, 'delta.range.start must be contained in document'); - // Validate ending row offset. - if (delta.lines.length - 1 != delta.range.end.row - delta.range.start.row) - fnThrow('Range row offsets does not match delta lines'); + // Validate that the end point is contained in the document (remove deltas only). + var end = delta.range.end; + if (delta.action == 'remove' && !positionInDocument(docLines, end)) + throwDeltaError(delta, 'delta.range.end must contained in document for "remove" actions'); - // TODO: - // - Validate that the ending column offset matches the lines. - // - Validate the deleted lines match the lines in the document. - // - Vaiidate that an insert delta does not contain more than 65001 entries. + // Validate that the .range size matches the .lines size. + var numRangeRows = end.row - start.row; + var numRangeLastLineChars = (end.column - (numRangeRows == 0 ? start.column : 0)); + if (numRangeRows != delta.lines.length - 1 || delta.lines[numRangeRows].length != numRangeLastLineChars) + throwDeltaError(delta, 'delta.range must match delta lines'); } -exports.applyDelta = function(lines, delta) { +exports.applyDelta = function(docLines, delta) { // Validate delta. - validateDelta(lines, delta); + validateDelta(docLines, delta); // Apply delta. if (delta.range.start.row == delta.range.end.row) @@ -96,15 +100,15 @@ exports.applyDelta = function(lines, delta) { var row = delta.range.start.row; var startColumn = delta.range.start.column; var endColumn = delta.range.end.column; - var line = lines[row]; + var line = docLines[row]; switch (delta.action) { case 'insert': - lines[row] = line.substring(0, startColumn) + delta.lines[0] + line.substring(startColumn); + docLines[row] = line.substring(0, startColumn) + delta.lines[0] + line.substring(startColumn); break; case 'remove': - lines[row] = line.substring(0, startColumn) + line.substring(endColumn); + docLines[row] = line.substring(0, startColumn) + line.substring(endColumn); break; } } else { @@ -113,20 +117,20 @@ exports.applyDelta = function(lines, delta) { switch (delta.action) { case 'insert': - splitLine(lines, delta.range.start); - lines.splice.apply(lines, [delta.range.start.row + 1, 0].concat(delta.lines)); - joinLineWithNext(lines, delta.range.start.row); - joinLineWithNext(lines, delta.range.end.row); + splitLine(docLines, delta.range.start); + docLines.splice.apply(docLines, [delta.range.start.row + 1, 0].concat(delta.lines)); + joinLineWithNext(docLines, delta.range.start.row); + joinLineWithNext(docLines, delta.range.end.row); break; case 'remove': - splitLine(lines, delta.range.end); - splitLine(lines, delta.range.start); - lines.splice( + splitLine(docLines, delta.range.end); + splitLine(docLines, delta.range.start); + docLines.splice( delta.range.start.row + 1, // Where to start deleting delta.range.end.row - delta.range.start.row + 1 // Num lines to delete. ); - joinLineWithNext(lines, delta.range.start.row); + joinLineWithNext(docLines, delta.range.start.row); break; } } diff --git a/lib/ace/worker/mirror.js b/lib/ace/worker/mirror.js index 7a3318fb..d55e84f4 100644 --- a/lib/ace/worker/mirror.js +++ b/lib/ace/worker/mirror.js @@ -1,6 +1,7 @@ define(function(require, exports, module) { "use strict"; +var Range = require("../range").Range; var Document = require("../document").Document; var lang = require("../lib/lang"); @@ -12,7 +13,17 @@ var Mirror = exports.Mirror = function(sender) { var _self = this; sender.on("change", function(e) { - doc.applyDeltas(e.data); + + // Convert delta.range back into a Range instance since + // window.onMessage loses non-primitive data. See http://jsfiddle.net/nqJfw/1/. + var deltas = e.data; + for (var i in deltas) + { + var delta = deltas[i]; + delta.range = Range.fromPoints(delta.range.start, delta.range.end); + } + + doc.applyDeltas(deltas); if (_self.$timeout) return deferredUpdate.schedule(_self.$timeout); _self.onUpdate(); From f59708a5ba7b049271db9846ae8a71e88a9f6625 Mon Sep 17 00:00:00 2001 From: aldendaniels Date: Sat, 11 Jan 2014 12:00:10 -0600 Subject: [PATCH 014/476] Add a doNotValidate param to applyDelta Set it to true in insertInLine/removeInLine. Also sped up indent/dedent by using insertInLine and removeInLine. --- lib/ace/apply_delta.js | 5 +++-- lib/ace/document.js | 12 ++++++------ lib/ace/edit_session.js | 22 +++++++++++----------- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/lib/ace/apply_delta.js b/lib/ace/apply_delta.js index 3d40d614..1317ce98 100644 --- a/lib/ace/apply_delta.js +++ b/lib/ace/apply_delta.js @@ -86,10 +86,11 @@ function validateDelta(docLines, delta) { throwDeltaError(delta, 'delta.range must match delta lines'); } -exports.applyDelta = function(docLines, delta) { +exports.applyDelta = function(docLines, delta, doNotValidate) { // Validate delta. - validateDelta(docLines, delta); + if (!doNotValidate) + validateDelta(docLines, delta); // Apply delta. if (delta.range.start.row == delta.range.end.row) diff --git a/lib/ace/document.js b/lib/ace/document.js index cefaa136..c4f8a9eb 100644 --- a/lib/ace/document.js +++ b/lib/ace/document.js @@ -322,7 +322,7 @@ var Document = function(textOrLines) { action: "insert", range: Range.fromPoints(position, endPoint), lines: [text] - }); + }, true /*doNotValidate*/); return endPoint; }; @@ -449,8 +449,8 @@ var Document = function(textOrLines) { this.applyDelta({ action: 'remove', range: range, - lines: this.getLinesForRange(range) - }); + lines: this.getLinesForRange(range), + }, true /*doNotValidate*/); return range.start; }; @@ -567,7 +567,7 @@ var Document = function(textOrLines) { * Applies `delta` to the document. * @param {Object} delta A delta object (can include 'insert' and 'remove' actions) **/ - this.applyDelta = function(delta) { + this.applyDelta = function(delta, doNotValidate) { // Split large insert deltas. This is necessary because: // 1. We need to support splicing delta lines into the document via $lines.splice.apply(...) @@ -596,8 +596,8 @@ var Document = function(textOrLines) { } // Apply. - applyDelta(this.$lines, delta); - this._emit("change", { data: delta }); + applyDelta(this.$lines, delta, doNotValidate); + this._emit("change", { data: delta }); }; /** diff --git a/lib/ace/edit_session.js b/lib/ace/edit_session.js index 7312f23b..414b68c1 100644 --- a/lib/ace/edit_session.js +++ b/lib/ace/edit_session.js @@ -1384,7 +1384,7 @@ var EditSession = function(text, mode) { this.indentRows = function(startRow, endRow, indentString) { indentString = indentString.replace(/\t/g, this.getTabString()); for (var row=startRow; row<=endRow; row++) - this.insert({row: row, column:0}, indentString); + this.doc.insertInLine({row: row, column: 0}, indentString); }; /** @@ -1395,25 +1395,25 @@ var EditSession = function(text, mode) { **/ this.outdentRows = function (range) { var rowRange = range.collapseRows(); - var deleteRange = new Range(0, 0, 0, 0); var size = this.getTabSize(); - + for (var i = rowRange.start.row; i <= rowRange.end.row; ++i) { var line = this.getLine(i); - - deleteRange.start.row = i; - deleteRange.end.row = i; + var row = i; + var startCol = 0; + var endCol = 0; + for (var j = 0; j < size; ++j) if (line.charAt(j) != ' ') break; if (j < size && line.charAt(j) == '\t') { - deleteRange.start.column = j; - deleteRange.end.column = j + 1; + startCol = j; + endCol = j + 1; } else { - deleteRange.start.column = 0; - deleteRange.end.column = j; + startCol = 0; + endCol = j; } - this.remove(deleteRange); + this.doc.removeInLine(row, startCol, endCol); } }; From ddd695ee3f2a1e851c842d4d17851341067d499b Mon Sep 17 00:00:00 2001 From: aldendaniels Date: Sat, 11 Jan 2014 14:43:47 -0600 Subject: [PATCH 015/476] Store single-line deltas as .text instead of .lines in undo history Stores single-line delta content as .text instead of .lines in undo history. This is done without modifying the original delta object in case the caller still retains a handle to the original. --- lib/ace/undomanager.js | 80 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 15 deletions(-) diff --git a/lib/ace/undomanager.js b/lib/ace/undomanager.js index 304dac23..4d978d46 100644 --- a/lib/ace/undomanager.js +++ b/lib/ace/undomanager.js @@ -61,14 +61,19 @@ var UndoManager = function() { * **/ this.execute = function(options) { - var deltas = options.args[0]; + + // Normalize deltas for storage. + var deltaSets = this.$serializeDeltas(options.args[0]); + + // Add deltas to undo stack. this.$doc = options.args[1]; if (options.merge && this.hasUndo()){ - deltas = this.$undoStack.pop().concat(deltas); + deltaSets = this.$undoStack.pop().concat(deltaSets); } - this.$undoStack.push(deltas); + this.$undoStack.push(deltaSets); + + // Reset redo stack. this.$redoStack = []; - if (this.dirtyCounter < 0) { // The user has made a change after undoing past the last clean state. // We can never get back to a clean state now until markClean() is called. @@ -85,12 +90,11 @@ var UndoManager = function() { * @returns {Range} The range of the undo. **/ this.undo = function(dontSelect) { - var deltas = this.$undoStack.pop(); + var deltaSets = this.$undoStack.pop(); var undoSelectionRange = null; - if (deltas) { - undoSelectionRange = - this.$doc.undoChanges(deltas, dontSelect); - this.$redoStack.push(deltas); + if (deltaSets) { + undoSelectionRange = this.$doc.undoChanges(this.$deserializeDeltas(deltaSets), dontSelect); + this.$redoStack.push(deltaSets); this.dirtyCounter--; } @@ -104,15 +108,14 @@ var UndoManager = function() { * **/ this.redo = function(dontSelect) { - var deltas = this.$redoStack.pop(); + var deltaSets = this.$redoStack.pop(); var redoSelectionRange = null; - if (deltas) { + if (deltaSets) { redoSelectionRange = - this.$doc.redoChanges(deltas, dontSelect); - this.$undoStack.push(deltas); + this.$doc.redoChanges(this.$deserializeDeltas(deltaSets), dontSelect); + this.$undoStack.push(deltaSets); this.dirtyCounter++; } - return redoSelectionRange; }; @@ -160,7 +163,54 @@ var UndoManager = function() { this.isClean = function() { return this.dirtyCounter === 0; }; - + + // Serializes deltaSets to reduce memory usage. + this.$serializeDeltas = function(deltaSets) + { + return this.$cloneDeltaSetsObj(deltaSets, $serializeDelta); + } + function $serializeDelta(delta){ + return { + action: delta.action, + range: delta.range, + lines: (delta.lines.length == 1 ? null : delta.lines), + text: (delta.lines.length == 1 ? delta.lines[0] : null ), + }; + } + + // Deserializes deltaSets to allow application to the document. + this.$deserializeDeltas = function(deltaSets) + { + return this.$cloneDeltaSetsObj(deltaSets, $deserializeDelta); + } + function $deserializeDelta(delta){ + return { + action: delta.action, + range: delta.range, + lines: (delta.text === null ? delta.lines : [delta.text]) + }; + } + + // Helper for delta serialization and deserialization. + this.$cloneDeltaSetsObj = function(deltaSets_old, fnGetModifiedDelta) + { + var deltaSets_new = new Array(deltaSets_old.length); + for (var i in deltaSets_old) + { + var deltaSet_old = deltaSets_old[i]; + var deltaSet_new = { group: deltaSet_old.group, deltas: new Array(deltaSet_old.length)}; + + for (var i_ in deltaSet_old.deltas) + { + var delta_old = deltaSet_old.deltas[i_]; + deltaSet_new.deltas[i_] = fnGetModifiedDelta(delta_old); + } + + deltaSets_new[i] = deltaSet_new; + } + return deltaSets_new; + } + }).call(UndoManager.prototype); exports.UndoManager = UndoManager; From 35a27fd1ba52cccb765f515f3924d9ba579a7eea Mon Sep 17 00:00:00 2001 From: aldendaniels Date: Sat, 11 Jan 2014 14:45:08 -0600 Subject: [PATCH 016/476] Treat deltas with empty ranges as NOOPs --- lib/ace/document.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/ace/document.js b/lib/ace/document.js index c4f8a9eb..e203d77c 100644 --- a/lib/ace/document.js +++ b/lib/ace/document.js @@ -569,6 +569,10 @@ var Document = function(textOrLines) { **/ this.applyDelta = function(delta, doNotValidate) { + // An empty range is a NOOP. + if (delta.range.isEmpty()) + return; + // Split large insert deltas. This is necessary because: // 1. We need to support splicing delta lines into the document via $lines.splice.apply(...) // 2. fn.apply() doesn't work for a large number of params. The mallest threshold is on safari 0xFFFF. From 6b280bf6bb48aa81fc73582f85877f5626f1eae0 Mon Sep 17 00:00:00 2001 From: aldendaniels Date: Sat, 11 Jan 2014 15:23:20 -0600 Subject: [PATCH 017/476] Code review fixes (mostly formatting) - Fix unconventional '{' formatting - Reformat `UndoManager` changes - Revert change from `insertInLine` to `insert` in text.js --- lib/ace/anchor.js | 7 +++---- lib/ace/apply_delta.js | 6 ++---- lib/ace/document.js | 12 ++++++------ lib/ace/mode/text.js | 4 ++-- lib/ace/placeholder.js | 4 ++-- lib/ace/undomanager.js | 32 ++++++++++++++------------------ lib/ace/worker/mirror.js | 3 +-- 7 files changed, 30 insertions(+), 38 deletions(-) diff --git a/lib/ace/anchor.js b/lib/ace/anchor.js index fee42bb5..5622bf5c 100644 --- a/lib/ace/anchor.js +++ b/lib/ace/anchor.js @@ -101,14 +101,13 @@ var Anchor = exports.Anchor = function(doc, row, column) { **/ this.onChange = function(e) { - function _pointsInOrder(point1, point2, equalPointsInOrder) - { + function _pointsInOrder(point1, point2, equalPointsInOrder) { var bColIsAfter = equalPointsInOrder ? point1.column <= point2.column : point1.column < point2.column; return (point1.row < point2.row) || (point1.row == point2.row && bColIsAfter); } - function getTransformedPoint(delta, point, moveIfEqual) - { + function getTransformedPoint(delta, point, moveIfEqual) { + // Get delta info. var deltaIsInsert = (delta.action == 'insert') var deltaRowShift = (deltaIsInsert ? 1 : -1) * (delta.range.end.row - delta.range.start.row); diff --git a/lib/ace/apply_delta.js b/lib/ace/apply_delta.js index 1317ce98..86bc89c6 100644 --- a/lib/ace/apply_delta.js +++ b/lib/ace/apply_delta.js @@ -49,8 +49,7 @@ function throwDeltaError(delta, errorText){ throw 'Invalid Delta: ' + errorText; } -function positionInDocument(docLines, position) -{ +function positionInDocument(docLines, position) { return position.row >= 0 && position.row < docLines.length && position.column >= 0 && position.column <= docLines[position.row].length; } @@ -93,8 +92,7 @@ exports.applyDelta = function(docLines, delta, doNotValidate) { validateDelta(docLines, delta); // Apply delta. - if (delta.range.start.row == delta.range.end.row) - { + if (delta.range.start.row == delta.range.end.row) { // Apply single-line delta. // Note: The multi-line code below correctly handle single-line // deltas too, but we need to short-circuit for speed. diff --git a/lib/ace/document.js b/lib/ace/document.js index e203d77c..97d69968 100644 --- a/lib/ace/document.js +++ b/lib/ace/document.js @@ -108,14 +108,15 @@ var Document = function(textOrLines) { **/ // check for IE split bug - if ("aaa".split(/a/).length === 0) + if ("aaa".split(/a/).length === 0) { this.$split = function(text) { return text.replace(/\r\n|\r/g, "\n").split("\n"); }; - else + } else { this.$split = function(text) { return text.split(/\r\n|\r|\n/); }; + } this.$detectNewLine = function(text) { @@ -582,8 +583,8 @@ var Document = function(textOrLines) { // as shown in https://gist.github.com/aldendaniels/8367109#file-document-snippet-js // If we do this, update validateDelta() to limit the number of lines in a delete delta. var bIsInsert = delta.action == 'insert'; - while (bIsInsert && delta.lines.length > 65001) - { + while (bIsInsert && delta.lines.length > 65001) { + // Get split deltas. var lines = delta.lines.splice(0, 65000); lines.push(''); @@ -608,8 +609,7 @@ var Document = function(textOrLines) { * Reverts `delta` from the document. * @param {Object} delta A delta object (can include 'insert' and 'remove' actions) **/ - this.revertDelta = function(delta) - { + this.revertDelta = function(delta) { this.applyDelta({ action: (delta.action == 'insert' ? 'remove' : 'insert'), range: delta.range.clone(), diff --git a/lib/ace/mode/text.js b/lib/ace/mode/text.js index 343fc271..d04fed78 100644 --- a/lib/ace/mode/text.js +++ b/lib/ace/mode/text.js @@ -138,9 +138,9 @@ var Mode = function() { var comment = function(line, i) { if (!ignoreBlankLines || /\S/.test(line)) { if (shouldInsertSpace(line, minIndent, minIndent)) - doc.insert({row: i, column: minIndent}, commentWithSpace); + doc.insertInLine({row: i, column: minIndent}, commentWithSpace); else - doc.insert({row: i, column: minIndent}, lineCommentStart); + doc.insertInLine({row: i, column: minIndent}, lineCommentStart); } }; var testRemove = function(line, i) { diff --git a/lib/ace/placeholder.js b/lib/ace/placeholder.js index df0bfa9e..1d345a24 100644 --- a/lib/ace/placeholder.js +++ b/lib/ace/placeholder.js @@ -161,7 +161,7 @@ var PlaceHolder = function(session, length, pos, others, mainClass, othersClass) var distanceFromStart = range.start.column - this.pos.column; this.length += lengthDiff; if(!this.session.$fromUndo) { - if(delta.action === "insert") { + if(delta.action === 'insert') { for (var i = this.others.length - 1; i >= 0; i--) { var otherPos = this.others[i]; var newPos = {row: otherPos.row, column: otherPos.column + distanceFromStart}; @@ -179,7 +179,7 @@ var PlaceHolder = function(session, length, pos, others, mainClass, othersClass) } } // Special case: insert in beginning - if(range.start.column === this.pos.column && delta.action === "insert") { + if(range.start.column === this.pos.column && delta.action === 'insert') { setTimeout(function() { this.pos.setPosition(this.pos.row, this.pos.column - lengthDiff); for (var i = 0; i < this.others.length; i++) { diff --git a/lib/ace/undomanager.js b/lib/ace/undomanager.js index 4d978d46..209cf3db 100644 --- a/lib/ace/undomanager.js +++ b/lib/ace/undomanager.js @@ -165,10 +165,15 @@ var UndoManager = function() { }; // Serializes deltaSets to reduce memory usage. - this.$serializeDeltas = function(deltaSets) - { - return this.$cloneDeltaSetsObj(deltaSets, $serializeDelta); - } + this.$serializeDeltas = function(deltaSets) { + return cloneDeltaSetsObj(deltaSets, $serializeDelta); + }; + + // Deserializes deltaSets to allow application to the document. + this.$deserializeDeltas = function(deltaSets) { + return cloneDeltaSetsObj(deltaSets, $deserializeDelta); + }; + function $serializeDelta(delta){ return { action: delta.action, @@ -177,13 +182,8 @@ var UndoManager = function() { text: (delta.lines.length == 1 ? delta.lines[0] : null ), }; } - - // Deserializes deltaSets to allow application to the document. - this.$deserializeDeltas = function(deltaSets) - { - return this.$cloneDeltaSetsObj(deltaSets, $deserializeDelta); - } - function $deserializeDelta(delta){ + + function $deserializeDelta(delta) { return { action: delta.action, range: delta.range, @@ -191,17 +191,13 @@ var UndoManager = function() { }; } - // Helper for delta serialization and deserialization. - this.$cloneDeltaSetsObj = function(deltaSets_old, fnGetModifiedDelta) - { + function cloneDeltaSetsObj(deltaSets_old, fnGetModifiedDelta) { var deltaSets_new = new Array(deltaSets_old.length); - for (var i in deltaSets_old) - { + for (var i in deltaSets_old) { var deltaSet_old = deltaSets_old[i]; var deltaSet_new = { group: deltaSet_old.group, deltas: new Array(deltaSet_old.length)}; - for (var i_ in deltaSet_old.deltas) - { + for (var i_ in deltaSet_old.deltas) { var delta_old = deltaSet_old.deltas[i_]; deltaSet_new.deltas[i_] = fnGetModifiedDelta(delta_old); } diff --git a/lib/ace/worker/mirror.js b/lib/ace/worker/mirror.js index d55e84f4..2f33fea6 100644 --- a/lib/ace/worker/mirror.js +++ b/lib/ace/worker/mirror.js @@ -17,8 +17,7 @@ var Mirror = exports.Mirror = function(sender) { // Convert delta.range back into a Range instance since // window.onMessage loses non-primitive data. See http://jsfiddle.net/nqJfw/1/. var deltas = e.data; - for (var i in deltas) - { + for (var i in deltas) { var delta = deltas[i]; delta.range = Range.fromPoints(delta.range.start, delta.range.end); } From 7f1bc7af2fcc431241c2e8812588164394444df9 Mon Sep 17 00:00:00 2001 From: aldendaniels Date: Sat, 11 Jan 2014 15:48:38 -0600 Subject: [PATCH 018/476] Break out Anchor.onChange helper functions This should be faster since we don't have to re-initialize the helper functions each time Anchor.onChange is fired. --- lib/ace/anchor.js | 78 +++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/lib/ace/anchor.js b/lib/ace/anchor.js index 5622bf5c..c9c27dfe 100644 --- a/lib/ace/anchor.js +++ b/lib/ace/anchor.js @@ -100,50 +100,50 @@ var Anchor = exports.Anchor = function(doc, row, column) { * **/ this.onChange = function(e) { + var delta = e.data; + var point = this.$getTransformedPoint(delta, {row: this.row, column: this.column}); + this.setPosition(point.row, point.column, true); + }; + + function $pointsInOrder(point1, point2, equalPointsInOrder) { + var bColIsAfter = equalPointsInOrder ? point1.column <= point2.column : point1.column < point2.column; + return (point1.row < point2.row) || (point1.row == point2.row && bColIsAfter); + } + + this.$getTransformedPoint = function (delta, point) { - function _pointsInOrder(point1, point2, equalPointsInOrder) { - var bColIsAfter = equalPointsInOrder ? point1.column <= point2.column : point1.column < point2.column; - return (point1.row < point2.row) || (point1.row == point2.row && bColIsAfter); - } - - function getTransformedPoint(delta, point, moveIfEqual) { - - // Get delta info. - var deltaIsInsert = (delta.action == 'insert') - var deltaRowShift = (deltaIsInsert ? 1 : -1) * (delta.range.end.row - delta.range.start.row); - var deltaColShift = (deltaIsInsert ? 1 : -1) * (delta.range.end.column - delta.range.start.column); - var deltaStart = delta.range.start; - var deltaEnd = (deltaIsInsert ? deltaStart : delta.range.end); // Collapse insert range. - - // DELTA AFTER POINT: No change needed. - if (_pointsInOrder(point, deltaStart, moveIfEqual)) { - return { - row: point.row, - column: point.column - }; - } - - // DELTA BEFORE POINT: Move point by delta shift. - if (_pointsInOrder(deltaEnd, point, !moveIfEqual)) { - return { - row: point.row + deltaRowShift, - column: point.column + (point.row == deltaEnd.row ? deltaColShift : 0) - }; - } - - // DELTA ENVELOPS POINT (delete only): Move point to delta start. - if (delta.action != 'remove') - throw 'Delete action expected.'; - + // Get delta info. + var moveIfEqual = this.$insertRight; + var deltaIsInsert = (delta.action == 'insert') + var deltaRowShift = (deltaIsInsert ? 1 : -1) * (delta.range.end.row - delta.range.start.row); + var deltaColShift = (deltaIsInsert ? 1 : -1) * (delta.range.end.column - delta.range.start.column); + var deltaStart = delta.range.start; + var deltaEnd = (deltaIsInsert ? deltaStart : delta.range.end); // Collapse insert range. + + // DELTA AFTER POINT: No change needed. + if ($pointsInOrder(point, deltaStart, moveIfEqual)) { return { - row: deltaStart.row, - column: deltaStart.column + row: point.row, + column: point.column }; } - var delta = e.data; - var point = getTransformedPoint(delta, {row: this.row, column: this.column}, this.$insertRight); - this.setPosition(point.row, point.column, true); + // DELTA BEFORE POINT: Move point by delta shift. + if ($pointsInOrder(deltaEnd, point, !moveIfEqual)) { + return { + row: point.row + deltaRowShift, + column: point.column + (point.row == deltaEnd.row ? deltaColShift : 0) + }; + } + + // DELTA ENVELOPS POINT (delete only): Move point to delta start. + if (delta.action != 'remove') + throw 'Delete action expected.'; + + return { + row: deltaStart.row, + column: deltaStart.column + }; }; /** From 4299db01bdb4b6a578e8ed678fe82c32457d7601 Mon Sep 17 00:00:00 2001 From: nightwing Date: Sat, 18 Jan 2014 17:24:11 +0400 Subject: [PATCH 019/476] remove delta.range --- lib/ace/anchor.js | 17 ++-- lib/ace/apply_delta.js | 65 ++++++++------- lib/ace/background_tokenizer.js | 7 +- lib/ace/commands/command_manager.js | 4 +- lib/ace/document.js | 107 ++++++++++++------------- lib/ace/edit_session.js | 33 ++++---- lib/ace/edit_session/folding.js | 5 +- lib/ace/editor.js | 7 +- lib/ace/ext/elastic_tabstops_lite.js | 2 +- lib/ace/keyboard/vim/maps/operators.js | 2 - lib/ace/layer/gutter.js | 5 +- lib/ace/lib/lang.js | 1 - lib/ace/line_widgets.js | 5 +- lib/ace/undomanager.js | 20 ++--- lib/ace/worker/mirror.js | 11 +-- 15 files changed, 136 insertions(+), 155 deletions(-) diff --git a/lib/ace/anchor.js b/lib/ace/anchor.js index 421f9e5f..4511a4ce 100644 --- a/lib/ace/anchor.js +++ b/lib/ace/anchor.js @@ -111,14 +111,13 @@ var Anchor = exports.Anchor = function(doc, row, column) { } this.$getTransformedPoint = function (delta, point) { - // Get delta info. var moveIfEqual = this.$insertRight; - var deltaIsInsert = (delta.action == 'insert') - var deltaRowShift = (deltaIsInsert ? 1 : -1) * (delta.range.end.row - delta.range.start.row); - var deltaColShift = (deltaIsInsert ? 1 : -1) * (delta.range.end.column - delta.range.start.column); - var deltaStart = delta.range.start; - var deltaEnd = (deltaIsInsert ? deltaStart : delta.range.end); // Collapse insert range. + var deltaIsInsert = (delta.action == "insert") + var deltaRowShift = (deltaIsInsert ? 1 : -1) * (delta.end.row - delta.start.row); + var deltaColShift = (deltaIsInsert ? 1 : -1) * (delta.end.column - delta.start.column); + var deltaStart = delta.start; + var deltaEnd = (deltaIsInsert ? deltaStart : delta.end); // Collapse insert range. // DELTA AFTER POINT: No change needed. if ($pointsInOrder(point, deltaStart, moveIfEqual)) { @@ -137,8 +136,8 @@ var Anchor = exports.Anchor = function(doc, row, column) { } // DELTA ENVELOPS POINT (delete only): Move point to delta start. - if (delta.action != 'remove') - throw 'Delete action expected.'; + if (delta.action != "remove") + throw "Delete action expected."; return { row: deltaStart.row, @@ -181,7 +180,7 @@ var Anchor = exports.Anchor = function(doc, row, column) { }; /** - * When called, the `'change'` event listener is removed. + * When called, the `"change"` event listener is removed. * **/ this.detach = function() { diff --git a/lib/ace/apply_delta.js b/lib/ace/apply_delta.js index 86bc89c6..3996920c 100644 --- a/lib/ace/apply_delta.js +++ b/lib/ace/apply_delta.js @@ -41,12 +41,12 @@ function splitLine (docLines, position) { function joinLineWithNext(docLines, row) { docLines[row] += docLines[row + 1]; - docLines.splice(row + 1, 1); + docLines.splice(row + 1, 1); } function throwDeltaError(delta, errorText){ - console.log('Invalid Delta:', delta); - throw 'Invalid Delta: ' + errorText; + console.log("Invalid Delta:", delta); + throw "Invalid Delta: " + errorText; } function positionInDocument(docLines, position) { @@ -57,32 +57,32 @@ function positionInDocument(docLines, position) { function validateDelta(docLines, delta) { // Validate action string. - if (delta.action != 'insert' && delta.action != 'remove') - throwDeltaError(delta, 'delta.action must be "insert" or "remove"'); + if (delta.action != "insert" && delta.action != "remove") + throwDeltaError(delta, "delta.action must be 'insert' or 'remove'"); // Validate lines type. if (!(delta.lines instanceof Array)) - throwDeltaError(delta, 'delta.lines must be an Array'); + throwDeltaError(delta, "delta.lines must be an Array"); // Validate range type. - if (!(delta.range instanceof Range)) - throwDeltaError(delta, 'delta.range must be an instance of the Range class'); + if (!delta.start || !delta.end) + throwDeltaError(delta, "delta.start/end must be an present"); // Validate that the start point is contained in the document. - var start = delta.range.start; - if (!positionInDocument(docLines, delta.range.start)) - throwDeltaError(delta, 'delta.range.start must be contained in document'); + var start = delta.start; + if (!positionInDocument(docLines, delta.start)) + throwDeltaError(delta, "delta.start must be contained in document"); // Validate that the end point is contained in the document (remove deltas only). - var end = delta.range.end; - if (delta.action == 'remove' && !positionInDocument(docLines, end)) - throwDeltaError(delta, 'delta.range.end must contained in document for "remove" actions'); + var end = delta.end; + if (delta.action == "remove" && !positionInDocument(docLines, end)) + throwDeltaError(delta, "delta.end must contained in document for 'remove' actions"); // Validate that the .range size matches the .lines size. var numRangeRows = end.row - start.row; var numRangeLastLineChars = (end.column - (numRangeRows == 0 ? start.column : 0)); if (numRangeRows != delta.lines.length - 1 || delta.lines[numRangeRows].length != numRangeLastLineChars) - throwDeltaError(delta, 'delta.range must match delta lines'); + throwDeltaError(delta, "delta.range must match delta lines"); } exports.applyDelta = function(docLines, delta, doNotValidate) { @@ -92,21 +92,21 @@ exports.applyDelta = function(docLines, delta, doNotValidate) { validateDelta(docLines, delta); // Apply delta. - if (delta.range.start.row == delta.range.end.row) { + if (delta.start.row == delta.end.row) { // Apply single-line delta. // Note: The multi-line code below correctly handle single-line // deltas too, but we need to short-circuit for speed. - var row = delta.range.start.row; - var startColumn = delta.range.start.column; - var endColumn = delta.range.end.column; + var row = delta.start.row; + var startColumn = delta.start.column; + var endColumn = delta.end.column; var line = docLines[row]; switch (delta.action) { - case 'insert': + case "insert": docLines[row] = line.substring(0, startColumn) + delta.lines[0] + line.substring(startColumn); break; - case 'remove': + case "remove": docLines[row] = line.substring(0, startColumn) + line.substring(endColumn); break; } @@ -114,22 +114,21 @@ exports.applyDelta = function(docLines, delta, doNotValidate) { // Apply multi-line delta. switch (delta.action) { - - case 'insert': - splitLine(docLines, delta.range.start); - docLines.splice.apply(docLines, [delta.range.start.row + 1, 0].concat(delta.lines)); - joinLineWithNext(docLines, delta.range.start.row); - joinLineWithNext(docLines, delta.range.end.row); + case "insert": + splitLine(docLines, delta.start); + docLines.splice.apply(docLines, [delta.start.row + 1, 0].concat(delta.lines)); + joinLineWithNext(docLines, delta.start.row); + joinLineWithNext(docLines, delta.end.row); break; - case 'remove': - splitLine(docLines, delta.range.end); - splitLine(docLines, delta.range.start); + case "remove": + splitLine(docLines, delta.end); + splitLine(docLines, delta.start); docLines.splice( - delta.range.start.row + 1, // Where to start deleting - delta.range.end.row - delta.range.start.row + 1 // Num lines to delete. + delta.start.row + 1, // Where to start deleting + delta.end.row - delta.start.row + 1 // Num lines to delete. ); - joinLineWithNext(docLines, delta.range.start.row); + joinLineWithNext(docLines, delta.start.row); break; } } diff --git a/lib/ace/background_tokenizer.js b/lib/ace/background_tokenizer.js index 81ea4a0f..07862188 100644 --- a/lib/ace/background_tokenizer.js +++ b/lib/ace/background_tokenizer.js @@ -177,13 +177,12 @@ var BackgroundTokenizer = function(tokenizer, editor) { } this.$updateOnChange = function(delta) { - var range = delta.range; - var startRow = range.start.row; - var len = range.end.row - startRow; + var startRow = delta.start.row; + var len = delta.end.row - startRow; if (len === 0) { this.lines[startRow] = null; - } else if (delta.action == 'remove') { + } else if (delta.action == "remove") { this.lines.splice(startRow, len + 1, null); this.states.splice(startRow, len + 1, null); } else { diff --git a/lib/ace/commands/command_manager.js b/lib/ace/commands/command_manager.js index 72a9942d..537942ab 100644 --- a/lib/ace/commands/command_manager.js +++ b/lib/ace/commands/command_manager.js @@ -13,7 +13,7 @@ var EventEmitter = require("../lib/event_emitter").EventEmitter; /** * new CommandManager(platform, commands) - * @param {String} platform Identifier for the platform; must be either `'mac'` or `'win'` + * @param {String} platform Identifier for the platform; must be either `"mac"` or `"win"` * @param {Array} commands A list of commands * **/ @@ -33,7 +33,7 @@ oop.inherits(CommandManager, HashHandler); oop.implement(this, EventEmitter); this.exec = function(command, editor, args) { - if (typeof command === 'string') + if (typeof command === "string") command = this.commands[command]; if (!command) diff --git a/lib/ace/document.js b/lib/ace/document.js index 97d69968..43d1a73c 100644 --- a/lib/ace/document.js +++ b/lib/ace/document.js @@ -53,7 +53,7 @@ var Anchor = require("./anchor").Anchor; **/ var Document = function(textOrLines) { - this.$lines = ['']; + this.$lines = [""]; // There has to be one line at least in the document. If you pass an empty // string to the insert function, nothing will happen. Workaround. @@ -99,7 +99,7 @@ var Document = function(textOrLines) { }; /** - * Splits a string of text on any newline (`\n`) or carriage-return ('\r') characters. + * Splits a string of text on any newline (`\n`) or carriage-return (`\r`) characters. * * @method $split * @param {String} text The text to work with @@ -225,12 +225,9 @@ var Document = function(textOrLines) { this.getLinesForRange = function(range) { var lines; if (range.start.row == range.end.row) { - // Handle a single-line range. - lines = [this.getLine(range.start.row).substring(range.start.column, range.end.column)]; - + lines = [this.getLine(range.start.row).substring(range.start.column, range.end.column)]; } else { - // Handle a multi-line range. lines = this.getLines(range.start.row, range.end.row); lines[0] = (lines[0] || "").substring(range.start.column); @@ -254,7 +251,6 @@ var Document = function(textOrLines) { }; this.$getClippedRange = function(range) { - // Get Range object. if (!range instanceof Range) range = Range.fromPoints(range.start, range.end); @@ -267,16 +263,16 @@ var Document = function(textOrLines) { // Deprecated methods retained for backwards compatibility. this.insertLines = function(row, lines) { - console.warn('Use of document.insertLines is deprecated. Use the insertFullLines method instead.'); + console.warn("Use of document.insertLines is deprecated. Use the insertFullLines method instead."); return this.insertFullLines(row, lines); }; this.removeLines = function(firstRow, lastRow) { - console.warn('Use of document.removeLines is deprecated. Use the removeFullLines method instead.'); + console.warn("Use of document.removeLines is deprecated. Use the removeFullLines method instead."); return this.removeFullLines(firstRow, lastRow); }; this.insertNewLine = function(position) { - console.warn('Use of document.insertNewLine is deprecated. Use insertMergedLines(position, [\'\', \'\']) instead.'); - return this.insertMergedLines(position, ['', '']); + console.warn("Use of document.insertNewLine is deprecated. Use insertMergedLines(position, [\'\', \'\']) instead."); + return this.insertMergedLines(position, ["", ""]); }; /** @@ -287,7 +283,6 @@ var Document = function(textOrLines) { * **/ this.insert = function(position, text) { - // Only detect new lines if the document has no line break yet. if (this.getLength() <= 1) this.$detectNewLine(text); @@ -296,7 +291,7 @@ var Document = function(textOrLines) { }; /** - * Inserts `text` into the `position` at the current row. This method also triggers the `'change'` event. + * Inserts `text` into the `position` at the current row. This method also triggers the `"change"` event. * * This differs from the `insert` method in two ways: * 1. This does NOT handle newline characters (single-line text only). @@ -310,7 +305,6 @@ var Document = function(textOrLines) { * ``` **/ this.insertInLine = function(position, text) { - // Calculate insertion range end point. this.$clipPosition(position); var endPoint = { @@ -318,10 +312,12 @@ var Document = function(textOrLines) { column : position.column + text.length }; + var range = Range.fromPoints(position, endPoint); // Apply delta (emits change). this.applyDelta({ action: "insert", - range: Range.fromPoints(position, endPoint), + start: range.start, + end: range.end, lines: [text] }, true /*doNotValidate*/); @@ -346,7 +342,7 @@ var Document = function(textOrLines) { **/ /** - * Inserts the elements in `lines` into the document as full lines (does not merge with existing line), starting at the row index given by `row`. This method also triggers the `'change'` event. + * Inserts the elements in `lines` into the document as full lines (does not merge with existing line), starting at the row index given by `row`. This method also triggers the `"change"` event. * @param {Number} row The index of the row to insert at * @param {Array} lines An array of strings * @returns {Object} Contains the final row and column, like this: @@ -368,11 +364,11 @@ var Document = function(textOrLines) { var column = 0; if (row < this.getLength()) { // Insert before the specified row. - lines = lines.concat(['']); + lines = lines.concat([""]); column = 0; } else { // Insert after the last row in the document. - lines = [''].concat(lines); + lines = [""].concat(lines); row--; column = this.$lines[row].length; } @@ -382,7 +378,7 @@ var Document = function(textOrLines) { }; /** - * Inserts the elements in `lines` into the document, starting at the position index given by `row`. This method also triggers the `'change'` event. + * Inserts the elements in `lines` into the document, starting at the position index given by `row`. This method also triggers the `"change"` event. * @param {Number} row The index of the row to insert at * @param {Array} lines An array of strings * @returns {Object} Contains the final row and column, like this: @@ -396,7 +392,6 @@ var Document = function(textOrLines) { * **/ this.insertMergedLines = function(position, lines) { - // Calculate insertion range end point. this.$clipPosition(position); var endPoint = { @@ -407,7 +402,8 @@ var Document = function(textOrLines) { // Apply delta (emits change). this.applyDelta({ action: "insert", - range: Range.fromPoints(position, endPoint), + start: position, + end: endPoint, lines: lines }); @@ -421,19 +417,19 @@ var Document = function(textOrLines) { * **/ this.remove = function(range) { - // Apply delta (emits change). range = this.$getClippedRange(range); this.applyDelta({ - action: 'remove', - range: range, + action: "remove", + start: range.start, + end: range.end, lines: this.getLinesForRange(range), }); - return range.start; + return range.start; }; /** - * Removes the specified columns from the `row`. This method also triggers the `'change'` event. + * Removes the specified columns from the `row`. This method also triggers the `"change"` event. * @param {Number} row The row to remove from * @param {Number} startColumn The column to start removing at * @param {Number} endColumn The column to stop removing at @@ -441,15 +437,15 @@ var Document = function(textOrLines) { * **/ this.removeInLine = function(row, startColumn, endColumn) { - // Calculate deleteion range. var range = new Range(row, startColumn, row, endColumn); range = this.$getClippedRange(range); // Apply delta (emits change). this.applyDelta({ - action: 'remove', - range: range, + action: "remove", + start: range.start, + end: range.end, lines: this.getLinesForRange(range), }, true /*doNotValidate*/); @@ -457,14 +453,13 @@ var Document = function(textOrLines) { }; /** - * Removes a range of full lines. This method also triggers the `'change'` event. + * Removes a range of full lines. This method also triggers the `"change"` event. * @param {Number} firstRow The first row to be removed * @param {Number} lastRow The last row to be removed * @returns {[String]} Returns all the removed lines. * **/ this.removeFullLines = function(firstRow, lastRow) { - // Clip to document. firstRow = Math.min(Math.max(0, firstRow), this.getLength() - 1); lastRow = Math.min(Math.max(0, lastRow ), this.getLength() - 1); @@ -485,8 +480,9 @@ var Document = function(textOrLines) { // Apply delta (emits change). this.applyDelta({ - action: 'remove', - range: range, + action: "remove", + start: range.start, + end: range.end, lines: this.getLinesForRange(range) }); @@ -495,18 +491,19 @@ var Document = function(textOrLines) { }; /** - * Removes the new line between `row` and the row immediately following it. This method also triggers the `'change'` event. + * Removes the new line between `row` and the row immediately following it. This method also triggers the `"change"` event. * @param {Number} row The row to check * **/ this.removeNewLine = function(row) { - if (row < this.getLength() - 1 && row >= 0) { + var range = new Range(row, this.getLine(row).length, row + 1, 0); // Apply delta (emits change). this.applyDelta({ - action: 'remove', - range: new Range(row, this.getLine(row).length, row + 1, 0), - lines: ['', ''] + action: "remove", + start: range.start, + end: range.end, + lines: ["", ""] }); } }; @@ -546,7 +543,7 @@ var Document = function(textOrLines) { /** * Applies all changes in `deltas` to the document. - * @param {Array} deltas An array of delta objects (can include 'insert' and 'remove' actions) + * @param {Array} deltas An array of delta objects (can include "insert" and "remove" actions) **/ this.applyDeltas = function(deltas) { for (var i=0; i=0; i--) { @@ -566,12 +563,11 @@ var Document = function(textOrLines) { /** * Applies `delta` to the document. - * @param {Object} delta A delta object (can include 'insert' and 'remove' actions) + * @param {Object} delta A delta object (can include "insert" and "remove" actions) **/ this.applyDelta = function(delta, doNotValidate) { - // An empty range is a NOOP. - if (delta.range.isEmpty()) + if (!Range.comparePoints(delta.start, delta.end)) return; // Split large insert deltas. This is necessary because: @@ -582,37 +578,40 @@ var Document = function(textOrLines) { // delta handling is too slow. If we make delete delta handling faster we can split all large deltas // as shown in https://gist.github.com/aldendaniels/8367109#file-document-snippet-js // If we do this, update validateDelta() to limit the number of lines in a delete delta. - var bIsInsert = delta.action == 'insert'; + var bIsInsert = delta.action == "insert"; while (bIsInsert && delta.lines.length > 65001) { - // Get split deltas. var lines = delta.lines.splice(0, 65000); - lines.push(''); + lines.push(""); + var range = new Range(delta.start.row, delta.start.column, + delta.start.row + 65000, 0) this.applyDelta({ action: delta.action, lines: lines, - range: new Range(delta.range.start.row, delta.range.start.column, - delta.range.start.row + 65000, 0) + start: range.start, + end: range.end, }); // Update remaining delta. - delta.range.start.row += 65000; - delta.range.start.column = 0; + delta.start.row += 65000; + delta.start.column = 0; } // Apply. applyDelta(this.$lines, delta, doNotValidate); - this._emit("change", { data: delta }); + this._emit("change", { data: delta }); }; /** * Reverts `delta` from the document. - * @param {Object} delta A delta object (can include 'insert' and 'remove' actions) + * @param {Object} delta A delta object (can include "insert" and "remove" actions) **/ this.revertDelta = function(delta) { + var range = Range.fromPoints(delta.start, delta.end); this.applyDelta({ - action: (delta.action == 'insert' ? 'remove' : 'insert'), - range: delta.range.clone(), + action: (delta.action == "insert" ? "remove" : "insert"), + start: range.start, + end: range.end, lines: delta.lines.slice() }); }; diff --git a/lib/ace/edit_session.js b/lib/ace/edit_session.js index 7cfa344c..4baa525f 100644 --- a/lib/ace/edit_session.js +++ b/lib/ace/edit_session.js @@ -253,7 +253,7 @@ var EditSession = function(text, mode) { var delta = e.data; this.$modified = true; - this.$resetRowCache(delta.range.start.row); + this.$resetRowCache(delta.start.row); var removedFolds = this.$updateInternalDataOnChange(e); if (!this.$fromUndo && this.$undoManager && !delta.ignore) { @@ -1247,30 +1247,29 @@ var EditSession = function(text, mode) { var range, point; var lastDeltaIsInsert = false; if (isInsert(delta)) { - range = Range.fromPoints(delta.range.start, delta.range.end); + range = Range.fromPoints(delta.start, delta.end); lastDeltaIsInsert = true; } else { - range = Range.fromPoints(delta.range.start, delta.range.start); + range = Range.fromPoints(delta.start, delta.start); lastDeltaIsInsert = false; } for (var i = 1; i < deltas.length; i++) { delta = deltas[i]; if (isInsert(delta)) { - point = delta.range.start; + point = delta.start; if (range.compare(point.row, point.column) == -1) { - range.setStart(delta.range.start); + range.setStart(point); } - point = delta.range.end; + point = delta.end; if (range.compare(point.row, point.column) == 1) { - range.setEnd(delta.range.end); + range.setEnd(point); } lastDeltaIsInsert = true; } else { - point = delta.range.start; + point = delta.start; if (range.compare(point.row, point.column) == -1) { - range = - Range.fromPoints(delta.range.start, delta.range.start); + range = Range.fromPoints(delta.start, delta.start); } lastDeltaIsInsert = false; } @@ -1676,8 +1675,8 @@ var EditSession = function(text, mode) { this.$updateInternalDataOnChange = function(e) { var useWrapMode = this.$useWrapMode; var action = e.data.action; - var start = e.data.range.start; - var end = e.data.range.end; + var start = e.data.start; + var end = e.data.end; var firstRow = start.row; var lastRow = end.row; var len = lastRow - firstRow; @@ -1685,11 +1684,11 @@ var EditSession = function(text, mode) { this.$updating = true; if (len != 0) { - if (action == 'remove') { + if (action == "remove") { this[useWrapMode ? "$wrapData" : "$rowLengthCache"].splice(firstRow, len); var foldLines = this.$foldData; - removedFolds = this.getFoldsInRange(e.data.range); + removedFolds = this.getFoldsInRange(e.data); this.removeFolds(removedFolds); var foldLine = this.getFoldLine(end.row); @@ -1759,10 +1758,10 @@ var EditSession = function(text, mode) { } else { // Realign folds. E.g. if you add some new chars before a fold, the // fold should "move" to the right. - len = Math.abs(e.data.range.start.column - e.data.range.end.column); - if (action == 'remove') { + len = Math.abs(e.data.start.column - e.data.end.column); + if (action == "remove") { // Get all the folds in the change range and remove them. - removedFolds = this.getFoldsInRange(e.data.range); + removedFolds = this.getFoldsInRange(e.data); this.removeFolds(removedFolds); len = -len; diff --git a/lib/ace/edit_session/folding.js b/lib/ace/edit_session/folding.js index 25adc909..857382d8 100644 --- a/lib/ace/edit_session/folding.js +++ b/lib/ace/edit_session/folding.js @@ -830,9 +830,8 @@ function Folding() { this.updateFoldWidgets = function(e) { var delta = e.data; - var range = delta.range; - var firstRow = range.start.row; - var len = range.end.row - firstRow; + var firstRow = delta.start.row; + var len = delta.end.row - firstRow; if (len === 0) { this.foldWidgets[firstRow] = null; diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 6b046a89..16d79785 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -607,11 +607,10 @@ var Editor = function(renderer, session) { * **/ this.onDocumentChange = function(e) { - // Rerender and emit "change" event. - var range = e.data.range; - var lastRow = (range.start.row == range.end.row ? range.end.row : Infinity); - this.renderer.updateLines(range.start.row, lastRow); + var delta = e.data; + var lastRow = (delta.start.row == delta.end.row ? delta.end.row : Infinity); + this.renderer.updateLines(delta.start.row, lastRow); this._signal("change", e); // Update cursor because tab characters can influence the cursor position. diff --git a/lib/ace/ext/elastic_tabstops_lite.js b/lib/ace/ext/elastic_tabstops_lite.js index 9901c5df..ce5e55f9 100644 --- a/lib/ace/ext/elastic_tabstops_lite.js +++ b/lib/ace/ext/elastic_tabstops_lite.js @@ -45,7 +45,7 @@ var ElasticTabstopsLite = function(editor) { recordChanges = true; }; this.onChange = function(e) { - var range = e.data.range + var range = e.data; if (recordChanges) { if (changedRows.indexOf(range.start.row) == -1) changedRows.push(range.start.row); diff --git a/lib/ace/keyboard/vim/maps/operators.js b/lib/ace/keyboard/vim/maps/operators.js index 067562a0..73c6922e 100644 --- a/lib/ace/keyboard/vim/maps/operators.js +++ b/lib/ace/keyboard/vim/maps/operators.js @@ -94,11 +94,9 @@ module.exports = { editor.removeLines(); util.insertMode(editor); } - break; default: if (range) { - // range.end.column ++; editor.session.remove(range); util.insertMode(editor); diff --git a/lib/ace/layer/gutter.js b/lib/ace/layer/gutter.js index 45c34565..59aa35fd 100644 --- a/lib/ace/layer/gutter.js +++ b/lib/ace/layer/gutter.js @@ -103,9 +103,8 @@ var Gutter = function(parentEl) { if (!this.$annotations.length) return; var delta = e.data; - var range = delta.range; - var firstRow = range.start.row; - var len = range.end.row - firstRow; + var firstRow = delta.start.row; + var len = delta.end.row - firstRow; if (len === 0) { // do nothing } else if (delta.action == 'remove') { diff --git a/lib/ace/lib/lang.js b/lib/ace/lib/lang.js index d6a98149..863bbb55 100644 --- a/lib/ace/lib/lang.js +++ b/lib/ace/lib/lang.js @@ -150,7 +150,6 @@ exports.getMatchOffsets = function(string, regExp) { /* deprecated */ exports.deferredCall = function(fcn) { - var timer = null; var callback = function() { timer = null; diff --git a/lib/ace/line_widgets.js b/lib/ace/line_widgets.js index 651f038d..185f6de4 100644 --- a/lib/ace/line_widgets.js +++ b/lib/ace/line_widgets.js @@ -119,9 +119,8 @@ function LineWidgets(session) { if (!lineWidgets) return; var delta = e.data; - var range = delta.range; - var startRow = range.start.row; - var len = range.end.row - startRow; + var startRow = delta.start.row; + var len = delta.end.row - startRow; if (len === 0) { // return diff --git a/lib/ace/undomanager.js b/lib/ace/undomanager.js index bd95bae9..71b4ba65 100644 --- a/lib/ace/undomanager.js +++ b/lib/ace/undomanager.js @@ -178,29 +178,31 @@ var UndoManager = function() { function $serializeDelta(delta){ return { action: delta.action, - range: delta.range, - lines: (delta.lines.length == 1 ? null : delta.lines), - text: (delta.lines.length == 1 ? delta.lines[0] : null ), + start: delta.start, + end: delta.end, + lines: delta.lines.length == 1 ? null : delta.lines, + text: delta.lines.length == 1 ? delta.lines[0] : null, }; } function $deserializeDelta(delta) { return { action: delta.action, - range: delta.range, - lines: (delta.text === null ? delta.lines : [delta.text]) + start: delta.start, + end: delta.end, + lines: delta.lines || [delta.text] }; } function cloneDeltaSetsObj(deltaSets_old, fnGetModifiedDelta) { var deltaSets_new = new Array(deltaSets_old.length); - for (var i in deltaSets_old) { + for (var i = 0; i < deltaSets_old.length; i++) { var deltaSet_old = deltaSets_old[i]; var deltaSet_new = { group: deltaSet_old.group, deltas: new Array(deltaSet_old.length)}; - for (var i_ in deltaSet_old.deltas) { - var delta_old = deltaSet_old.deltas[i_]; - deltaSet_new.deltas[i_] = fnGetModifiedDelta(delta_old); + for (var j = 0; j < deltaSet_old.deltas.length; j++) { + var delta_old = deltaSet_old.deltas[j]; + deltaSet_new.deltas[j] = fnGetModifiedDelta(delta_old); } deltaSets_new[i] = deltaSet_new; diff --git a/lib/ace/worker/mirror.js b/lib/ace/worker/mirror.js index 2f33fea6..8254f205 100644 --- a/lib/ace/worker/mirror.js +++ b/lib/ace/worker/mirror.js @@ -13,16 +13,7 @@ var Mirror = exports.Mirror = function(sender) { var _self = this; sender.on("change", function(e) { - - // Convert delta.range back into a Range instance since - // window.onMessage loses non-primitive data. See http://jsfiddle.net/nqJfw/1/. - var deltas = e.data; - for (var i in deltas) { - var delta = deltas[i]; - delta.range = Range.fromPoints(delta.range.start, delta.range.end); - } - - doc.applyDeltas(deltas); + doc.applyDeltas(e.data); if (_self.$timeout) return deferredUpdate.schedule(_self.$timeout); _self.onUpdate(); From c8d1df203ed8772805369c331f7c160f04af4bfc Mon Sep 17 00:00:00 2001 From: nightwing Date: Sun, 19 Jan 2014 04:26:02 +0400 Subject: [PATCH 020/476] restore single line delta optimization in anchor.js --- lib/ace/anchor.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/ace/anchor.js b/lib/ace/anchor.js index 4511a4ce..3431b3c4 100644 --- a/lib/ace/anchor.js +++ b/lib/ace/anchor.js @@ -101,6 +101,12 @@ var Anchor = exports.Anchor = function(doc, row, column) { **/ this.onChange = function(e) { var delta = e.data; + if (delta.start.row == delta.end.row && delta.start.row != this.row) + return; + + if (delta.start.row > this.row) + return; + var point = this.$getTransformedPoint(delta, {row: this.row, column: this.column}); this.setPosition(point.row, point.column, true); }; From 46f3d77068efbf8d1bfc3e0b3fb840c0bb90630d Mon Sep 17 00:00:00 2001 From: nightwing Date: Sun, 19 Jan 2014 04:27:07 +0400 Subject: [PATCH 021/476] simplify apply_delta.js --- lib/ace/apply_delta.js | 45 +++++++++++++----------------------------- 1 file changed, 14 insertions(+), 31 deletions(-) diff --git a/lib/ace/apply_delta.js b/lib/ace/apply_delta.js index 3996920c..ed019ccb 100644 --- a/lib/ace/apply_delta.js +++ b/lib/ace/apply_delta.js @@ -31,19 +31,6 @@ define(function(require, exports, module) { "use strict"; -var Range = require("./range").Range; - -function splitLine (docLines, position) { - var text = docLines[position.row]; - docLines[position.row] = text.slice(0, position.column); - docLines.splice(position.row + 1, 0, text.slice(position.column)); -} - -function joinLineWithNext(docLines, row) { - docLines[row] += docLines[row + 1]; - docLines.splice(row + 1, 1); -} - function throwDeltaError(delta, errorText){ console.log("Invalid Delta:", delta); throw "Invalid Delta: " + errorText; @@ -91,44 +78,40 @@ exports.applyDelta = function(docLines, delta, doNotValidate) { if (!doNotValidate) validateDelta(docLines, delta); + var row = delta.start.row; + var startColumn = delta.start.column; + var line = docLines[row]; // Apply delta. - if (delta.start.row == delta.end.row) { + if (row == delta.end.row) { // Apply single-line delta. // Note: The multi-line code below correctly handle single-line // deltas too, but we need to short-circuit for speed. - var row = delta.start.row; - var startColumn = delta.start.column; - var endColumn = delta.end.column; - var line = docLines[row]; + var endColumn = delta.end.column; switch (delta.action) { - case "insert": docLines[row] = line.substring(0, startColumn) + delta.lines[0] + line.substring(startColumn); break; - case "remove": docLines[row] = line.substring(0, startColumn) + line.substring(endColumn); break; } } else { - // Apply multi-line delta. switch (delta.action) { case "insert": - splitLine(docLines, delta.start); - docLines.splice.apply(docLines, [delta.start.row + 1, 0].concat(delta.lines)); - joinLineWithNext(docLines, delta.start.row); - joinLineWithNext(docLines, delta.end.row); + var line = docLines[row]; + var args = [row, 1].concat(delta.lines); + docLines.splice.apply(docLines, args); + docLines[row] = line.substring(0, startColumn) + docLines[row]; + docLines[row + delta.lines.length - 1] += line.substring(startColumn); break; - case "remove": - splitLine(docLines, delta.end); - splitLine(docLines, delta.start); + var endRow docLines.splice( - delta.start.row + 1, // Where to start deleting - delta.end.row - delta.start.row + 1 // Num lines to delete. + row, // Where to start deleting + delta.end.row - delta.start.row + 1, // Num lines to delete. + line.substring(0, startColumn) + docLines[delta.end.row].substring(delta.end.column) ); - joinLineWithNext(docLines, delta.start.row); break; } } From 6b60bcbbd672aef1211ddcb58ca21225b3dba369 Mon Sep 17 00:00:00 2001 From: nightwing Date: Fri, 14 Feb 2014 22:35:22 +0400 Subject: [PATCH 022/476] continue refactoring --- lib/ace/apply_delta.js | 45 ++++------ lib/ace/document.js | 155 ++++++++++++++++---------------- lib/ace/edit_session.js | 24 ++--- lib/ace/range_list.js | 12 +-- lib/ace/undomanager.js | 7 +- lib/ace/worker/mirror.js | 14 ++- lib/ace/worker/worker_client.js | 12 ++- 7 files changed, 135 insertions(+), 134 deletions(-) diff --git a/lib/ace/apply_delta.js b/lib/ace/apply_delta.js index ed019ccb..98a1a857 100644 --- a/lib/ace/apply_delta.js +++ b/lib/ace/apply_delta.js @@ -42,7 +42,6 @@ function positionInDocument(docLines, position) { } function validateDelta(docLines, delta) { - // Validate action string. if (delta.action != "insert" && delta.action != "remove") throwDeltaError(delta, "delta.action must be 'insert' or 'remove'"); @@ -73,47 +72,37 @@ function validateDelta(docLines, delta) { } exports.applyDelta = function(docLines, delta, doNotValidate) { - - // Validate delta. if (!doNotValidate) validateDelta(docLines, delta); var row = delta.start.row; var startColumn = delta.start.column; var line = docLines[row]; - // Apply delta. - if (row == delta.end.row) { - // Apply single-line delta. - // Note: The multi-line code below correctly handle single-line - // deltas too, but we need to short-circuit for speed. - var endColumn = delta.end.column; - switch (delta.action) { - case "insert": + switch (delta.action) { + case "insert": + var lines = delta.lines; + if (lines.length === 1) { docLines[row] = line.substring(0, startColumn) + delta.lines[0] + line.substring(startColumn); - break; - case "remove": - docLines[row] = line.substring(0, startColumn) + line.substring(endColumn); - break; - } - } else { - // Apply multi-line delta. - switch (delta.action) { - case "insert": + } else { var line = docLines[row]; var args = [row, 1].concat(delta.lines); docLines.splice.apply(docLines, args); docLines[row] = line.substring(0, startColumn) + docLines[row]; docLines[row + delta.lines.length - 1] += line.substring(startColumn); - break; - case "remove": - var endRow + } + break; + case "remove": + var endColumn = delta.end.column; + var endRow = delta.end.row; + if (row === endRow) { + docLines[row] = line.substring(0, startColumn) + line.substring(endColumn); + } else { docLines.splice( - row, // Where to start deleting - delta.end.row - delta.start.row + 1, // Num lines to delete. - line.substring(0, startColumn) + docLines[delta.end.row].substring(delta.end.column) + row, endRow - row + 1, + line.substring(0, startColumn) + docLines[endRow].substring(endColumn) ); - break; - } + } + break; } } }); diff --git a/lib/ace/document.js b/lib/ace/document.js index 43d1a73c..1abb95a2 100644 --- a/lib/ace/document.js +++ b/lib/ace/document.js @@ -39,13 +39,12 @@ var Anchor = require("./anchor").Anchor; /** * Contains the text of the document. Document can be attached to several [[EditSession `EditSession`]]s. - * * At its core, `Document`s are just an array of strings, with each row in the document matching up to the array index. * * @class Document **/ - /** +/** * * Creates a new `Document`. If `text` is included, the `Document` contains those strings; otherwise, it's empty. * @param {String | Array} text The starting text @@ -76,9 +75,9 @@ var Document = function(textOrLines) { * @param {String} text The text to use **/ this.setValue = function(text) { - var len = this.getLength(); - this.remove(new Range(0, 0, len, this.getLine(len-1).length)); - this.insert({row: 0, column:0}, text); + var len = this.getLength() - 1; + this.remove(new Range(0, 0, len, this.getLine(len).length)); + this.insert({row: 0, column: 0}, text); }; /** @@ -224,7 +223,7 @@ var Document = function(textOrLines) { **/ this.getLinesForRange = function(range) { var lines; - if (range.start.row == range.end.row) { + if (range.start.row === range.end.row) { // Handle a single-line range. lines = [this.getLine(range.start.row).substring(range.start.column, range.end.column)]; } else { @@ -238,29 +237,6 @@ var Document = function(textOrLines) { return lines; }; - this.$clipPosition = function(position) { - var length = this.getLength(); - if (position.row >= length) { - position.row = Math.max(0, length - 1); - position.column = this.getLine(length - 1).length; - } else { - position.row = Math.max(0, position.row); - position.column = Math.min(Math.max(position.column, 0), this.getLine(position.row).length); - } - return position; - }; - - this.$getClippedRange = function(range) { - // Get Range object. - if (!range instanceof Range) - range = Range.fromPoints(range.start, range.end); - - // Return clipped range. - this.$clipPosition(range.start); - this.$clipPosition(range.end); - return range; - }; - // Deprecated methods retained for backwards compatibility. this.insertLines = function(row, lines) { console.warn("Use of document.insertLines is deprecated. Use the insertFullLines method instead."); @@ -305,25 +281,54 @@ var Document = function(textOrLines) { * ``` **/ this.insertInLine = function(position, text) { - // Calculate insertion range end point. - this.$clipPosition(position); - var endPoint = { - row : position.row, - column : position.column + text.length - }; + var start = this.clippedPos(position.row, position.column); + var end = this.pos(position.row, position.column + text.length); - var range = Range.fromPoints(position, endPoint); - // Apply delta (emits change). this.applyDelta({ action: "insert", - start: range.start, - end: range.end, + start: start, + end: end, lines: [text] - }, true /*doNotValidate*/); + }, true); - return endPoint; + return this.clonePos(end); }; + this.clippedPos = function(row, column) { + var length = this.getLength(); + if (row === undefined) { + row = length; + } else if (row < 0) { + row = 0; + } else if (row >= length) { + row = length - 1; + column = undefined + } + var line = this.getLine(row); + column = Math.min(Math.max(column, 0), line.length); + return {row: row, column: column}; + }; + + this.clonePos = function(pos) { + return {row: pos.row, column: pos.column}; + }; + + this.pos = function(row, column) { + return {row: row, column: column}; + }; + + this.$clipPosition = function(position) { + var length = this.getLength(); + if (position.row >= length) { + position.row = Math.max(0, length - 1); + position.column = this.getLine(length - 1).length; + } else { + position.row = Math.max(0, position.row); + position.column = Math.min(Math.max(position.column, 0), this.getLine(position.row).length); + } + return position; + }; + /** * Fires whenever the document changes. * @@ -392,14 +397,12 @@ var Document = function(textOrLines) { * **/ this.insertMergedLines = function(position, lines) { - // Calculate insertion range end point. this.$clipPosition(position); var endPoint = { row : position.row + lines.length - 1, column : (lines.length == 1 ? position.column : 0) + lines[lines.length - 1].length }; - // Apply delta (emits change). this.applyDelta({ action: "insert", start: position, @@ -417,15 +420,14 @@ var Document = function(textOrLines) { * **/ this.remove = function(range) { - // Apply delta (emits change). - range = this.$getClippedRange(range); + var start = this.clippedPos(range.start.row, range.start.column); this.applyDelta({ action: "remove", - start: range.start, - end: range.end, + start: start, + end: this.clippedPos(range.end.row, range.end.column), lines: this.getLinesForRange(range), }); - return range.start; + return this.clonePos(start); }; /** @@ -437,19 +439,16 @@ var Document = function(textOrLines) { * **/ this.removeInLine = function(row, startColumn, endColumn) { - // Calculate deleteion range. - var range = new Range(row, startColumn, row, endColumn); - range = this.$getClippedRange(range); + var start = this.clippedPos(row, startColumn); - // Apply delta (emits change). this.applyDelta({ action: "remove", - start: range.start, - end: range.end, + start: start, + end: this.clippedPos(row, endColumn), lines: this.getLinesForRange(range), - }, true /*doNotValidate*/); + }, true); - return range.start; + return this.clonePos(start); }; /** @@ -478,7 +477,6 @@ var Document = function(textOrLines) { // Store delelted lines with bounding newlines ommitted (maintains previous behavior). var deletedLines = this.$lines.slice(firstRow, lastRow + 1); - // Apply delta (emits change). this.applyDelta({ action: "remove", start: range.start, @@ -496,13 +494,11 @@ var Document = function(textOrLines) { * **/ this.removeNewLine = function(row) { - if (row < this.getLength() - 1 && row >= 0) { - var range = new Range(row, this.getLine(row).length, row + 1, 0); - // Apply delta (emits change). + if (row < this.getLength() - 1 && row >= 0) { this.applyDelta({ action: "remove", - start: range.start, - end: range.end, + start: this.pos(row, this.getLine(row).length), + end: this.pos(row + 1, 0), lines: ["", ""] }); } @@ -566,10 +562,21 @@ var Document = function(textOrLines) { * @param {Object} delta A delta object (can include "insert" and "remove" actions) **/ this.applyDelta = function(delta, doNotValidate) { + var isInsert = delta.action == "insert"; // An empty range is a NOOP. - if (!Range.comparePoints(delta.start, delta.end)) + if (isInsert ? !delta.lines.length + : !Range.comparePoints(delta.start, delta.end)) return; + if (isInsert && delta.lines.length > 0xFFFF) + this.$splitAndapplyLargeDelta(delta); + + // Apply. + applyDelta(this.$lines, delta, doNotValidate); + this._signal("change", {data: delta}); + }; + + this.$splitAndapplyLargeDelta = function(delta) { // Split large insert deltas. This is necessary because: // 1. We need to support splicing delta lines into the document via $lines.splice.apply(...) // 2. fn.apply() doesn't work for a large number of params. The mallest threshold is on safari 0xFFFF. @@ -578,28 +585,18 @@ var Document = function(textOrLines) { // delta handling is too slow. If we make delete delta handling faster we can split all large deltas // as shown in https://gist.github.com/aldendaniels/8367109#file-document-snippet-js // If we do this, update validateDelta() to limit the number of lines in a delete delta. - var bIsInsert = delta.action == "insert"; - while (bIsInsert && delta.lines.length > 65001) { + while (delta.lines.length > 0xFFFF) { // Get split deltas. - var lines = delta.lines.splice(0, 65000); + var lines = delta.lines.splice(0, 0xFFFF); lines.push(""); - var range = new Range(delta.start.row, delta.start.column, - delta.start.row + 65000, 0) + var start = delta.start; this.applyDelta({ action: delta.action, lines: lines, - start: range.start, - end: range.end, - }); - - // Update remaining delta. - delta.start.row += 65000; - delta.start.column = 0; + start: this.pos(start.row, start.column), + end: this.pos(start.row += 0xFFFF, start.column = 0) // Updates remaining delta. + }, true); } - - // Apply. - applyDelta(this.$lines, delta, doNotValidate); - this._emit("change", { data: delta }); }; /** diff --git a/lib/ace/edit_session.js b/lib/ace/edit_session.js index 4baa525f..c9759fb1 100644 --- a/lib/ace/edit_session.js +++ b/lib/ace/edit_session.js @@ -1394,25 +1394,25 @@ var EditSession = function(text, mode) { **/ this.outdentRows = function (range) { var rowRange = range.collapseRows(); + var deleteRange = new Range(0, 0, 0, 0); var size = this.getTabSize(); - + for (var i = rowRange.start.row; i <= rowRange.end.row; ++i) { var line = this.getLine(i); - var row = i; - var startCol = 0; - var endCol = 0; - + + deleteRange.start.row = i; + deleteRange.end.row = i; for (var j = 0; j < size; ++j) if (line.charAt(j) != ' ') break; if (j < size && line.charAt(j) == '\t') { - startCol = j; - endCol = j + 1; + deleteRange.start.column = j; + deleteRange.end.column = j + 1; } else { - startCol = 0; - endCol = j; + deleteRange.start.column = 0; + deleteRange.end.column = j; } - this.doc.removeInLine(row, startCol, endCol); + this.remove(deleteRange); } }; @@ -1684,7 +1684,7 @@ var EditSession = function(text, mode) { this.$updating = true; if (len != 0) { - if (action == "remove") { + if (action === "remove") { this[useWrapMode ? "$wrapData" : "$rowLengthCache"].splice(firstRow, len); var foldLines = this.$foldData; @@ -1759,7 +1759,7 @@ var EditSession = function(text, mode) { // Realign folds. E.g. if you add some new chars before a fold, the // fold should "move" to the right. len = Math.abs(e.data.start.column - e.data.end.column); - if (action == "remove") { + if (action === "remove") { // Get all the folds in the change range and remove them. removedFolds = this.getFoldsInRange(e.data); this.removeFolds(removedFolds); diff --git a/lib/ace/range_list.js b/lib/ace/range_list.js index 0cbcc394..a112139a 100644 --- a/lib/ace/range_list.js +++ b/lib/ace/range_list.js @@ -181,13 +181,13 @@ var RangeList = function() { }; this.$onChange = function(e) { - var changeRange = e.data.range; - if (e.data.action[0] == "i"){ - var start = changeRange.start; - var end = changeRange.end; + var delta = e.data; + if (delta.action == "insert"){ + var start = delta.start; + var end = delta.end; } else { - var end = changeRange.start; - var start = changeRange.end; + var end = delta.start; + var start = delta.end; } var startRow = start.row; var endRow = end.row; diff --git a/lib/ace/undomanager.js b/lib/ace/undomanager.js index 71b4ba65..6da50a8b 100644 --- a/lib/ace/undomanager.js +++ b/lib/ace/undomanager.js @@ -61,10 +61,9 @@ var UndoManager = function() { * **/ this.execute = function(options) { - // Normalize deltas for storage. - var deltaSets = this.$serializeDeltas(options.args[0]); - + // var deltaSets = this.$serializeDeltas(options.args[0]); + var deltaSets = options.args[0]; // Add deltas to undo stack. this.$doc = options.args[1]; if (options.merge && this.hasUndo()){ @@ -207,7 +206,7 @@ var UndoManager = function() { deltaSets_new[i] = deltaSet_new; } - return deltaSets_new; + return deltaSets_new; } }).call(UndoManager.prototype); diff --git a/lib/ace/worker/mirror.js b/lib/ace/worker/mirror.js index 8254f205..514d377f 100644 --- a/lib/ace/worker/mirror.js +++ b/lib/ace/worker/mirror.js @@ -13,7 +13,19 @@ var Mirror = exports.Mirror = function(sender) { var _self = this; sender.on("change", function(e) { - doc.applyDeltas(e.data); + var data = e.data; + if (data[0].start) { + doc.applyDeltas(data); + } else { + for (var i = 0; i < data.length; i += 2) { + if (Array.isArray(data[i+1])) + var d = {action: "insert", start: data[i], lines: data[i+1]}; + else + var d = {action: "remove", start: data[i],end: data[i+1]}; + + doc.applyDelta(d, true); + } + } if (_self.$timeout) return deferredUpdate.schedule(_self.$timeout); _self.onUpdate(); diff --git a/lib/ace/worker/worker_client.js b/lib/ace/worker/worker_client.js index cb18998f..f5902f90 100644 --- a/lib/ace/worker/worker_client.js +++ b/lib/ace/worker/worker_client.js @@ -162,17 +162,21 @@ var WorkerClient = function(topLevelNamespaces, mod, classname, workerUrl) { this.changeListener = function(e) { if (!this.deltaQueue) { - this.deltaQueue = [e.data]; + this.deltaQueue = []; setTimeout(this.$sendDeltaQueue, 0); - } else - this.deltaQueue.push(e.data); + } + var delta = e.data; + if (delta.action == "insert") + this.deltaQueue.push(delta.start, delta.lines); + else + this.deltaQueue.push(delta.start, delta.end); }; this.$sendDeltaQueue = function() { var q = this.deltaQueue; if (!q) return; this.deltaQueue = null; - if (q.length > 20 && q.length > this.$doc.getLength() >> 1) { + if (q.length > 50 && q.length > this.$doc.getLength() >> 1) { this.call("setValue", [this.$doc.getValue()]); } else this.emit("change", {data: q}); From 27b6d6dcd38557b2836a71d3933db8270ee7a343 Mon Sep 17 00:00:00 2001 From: nightwing Date: Sat, 15 Feb 2014 00:01:30 +0400 Subject: [PATCH 023/476] apply arguments max length also includes actual call stack --- lib/ace/document.js | 45 +++++++++++++++++++++++------------------- lib/ace/placeholder.js | 2 +- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/lib/ace/document.js b/lib/ace/document.js index 1abb95a2..99b7bb2d 100644 --- a/lib/ace/document.js +++ b/lib/ace/document.js @@ -302,9 +302,11 @@ var Document = function(textOrLines) { row = 0; } else if (row >= length) { row = length - 1; - column = undefined - } + column = undefined; + } var line = this.getLine(row); + if (column == undefined) + column = line.length; column = Math.min(Math.max(column, 0), line.length); return {row: row, column: column}; }; @@ -421,31 +423,33 @@ var Document = function(textOrLines) { **/ this.remove = function(range) { var start = this.clippedPos(range.start.row, range.start.column); + var end = this.clippedPos(range.end.row, range.end.column); this.applyDelta({ action: "remove", start: start, - end: this.clippedPos(range.end.row, range.end.column), - lines: this.getLinesForRange(range), + end: end, + lines: this.getLinesForRange({start: start, end: end}), }); return this.clonePos(start); }; /** - * Removes the specified columns from the `row`. This method also triggers the `"change"` event. - * @param {Number} row The row to remove from - * @param {Number} startColumn The column to start removing at - * @param {Number} endColumn The column to stop removing at - * @returns {Object} Returns an object containing `startRow` and `startColumn`, indicating the new row and column values.
If `startColumn` is equal to `endColumn`, this function returns nothing. - * - **/ + * Removes the specified columns from the `row`. This method also triggers a `"change"` event. + * @param {Number} row The row to remove from + * @param {Number} startColumn The column to start removing at + * @param {Number} endColumn The column to stop removing at + * @returns {Object} Returns an object containing `startRow` and `startColumn`, indicating the new row and column values.
If `startColumn` is equal to `endColumn`, this function returns nothing. + * + **/ this.removeInLine = function(row, startColumn, endColumn) { - var start = this.clippedPos(row, startColumn); + var start = this.clippedPos(row, startColumn); + var end = this.clippedPos(row, endColumn); this.applyDelta({ action: "remove", start: start, - end: this.clippedPos(row, endColumn), - lines: this.getLinesForRange(range), + end: end, + lines: this.getLinesForRange({start: start, end: end}), }, true); return this.clonePos(start); @@ -568,7 +572,7 @@ var Document = function(textOrLines) { : !Range.comparePoints(delta.start, delta.end)) return; - if (isInsert && delta.lines.length > 0xFFFF) + if (isInsert && delta.lines.length > 0xF000) this.$splitAndapplyLargeDelta(delta); // Apply. @@ -579,22 +583,23 @@ var Document = function(textOrLines) { this.$splitAndapplyLargeDelta = function(delta) { // Split large insert deltas. This is necessary because: // 1. We need to support splicing delta lines into the document via $lines.splice.apply(...) - // 2. fn.apply() doesn't work for a large number of params. The mallest threshold is on safari 0xFFFF. + // 2. fn.apply() doesn't work for a large number of params. The smallest threshold is on safari 0xFFFF. + // we use 0xF000 to leave some space for actual stack // // To Do: Ideally we'd be consistent and also split 'delete' deltas. We don't do this now, because delete // delta handling is too slow. If we make delete delta handling faster we can split all large deltas // as shown in https://gist.github.com/aldendaniels/8367109#file-document-snippet-js // If we do this, update validateDelta() to limit the number of lines in a delete delta. - while (delta.lines.length > 0xFFFF) { + while (delta.lines.length > 0xF000) { // Get split deltas. - var lines = delta.lines.splice(0, 0xFFFF); + var lines = delta.lines.splice(0, 0xF000); lines.push(""); var start = delta.start; this.applyDelta({ action: delta.action, lines: lines, - start: this.pos(start.row, start.column), - end: this.pos(start.row += 0xFFFF, start.column = 0) // Updates remaining delta. + start: this.pos(start.row, start.column), + end: this.pos(start.row += 0xF000, start.column = 0) // Updates remaining delta. }, true); } }; diff --git a/lib/ace/placeholder.js b/lib/ace/placeholder.js index 1d345a24..d7a2d6e9 100644 --- a/lib/ace/placeholder.js +++ b/lib/ace/placeholder.js @@ -150,7 +150,7 @@ var PlaceHolder = function(session, length, pos, others, mainClass, othersClass) **/ this.onUpdate = function(event) { var delta = event.data; - var range = delta.range; + var range = delta; if(range.start.row !== range.end.row) return; if(range.start.row !== this.pos.row) return; if (this.$updating) return; From ad54d2c46cb8b161295ec215d4495c4b69adb661 Mon Sep 17 00:00:00 2001 From: nightwing Date: Wed, 2 Jul 2014 22:00:51 +0400 Subject: [PATCH 024/476] disabled validateDelta since it breaks autocompletion popup --- lib/ace/apply_delta.js | 10 +++++----- lib/ace/autocomplete/popup.js | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/ace/apply_delta.js b/lib/ace/apply_delta.js index 98a1a857..98bfe148 100644 --- a/lib/ace/apply_delta.js +++ b/lib/ace/apply_delta.js @@ -52,7 +52,7 @@ function validateDelta(docLines, delta) { // Validate range type. if (!delta.start || !delta.end) - throwDeltaError(delta, "delta.start/end must be an present"); + throwDeltaError(delta, "delta.start/end must be an present"); // Validate that the start point is contained in the document. var start = delta.start; @@ -72,19 +72,19 @@ function validateDelta(docLines, delta) { } exports.applyDelta = function(docLines, delta, doNotValidate) { - if (!doNotValidate) - validateDelta(docLines, delta); + // disabled validation since it breaks autocompletion popup + // if (!doNotValidate) + // validateDelta(docLines, delta); var row = delta.start.row; var startColumn = delta.start.column; - var line = docLines[row]; + var line = docLines[row] || ""; switch (delta.action) { case "insert": var lines = delta.lines; if (lines.length === 1) { docLines[row] = line.substring(0, startColumn) + delta.lines[0] + line.substring(startColumn); } else { - var line = docLines[row]; var args = [row, 1].concat(delta.lines); docLines.splice.apply(docLines, args); docLines[row] = line.substring(0, startColumn) + docLines[row]; diff --git a/lib/ace/autocomplete/popup.js b/lib/ace/autocomplete/popup.js index a0ab7604..471cdb90 100644 --- a/lib/ace/autocomplete/popup.js +++ b/lib/ace/autocomplete/popup.js @@ -213,8 +213,8 @@ var AcePopup = function(parentNode) { popup.data = []; popup.setData = function(list) { - popup.data = list || []; popup.setValue(lang.stringRepeat("\n", list.length), -1); + popup.data = list || []; popup.setRow(0); }; popup.getData = function(row) { From 0eaae8712e4e52e03ffc3165f86cff0b8dc92c39 Mon Sep 17 00:00:00 2001 From: nightwing Date: Tue, 22 Jul 2014 16:32:40 +0400 Subject: [PATCH 025/476] do not rename kr_theme to kr during the build --- Makefile.dryice.js | 2 +- lib/ace/config.js | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/Makefile.dryice.js b/Makefile.dryice.js index 2bc36f8b..bac090f7 100755 --- a/Makefile.dryice.js +++ b/Makefile.dryice.js @@ -359,7 +359,7 @@ function buildAce(options) { buildSubmodule(options, { projectType: "theme", require: ["ace/theme/" + name] - }, "theme-" + name.replace("_theme", "")); + }, "theme-" + name); }); // keybindings ["vim", "emacs"].forEach(function(name) { diff --git a/lib/ace/config.js b/lib/ace/config.js index f4e4c89a..94e9465f 100644 --- a/lib/ace/config.js +++ b/lib/ace/config.js @@ -80,11 +80,7 @@ exports.moduleUrl = function(name, component) { // todo make this configurable or get rid of '-' var sep = component == "snippets" ? "/" : "-"; - var base = parts[parts.length - 1]; - if (sep == "-") { - var re = new RegExp("^" + component + "[\\-_]|[\\-_]" + component + "$", "g"); - base = base.replace(re, ""); - } + var base = parts[parts.length - 1]; if ((!base || base == component) && parts.length > 1) base = parts[parts.length - 2]; From 23f678229aaad720b8f82d98743bece56114a2de Mon Sep 17 00:00:00 2001 From: nightwing Date: Tue, 22 Jul 2014 16:57:14 +0400 Subject: [PATCH 026/476] update config_test.js --- lib/ace/config_test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ace/config_test.js b/lib/ace/config_test.js index d09a2801..ac664fae 100644 --- a/lib/ace/config_test.js +++ b/lib/ace/config_test.js @@ -43,7 +43,7 @@ module.exports = { "test: path resolution" : function() { config.set("packaged", "true"); var url = config.moduleUrl("kr_theme", "theme"); - assert.equal(url, "theme-kr.js"); + assert.equal(url, "theme-kr_theme.js"); config.set("basePath", "a/b"); url = config.moduleUrl("m/theme", "theme"); From 80f42e13ffe424ea5ac1f4f68b7e2173a9c39778 Mon Sep 17 00:00:00 2001 From: nightwing Date: Mon, 28 Jul 2014 19:10:41 +0400 Subject: [PATCH 027/476] accept mode options object in static highlight extension --- lib/ace/ext/static_highlight.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/ace/ext/static_highlight.js b/lib/ace/ext/static_highlight.js index 1d69a9bb..9116467b 100644 --- a/lib/ace/ext/static_highlight.js +++ b/lib/ace/ext/static_highlight.js @@ -98,7 +98,6 @@ var highlight = function(el, opts, callback) { * and `css`. * @returns {object} An object containing the properties `html` and `css`. */ - highlight.render = function(input, mode, theme, lineStart, disableGutter, callback) { var waiting = 1; var modeCache = EditSession.prototype.$modes; @@ -112,11 +111,17 @@ highlight.render = function(input, mode, theme, lineStart, disableGutter, callba --waiting || done(); }); } - + // allow setting mode options e.h {path: "ace/mode/php", inline:true} + var modeOptions; + if (mode && typeof mode === "object" && !mode.getTokenizer) { + modeOptions = mode; + mode = modeOptions.path; + } if (typeof mode == "string") { waiting++; config.loadModule(['mode', mode], function(m) { - if (!modeCache[mode]) modeCache[mode] = new m.Mode(); + if (!modeCache[mode] || modeOptions) + modeCache[mode] = new m.Mode(modeOptions); mode = modeCache[mode]; --waiting || done(); }); @@ -130,14 +135,13 @@ highlight.render = function(input, mode, theme, lineStart, disableGutter, callba return --waiting || done(); }; -/* +/** * Transforms a given input code snippet into HTML using the given mode * @param {string} input Code snippet * @param {mode} mode Mode loaded from /ace/mode (use 'ServerSideHiglighter.getMode') * @param {string} r Code snippet * @returns {object} An object containing: html, css */ - highlight.renderSync = function(input, mode, theme, lineStart, disableGutter) { lineStart = parseInt(lineStart || 1, 10); From c673190bf79d1e6c04aa372f0b39e31445031f17 Mon Sep 17 00:00:00 2001 From: nightwing Date: Tue, 22 Jul 2014 03:20:43 +0400 Subject: [PATCH 028/476] allow binding multiple commands to same key --- lib/ace/commands/command_manager.js | 18 ++-- lib/ace/editor.js | 49 +++------ lib/ace/ext/language_tools.js | 4 +- lib/ace/keyboard/emacs.js | 4 +- lib/ace/keyboard/hash_handler.js | 149 +++++++++++++++++----------- lib/ace/lib/keys.js | 3 + 6 files changed, 122 insertions(+), 105 deletions(-) diff --git a/lib/ace/commands/command_manager.js b/lib/ace/commands/command_manager.js index 72a9942d..7b017ed2 100644 --- a/lib/ace/commands/command_manager.js +++ b/lib/ace/commands/command_manager.js @@ -2,13 +2,12 @@ define(function(require, exports, module) { "use strict"; var oop = require("../lib/oop"); -var HashHandler = require("../keyboard/hash_handler").HashHandler; +var MultiHashHandler = require("../keyboard/hash_handler").MultiHashHandler; var EventEmitter = require("../lib/event_emitter").EventEmitter; /** * @class CommandManager * - * **/ /** @@ -19,20 +18,27 @@ var EventEmitter = require("../lib/event_emitter").EventEmitter; **/ var CommandManager = function(platform, commands) { - HashHandler.call(this, commands, platform); + MultiHashHandler.call(this, commands, platform); this.byName = this.commands; this.setDefaultHandler("exec", function(e) { return e.command.exec(e.editor, e.args || {}); }); }; -oop.inherits(CommandManager, HashHandler); +oop.inherits(CommandManager, MultiHashHandler); (function() { oop.implement(this, EventEmitter); this.exec = function(command, editor, args) { + if (Array.isArray(command)) { + for (var i = command.length; i--; ) { + if (this.exec(command[i], editor, args)) return true; + } + return false; + } + if (typeof command === 'string') command = this.commands[command]; @@ -43,10 +49,10 @@ oop.inherits(CommandManager, HashHandler); return false; var e = {editor: editor, command: command, args: args}; - var retvalue = this._emit("exec", e); + e.returnValue = this._emit("exec", e); this._signal("afterExec", e); - return retvalue === false ? false : true; + return e.returnValue === false ? false : true; }; this.toggleRecording = function(editor) { diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 6a68e5de..6c1a9200 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -114,30 +114,8 @@ var Editor = function(renderer, session) { function last(a) {return a[a.length - 1]} this.selections = []; - this.commands.on("exec", function(e) { - this.startOperation(e); - - var command = e.command; - if (command.aceCommandGroup == "fileJump") { - var prev = this.prevOp; - if (!prev || prev.command.aceCommandGroup != "fileJump") { - this.lastFileJumpPos = last(this.selections); - } - } else { - this.lastFileJumpPos = null; - } - }.bind(this), true); - - this.commands.on("afterExec", function(e) { - var command = e.command; - - if (command.aceCommandGroup == "fileJump") { - if (this.lastFileJumpPos && !this.curOp.selectionChanged) { - this.selection.fromJSON(this.lastFileJumpPos); - } - } - this.endOperation(e); - }.bind(this), true); + this.commands.on("exec", this.startOperation.bind(this), true); + this.commands.on("afterExec", this.endOperation.bind(this), true); this.$opResetTimer = lang.delayedCall(this.endOperation.bind(this)); @@ -172,18 +150,16 @@ var Editor = function(renderer, session) { scrollTop: this.renderer.scrollTop }; - var command = this.curOp.command; - if (command && command.scrollIntoView) - this.$blockScrolling++; - - this.selections.push(this.selection.toJSON()); + // this.selections.push(this.selection.toJSON()); }; - this.endOperation = function() { + this.endOperation = function(e) { if (this.curOp) { + if (e && e.returnValue === false) + return this.curOp = null; + var command = this.curOp.command; if (command && command.scrollIntoView) { - this.$blockScrolling--; switch (command.scrollIntoView) { case "center": this.renderer.scrollCursorIntoView(null, 0.5); @@ -254,19 +230,19 @@ var Editor = function(renderer, session) { * @param {String} keyboardHandler The new key handler * **/ - this.setKeyboardHandler = function(keyboardHandler) { - if (!keyboardHandler) { - this.keyBinding.setKeyboardHandler(null); - } else if (typeof keyboardHandler === "string") { + this.setKeyboardHandler = function(keyboardHandler, cb) { + if (keyboardHandler && typeof keyboardHandler === "string") { this.$keybindingId = keyboardHandler; var _self = this; config.loadModule(["keybinding", keyboardHandler], function(module) { if (_self.$keybindingId == keyboardHandler) _self.keyBinding.setKeyboardHandler(module && module.handler); + cb && cb(); }); } else { this.$keybindingId = null; this.keyBinding.setKeyboardHandler(keyboardHandler); + cb && cb(); } }; @@ -925,9 +901,8 @@ var Editor = function(renderer, session) { this.insert(e.text, true); }; - this.execCommand = function(command, args) { - this.commands.exec(command, this, args); + return this.commands.exec(command, this, args); }; /** diff --git a/lib/ace/ext/language_tools.js b/lib/ace/ext/language_tools.js index d8b2a1dd..9d10695d 100644 --- a/lib/ace/ext/language_tools.js +++ b/lib/ace/ext/language_tools.js @@ -80,9 +80,7 @@ exports.snippetCompleter = snippetCompleter; var expandSnippet = { name: "expandSnippet", exec: function(editor) { - var success = snippetManager.expandWithTab(editor); - if (!success) - editor.execCommand("indent"); + return snippetManager.expandWithTab(editor); }, bindKey: "Tab" }; diff --git a/lib/ace/keyboard/emacs.js b/lib/ace/keyboard/emacs.js index f1adb9ef..c15f07d0 100644 --- a/lib/ace/keyboard/emacs.js +++ b/lib/ace/keyboard/emacs.js @@ -64,7 +64,7 @@ exports.handler.attach = function(editor) { initialized = true; dom.importCssString('\ .emacs-mode .ace_cursor{\ - border: 2px rgba(50,250,50,0.8) solid!important;\ + border: 1px rgba(50,250,50,0.8) solid!important;\ -moz-box-sizing: border-box!important;\ -webkit-box-sizing: border-box!important;\ box-sizing: border-box!important;\ @@ -195,6 +195,8 @@ exports.handler.onPaste = function(e, editor) { }; exports.handler.bindKey = function(key, command) { + if (typeof key == "object") + key = key[this.platform]; if (!key) return; diff --git a/lib/ace/keyboard/hash_handler.js b/lib/ace/keyboard/hash_handler.js index cb02f170..06badccd 100644 --- a/lib/ace/keyboard/hash_handler.js +++ b/lib/ace/keyboard/hash_handler.js @@ -33,37 +33,25 @@ define(function(require, exports, module) { var keyUtil = require("../lib/keys"); var useragent = require("../lib/useragent"); +var KEY_MODS = keyUtil.KEY_MODS; function HashHandler(config, platform) { this.platform = platform || (useragent.isMac ? "mac" : "win"); this.commands = {}; this.commandKeyBinding = {}; - - // todo remove this after a while - if (this.__defineGetter__ && this.__defineSetter__ && typeof console != "undefined" && console.error) { - var warned = false; - var warn = function() { - if (!warned) { - warned = true; - console.error("commmandKeyBinding has too many m's. use commandKeyBinding"); - } - }; - this.__defineGetter__("commmandKeyBinding", function() { - warn(); - return this.commandKeyBinding; - }); - this.__defineSetter__("commmandKeyBinding", function(val) { - warn(); - return this.commandKeyBinding = val; - }); - } else { - this.commmandKeyBinding = this.commandKeyBinding; - } - this.addCommands(config); -}; + this.$singleCommand = true; +} + +function MultiHashHandler(config, platform) { + HashHandler.call(this, config, platform); + this.$singleCommand = false; +} + +MultiHashHandler.prototype = HashHandler.prototype; (function() { + this.addCommand = function(command) { if (this.commands[command.name]) @@ -75,37 +63,76 @@ function HashHandler(config, platform) { this._buildKeyHash(command); }; - this.removeCommand = function(command) { - var name = (typeof command === 'string' ? command : command.name); + this.removeCommand = function(command, keepCommand) { + var name = command && (typeof command === 'string' ? command : command.name); command = this.commands[name]; - delete this.commands[name]; + if (!keepCommand) + delete this.commands[name]; // exhaustive search is brute force but since removeCommand is // not a performance critical operation this should be OK var ckb = this.commandKeyBinding; - for (var hashId in ckb) { - for (var key in ckb[hashId]) { - if (ckb[hashId][key] == command) - delete ckb[hashId][key]; + for (var keyId in ckb) { + var cmdGroup = ckb[keyId]; + if (cmdGroup == command) { + delete ckb[keyId]; + } else if (Array.isArray(cmdGroup)) { + var i = cmdGroup.indexOf(command); + if (i != -1) { + cmdGroup.splice(i, 1); + if (cmdGroup.length == 1) + ckb[keyId] = cmdGroup[0]; + } } } }; - this.bindKey = function(key, command) { - if(!key) + this.bindKey = function(key, command, asDefault) { + if (typeof key == "object") + key = key[this.platform]; + if (!key) return; - if (typeof command == "function") { - this.addCommand({exec: command, bindKey: key, name: command.name || key}); - return; - } - - var ckb = this.commandKeyBinding; + if (typeof command == "function") + return this.addCommand({exec: command, bindKey: key, name: command.name || key}); + key.split("|").forEach(function(keyPart) { - var binding = this.parseKeys(keyPart, command); - var hashId = binding.hashId; - (ckb[hashId] || (ckb[hashId] = {}))[binding.key] = command; + var chain = ""; + if (keyPart.indexOf(" ") != -1) { + var parts = keyPart.split(/\s+/); + keyPart = parts.pop(); + parts.forEach(function(keyPart) { + var binding = this.parseKeys(keyPart); + var id = KEY_MODS[binding.hashId] + binding.key; + chain += (chain ? " " : "") + id; + this._addCommandToBinding(chain, "chainKeys"); + }, this); + chain += " "; + } + var binding = this.parseKeys(keyPart); + var id = KEY_MODS[binding.hashId] + binding.key; + this._addCommandToBinding(chain + id, command, asDefault); }, this); }; + + this._addCommandToBinding = function(keyId, command, asDefault) { + var ckb = this.commandKeyBinding, i; + if (!command) { + delete ckb[keyId]; + } else if (!ckb[keyId] || this.$singleCommand) { + ckb[keyId] = command; + } else { + if (!Array.isArray(ckb[keyId])) { + ckb[keyId] = [ckb[keyId]]; + } else if ((i = ckb[keyId].indexOf(command)) != -1) { + ckb[keyId].splice(i, 1); + } + + if (asDefault || command.isDefault) + ckb[keyId].unshift(command); + else + ckb[keyId].push(command); + } + }; this.addCommands = function(commands) { commands && Object.keys(commands).forEach(function(name) { @@ -142,21 +169,12 @@ function HashHandler(config, platform) { }; this._buildKeyHash = function(command) { - var binding = command.bindKey; - if (!binding) - return; - - var key = typeof binding == "string" ? binding: binding[this.platform]; - this.bindKey(key, command); + this.bindKey(command.bindKey, command); }; // accepts keys in the form ctrl+Enter or ctrl-Enter // keys without modifiers or shift only this.parseKeys = function(keys) { - // todo support keychains - if (keys.indexOf(" ") != -1) - keys = keys.split(/\s+/).pop(); - var parts = keys.toLowerCase().split(/[\-\+]([\-\+])?/).filter(function(x){return x}); var key = parts.pop(); @@ -173,7 +191,7 @@ function HashHandler(config, platform) { var modifier = keyUtil.KEY_MODS[parts[i]]; if (modifier == null) { if (typeof console != "undefined") - console.error("invalid modifier " + parts[i] + " in " + keys); + console.error("invalid modifier " + parts[i] + " in " + keys); return false; } hashId |= modifier; @@ -182,17 +200,32 @@ function HashHandler(config, platform) { }; this.findKeyCommand = function findKeyCommand(hashId, keyString) { - var ckbr = this.commandKeyBinding; - return ckbr[hashId] && ckbr[hashId][keyString]; + var key = KEY_MODS[hashId] + keyString; + return this.commandKeyBinding[key]; }; this.handleKeyboard = function(data, hashId, keyString, keyCode) { - return { - command: this.findKeyCommand(hashId, keyString) - }; + var key = KEY_MODS[hashId] + keyString; + var command = this.commandKeyBinding[key]; + if (data.$keyChain) { + data.$keyChain += " " + key; + command = this.commandKeyBinding[data.$keyChain] || command; + } + + if (command) { + if (command == "chainKeys" || command[command.length - 1] == "chainKeys") { + data.$keyChain = data.$keyChain || key; + return {command: "null"}; + } + } + + if (data.$keyChain && keyCode > 0) + data.$keyChain = ""; + return {command: command}; }; -}).call(HashHandler.prototype) +}).call(HashHandler.prototype); exports.HashHandler = HashHandler; +exports.MultiHashHandler = MultiHashHandler; }); diff --git a/lib/ace/lib/keys.js b/lib/ace/lib/keys.js index e708a5d4..e3c19214 100644 --- a/lib/ace/lib/keys.js +++ b/lib/ace/lib/keys.js @@ -143,6 +143,9 @@ var Keys = (function() { } })(); + ret.KEY_MODS[0] = ""; + ret.KEY_MODS[-1] = "input"; + return ret; })(); oop.mixin(exports, Keys); From b2612c6039e74972cff414eb17f2346993609fed Mon Sep 17 00:00:00 2001 From: nightwing Date: Tue, 29 Jul 2014 17:40:43 +0400 Subject: [PATCH 029/476] cleanup emmet extension --- lib/ace/ext/emmet.js | 82 +++++++++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 31 deletions(-) diff --git a/lib/ace/ext/emmet.js b/lib/ace/ext/emmet.js index e547ae4d..75eefa3f 100644 --- a/lib/ace/ext/emmet.js +++ b/lib/ace/ext/emmet.js @@ -34,15 +34,7 @@ var HashHandler = require("ace/keyboard/hash_handler").HashHandler; var Editor = require("ace/editor").Editor; var snippetManager = require("ace/snippets").snippetManager; var Range = require("ace/range").Range; -var emmet; - -Editor.prototype.indexToPosition = function(index) { - return this.session.doc.indexToPosition(index); -}; - -Editor.prototype.positionToIndex = function(pos) { - return this.session.doc.positionToIndex(pos); -}; +var emmet, emmetPath; /** * Implementation of {@link IEmmetEditor} interface for Ace @@ -72,9 +64,10 @@ AceEmmetEditor.prototype = { getSelectionRange: function() { // TODO should start be caret position instead? var range = this.ace.getSelectionRange(); + var doc = this.ace.session.doc; return { - start: this.ace.positionToIndex(range.start), - end: this.ace.positionToIndex(range.end) + start: doc.positionToIndex(range.start), + end: doc.positionToIndex(range.end) }; }, @@ -91,9 +84,10 @@ AceEmmetEditor.prototype = { * editor.createSelection(15); */ createSelection: function(start, end) { + var doc = this.ace.session.doc; this.ace.selection.setRange({ - start: this.ace.indexToPosition(start), - end: this.ace.indexToPosition(end) + start: doc.indexToPosition(start), + end: doc.indexToPosition(end) }); }, @@ -106,9 +100,10 @@ AceEmmetEditor.prototype = { * alert(range.start + ', ' + range.end); */ getCurrentLineRange: function() { - var row = this.ace.getCursorPosition().row; - var lineLength = this.ace.session.getLine(row).length; - var index = this.ace.positionToIndex({row: row, column: 0}); + var ace = this.ace; + var row = ace.getCursorPosition().row; + var lineLength = ace.session.getLine(row).length; + var index = ace.session.doc.positionToIndex({row: row, column: 0}); return { start: index, end: index + lineLength @@ -121,7 +116,7 @@ AceEmmetEditor.prototype = { */ getCaretPos: function(){ var pos = this.ace.getCursorPosition(); - return this.ace.positionToIndex(pos); + return this.ace.session.doc.positionToIndex(pos); }, /** @@ -129,7 +124,7 @@ AceEmmetEditor.prototype = { * @param {Number} index Caret position */ setCaretPos: function(index){ - var pos = this.ace.indexToPosition(index); + var pos = this.ace.session.doc.indexToPosition(index); this.ace.selection.moveToPosition(pos); }, @@ -169,14 +164,15 @@ AceEmmetEditor.prototype = { start = 0; var editor = this.ace; - var range = Range.fromPoints(editor.indexToPosition(start), editor.indexToPosition(end)); + var doc = editor.session.doc; + var range = Range.fromPoints(doc.indexToPosition(start), doc.indexToPosition(end)); editor.session.remove(range); range.end = range.start; //editor.selection.setRange(range); value = this.$updateTabstops(value); - snippetManager.insertSnippet(editor, value) + snippetManager.insertSnippet(editor, value); }, /** @@ -292,7 +288,7 @@ AceEmmetEditor.prototype = { lastZero = range.create(data.start, result); } - return result + return result; }, escape: function(ch) { if (ch == '$') return '\\$'; @@ -363,12 +359,17 @@ exports.runEmmetCommand = function(editor) { }, 0); } + var pos = editor.selection.lead; + var token = editor.session.getTokenAt(pos.row, pos.column); + if (token && /\btag\b/.test(token.type)) + return false; + try { var result = actions.run(this.action, editorProxy); } catch(e) { editor._signal("changeStatus", typeof e == "string" ? e : e.message); console.log(e); - result = false + result = false; } return result; }; @@ -383,21 +384,36 @@ for (var command in keymap) { }); } +exports.updateCommands = function(editor, enabled) { + if (enabled) { + editor.keyBinding.addKeyboardHandler(exports.commands); + } else { + editor.keyBinding.removeKeyboardHandler(exports.commands); + } +}; + +exports.isSupportedMode = function(modeId) { + return modeId && /css|less|scss|sass|stylus|html|php|twig/.test(modeId); +}; + var onChangeMode = function(e, target) { var editor = target; if (!editor) return; - var modeId = editor.session.$modeId; - var enabled = modeId && /css|less|scss|sass|stylus|html|php/.test(modeId); + var enabled = exports.isSupportedMode(editor.session.$modeId); if (e.enableEmmet === false) enabled = false; - if (enabled) - editor.keyBinding.addKeyboardHandler(exports.commands); - else - editor.keyBinding.removeKeyboardHandler(exports.commands); + if (enabled) { + if (typeof emmetPath == "string") { + require("ace/config").loadModule(emmetPath, function() { + + }); + emmetPath = null; + } + } + exports.updateCommands(editor, enabled); }; - exports.AceEmmetEditor = AceEmmetEditor; require("ace/config").defineOptions(Editor.prototype, "editor", { enableEmmet: { @@ -409,7 +425,11 @@ require("ace/config").defineOptions(Editor.prototype, "editor", { } }); - -exports.setCore = function(e) {emmet = e;}; +exports.setCore = function(e) { + if (typeof e == "string") + emmetPath = e; + else + emmet = e; +}; }); From a00ce9cd72d5b2f417fb8ed5d9282bfaa5a54182 Mon Sep 17 00:00:00 2001 From: nightwing Date: Fri, 1 Aug 2014 20:47:49 +0400 Subject: [PATCH 030/476] update keyboard shortcuts extension --- .../get_editor_keyboard_shortcuts.js | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/lib/ace/ext/menu_tools/get_editor_keyboard_shortcuts.js b/lib/ace/ext/menu_tools/get_editor_keyboard_shortcuts.js index e412bfba..99e006b0 100644 --- a/lib/ace/ext/menu_tools/get_editor_keyboard_shortcuts.js +++ b/lib/ace/ext/menu_tools/get_editor_keyboard_shortcuts.js @@ -69,29 +69,20 @@ module.exports.getEditorKeybordShortcuts = function(editor) { editor.keyBinding.$handlers.forEach(function(handler) { var ckb = handler.commandKeyBinding; for (var i in ckb) { - var modifier = parseInt(i); - if (modifier == -1) { - modifier = ""; - } else if(isNaN(modifier)) { - modifier = i; - } else { - modifier = "" + - (modifier & KEY_MODS.command ? "Cmd-" : "") + - (modifier & KEY_MODS.ctrl ? "Ctrl-" : "") + - (modifier & KEY_MODS.alt ? "Alt-" : "") + - (modifier & KEY_MODS.shift ? "Shift-" : ""); - } - for (var key in ckb[i]) { - var command = ckb[i][key] + var key = i.replace(/(^|-)\w/g, function(x) { return x.toUpperCase(); }); + var commands = ckb[i]; + if (!Array.isArray(commands)) + commands = [commands]; + commands.forEach(function(command) { if (typeof command != "string") command = command.name if (commandMap[command]) { - commandMap[command].key += "|" + modifier + key; + commandMap[command].key += "|" + key; } else { - commandMap[command] = {key: modifier+key, command: command}; + commandMap[command] = {key: key, command: command}; keybindings.push(commandMap[command]); - } - } + } + }); } }); return keybindings; From 5744a3c6e3fc0c15b4fb22803db339e690f38f7f Mon Sep 17 00:00:00 2001 From: nightwing Date: Fri, 1 Aug 2014 20:53:08 +0400 Subject: [PATCH 031/476] fix bracket matching in vala mode --- lib/ace/mode/vala_highlight_rules.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/ace/mode/vala_highlight_rules.js b/lib/ace/mode/vala_highlight_rules.js index f199426d..b4a96a3d 100644 --- a/lib/ace/mode/vala_highlight_rules.js +++ b/lib/ace/mode/vala_highlight_rules.js @@ -136,7 +136,7 @@ var ValaHighlightRules = function() { [ { token: 'meta.class.vala', regex: '(?=\\w?[\\w\\s]*(?:class|(?:@)?interface|enum|struct|namespace)\\s+\\w+)', push: - [ { token: 'punctuation.section.class.end.vala', + [ { token: 'paren.vala', regex: '}', next: 'pop' }, { include: '#storage-modifiers' }, @@ -166,10 +166,10 @@ var ValaHighlightRules = function() { { include: '#object-types-inherited' }, { include: '#comments' }, { defaultToken: 'meta.definition.class.implemented.interfaces.vala' } ] }, - { token: 'meta.class.body.vala', + { token: 'paren.vala', regex: '{', push: - [ { token: 'meta.class.body.vala', regex: '(?=})', next: 'pop' }, + [ { token: 'paren.vala', regex: '(?=})', next: 'pop' }, { include: '#class-body' }, { defaultToken: 'meta.class.body.vala' } ] }, { defaultToken: 'meta.class.vala' } ], @@ -266,7 +266,7 @@ var ValaHighlightRules = function() { [ { token: 'meta.method.vala', regex: '(?!new)(?=\\w.*\\s+)(?=[^=]+\\()', push: - [ { token: 'meta.method.vala', regex: '}|(?=;)', next: 'pop' }, + [ { token: 'paren.vala', regex: '}|(?=;)', next: 'pop' }, { include: '#storage-modifiers' }, { token: [ 'entity.name.function.vala', 'meta.method.identifier.vala' ], regex: '([\\~\\w\\.]+)(\\s*\\()', @@ -285,10 +285,10 @@ var ValaHighlightRules = function() { { include: '#all-types' }, { defaultToken: 'meta.method.return-type.vala' } ] }, { include: '#throws' }, - { token: 'meta.method.body.vala', + { token: 'paren.vala', regex: '{', push: - [ { token: 'meta.method.body.vala', regex: '(?=})', next: 'pop' }, + [ { token: 'paren.vala', regex: '(?=})', next: 'pop' }, { include: '#code' }, { defaultToken: 'meta.method.body.vala' } ] }, { defaultToken: 'meta.method.vala' } ] } ], From dfee7408020c6c86b41983d78b10b856720202df Mon Sep 17 00:00:00 2001 From: nightwing Date: Fri, 1 Aug 2014 22:33:32 +0400 Subject: [PATCH 032/476] update command_manager test --- lib/ace/commands/command_manager_test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ace/commands/command_manager_test.js b/lib/ace/commands/command_manager_test.js index 76d973bb..902600be 100644 --- a/lib/ace/commands/command_manager_test.js +++ b/lib/ace/commands/command_manager_test.js @@ -188,7 +188,7 @@ module.exports = { assert.equal(command, "cm2"); var command = this.cm.findKeyCommand(0, "return"); - assert.equal(command, "cm3"); + assert.equal(command + "", ["cm4", "cm3"] + ""); } }; From b990ad3f90f14f99cfbe8bcb2a807027cfee7228 Mon Sep 17 00:00:00 2001 From: nightwing Date: Sat, 2 Aug 2014 00:56:34 +0400 Subject: [PATCH 033/476] update csslint --- lib/ace/mode/css/csslint.js | 968 ++++++++++++++++++++++++------------ lib/ace/mode/css_worker.js | 7 +- 2 files changed, 647 insertions(+), 328 deletions(-) diff --git a/lib/ace/mode/css/csslint.js b/lib/ace/mode/css/csslint.js index a3a7a9bd..c3c79a80 100644 --- a/lib/ace/mode/css/csslint.js +++ b/lib/ace/mode/css/csslint.js @@ -4,7 +4,7 @@ CSSLint Copyright (c) 2014 Nicole Sullivan and Nicholas C. Zakas. All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal +of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is @@ -13,7 +13,7 @@ furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER @@ -22,7 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/* Build: v0.10.0 31-March-2014 08:16:48 */ +/* Build: v0.10.0 22-July-2014 01:17:52 */ /*! Parser-Lib Copyright (c) 2009-2011 Nicholas C. Zakas. All rights reserved. @@ -46,11 +46,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/* Version v0.2.4, Build time: 7-January-2014 07:32:49 */ +/* Version v0.2.5, Build time: 7-May-2014 03:37:38 */ var parserlib = {}; (function(){ - /** * A generic base to inherit from for any object * that needs event handling. @@ -506,7 +505,7 @@ SyntaxUnit.prototype = { * @method valueOf */ valueOf: function(){ - return this.toString(); + return this.text; }, /** @@ -921,8 +920,6 @@ TokenStreamBase.prototype = { }; - - parserlib.util = { StringReader: StringReader, SyntaxError : SyntaxError, @@ -931,8 +928,6 @@ EventTarget : EventTarget, TokenStreamBase : TokenStreamBase }; })(); - - /* Parser-Lib Copyright (c) 2009-2011 Nicholas C. Zakas. All rights reserved. @@ -956,7 +951,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/* Version v0.2.4, Build time: 7-January-2014 07:32:49 */ +/* Version v0.2.5, Build time: 7-May-2014 03:37:38 */ (function(){ var EventTarget = parserlib.util.EventTarget, TokenStreamBase = parserlib.util.TokenStreamBase, @@ -964,7 +959,6 @@ StringReader = parserlib.util.StringReader, SyntaxError = parserlib.util.SyntaxError, SyntaxUnit = parserlib.util.SyntaxUnit; - var Colors = { aliceblue :"#f0f8ff", antiquewhite :"#faebd7", @@ -1182,7 +1176,6 @@ function Combinator(text, line, col){ Combinator.prototype = new SyntaxUnit(); Combinator.prototype.constructor = Combinator; - /*global SyntaxUnit, Parser*/ /** * Represents a media feature, such as max-width:500. @@ -1215,7 +1208,6 @@ function MediaFeature(name, value){ MediaFeature.prototype = new SyntaxUnit(); MediaFeature.prototype.constructor = MediaFeature; - /*global SyntaxUnit, Parser*/ /** * Represents an individual media query. @@ -1259,7 +1251,6 @@ function MediaQuery(modifier, mediaType, features, line, col){ MediaQuery.prototype = new SyntaxUnit(); MediaQuery.prototype.constructor = MediaQuery; - /*global Tokens, TokenStream, SyntaxError, Properties, Validation, ValidationError, SyntaxUnit, PropertyValue, PropertyValuePart, SelectorPart, SelectorSubPart, Selector, PropertyName, Combinator, MediaFeature, MediaQuery, EventTarget */ @@ -1521,7 +1512,7 @@ Parser.prototype = function(){ tokenStream.mustMatch([Tokens.STRING, Tokens.URI]); //grab the URI value - uri = tokenStream.token().value.replace(/(?:url\()?["']([^"']+)["']\)?/, "$1"); + uri = tokenStream.token().value.replace(/^(?:url\()?["']?([^"']+?)["']?\)?$/, "$1"); this._readWhitespace(); @@ -1626,8 +1617,10 @@ Parser.prototype = function(){ while(true) { if (tokenStream.peek() == Tokens.PAGE_SYM){ this._page(); - } else if (tokenStream.peek() == Tokens.FONT_FACE_SYM){ + } else if (tokenStream.peek() == Tokens.FONT_FACE_SYM){ this._font_face(); + } else if (tokenStream.peek() == Tokens.VIEWPORT_SYM){ + this._viewport(); } else if (!this._ruleset()){ break; } @@ -2823,7 +2816,7 @@ Parser.prototype = function(){ value = null, operator = null; - value = this._term(); + value = this._term(inFunction); if (value !== null){ values.push(value); @@ -2840,7 +2833,7 @@ Parser.prototype = function(){ valueParts = []; }*/ - value = this._term(); + value = this._term(inFunction); if (value === null){ break; @@ -2858,7 +2851,7 @@ Parser.prototype = function(){ return values.length > 0 ? new PropertyValue(values, values[0].line, values[0].col) : null; }, - _term: function(){ + _term: function(inFunction){ /* * term @@ -2872,6 +2865,7 @@ Parser.prototype = function(){ var tokenStream = this._tokenStream, unary = null, value = null, + endChar = null, token, line, col; @@ -2892,6 +2886,20 @@ Parser.prototype = function(){ col = tokenStream.token().startCol; } + //see if it's a simple block + } else if (inFunction && tokenStream.match([Tokens.LPAREN, Tokens.LBRACE, Tokens.LBRACKET])){ + + token = tokenStream.token(); + endChar = token.endChar; + value = token.value + this._expr(inFunction).text; + if (unary === null){ + line = tokenStream.token().startLine; + col = tokenStream.token().startCol; + } + tokenStream.mustMatch(Tokens.type(endChar)); + value += endChar; + this._readWhitespace(); + //see if there's a simple match } else if (tokenStream.match([Tokens.NUMBER, Tokens.PERCENTAGE, Tokens.LENGTH, Tokens.ANGLE, Tokens.TIME, @@ -3536,7 +3544,6 @@ nth ['-'|'+']? INTEGER | {O}{D}{D} | {E}{V}{E}{N} ] S* ; */ - /*global Validation, ValidationTypes, ValidationError*/ var Properties = { @@ -3553,6 +3560,7 @@ var Properties = { "animation-delay" : { multi: "