-
Notifications
You must be signed in to change notification settings - Fork 795
fix(diff): align inline highlight offsets with tab-expanded text #1709
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -155,13 +155,13 @@ def _apply_inline_diff( | |||||||||||||||
| colors = get_diff_colors() | ||||||||||||||||
| paired = min(len(del_lines), len(add_lines)) | ||||||||||||||||
| for j in range(paired): | ||||||||||||||||
| old_code = del_lines[j].code | ||||||||||||||||
| new_code = add_lines[j].code | ||||||||||||||||
| sm = SequenceMatcher(None, old_code, new_code) | ||||||||||||||||
| old_text = _highlight(highlighter, del_lines[j].code) | ||||||||||||||||
| new_text = _highlight(highlighter, add_lines[j].code) | ||||||||||||||||
| # Compare the highlighted plain text so offsets match the Text objects | ||||||||||||||||
| # (highlighting may expand tabs, so raw .code offsets would be wrong). | ||||||||||||||||
| sm = SequenceMatcher(None, old_text.plain, new_text.plain) | ||||||||||||||||
| if sm.ratio() < _INLINE_DIFF_MIN_RATIO: | ||||||||||||||||
|
||||||||||||||||
| if sm.ratio() < _INLINE_DIFF_MIN_RATIO: | |
| if sm.ratio() < _INLINE_DIFF_MIN_RATIO: | |
| # Too dissimilar for inline word-level diff; still reuse the | |
| # already-highlighted Text objects to avoid re-highlighting | |
| # these lines in _highlight_hunk's second pass. | |
| del_lines[j].content = old_text | |
| add_lines[j].content = new_text |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -163,6 +163,38 @@ def test_unequal_block_sizes_partial_pairing(self) -> None: | |||||||||||||||||
| # 3rd delete not paired | ||||||||||||||||||
| assert not deletes[2].is_inline_paired | ||||||||||||||||||
|
|
||||||||||||||||||
| def test_inline_diff_with_tabs(self) -> None: | ||||||||||||||||||
| """Inline highlight offsets must account for tab-to-space expansion.""" | ||||||||||||||||||
| old = "\told_value = 1" | ||||||||||||||||||
| new = "\tnew_value = 2" | ||||||||||||||||||
| hunks = _build_diff_lines(old, new, 1, 1) | ||||||||||||||||||
| hl = _make_highlighter("test.py") | ||||||||||||||||||
| _highlight_hunk(hl, hunks[0]) | ||||||||||||||||||
| deletes = [dl for dl in hunks[0] if dl.kind == DiffLineKind.DELETE] | ||||||||||||||||||
| adds = [dl for dl in hunks[0] if dl.kind == DiffLineKind.ADD] | ||||||||||||||||||
| assert deletes[0].is_inline_paired | ||||||||||||||||||
| assert adds[0].is_inline_paired | ||||||||||||||||||
| del_plain = deletes[0].content.plain | ||||||||||||||||||
| add_plain = adds[0].content.plain | ||||||||||||||||||
| assert "old_value" in del_plain | ||||||||||||||||||
| assert "new_value" in add_plain | ||||||||||||||||||
| # Verify the highlight spans cover the actual changed words, | ||||||||||||||||||
| # not characters shifted by unexpanded-tab offsets. | ||||||||||||||||||
| from kimi_cli.utils.rich.diff_render import get_diff_colors | ||||||||||||||||||
|
|
||||||||||||||||||
| colors = get_diff_colors() | ||||||||||||||||||
| del_hl_spans = [ | ||||||||||||||||||
| (s.start, s.end) for s in deletes[0].content._spans if s.style == colors.del_hl | ||||||||||||||||||
| ] | ||||||||||||||||||
| add_hl_spans = [ | ||||||||||||||||||
| (s.start, s.end) for s in adds[0].content._spans if s.style == colors.add_hl | ||||||||||||||||||
|
Comment on lines
+187
to
+190
|
||||||||||||||||||
| (s.start, s.end) for s in deletes[0].content._spans if s.style == colors.del_hl | |
| ] | |
| add_hl_spans = [ | |
| (s.start, s.end) for s in adds[0].content._spans if s.style == colors.add_hl | |
| (s.start, s.end) for s in deletes[0].content.spans if s.style == colors.del_hl | |
| ] | |
| add_hl_spans = [ | |
| (s.start, s.end) for s in adds[0].content.spans if s.style == colors.add_hl |
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.
_apply_inline_diffnow highlights both lines before checking the similarity ratio, but whensm.ratio() < _INLINE_DIFF_MIN_RATIOitcontinues without storing thoseTextobjects;_highlight_hunkthen highlights the same lines again in its second pass (if dl.content is None). In replace blocks with many low-similarity pairs, this doubles syntax-highlighting work and can significantly slow rendering for large diffs.Useful? React with 👍 / 👎.