Skip to content

feat(vcs): support Find Usages on both sides of a diff - #6771

Draft
nako-ruru wants to merge 7 commits into
l0ng-ai:mainfrom
nako-ruru:fix/revision-pane-find-usages
Draft

nako-ruru wants to merge 7 commits into
l0ng-ai:mainfrom
nako-ruru:fix/revision-pane-find-usages

Conversation

@nako-ruru

Copy link
Copy Markdown
Contributor

Blocked by #6735 — merge that first; this PR's diff shrinks to its own change
once #6735 is in.

What and why

Find Usages now works on both sides of a Compare: the revision pane on
the left, and the working copy on the right. Before this, the left side sent
the revision's git-revision: URI straight to the language servers, which
reject it (java-analyzer "Not a file URI", gopls "DocumentURI scheme is not
file") — you got error toasts instead of results. Clicking a result opens the
working copy at the right line even when the line numbers differ between the
two sides.

Depends on #6735 for the underlying jump/navigation machinery (busy→swap→request→restore);
without it the left-side search has no way to hand the revision's text to the
servers.

How it was verified

Commands:

cargo check -p ide-host -p ide-lsp
# passed; only warning is the pre-existing unused `doc` in revision_targets
# (from #6735, untouched here)

pnpm typecheck
# passed (8 tsconfigs, no errors)
  • Windows CI build of ref a1657a50e (build-with-ref) — dispatched, result
    pending at draft time.
  • Unit test revision_hits_map_onto_the_working_copy added (an inserted line
    shifts the revision hit to the right working-copy line; other files
    untouched). Not run locally — local cargo builds are off-limits per project
    constraint; flagged here rather than claimed.
  • Manual check in a Compare (screenshot below): Find Usages on
    covering_element from the left pane → 53 results; clicking one opens the
    working copy at the mapped line. Repeated searches and pane switching leave
    the working copy intact.

Review focus

  • revision_references (ide-lsp): runs the search by temporarily putting the
    revision's text in place of the working document (same busy→swap→request→restore
    shape as revision_definition); the working copy is restored when the request ends.
  • map_revision_references (ide-host): maps same-file hits from the revision's
    line numbers to the working copy through the file's diff — including partial
    results arriving mid-search; cross-file hits keep their own coordinates.
  • Working copy not open for the path → empty result instead of an error.

bright and others added 6 commits November 28, 2026 19:13
Pass an explicit CaretContext built from the clicked diff pane so references use that pane's attached document instead of the active editor.
The revision (HEAD) side of a diff now supports hover links and Ctrl+click like an editor, with stock language servers: the IDE sets the document's text to the revision's, asks for a definition, and restores the working copy when it is needed again.

- A rev side attaches its text as a git-revision: document.
- lsp_goto routes Definition on such a document to the working document of the blob's path and LspManager::revision_definition.
- The manager keeps the revision text in place while the diff is in use and restores every document of the file on the next working-copy request or edit; server versions are owned per file.
- Targets inside the same blob are mapped from revision lines to working-copy lines.

Only standard LSP: no language server changes.

@arookieofc-ai arookieofc-ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Read the whole thing — the similar-based line mapping, the git-revision: URI helpers, the LspManager text swap and restore, the LspObserver skip, the DiffEditor context-menu wiring, and the tests.

Swapping the working document's text is the right primitive for this. The server already has the working copy open, so putting the revision's text on it for the duration of one request gets a real answer from a real server without a second document, a second server, or a git-revision: URI the server would reject. RevisionBusy's Drop is the careful part: a cancelled request (the editor abandons a jump) still clears the flag, so the document cannot be left permanently "busy" and permanently swapped. next_version's per-file counter, with the comment about an editor tab and a diff pane sharing one server document, is the right fix for the version-collision problem that would otherwise make the server drop the change as stale.

Also good: extracting RevisionTarget / revision_target / revision_blob_path so the URI is decoded in one place instead of twice; restore_revision at the top of the request path and of did_change, so the working text comes back before anything else reads it; the LspObserver skip with its comment explaining why a revision blob is never did_opened; findUsages(c: CaretContext | null = caretContext()) keeping every existing caller working; and the gitRevisionUri unit test decoding the payload back rather than asserting on the string.

Things I would fix before merge:

  1. revision_hits_map_onto_the_working_copy cannot pass. It asserts refs.locations[0].range.end.line == 4, but the code computes line = map_line("a\nb\nc\n", "a\nX\nb\nc\n", 1) = 2, then delta = 2 - 1 = 1, then end.line = 1 + 1 = 2. The assertion should be 2. As written the PR's test suite is red, which also means the mapping has not actually been exercised end to end.

  2. lsp_goto swallows the "no target" signal. return Ok(revision_targets(...).await?.unwrap_or_default()) — but revision_targets never returns Ok(None); every path returns Ok(Some(...)). Its own doc comment says a server without the revision method "ends with no target, leaving the ordinary path to answer", and the return means the ordinary path never runs. The Ok(Some(Vec::new())) case (no working copy open) is likewise turned into an empty result rather than a fallback. Either return None from revision_targets and fall through, or drop the sentence from the comment.

  3. map_revision_references runs a full Myers diff per location. map_line re-diffs the whole file on every call, and one calls it once per hit, so N hits in one file cost N full-file diffs. Build the line map once per file and look up each hit.

  4. revision_busy is keyed by DocId while swapped is keyed by PathBuf. restore_revision returns early only when this document is busy, so a request from the editor tab on the same file is not blocked by the diff pane's busy flag and can put the working text back mid-request. Keying the busy set by path would make the guard match what it is protecting.

  5. send_full_text writes to the server's stdin while the docs lock is held. Both swap_revision_text and restore_revision call it inside the docs guard, so a blocked or slow server pipe stalls every document operation in the manager. Collecting the notifications and sending them after the guard is dropped would keep the lock short.

  6. arm_revision_restore spawns an uncancellable 30 s timer per request. A diff pane left open re-arms on every jump, and a timer armed by an older request can fire after a newer swap and restore the wrong text. There is no handle to cancel it, and the 30 s is a bare literal.

  7. DiffEditor.tsx's revisionUri re-implements gitRevisionUri inline. The same PR extracts the helper into revisionInput.ts and then duplicates the hex encoding in DiffEditor.tsx. Worse, the two build different payloads for the same file: revisionUri uses side.path ?? path (absolute) while revisionUsageTarget uses relTo(repo, filePath) (repo-relative). Both happen to resolve through repo.join, but two URIs for one document is exactly the kind of drift the extraction was meant to prevent.

  8. findUsagesItem calls withDocument(target.path, …) with a git-revision: URI. LspObserver now explicitly refuses to did_open such a URI, so opening it as a document is at best a second, unanalysed document — and the request that follows would go through revision_references, which needs the working copy open, not this one.

  9. Only GotoKind::Definition is handled. Other goto kinds in a revision pane fall through to position_document with a git-revision: URI, which is the case the comment says the servers reject.

  10. working_text's std::fs::read + String::from_utf8_lossy fallback bypasses the project's encoding detection that revision_text used for the blob, so a non-UTF-8 file maps lines through mojibake and the jump lands on the wrong line.

  11. is_revision_document swallows errors with .ok()?. A real failure (no project, no identity) looks like "not a revision document" and silently takes the ordinary path, which is the path that cannot work for a revision pane.

  12. swapped and served are keyed by PathBuf and never pruned. Two diff panes on the same file at different revisions collide on one entry, and served grows without bound for the life of the session.

  13. revision_blob_path re-parses the URI (base64 + JSON) after revision_virtual_document already did, so every jump decodes the same payload twice.

  14. lsp_semantic_tokens returns Ok(None), so the diff pane loses colouring. The comment is honest about it; worth a follow-up rather than leaving it only there.

  15. did_change's cross-document text copy does not update the other document's version, so it holds new text with a stale version until its own next change arrives.

  16. navigation.ts's 10 s probe timeout is a bare literal keyed on doc?.path.startsWith("git-revision:"); a named constant next to the 3 s one would say why the revision case is different.

Nits, non-blocking:

  • map_line maps an empty new to line 0, which is a line that does not exist.
  • revisionUri's .at(-1)! yields "" for a path ending in /.
  • The lock order is now revision_busy → docs → swapped in three functions and is undocumented; the existing ledger-before-meta comment is the precedent for writing it down.
  • revision_targets's doc comment describes a fallback that the caller does not implement (see finding 2).

LGTM on the approach — swapping the working document's text for one request, with a busy flag that survives cancellation and a per-file version counter, is the right way to answer a jump inside a revision blob.

- Fix the mapping test expectation and build the line map once per file
- Make lsp_goto handle every goto kind consistently
- Key the revision busy guard by path and cancel stale restore timers
- Send full-text notifications outside the docs lock
- Reuse the working copy's encoding detection and decode the URI once
- Surface lookup errors instead of treating them as 'not a revision'
- Unify revision URI construction and drop the second-document fallback
- Name the revision navigation probe timeout

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants