-
Notifications
You must be signed in to change notification settings - Fork 22
Perform granular updates if reloading textual files #366
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
Open
krassowski
wants to merge
3
commits into
jupyter-server:main
Choose a base branch
from
krassowski:granular-file-reload
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+227
−26
Open
Changes from 2 commits
Commits
Show all changes
3 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 hidden or 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 |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| # Distributed under the terms of the Modified BSD License. | ||
|
|
||
| from collections.abc import Callable | ||
| from difflib import SequenceMatcher | ||
| from functools import partial | ||
| from typing import Any | ||
|
|
||
|
|
@@ -64,17 +65,40 @@ def set(self, value: str) -> None: | |
| :param value: The content of the document. | ||
| :type value: str | ||
| """ | ||
| if self.get() == value: | ||
| old_value = self.get() | ||
| if old_value == value: | ||
| # no-op if the values are already the same, | ||
| # to avoid side-effects such as cursor jumping to the top | ||
| return | ||
|
|
||
| with self._ydoc.transaction(): | ||
| # clear document | ||
| self._ysource.clear() | ||
| # initialize document | ||
| if value: | ||
| self._ysource += value | ||
| matcher = SequenceMatcher(a=old_value, b=value) | ||
|
|
||
| # for very different strings, just replace the whole content; | ||
| # this avoids generating a huge number of operations | ||
| if matcher.ratio() < 0.6: | ||
| # clear document | ||
| self._ysource.clear() | ||
| # initialize document | ||
| if value: | ||
| self._ysource += value | ||
| else: | ||
| operations = matcher.get_opcodes() | ||
| offset = 0 | ||
| for tag, i1, i2, j1, j2 in operations: | ||
| if tag == "replace": | ||
|
Collaborator
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. Since we require python 3.10+, maybe use |
||
| self._ysource[i1 + offset : i2 + offset] = value[j1:j2] | ||
| offset += (j2 - j1) - (i2 - i1) | ||
| elif tag == "delete": | ||
| del self._ysource[i1 + offset : i2 + offset] | ||
| offset -= i2 - i1 | ||
| elif tag == "insert": | ||
| self._ysource[i1 + offset : i2 + offset] = value[j1:j2] | ||
|
Collaborator
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. Why is this not using |
||
| offset += j2 - j1 | ||
| elif tag == "equal": | ||
| pass | ||
| else: | ||
| raise ValueError(f"Unknown tag '{tag}' in sequence matcher") | ||
|
|
||
| def observe(self, callback: Callable[[str, Any], None]) -> None: | ||
| """ | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Using 0.6 because:
As per https://docs.python.org/3/library/difflib.html#sequencematcher-examples
Instead of
ratio()we could usequick_ratio()orreal_quick_ratio(). In case if the ratio is above the threshold this does not matter because we would end up computingmatching_blocksanyways (this is computed and cached by a call to eitherraito()orget_opcodes(); in that case callingquick_ratioheuristic could actually end up taking more time as we end up doing more work. It all boils down to whether we think that we are more likely to see smaller updates or larger updates. One very cheap heuristic could be usinglenof both strings which is whatreal_quick_ratiodoes.