M3.4: completion, hover and signature help from Phpactor

Engine (crates/engine/src/cmd/lsp_assist.rs), on M3.5's pending outcomes:
- lsp.completion {id, position, triggerCharacter?}: items with label,
  kind, detail, Markdown documentation, insertText, textEdit,
  additionalTextEdits, sortText; sorted, capped at 500 (cut lists are
  incomplete), snippets turned into plain text.
- lsp.hover {id, position}: Markdown contents and range.
- lsp.signatureHelp {id, position}: signatures with UTF-16 parameter spans.
- The request is queued after every didChange; the answer is converted
  in the reply callback, off the engine lock. Positions are checked
  against the buffer. Server strings and lists are capped.
- lutra-lsp-fake answers completion and signature help.

Editor (ui/src/components/editor/assist/):
- Completion source with Lutra's triggers (identifiers, $, ->, ?->, ::,
  namespace \; not in strings or comments unless asked), the server's
  ranking as boost, text edits and imports applied in UTF-16 (CRLF too),
  Lucide kind icons.
- Hover documentation (350 ms) and editor.quickDocumentation (Mod+I).
- Signature help on ( and , following the cursor; editor.parameterInfo.
- editor.complete.
- Lutra's own Markdown renderer without HTML (XSS-tested), tokens only.
- Requests wait for the buffer sync to be idle and use runWaiting.
- New dependency @codemirror/autocomplete (MIT, already transitive).

Tests: 6 engine tests against the fake server, 9 unit tests, an ignored
real-Phpactor latency test, 29 Vitest tests; palette and menu tests list
the new UI commands.

Measured with the real Phpactor (Windows 11, debug build, 25 ms tick):
- Completion 61-109 ms while typing (median 76 ms; ~107 ms in the app).
- Hover and signature help ~16 ms median.
- A useful list 2.3 s after opening a file in a 20-class project, ~65 s
  for 2,000 classes (indexing).
- Phpactor 56-71 MB working set, ~6% of one core while typing.

Live-checked in the app on port 7934. Docs: Code completion 0% -> 5%
(est.), feature depth ~4.1% -> ~4.6%, breadth 26/155, architecture, G6,
ui-parity, language-parity, roadmap, README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-22 18:38:07 +02:00
parent 12d21d96f4
commit a212a44490
35 changed files with 3916 additions and 25 deletions

View file

@ -16,6 +16,9 @@
//! document version; `didClose` publishes no diagnostics; `didSave` sends
//! `fake/saved` with the text it received.
//! - Requests: `textDocument/hover` answers `hover L:C`,
//! `textDocument/completion` a scripted list (see [`completion`]),
//! `textDocument/signatureHelp` one signature whose active parameter is
//! the column modulo 2,
//! `textDocument/definition` the same position, `fake/echo` its params,
//! `fake/error` an error, `fake/sleep` never answers until cancelled
//! (then `RequestCancelled` and the notification `fake/cancelled`),
@ -47,6 +50,46 @@ struct Fake {
init_error: bool,
}
/// The fake answer to `textDocument/completion`: a `CompletionList` whose
/// first item replaces the word before the cursor through a `textEdit`
/// (with an import as `additionalTextEdits`), then a plain variable, a
/// snippet and an item echoing the trigger character. A document
/// containing `MANY` gets 700 numbered items instead.
fn completion(text: &str, position: &Value, context: &Value) -> Value {
if text.contains("MANY") {
let items: Vec<Value> = (0..700)
.map(|i| json!({"label": format!("item{i:03}"), "kind": 6}))
.collect();
return json!({"isIncomplete": false, "items": items});
}
let line = at(position, "/line").as_u64().unwrap_or(0);
let character = at(position, "/character").as_u64().unwrap_or(0);
let trigger = at(context, "/triggerCharacter").as_str().unwrap_or("none");
json!({
"isIncomplete": true,
"items": [
{
"label": "format",
"kind": 2,
"detail": "format(string $pattern): string",
"documentation": {"kind": "markdown", "value": "Formats **text**."},
"sortText": "0001",
"textEdit": {
"range": {"start": {"line": line, "character": character.saturating_sub(2)}, "end": {"line": line, "character": character}},
"newText": "format("
},
"additionalTextEdits": [{
"range": {"start": {"line": 0, "character": 5}, "end": {"line": 0, "character": 5}},
"newText": "\nuse App\\Fmt;"
}]
},
{"label": "$value", "kind": 6, "sortText": "0002"},
{"label": "pair", "kind": 3, "insertTextFormat": 2, "insertText": "pair(${1:\\$a}, $2)$0", "sortText": "0003"},
{"label": format!("trigger:{trigger}"), "kind": 14, "sortText": "0004"}
]
})
}
/// Runs the fake server until `exit` or the end of stdin.
fn main() {
let args: Vec<String> = std::env::args().skip(1).collect();
@ -148,7 +191,9 @@ impl Fake {
"positionEncoding": "utf-16",
"textDocumentSync": {"openClose": true, "change": change, "save": {"includeText": true}},
"hoverProvider": true,
"definitionProvider": true
"definitionProvider": true,
"completionProvider": {"triggerCharacters": [">", ":", "$", "\\"]},
"signatureHelpProvider": {"triggerCharacters": ["(", ","]}
},
"serverInfo": {"name": "fake"}
})),
@ -172,6 +217,38 @@ impl Fake {
Ok(json!([{"uri": uri, "range": {"start": position, "end": position}}])),
);
}
"textDocument/completion" => {
let uri = at(params, "/textDocument/uri").as_str().unwrap_or_default();
let text = self
.documents
.get(uri)
.map(Buffer::text)
.unwrap_or_default();
respond(
id,
Ok(completion(
&text,
at(params, "/position"),
at(params, "/context"),
)),
);
}
"textDocument/signatureHelp" => {
let position = at(params, "/position");
let active = at(position, "/character").as_u64().unwrap_or(0) % 2;
respond(
id,
Ok(json!({
"signatures": [{
"label": "pair(int $a, string $b): void",
"documentation": {"kind": "markdown", "value": "Makes a *pair*."},
"parameters": [{"label": "int $a"}, {"label": [13, 22]}]
}],
"activeSignature": 0,
"activeParameter": active
})),
);
}
"fake/echo" => respond(id, Ok(params.clone())),
"fake/error" => respond(id, Err(error(-32001, "boom"))),
"fake/sleep" => self.sleeping.push(id),