-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
An approach that considers the suggested edit location #2
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,9 +39,10 @@ var DIFF_EQUAL = 0; | |
* any common prefix or suffix off the texts before diffing. | ||
* @param {string} text1 Old string to be diffed. | ||
* @param {string} text2 New string to be diffed. | ||
* @param {Int} cursor_pos Expected edit position in text1 (optional) | ||
* @return {Array} Array of diff tuples. | ||
*/ | ||
function diff_main(text1, text2) { | ||
function diff_main(text1, text2, cursor_pos) { | ||
// Check for equality (speedup). | ||
if (text1 == text2) { | ||
if (text1) { | ||
|
@@ -73,6 +74,9 @@ function diff_main(text1, text2) { | |
diffs.push([DIFF_EQUAL, commonsuffix]); | ||
} | ||
diff_cleanupMerge(diffs); | ||
if (cursor_pos != null) { | ||
diffs = fix_cursor(diffs, cursor_pos); | ||
} | ||
return diffs; | ||
}; | ||
|
||
|
@@ -566,5 +570,120 @@ diff.INSERT = DIFF_INSERT; | |
diff.DELETE = DIFF_DELETE; | ||
diff.EQUAL = DIFF_EQUAL; | ||
|
||
|
||
module.exports = diff; | ||
|
||
/* | ||
* Modify a diff such that the cursor position points to the start of a change: | ||
* E.g. | ||
* cursor_normalize_diff([[DIFF_EQUAL, 'abc']], cursorPos) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cursorPos should be 1 here? |
||
* => [1, [[DIFF_EQUAL, 'a'], [DIFF_EQUAL, 'bc']]] | ||
* cursor_normalize_diff([[DIFF_INSERT, 'new'], [DIFF_DELETE, 'xyz']], 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the cursorPos should be 2 to produce the below result? |
||
* => [2, [[DIFF_INSERT, 'new'], [DIFF_DELETE, 'xy'], [DIFF_DELETE, 'z']]] | ||
* | ||
* @param {Array} diffs Array of diff tuples | ||
* @param {Int} cursor_pos Suggested edit position. Must not be out of bounds! | ||
* @return {Array} A tuple [cursor location in the modified diff, modified diff] | ||
*/ | ||
function cursor_normalize_diff (diffs, cursor_pos) { | ||
if (cursor_pos === 0) { | ||
return [DIFF_EQUAL, diffs]; | ||
} | ||
for (var current_pos = 0, i = 0; i < diffs.length; i++) { | ||
var d = diffs[i]; | ||
if (d[0] === DIFF_DELETE || d[0] === DIFF_EQUAL) { | ||
var next_pos = current_pos + d[1].length; | ||
if (cursor_pos === next_pos) { | ||
return [i + 1, diffs]; | ||
} else if (cursor_pos < next_pos) { | ||
// copy to prevent side effects | ||
diffs = diffs.slice(); | ||
// split d into two diff changes | ||
var split_pos = cursor_pos - current_pos; | ||
var d_left = [d[0], d[1].slice(0, split_pos)]; | ||
var d_right = [d[0], d[1].slice(split_pos)]; | ||
diffs.splice(i, 1, d_left, d_right); | ||
return [i + 1, diffs]; | ||
} else { | ||
current_pos = next_pos; | ||
} | ||
} | ||
} | ||
throw new Error('cursor_pos is out of bounds!') | ||
} | ||
|
||
/* | ||
* Modify a diff such that the edit position is "shifted" to the proposed edit location (cursor_position). | ||
* | ||
* Case 1) | ||
* Check if a naive shift is possible: | ||
* [0, X], [ 1, Y] -> [ 1, Y], [0, X] (if X + Y === Y + X) | ||
* [0, X], [-1, Y] -> [-1, Y], [0, X] (if X + Y === Y + X) - holds same result | ||
* Case 2) | ||
* Check if the following shifts are possible: | ||
* [0, 'pre'], [ 1, 'prefix'] -> [ 1, 'pre'], [0, 'pre'], [ 1, 'fix'] | ||
* [0, 'pre'], [-1, 'prefix'] -> [-1, 'pre'], [0, 'pre'], [-1, 'fix'] | ||
* ^ ^ | ||
* d d_next | ||
* | ||
* @param {Array} diffs Array of diff tuples | ||
* @param {Int} cursor_pos Suggested edit position. Must not be out of bounds! | ||
* @return {Array} Array of diff tuples | ||
*/ | ||
function fix_cursor (diffs, cursor_pos) { | ||
var norm = cursor_normalize_diff(diffs, cursor_pos); | ||
var ndiffs = norm[1]; | ||
var cursor_pointer = norm[0]; | ||
var d = ndiffs[cursor_pointer]; | ||
var d_next = ndiffs[cursor_pointer + 1]; | ||
|
||
if (d[0] !== DIFF_EQUAL) { | ||
// A modification happened at the cursor location. | ||
// This is the expected outcome, so we can return the original diff. | ||
return diffs; | ||
} else { | ||
if (d_next != null && d[1] + d_next[1] === d_next[1] + d[1]) { | ||
// Case 1) | ||
// It is possible to perform a naive shift | ||
ndiffs.splice(cursor_pointer, 2, d_next, d) | ||
return merge_tuples(ndiffs, cursor_pointer, 2) | ||
} else if (d_next != null && d_next[1].indexOf(d[1]) === 0) { | ||
// Case 2) | ||
// d[1] is a prefix of d_next[1] | ||
// We can assume that d_next[0] !== 0, since d[0] === 0 | ||
// Shift edit locations.. | ||
ndiffs.splice(cursor_pointer, 2, [d_next[0], d[1]], [0, d[1]]); | ||
var suffix = d_next[1].slice(d[1].length); | ||
if (suffix.length > 0) { | ||
ndiffs.splice(cursor_pointer + 2, 0, [d_next[0], suffix]); | ||
} | ||
return merge_tuples(ndiffs, cursor_pointer, 3) | ||
} else { | ||
// Not possible to perform any modification | ||
return diffs; | ||
} | ||
} | ||
|
||
} | ||
|
||
/* | ||
* Try to merge tuples with their neigbors in a given range. | ||
* E.g. [0, 'a'], [0, 'b'] -> [0, 'ab'] | ||
* | ||
* @param {Array} diffs Array of diff tuples. | ||
* @param {Int} start Position of the first element to merge (diffs[start] is also merged with diffs[start - 1]). | ||
* @param {Int} length Number of consecutive elements to check. | ||
* @return {Array} Array of merged diff tuples. | ||
*/ | ||
function merge_tuples (diffs, start, length) { | ||
// Check from (start-1) to (start+length). | ||
for (var i = start + length - 1; i >= 0 && i >= start - 1; i--) { | ||
if (i + 1 < diffs.length) { | ||
var left_d = diffs[i]; | ||
var right_d = diffs[i+1]; | ||
if (left_d[0] === right_d[1]) { | ||
diffs.splice(i, 2, [left_d[0], left_d[1] + right_d[1]]); | ||
} | ||
} | ||
} | ||
return diffs; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure on this but what does
merge_tuples
do thatdiff_cleanupMerge
does not? Is is possible to just usediff_cleanupMerge
afterfix_cursor
and not have to call/implementmerge_tuples
at all?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
diff_cleanupMerge
also shifts edits (see line 541), reversing some of my modifications:I could extend
diff_cleanupMerge
to accept some flag to ignore the shifting part. But then there is also the problem that it checks the whole array, and performs other modifications I don't fully understand.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay that's fine--did not know
diff_cleanupMerge
shifted as well. Maybe it could be something to be improved in the future, but it's not a large performance penalty or anything practically significant at the moment.