Lutra/crates/engine/tests/lsp_nav.rs
danny 578160a2ba 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>
2026-10-22 18:50:15 +02:00

575 lines
20 KiB
Rust

//! Navigation and rename (M3.5) against `lutra-lsp-fake` with scripted
//! answers (`--script`): definition, references, prepareRename and the
//! workspace edit of a rename (open buffers, closed files, CRLF ranges,
//! edits outside the project, stale buffers).
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing
)]
use std::ffi::OsString;
use std::fs;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use lutra_engine::lsp::Launch;
use lutra_engine::{Engine, EngineError, Outcome};
use lutra_lsp::ServerOptions;
use lutra_lsp::uri::path_to_uri;
use serde_json::{Value, json};
use tempfile::TempDir;
/// The fake server binary, built on demand (see `tests/lsp.rs`).
fn fake_server() -> PathBuf {
let exe = format!("lutra-lsp-fake{}", std::env::consts::EXE_SUFFIX);
let deps = std::env::current_exe().unwrap();
let path = deps.parent().unwrap().parent().unwrap().join(exe);
if !path.exists() {
let cargo = std::env::var_os("CARGO").unwrap_or_else(|| "cargo".into());
let status = std::process::Command::new(cargo)
.args(["build", "-p", "lutra-lsp", "--bin", "lutra-lsp-fake"])
.status()
.unwrap();
assert!(status.success(), "building lutra-lsp-fake failed");
}
path
}
/// A project with `files` and an engine running the fake server with the
/// script `script(root)` (a JSON object, see the fake server's docs).
struct Fixture {
/// The project.
dir: TempDir,
/// Holds the script file.
_scripts: TempDir,
/// The engine, with the server ready.
engine: Engine,
}
impl Fixture {
fn new(files: &[(&str, &str)], script: impl Fn(&Path) -> Value) -> Self {
let dir = tempfile::tempdir().unwrap();
for (name, text) in files {
let path = dir.path().join(name);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, text).unwrap();
}
let scripts = tempfile::tempdir().unwrap();
let script_path = scripts.path().join("script.json");
fs::write(&script_path, script(dir.path()).to_string()).unwrap();
let mut engine = Engine::new().unwrap();
engine.set_language_server(Some(Launch {
command: fake_server().into(),
args: vec![OsString::from("--script"), script_path.into_os_string()],
options: ServerOptions::default(),
tick: None,
}));
engine
.execute("project.open", json!({"path": dir.path()}))
.unwrap();
let mut fixture = Self {
dir,
_scripts: scripts,
engine,
};
fixture.pump_until(|e| e["kind"] == "lspStatus" && e["state"] == "ready");
fixture
}
/// Pumps until `done` holds for an event.
fn pump_until(&mut self, done: impl Fn(&Value) -> bool) -> Vec<Value> {
let deadline = Instant::now() + Duration::from_secs(20);
let mut seen = Vec::new();
while Instant::now() < deadline {
let mut hit = false;
for event in self.engine.pump_vfs().unwrap() {
let value = serde_json::to_value(&event).unwrap();
hit |= done(&value);
seen.push(value);
}
if hit {
return seen;
}
std::thread::sleep(Duration::from_millis(5));
}
panic!("condition not met within 20 s; events: {seen:#?}");
}
/// Opens a file; returns its buffer id.
fn open(&mut self, path: &str) -> u64 {
self.engine
.execute("file.open", json!({"path": path}))
.unwrap()
.value["id"]
.as_u64()
.unwrap()
}
/// Runs a command and waits for its pending answer, like a host.
fn run(&mut self, command: &str, params: Value) -> Result<(Value, Vec<Value>), EngineError> {
let mut out: Outcome = self.engine.execute(command, params)?;
let mut events: Vec<Value> = out
.events
.iter()
.map(|e| serde_json::to_value(e).unwrap())
.collect();
let Some(pending) = out.pending.take() else {
return Ok((out.value, events));
};
let resolved = self.engine.resolve(pending.wait())?;
events.extend(
resolved
.events
.iter()
.map(|e| serde_json::to_value(e).unwrap()),
);
Ok((resolved.value, events))
}
/// The text of buffer `id`.
fn text(&mut self, id: u64) -> Value {
self.engine
.execute("buffer.get", json!({"id": id}))
.unwrap()
.value
}
/// The absolute path of a project file.
fn path(&self, name: &str) -> PathBuf {
self.dir.path().join(name)
}
}
/// The `file:` URI of `name` under `root`.
fn uri(root: &Path, name: &str) -> String {
path_to_uri(&root.join(name)).unwrap()
}
/// An LSP range on one line.
fn range(line: u32, from: u32, to: u32) -> Value {
json!({"start": {"line": line, "character": from}, "end": {"line": line, "character": to}})
}
/// An LSP text edit on one line.
fn text_edit(line: u32, from: u32, to: u32, text: &str) -> Value {
json!({"range": range(line, from, to), "newText": text})
}
const A: &str = "<?php\nclass Greeter {\n public function hello() {}\n}\n";
const B: &str = "<?php\n$g = new Greeter();\n$g->hello();\n";
const C: &str = "<?php\r\n// call\r\n$x->hello(); $y->hello();\r\n";
#[test]
fn definition_returns_project_locations() {
let mut f = Fixture::new(&[("a.php", A)], |_| json!({}));
let id = f.open("a.php");
let (value, _) = f
.run(
"lsp.definition",
json!({"id": id, "position": {"line": 2, "column": 21}}),
)
.unwrap();
assert_eq!(
value,
json!({"locations": [{"path": "a.php", "range": {"start": {"line": 2, "column": 21}, "end": {"line": 2, "column": 21}}}]})
);
}
#[test]
fn bad_positions_and_buffers_fail_fast() {
let mut f = Fixture::new(&[("a.php", A)], |_| json!({}));
let id = f.open("a.php");
let err = f
.run(
"lsp.definition",
json!({"id": id, "position": {"line": 99, "column": 0}}),
)
.unwrap_err();
assert_eq!(err.code(), "invalid_params");
let err = f
.run(
"lsp.definition",
json!({"id": 999, "position": {"line": 0, "column": 0}}),
)
.unwrap_err();
assert_eq!(err.code(), "failed");
}
#[test]
fn references_are_sorted_deduplicated_and_inside_the_project() {
let outside = tempfile::tempdir().unwrap();
let outside_uri = path_to_uri(&outside.path().join("x.php")).unwrap();
let mut f = Fixture::new(&[("a.php", A), ("sub/b.php", B)], |root| {
json!({"capabilities": {"referencesProvider": true}, "responses": {
"textDocument/references": [
{"uri": uri(root, "sub/b.php"), "range": range(2, 4, 9)},
{"uri": uri(root, "a.php"), "range": range(2, 20, 25)},
{"uri": outside_uri, "range": range(0, 0, 1)},
{"uri": uri(root, "a.php"), "range": range(2, 20, 25)},
{"uri": "phar://stubs/Core.php", "range": range(0, 0, 1)}
]
}})
});
let id = f.open("a.php");
let (value, _) = f
.run(
"lsp.references",
json!({"id": id, "position": {"line": 2, "column": 22}}),
)
.unwrap();
let locations = value["locations"].as_array().unwrap();
assert_eq!(locations.len(), 2, "{value}");
assert_eq!(locations[0]["path"], "a.php");
assert_eq!(locations[0]["preview"], "public function hello() {}");
assert_eq!(locations[0]["previewColumn"], 16);
assert_eq!(locations[1]["path"], "sub/b.php");
assert_eq!(locations[1]["preview"], "$g->hello();");
}
#[test]
fn prepare_rename_uses_the_server_or_the_word() {
let mut f = Fixture::new(&[("a.php", A)], |_| {
json!({"capabilities": {"renameProvider": {"prepareProvider": true}}, "responses": {
"textDocument/prepareRename": {"range": range(2, 20, 25), "placeholder": "hello"}
}})
});
let id = f.open("a.php");
let (value, _) = f
.run(
"lsp.prepareRename",
json!({"id": id, "position": {"line": 2, "column": 22}}),
)
.unwrap();
assert_eq!(value["placeholder"], "hello");
assert_eq!(value["range"]["start"]["column"], 20);
let mut plain = Fixture::new(&[("a.php", A)], |_| json!({}));
let id = plain.open("a.php");
let (value, _) = plain
.run(
"lsp.prepareRename",
json!({"id": id, "position": {"line": 1, "column": 8}}),
)
.unwrap();
assert_eq!(value["placeholder"], "Greeter");
let (value, _) = plain
.run(
"lsp.prepareRename",
json!({"id": id, "position": {"line": 2, "column": 2}}),
)
.unwrap();
assert_eq!(value, Value::Null, "whitespace cannot be renamed");
}
/// The rename answer for `hello` → `greet` over a.php (open), sub/b.php and
/// c.php (closed, CRLF).
fn rename_script(root: &Path, extra: Option<Value>) -> Value {
let mut changes = json!({
uri(root, "a.php"): [text_edit(2, 20, 25, "greet")],
uri(root, "sub/b.php"): [text_edit(2, 4, 9, "greet")],
uri(root, "c.php"): [text_edit(2, 4, 9, "greet"), text_edit(2, 17, 22, "greet")]
});
if let Some(Value::Object(more)) = extra {
changes.as_object_mut().unwrap().extend(more);
}
json!({"capabilities": {"renameProvider": true}, "responses": {
"textDocument/rename": {"changes": changes}
}})
}
#[test]
fn rename_changes_open_buffers_and_closed_files() {
let mut f = Fixture::new(&[("a.php", A), ("sub/b.php", B), ("c.php", C)], |root| {
rename_script(root, None)
});
let id = f.open("a.php");
let (value, events) = f
.run(
"lsp.rename",
json!({"id": id, "position": {"line": 2, "column": 22}, "newName": "greet"}),
)
.unwrap();
assert_eq!(value["edits"], 4);
assert_eq!(value["written"], 2);
let files = value["files"].as_array().unwrap();
let paths: Vec<_> = files.iter().map(|f| f["path"].as_str().unwrap()).collect();
assert_eq!(paths, ["a.php", "c.php", "sub/b.php"]);
assert_eq!(files[0]["bufferId"], id);
assert_eq!(files[0]["version"], 2);
let buffer = f.text(id);
assert_eq!(
buffer["text"],
"<?php\nclass Greeter {\n public function greet() {}\n}\n"
);
assert_eq!(
buffer["dirty"], true,
"the open buffer is changed, not saved"
);
assert_eq!(fs::read_to_string(f.path("a.php")).unwrap(), A);
assert_eq!(
fs::read_to_string(f.path("sub/b.php")).unwrap(),
"<?php\n$g = new Greeter();\n$g->greet();\n"
);
assert_eq!(
fs::read_to_string(f.path("c.php")).unwrap(),
"<?php\r\n// call\r\n$x->greet(); $y->greet();\r\n",
"CRLF line breaks are kept and the ranges counted per line"
);
assert!(
events
.iter()
.any(|e| e["kind"] == "bufferChanged" && e["id"] == id),
"{events:?}"
);
let note = events
.iter()
.find(|e| e["kind"] == "notification")
.expect("a summary notification");
assert_eq!(note["messageKey"], "notification.lsp.renamed");
assert_eq!(note["params"]["files"], 3);
assert_eq!(note["params"]["written"], 2);
}
#[test]
fn rename_outside_the_project_changes_nothing() {
let outside = tempfile::tempdir().unwrap();
fs::write(outside.path().join("lib.php"), B).unwrap();
let lib = path_to_uri(&outside.path().join("lib.php")).unwrap();
let mut f = Fixture::new(&[("a.php", A), ("sub/b.php", B), ("c.php", C)], |root| {
rename_script(
root,
Some(json!({ lib.clone(): [text_edit(2, 4, 9, "greet")] })),
)
});
let id = f.open("a.php");
let err = f
.run(
"lsp.rename",
json!({"id": id, "position": {"line": 2, "column": 22}, "newName": "greet"}),
)
.unwrap_err();
assert!(err.to_string().contains("outside the project"), "{err}");
assert_eq!(f.text(id)["text"], A);
assert_eq!(f.text(id)["dirty"], false);
assert_eq!(fs::read_to_string(f.path("sub/b.php")).unwrap(), B);
assert_eq!(fs::read_to_string(f.path("c.php")).unwrap(), C);
assert_eq!(
fs::read_to_string(outside.path().join("lib.php")).unwrap(),
B
);
}
#[test]
fn rename_with_a_bad_range_changes_nothing() {
let mut f = Fixture::new(&[("a.php", A), ("sub/b.php", B), ("c.php", C)], |root| {
rename_script(
root,
Some(json!({ uri(root, "c.php"): [text_edit(2, 4, 99, "greet")] })),
)
});
let id = f.open("a.php");
let err = f
.run(
"lsp.rename",
json!({"id": id, "position": {"line": 2, "column": 22}, "newName": "greet"}),
)
.unwrap_err();
assert!(err.to_string().contains("c.php"), "{err}");
assert_eq!(f.text(id)["text"], A);
assert_eq!(fs::read_to_string(f.path("sub/b.php")).unwrap(), B);
}
#[test]
fn rename_refuses_a_buffer_edited_meanwhile() {
let mut f = Fixture::new(&[("a.php", A), ("sub/b.php", B), ("c.php", C)], |root| {
rename_script(root, None)
});
let id = f.open("a.php");
let mut out = f
.engine
.execute(
"lsp.rename",
json!({"id": id, "position": {"line": 2, "column": 22}, "newName": "greet"}),
)
.unwrap();
f.engine
.execute(
"buffer.edit",
json!({"id": id, "baseVersion": 1, "edits": [{"from": 0, "to": 0, "insert": "\n"}]}),
)
.unwrap();
let err = f
.engine
.resolve(out.pending.take().unwrap().wait())
.unwrap_err();
assert!(err.to_string().contains("changed while"), "{err}");
assert_eq!(fs::read_to_string(f.path("sub/b.php")).unwrap(), B);
}
#[test]
fn rename_rejects_bad_names() {
let mut f = Fixture::new(&[("a.php", A)], |root| rename_script(root, None));
let id = f.open("a.php");
for name in ["", "a\nb"] {
let err = f
.run(
"lsp.rename",
json!({"id": id, "position": {"line": 2, "column": 22}, "newName": name}),
)
.unwrap_err();
assert_eq!(err.code(), "invalid_params");
}
}
/// Against the real Phpactor (`LUTRA_PHPACTOR=<phar> [LUTRA_PHP=php] cargo
/// test -p lutra-engine --test lsp_nav real_phpactor -- --ignored
/// --nocapture`): definition across files, references, a method rename over
/// an open buffer and two closed files (one CRLF), and the rename back,
/// which finds every use only if Phpactor learned about the written files.
/// Prints the latencies.
#[test]
#[ignore = "needs a Phpactor PHAR in LUTRA_PHPACTOR"]
fn real_phpactor_navigates_and_renames_across_files() {
let phar = std::env::var("LUTRA_PHPACTOR").expect("LUTRA_PHPACTOR");
let php = std::env::var("LUTRA_PHP").unwrap_or_else(|_| "php".into());
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("src");
fs::create_dir_all(&src).unwrap();
fs::write(
dir.path().join("composer.json"),
r#"{"autoload": {"psr-4": {"App\\": "src/"}}}"#,
)
.unwrap();
fs::write(
src.join("Greeter.php"),
"<?php\n\nnamespace App;\n\nclass Greeter\n{\n public function hello(string $name): string\n {\n return \"Hello {$name}\";\n }\n}\n",
)
.unwrap();
fs::write(
src.join("App.php"),
"<?php\n\nnamespace App;\n\nclass App\n{\n public function run(): string\n {\n $greeter = new Greeter();\n return $greeter->hello('Lutra');\n }\n}\n",
)
.unwrap();
fs::write(
src.join("Other.php"),
"<?php\r\n\r\nnamespace App;\r\n\r\nfunction welcome(Greeter $g): string\r\n{\r\n return $g->hello(\"otter\") . $g->hello(\"sea otter\");\r\n}\r\n",
)
.unwrap();
let mut engine = Engine::new().unwrap();
engine.set_language_server(Some(lutra_engine::lsp::phpactor_launch(
&php,
&phar,
cfg!(windows),
)));
engine
.execute("project.open", json!({"path": dir.path()}))
.unwrap();
let deadline = Instant::now() + Duration::from_secs(60);
while engine.state().lsp.status().state != lutra_engine::lsp::LspState::Ready {
assert!(Instant::now() < deadline, "Phpactor never got ready");
engine.pump_vfs().unwrap();
std::thread::sleep(Duration::from_millis(10));
}
let id = engine
.execute("file.open", json!({"path": "src/App.php"}))
.unwrap()
.value["id"]
.as_u64()
.unwrap();
let mut run = |command: &str, params: Value| {
let started = Instant::now();
let mut out = engine.execute(command, params).unwrap();
let value = match out.pending.take() {
Some(pending) => engine.resolve(pending.wait()).unwrap().value,
None => out.value,
};
engine.pump_vfs().unwrap();
println!("{command}: {:?}", started.elapsed());
value
};
let at_hello = json!({"line": 9, "column": 27});
let indexing = Instant::now();
loop {
let found = run(
"lsp.definition",
json!({"id": id, "position": {"line": 8, "column": 24}}),
);
if found["locations"][0]["path"] == "src/Greeter.php" {
break;
}
assert!(
indexing.elapsed() < Duration::from_secs(60),
"no definition after 60 s: {found}"
);
std::thread::sleep(Duration::from_millis(250));
}
println!("first definition after {:?}", indexing.elapsed());
let definition = run(
"lsp.definition",
json!({"id": id, "position": {"line": 8, "column": 24}}),
);
assert_eq!(definition["locations"][0]["path"], "src/Greeter.php");
let references = run("lsp.references", json!({"id": id, "position": at_hello}));
assert_eq!(
references["locations"].as_array().unwrap().len(),
4,
"{references}"
);
let prepared = run("lsp.prepareRename", json!({"id": id, "position": at_hello}));
assert_eq!(prepared["placeholder"], "hello");
let renamed = run(
"lsp.rename",
json!({"id": id, "position": at_hello, "newName": "greet"}),
);
assert_eq!(renamed["edits"], 4, "{renamed}");
assert_eq!(renamed["written"], 2);
assert_eq!(
fs::read_to_string(src.join("Other.php")).unwrap(),
"<?php\r\n\r\nnamespace App;\r\n\r\nfunction welcome(Greeter $g): string\r\n{\r\n return $g->greet(\"otter\") . $g->greet(\"sea otter\");\r\n}\r\n"
);
let reindex = Instant::now();
loop {
let found = run("lsp.references", json!({"id": id, "position": at_hello}));
if found["locations"].as_array().map_or(0, Vec::len) == 4 {
break;
}
assert!(
reindex.elapsed() < Duration::from_secs(30),
"Phpactor never saw the written files: {found}"
);
std::thread::sleep(Duration::from_millis(100));
}
println!("index followed the rename after {:?}", reindex.elapsed());
let back = run(
"lsp.rename",
json!({"id": id, "position": at_hello, "newName": "hello"}),
);
assert_eq!(back["edits"], 4, "Phpactor saw the written files: {back}");
}
#[test]
fn without_a_server_the_commands_fail() {
let dir = tempfile::tempdir().unwrap();
fs::write(dir.path().join("a.php"), A).unwrap();
let mut engine = Engine::new().unwrap();
engine
.execute("project.open", json!({"path": dir.path()}))
.unwrap();
let id = engine
.execute("file.open", json!({"path": "a.php"}))
.unwrap()
.value["id"]
.as_u64()
.unwrap();
let err = engine
.execute(
"lsp.definition",
json!({"id": id, "position": {"line": 0, "column": 0}}),
)
.unwrap_err();
assert!(err.to_string().contains("not running"), "{err}");
}