-
Notifications
You must be signed in to change notification settings - Fork 21
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
Changes from 2 commits
a4b662d
ba21026
7e0627e
ca32120
b61975e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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": | ||
|
||
| 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] | ||
|
||
| 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: | ||
| """ | ||
|
|
||
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.