@eagleoutice/flowr-dev
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
61 lines • 1.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.computeEditRegion = computeEditRegion;
/**
* Computes a single minimal change region ({@link Parser.Edit}) that contains all modifications.
* @param oldContent - The original content.
* @param newContent - The changed content.
*/
function computeEditRegion(oldContent, newContent) {
const oldLen = oldContent.length;
const newLen = newContent.length;
// 1) Longest common prefix
let startIndex = 0;
while (startIndex < oldLen &&
startIndex < newLen &&
oldContent[startIndex] === newContent[startIndex]) {
startIndex++;
}
// 2) Longest common suffix, without overlapping the prefix
let oldSuffixIndex = oldLen;
let newSuffixIndex = newLen;
while (oldSuffixIndex > startIndex &&
newSuffixIndex > startIndex &&
oldContent[oldSuffixIndex - 1] === newContent[newSuffixIndex - 1]) {
oldSuffixIndex--;
newSuffixIndex--;
}
const oldEndIndex = oldSuffixIndex;
const newEndIndex = newSuffixIndex;
const [startPosition, oldEndPosition] = indexesToPoints(oldContent, [startIndex, oldEndIndex]);
const [newEndPosition] = indexesToPoints(newContent, [newEndIndex]);
return {
startIndex,
oldEndIndex,
newEndIndex,
startPosition,
oldEndPosition,
newEndPosition,
};
}
function indexesToPoints(text, orderedIndexes) {
let row = 0;
let column = 0;
let currentIndex = 0;
const points = [];
for (const targetIndex of orderedIndexes) {
while (currentIndex < targetIndex) {
if (text[currentIndex] === '\n') {
row++;
column = 0;
}
else {
column++;
}
currentIndex++;
}
points.push({ row, column });
}
return points;
}
//# sourceMappingURL=edit-computation.js.map