Skip to content
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 2 commits into from
Oct 19, 2016
Merged

An approach that considers the suggested edit location #2

merged 2 commits into from
Oct 19, 2016

Conversation

dmonad
Copy link
Contributor

@dmonad dmonad commented Oct 18, 2016

This is a naive approach to "shift" the edit location to the suggested edit location (cursor position). It is supposed to solve all cursor location problems for simple insert / delete modifications. This is related to slab/quill#746

Assuming the cursor location is at position 0, we check the following cases:
Case 1) Switch two diff tuples if it holds the same result. E.g. [0, "a"], [1, "a"] -> [1, "a"], [0, "a"]

[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) Try to shift the edit location to the beginning

[0, 'pre'], [ 1, 'prefix'] -> [ 1, 'pre'], [0, 'pre'], [ 1, 'fix']
[0, 'pre'], [-1, 'prefix'] -> [-1, 'pre'], [0, 'pre'], [-1, 'fix']

Does this implementation suffice?

Copy link
Owner

@jhchen jhchen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the approach a lot. It keeps things simple and separate.

* E.g.
* cursor_normalize_diff([[DIFF_EQUAL, 'abc']], cursorPos)
* => [1, [[DIFF_EQUAL, 'a'], [DIFF_EQUAL, 'bc']]]
* cursor_normalize_diff([[DIFF_INSERT, 'new'], [DIFF_DELETE, 'xyz']], 1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the cursorPos should be 2 to produce the below result?

/*
* Modify a diff such that the cursor position points to the start of a change:
* E.g.
* cursor_normalize_diff([[DIFF_EQUAL, 'abc']], cursorPos)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cursorPos should be 1 here?

@@ -73,6 +74,9 @@ function diff_main(text1, text2) {
diffs.push([DIFF_EQUAL, commonsuffix]);
}
diff_cleanupMerge(diffs);
if (cursor_pos != null) {
Copy link
Owner

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 that diff_cleanupMerge does not? Is is possible to just use diff_cleanupMerge after fix_cursor and not have to call/implement merge_tuples at all?

Copy link
Contributor Author

@dmonad dmonad Oct 19, 2016

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:

diff_cleanupMerge(fix_cursor([[0, 'aa'], [1, 'a']], 1)) 
= diff_cleanupMerge([[0, 'a'], [1, 'a'], [0, 'a']]) 
= [[0, 'aa'], [1, 'a']]

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.

Copy link
Owner

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.

@jhchen jhchen merged commit 3f63633 into jhchen:master Oct 19, 2016
pedrosanta added a commit to reedsy/quill-cursors that referenced this pull request Apr 26, 2017
This was in place because the issue of misplaced deltas regarding the
edit position when adding characters to a character sequence, because of
the diff strategy of the fast-diff lib detailed on this issue/PR,
slab/quill#746.

Since this has been fixed on fast-diff at
jhchen/fast-diff#2,
the loopback fix/workaround is no longer necessary.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants