diff --git a/AGENTS.md b/AGENTS.md index 6c12c3a..44b436a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -57,7 +57,7 @@ diffui/ - Branch-scoped state: reviewed status, comments and session live in `~/.config/diffui/{repo}/{branch}/`, where `{repo}` and `{branch}` are `_safe_name()` of the repo path and branch. `get_repo_root()` and `_cached_current_branch()` are `lru_cache`'d. `_save_json` writes a temp file and renames it. `_load_json` moves an unparseable file aside to `.corrupt`, so the next save cannot overwrite it. - Worktrees: `get_git_dir()` resolves the real git dir for regular repos and linked worktrees. - Comments: `author_type` is `user` or `agent`. `category` is bug, suggestion, nit or question. `status` is open or resolved. The web UI locks a user's comment for editing once an agent replies (`canEdit` in `CommentDisplay.js`). The server does not enforce that. `agentColor()` hashes the author name, so each agent gets its own color. -- Word-level diff: `pair_diff_lines` pairs consecutive removes and adds, and `word_diff_ranges` uses `difflib.SequenceMatcher` for the changed word spans. +- Word-level diff: `pair_diff_lines` pairs consecutive removes and adds, and `word_diff_ranges` uses `difflib.SequenceMatcher` for the changed word spans. Each word keeps its real character offsets (`_word_spans`), so indentation and runs of spaces do not shift the marks. - Diff line `index`: `parse_diff_to_json` numbers every line of the diff text, and saved comments anchor on that number. `split_into_hunks` returns the lines before the first `@@` as a pseudo hunk. `parse_diff_to_json` emits neither that hunk nor the `@@` lines, but both still advance `index`. Skipping the count shifts every anchor. `file_note` (from `file_change_note`) is set when the header holds a rename, mode change, new or deleted file, or binary marker. - `html=0` on `/api/diff` and `/api/file` skips Pygments HTML and word highlights. The TUI sends it and highlights `text` itself. - Settings: `SettingsUpdate` in `models.py` limits `editor` to code, cursor, vim or nvim and `agent_cli` to claude, codex, opencode or cursor. A new agent CLI also needs an entry in `_AGENT_CMDS` (`routes_review.py`) and `_INTERACTIVE_CMDS` (`routes_agent_terminal.py`). diff --git a/diffui/diff.py b/diffui/diff.py index 984782f..4ac1cff 100644 --- a/diffui/diff.py +++ b/diffui/diff.py @@ -83,26 +83,28 @@ def strip_diff_prefix(line: str) -> str: return line[1:] if line[:1] in ("+", "-", " ") else line -def _word_offsets(words: list[str]) -> list[int]: - offsets = [0] * (len(words) + 1) - for i, w in enumerate(words): - offsets[i + 1] = offsets[i] + len(w) + 1 - return offsets +_WORD_RE = re.compile(r"\S+") + + +def _word_spans(line: str) -> list[tuple[int, int]]: + # Real character offsets of each word, so leading indentation and runs of + # whitespace between words do not shift the ranges. + return [m.span() for m in _WORD_RE.finditer(line)] def word_diff_ranges(old_line: str, new_line: str) -> tuple[list[tuple[int, int]], list[tuple[int, int]]]: - old_words = old_line.split() - new_words = new_line.split() - old_off = _word_offsets(old_words) - new_off = _word_offsets(new_words) + old_spans = _word_spans(old_line) + new_spans = _word_spans(new_line) + old_words = [old_line[s:e] for s, e in old_spans] + new_words = [new_line[s:e] for s, e in new_spans] matcher = difflib.SequenceMatcher(None, old_words, new_words) old_ranges: list[tuple[int, int]] = [] new_ranges: list[tuple[int, int]] = [] for tag, i1, i2, j1, j2 in matcher.get_opcodes(): if tag in ("replace", "delete"): - old_ranges.append((old_off[i1], max(old_off[i2] - 1, old_off[i1]))) + old_ranges.append((old_spans[i1][0], old_spans[i2 - 1][1])) if tag in ("replace", "insert"): - new_ranges.append((new_off[j1], max(new_off[j2] - 1, new_off[j1]))) + new_ranges.append((new_spans[j1][0], new_spans[j2 - 1][1])) return old_ranges, new_ranges diff --git a/pyproject.toml b/pyproject.toml index 99cf8a6..7dd2c42 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "diffui" -version = "0.11.2" +version = "0.11.3" description = "UI for reviewing AI agent diffs" requires-python = ">=3.10" dependencies = [ diff --git a/tests/test_diff.py b/tests/test_diff.py index a268e2e..43b02f8 100644 --- a/tests/test_diff.py +++ b/tests/test_diff.py @@ -161,6 +161,10 @@ def test_no_match(self): ("old", "new", ([(0, 3)], [(0, 3)])), ("a c", "a b c", ([], [(2, 3)])), ("a b c", "a c", ([(2, 3)], [])), + pytest.param(" x = old", " x = new", ([(8, 11)], [(8, 11)]), id="leading-indent"), + pytest.param("\tx = old", "\tx = new", ([(5, 8)], [(5, 8)]), id="leading-tab"), + pytest.param("a old b", "a new b", ([(3, 6)], [(3, 6)]), id="runs-of-spaces"), + pytest.param(" a old new b", " a b", ([(4, 11)], []), id="deleted-run-spans-inner-space"), ], ) def test_word_diff_ranges(old, new, ranges): diff --git a/tests/test_highlight.py b/tests/test_highlight.py index b992343..9b7e39f 100644 --- a/tests/test_highlight.py +++ b/tests/test_highlight.py @@ -103,6 +103,13 @@ def test_line_index_counts_skipped_header_and_hunk_lines(self): by_text = {line["text"]: line["index"] for h in result["hunks"] for line in h["lines"]} assert by_text == {"-old": 4, "+new": 5, "-a": 7, "+b": 8} + def test_word_highlight_lands_on_the_changed_word_of_an_indented_line(self): + diff = ( + "diff --git a/f.txt b/f.txt\n--- a/f.txt\n+++ b/f.txt\n@@ -1 +1 @@\n- keep old\n+ keep new\n" + ) + lines = parse_diff_to_json(diff, "f.txt", CATPPUCCIN_MOCHA)["hunks"][-1]["lines"] + assert [">old" in lines[0]["html"], ">new" in lines[1]["html"]] == [True, True] + class TestHighlightFileToJson: def test_empty_content(self):