M3.5: go to definition, find references and rename through Phpactor

Engine (`cmd/lsp_nav.rs`, on the pending outcomes of 20d1d28):
- `lsp.definition` (F12) and `lsp.references` (Shift+F12, sorted, with
  line previews) return project-relative locations; locations outside
  the project, such as PHAR stubs and symlink escapes, are dropped.
- `lsp.prepareRename` uses the word at the cursor when the server has
  no prepareProvider.
- `lsp.rename` (F2) applies the workspace edit (`lsp_nav/edit.rs`):
  1. Check everything first: inside the project, ranges fit (UTF-16,
     CRLF), open buffers unchanged since the request, no file
     operations, no incomplete edit.
  2. Write closed files atomically through the VFS, restoring earlier
     writes when a later one fails.
  3. Change open buffers through `buffer.edit` (dirty, undoable).
  4. Send each written file to Phpactor as didOpen/didSave/didClose
     plus didChangeWatchedFiles; without this its index kept the old
     names. Then the summary notification `notification.lsp.renamed`.

UI (`components/editor/nav/`):
- Mod+click goes to the definition, with a pointer cursor while Mod is
  held.
- A location picker for several definitions (exclusive modal).
- A References tab in the bottom panel, grouped by file.
- An inline rename field with a use/file count (exclusive modal).
- `BufferSync.beginUndoableRemote` puts the engine's rename edit into
  the editor history as one undo step per buffer.
- Catalog strings and tokens only.

Fake LSP server: `--script FILE` with scripted capabilities and
answers (additive next to M3.4's arms).

Tests:
- `crates/engine/tests/lsp_nav.rs` (fake): definition; bad positions;
  references; prepareRename; a multi-file rename incl. a closed CRLF
  file; refusals for outside the project, a bad range, a stale buffer,
  bad names and no server.
- The ignored real-Phpactor test covers definition, references and a
  rename and back across 3 files.
- Vitest `ui/tests/nav/`.

Live (Phpactor 2026.06.25.0, Windows, port 7935, tick on):
- definition 93 ms cold / 7-18 ms warm
- references ~140 ms
- prepareRename ~11 ms
- rename 28-59 ms
- index follows written files in 140 ms
- Phpactor 57 MB working set, ~1.6% of a core while ticking idle

Gate: 13 steps, 197 s, Vitest 484 + 1 skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-22 18:50:15 +02:00
parent b3fd7c6478
commit 578160a2ba
28 changed files with 3730 additions and 34 deletions

View file

@ -24,6 +24,10 @@
//! (then `RequestCancelled` and the notification `fake/cancelled`),
//! `fake/crash` exits with code 7, `fake/garbage` writes malformed frames
//! before answering. `shutdown` answers `null`; `exit` exits with 0.
//! - `--script FILE` reads a JSON object `{capabilities?, responses?}`:
//! `capabilities` are merged into the `initialize` answer, and
//! `responses` maps a request method to the result it gets, before the
//! built-in answers (M3.5: references, prepareRename, rename).
use std::collections::HashMap;
use std::io::{self, BufReader, Write};
@ -48,6 +52,8 @@ struct Fake {
full_sync: bool,
/// Whether `initialize` fails.
init_error: bool,
/// The `--script` file's content, `null` without one.
script: Value,
}
/// The fake answer to `textDocument/completion`: a `CompletionList` whose
@ -103,6 +109,7 @@ fn main() {
responses: Vec::new(),
full_sync: args.iter().any(|a| a == "--sync-full"),
init_error: args.iter().any(|a| a == "--init-error"),
script: script(&args),
};
let mut reader = BufReader::new(io::stdin().lock());
while let Ok(Some(frame)) = jsonrpc::read_frame(&mut reader, MAX_MESSAGE_BYTES) {
@ -115,6 +122,21 @@ fn main() {
}
}
/// The JSON in the file after `--script`, `null` without one; exits with 4
/// when it cannot be read.
fn script(args: &[String]) -> Value {
let Some(at) = args.iter().position(|a| a == "--script") else {
return Value::Null;
};
let Some(path) = args.get(at + 1) else {
exit(4);
};
match std::fs::read_to_string(path).map(|text| serde_json::from_str(&text)) {
Ok(Ok(value)) => value,
_ => exit(4),
}
}
/// Writes one message to stdout; exits if stdout is gone.
fn send(value: &Value) {
let mut stdout = io::stdout().lock();
@ -180,23 +202,34 @@ impl Fake {
/// Answers a request.
fn request(&mut self, id: Value, method: &str, params: &Value) {
if let Some(result) = self
.script
.pointer(&format!("/responses/{}", method.replace('/', "~1")))
{
respond(id, Ok(result.clone()));
return;
}
match method {
"initialize" if self.init_error => respond(id, Err(error(-32002, "no init"))),
"initialize" => {
let change = if self.full_sync { 1 } else { 2 };
let mut capabilities = json!({
"positionEncoding": "utf-16",
"textDocumentSync": {"openClose": true, "change": change, "save": {"includeText": true}},
"hoverProvider": true,
"definitionProvider": true,
"completionProvider": {"triggerCharacters": [">", ":", "$", "\\"]},
"signatureHelpProvider": {"triggerCharacters": ["(", ","]}
});
if let (Some(all), Some(extra)) = (
capabilities.as_object_mut(),
self.script.get("capabilities").and_then(Value::as_object),
) {
all.extend(extra.clone());
}
respond(
id,
Ok(json!({
"capabilities": {
"positionEncoding": "utf-16",
"textDocumentSync": {"openClose": true, "change": change, "save": {"includeText": true}},
"hoverProvider": true,
"definitionProvider": true,
"completionProvider": {"triggerCharacters": [">", ":", "$", "\\"]},
"signatureHelpProvider": {"triggerCharacters": ["(", ","]}
},
"serverInfo": {"name": "fake"}
})),
Ok(json!({"capabilities": capabilities, "serverInfo": {"name": "fake"}})),
);
}
"shutdown" => respond(id, Ok(Value::Null)),