atom-patch
Advanced tools
Comparing version 0.0.9 to 0.1.0
@@ -51,4 +51,4 @@ 'use strict'; | ||
}, { | ||
key: 'rewind', | ||
value: function rewind() { | ||
key: 'moveToBeginning', | ||
value: function moveToBeginning() { | ||
this.reset(); | ||
@@ -61,20 +61,30 @@ | ||
}, { | ||
key: 'moveToEnd', | ||
value: function moveToEnd() { | ||
this.reset(); | ||
while (this.currentNode && this.currentNode.right) { | ||
this.descendRight(); | ||
} | ||
} | ||
}, { | ||
key: 'getChanges', | ||
value: function getChanges() { | ||
this.rewind(); | ||
this.moveToBeginning(); | ||
var changes = []; | ||
while (this.moveToSuccessor()) { | ||
var inChange = this.inChange(); | ||
if (inChange) { | ||
var change = { | ||
start: this.outputStart, | ||
oldExtent: (0, _pointHelpers.traversalDistance)(this.inputEnd, this.inputStart), | ||
newExtent: (0, _pointHelpers.traversalDistance)(this.outputEnd, this.outputStart) | ||
}; | ||
if (this.currentNode.newText != null) { | ||
change.newText = this.currentNode.newText; | ||
} | ||
changes.push(change); | ||
if (!this.inChange()) continue; | ||
var change = { | ||
oldStart: this.inputStart, | ||
newStart: this.outputStart, | ||
oldExtent: (0, _pointHelpers.traversalDistance)(this.inputEnd, this.inputStart), | ||
newExtent: (0, _pointHelpers.traversalDistance)(this.outputEnd, this.outputStart) | ||
}; | ||
if (this.currentNode.newText != null) { | ||
change.newText = this.currentNode.newText; | ||
} | ||
changes.push(change); | ||
} | ||
@@ -233,2 +243,22 @@ | ||
}, { | ||
key: 'moveToPredecessor', | ||
value: function moveToPredecessor() { | ||
if (!this.currentNode) return false; | ||
if (this.currentNode.left) { | ||
this.descendLeft(); | ||
while (this.currentNode.right) { | ||
this.descendRight(); | ||
} | ||
return true; | ||
} else { | ||
while (this.currentNode.parent && this.currentNode.parent.left === this.currentNode) { | ||
this.ascend(); | ||
} | ||
this.ascend(); | ||
return this.currentNode != null; | ||
} | ||
} | ||
}, { | ||
key: 'ascend', | ||
@@ -235,0 +265,0 @@ value: function ascend() { |
@@ -22,2 +22,37 @@ 'use strict'; | ||
var Patch = (function () { | ||
_createClass(Patch, null, [{ | ||
key: 'compose', | ||
value: function compose(patches) { | ||
var composedPatch = new Patch(); | ||
for (var index = 0; index < patches.length; index++) { | ||
var changes = patches[index].getChanges(); | ||
if ((index & 1) === 0) { | ||
// flip | ||
for (var i = 0; i < changes.length; i++) { | ||
var _changes$i = changes[i]; | ||
var newStart = _changes$i.newStart; | ||
var oldExtent = _changes$i.oldExtent; | ||
var newExtent = _changes$i.newExtent; | ||
var newText = _changes$i.newText; | ||
composedPatch.splice(newStart, oldExtent, newExtent, { text: newText }); | ||
} | ||
} else { | ||
// flop | ||
for (var i = changes.length - 1; i >= 0; i--) { | ||
var _changes$i2 = changes[i]; | ||
var oldStart = _changes$i2.oldStart; | ||
var oldExtent = _changes$i2.oldExtent; | ||
var newExtent = _changes$i2.newExtent; | ||
var newText = _changes$i2.newText; | ||
composedPatch.splice(oldStart, oldExtent, newExtent, { text: newText }); | ||
} | ||
} | ||
} | ||
return composedPatch.getChanges(); | ||
} | ||
}]); | ||
function Patch() { | ||
@@ -31,2 +66,3 @@ var params = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
this.iterator = this.buildIterator(); | ||
this.cachedChanges = null; | ||
} | ||
@@ -85,18 +121,18 @@ | ||
key: 'spliceWithText', | ||
value: function spliceWithText(start, oldExtent, newText, options) { | ||
this.splice(start, oldExtent, (0, _textHelpers.getExtent)(newText), { text: newText }); | ||
value: function spliceWithText(newStart, oldExtent, newText, options) { | ||
this.splice(newStart, oldExtent, (0, _textHelpers.getExtent)(newText), { text: newText }); | ||
} | ||
}, { | ||
key: 'splice', | ||
value: function splice(outputStart, oldExtent, newExtent, options) { | ||
value: function splice(newStart, oldExtent, newExtent, options) { | ||
if ((0, _pointHelpers.isZero)(oldExtent) && (0, _pointHelpers.isZero)(newExtent)) return; | ||
var outputOldEnd = (0, _pointHelpers.traverse)(outputStart, oldExtent); | ||
var outputNewEnd = (0, _pointHelpers.traverse)(outputStart, newExtent); | ||
var oldEnd = (0, _pointHelpers.traverse)(newStart, oldExtent); | ||
var newEnd = (0, _pointHelpers.traverse)(newStart, newExtent); | ||
var startNode = this.iterator.insertSpliceBoundary(outputStart); | ||
var startNode = this.iterator.insertSpliceBoundary(newStart); | ||
startNode.isChangeStart = true; | ||
this.splayNode(startNode); | ||
var endNode = this.iterator.insertSpliceBoundary(outputOldEnd, startNode); | ||
var endNode = this.iterator.insertSpliceBoundary(oldEnd, startNode); | ||
endNode.isChangeEnd = true; | ||
@@ -110,4 +146,4 @@ this.splayNode(endNode); | ||
endNode.outputExtent = (0, _pointHelpers.traverse)(outputNewEnd, (0, _pointHelpers.traversalDistance)(endNode.outputExtent, endNode.outputLeftExtent)); | ||
endNode.outputLeftExtent = outputNewEnd; | ||
endNode.outputExtent = (0, _pointHelpers.traverse)(newEnd, (0, _pointHelpers.traversalDistance)(endNode.outputExtent, endNode.outputLeftExtent)); | ||
endNode.outputLeftExtent = newEnd; | ||
endNode.newText = options && options.text; | ||
@@ -133,2 +169,4 @@ | ||
} | ||
this.cachedChanges = null; | ||
} | ||
@@ -138,3 +176,7 @@ }, { | ||
value: function getChanges() { | ||
return this.iterator.getChanges(); | ||
if (this.cachedChanges == null) { | ||
this.cachedChanges = this.iterator.getChanges(); | ||
} | ||
return this.cachedChanges; | ||
} | ||
@@ -141,0 +183,0 @@ }, { |
@@ -8,2 +8,3 @@ "use strict"; | ||
exports.isZero = isZero; | ||
exports.isInfinity = isInfinity; | ||
exports.min = min; | ||
@@ -31,2 +32,6 @@ exports.traverse = traverse; | ||
function isInfinity(point) { | ||
return point.row === INFINITY_POINT || point.column === Infinity; | ||
} | ||
function min(a, b) { | ||
@@ -33,0 +38,0 @@ if (compare(a, b) <= 0) { |
{ | ||
"name": "atom-patch", | ||
"version": "0.0.9", | ||
"version": "0.1.0", | ||
"description": "A data structure to efficiently represent the results of applying patches.", | ||
@@ -5,0 +5,0 @@ "main": "dist/patch.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29238
680