//! 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 { 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), EngineError> { let mut out: Outcome = self.engine.execute(command, params)?; let mut events: Vec = 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 = "hello();\n"; const C: &str = "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 { 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"], "greet();\n" ); assert_eq!( fs::read_to_string(f.path("c.php")).unwrap(), "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= [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"), "hello('Lutra');\n }\n}\n", ) .unwrap(); fs::write( src.join("Other.php"), "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(), "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}"); }