Skip to content

Commit

Permalink
#1744: Refactor document delta handling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aldendaniels committed Jan 1, 2014
1 parent a9d1948 commit 2e6f127
Show file tree
Hide file tree
Showing 28 changed files with 434 additions and 447 deletions.
104 changes: 46 additions & 58 deletions lib/ace/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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
});
}
} else if (delta.action == "removeLines") {
if (start.row <= row) {
if (end.row <= row)
row -= end.row - start.row;
else {
row = start.row;
column = 0;
}

// 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
});
}

this.setPosition(row, column, true);

var delta = e.data;
var point = getTransformedPoint(delta, {row: this.row, column: this.column}, this.$insertRight);
this.setPosition(point.row, point.column, true);
};

/**
Expand Down
16 changes: 8 additions & 8 deletions lib/ace/anchor_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,31 @@ 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);
},

"test insert lines before cursor should move anchor row": function() {
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);
},

"test insert new line before cursor should move anchor column": function() {
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);
},

"test insert new line in anchor line before anchor should move anchor column and row": function() {
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);
},

Expand Down Expand Up @@ -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);
},

Expand All @@ -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);
},

Expand Down Expand Up @@ -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");
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ace/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(); },
Expand Down
2 changes: 1 addition & 1 deletion lib/ace/background_tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/ace/background_tokenizer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/ace/commands/default_commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}, {
Expand Down
Loading

0 comments on commit 2e6f127

Please sign in to comment.