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>
178 lines
8.1 KiB
TypeScript
178 lines
8.1 KiB
TypeScript
/**
|
|
* Tests of hover documentation and signature help (M3.4,
|
|
* `assist/hover.ts`, `assist/signature.ts`): the queries they send, the
|
|
* tooltip range, safe rendering, the highlighted parameter, the counter,
|
|
* typing `(` and `,`, following the cursor and closing with Escape.
|
|
*/
|
|
import { EditorState, type Extension } from "@codemirror/state";
|
|
import { EditorView } from "@codemirror/view";
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { assistExtensions } from "../../../src/components/editor/assist";
|
|
import { assistEnabled, quickDocumentation } from "../../../src/components/editor/assist/commands";
|
|
import { hoverAt } from "../../../src/components/editor/assist/hover";
|
|
import {
|
|
lspSignatureHelp,
|
|
renderSignature,
|
|
setSignature,
|
|
signatureField,
|
|
} from "../../../src/components/editor/assist/signature";
|
|
import { toHover, toSignatureHelp, type SignatureHelpResult } from "../../../src/components/editor/assist/types";
|
|
import { fakeBuffer, fakeTransport } from "./fakes";
|
|
|
|
/** Views created by a test, destroyed after it. */
|
|
let views: EditorView[] = [];
|
|
|
|
afterEach(() => {
|
|
for (const view of views) {
|
|
view.destroy();
|
|
}
|
|
views = [];
|
|
});
|
|
|
|
/** A mounted view with `doc` and `extensions`. */
|
|
function mount(doc: string, extensions: Extension = []): EditorView {
|
|
const parent = document.createElement("div");
|
|
document.body.append(parent);
|
|
const view = new EditorView({ state: EditorState.create({ doc, extensions }), parent });
|
|
views.push(view);
|
|
return view;
|
|
}
|
|
|
|
/** A signature help result for `pair(int $a, string $b)`. */
|
|
const pair: SignatureHelpResult = {
|
|
signatures: [
|
|
{
|
|
label: "pair(int $a, string $b): void",
|
|
documentation: "Makes a *pair*.",
|
|
parameters: [{ label: [5, 11], documentation: "the `first`" }, { label: [13, 22] }],
|
|
},
|
|
],
|
|
activeSignature: 0,
|
|
activeParameter: 1,
|
|
};
|
|
|
|
describe("hover", () => {
|
|
it("asks for the hovered position and spans the returned range", async () => {
|
|
const view = mount("<?php\r\n$x = new Foo();".replace("\r\n", "\n"));
|
|
const transport = fakeTransport({
|
|
contents: "```php\nclass Foo\n```\n<img src=x onerror=alert(1)>",
|
|
range: { start: { line: 1, column: 9 }, end: { line: 1, column: 12 } },
|
|
});
|
|
const tooltip = await hoverAt(view, 16, fakeBuffer(3), transport);
|
|
expect(transport.calls).toEqual([{ command: "lsp.hover", params: { id: 3, position: { line: 1, column: 10 } } }]);
|
|
expect(tooltip?.pos).toBe(15);
|
|
expect(tooltip?.end).toBe(18);
|
|
expect(tooltip?.above).toBe(true);
|
|
const dom = tooltip?.create(view).dom as HTMLElement;
|
|
expect(dom.classList.contains("lt-assist-hover")).toBe(true);
|
|
expect(dom.querySelector("pre code")?.textContent).toBe("class Foo");
|
|
expect(dom.querySelector("img")).toBeNull();
|
|
expect(dom.textContent).toContain("<img src=x onerror=alert(1)>");
|
|
});
|
|
|
|
it("shows nothing for empty or failed answers", async () => {
|
|
const view = mount("<?php\n");
|
|
expect(await hoverAt(view, 2, fakeBuffer(1), fakeTransport({ contents: " " }))).toBeNull();
|
|
expect(await hoverAt(view, 2, fakeBuffer(1), fakeTransport(null, { code: "failed", message: "x" }))).toBeNull();
|
|
expect(toHover({ contents: 5 })).toBeNull();
|
|
expect(toHover({ contents: "a", range: { start: { line: -1 } } })).toEqual({ contents: "a" });
|
|
});
|
|
});
|
|
|
|
describe("renderSignature", () => {
|
|
it("highlights the active parameter and shows its and the signature's docs", () => {
|
|
const dom = renderSignature(pair);
|
|
expect(dom.querySelector(".lt-assist-signature-label")?.textContent).toBe("pair(int $a, string $b): void");
|
|
expect(dom.querySelector(".lt-assist-signature-active")?.textContent).toBe("string $b");
|
|
expect(dom.querySelectorAll(".lt-assist-signature-doc")).toHaveLength(1);
|
|
const first = renderSignature({ ...pair, activeParameter: 0 });
|
|
expect(first.querySelector(".lt-assist-signature-active")?.textContent).toBe("int $a");
|
|
expect(first.querySelectorAll(".lt-assist-signature-doc")).toHaveLength(2);
|
|
expect(first.querySelector(".lt-assist-signature-doc code")?.textContent).toBe("first");
|
|
});
|
|
|
|
it("counts several signatures and lets a signature override the active parameter", () => {
|
|
const two: SignatureHelpResult = {
|
|
signatures: [...pair.signatures, { label: "pair(int $a)", parameters: [{ label: [5, 11] }], activeParameter: 0 }],
|
|
activeSignature: 1,
|
|
activeParameter: 3,
|
|
};
|
|
const dom = renderSignature(two);
|
|
expect(dom.querySelector(".lt-assist-signature-count")?.textContent).toBe("2 of 2");
|
|
expect(dom.querySelector(".lt-assist-signature-active")?.textContent).toBe("int $a");
|
|
});
|
|
|
|
it("checks the untrusted result", () => {
|
|
expect(toSignatureHelp({ signatures: [] })).toBeNull();
|
|
expect(toSignatureHelp(null)).toBeNull();
|
|
const help = toSignatureHelp({
|
|
signatures: [{ label: "f(a)", parameters: [{ label: [2, 3] }, { label: [2, 99] }, { label: "a" }, "junk"] }],
|
|
activeSignature: 7,
|
|
activeParameter: -1,
|
|
});
|
|
expect(help?.activeSignature).toBe(0);
|
|
expect(help?.activeParameter).toBe(0);
|
|
expect(help?.signatures[0]?.parameters.map((p) => p.label)).toEqual([[2, 3], null, null, null]);
|
|
});
|
|
});
|
|
|
|
describe("signature help while typing", () => {
|
|
it("opens on (, follows the cursor and closes with Escape", async () => {
|
|
const transport = fakeTransport({
|
|
signatures: [{ label: "pair(int $a, string $b): void", parameters: [{ label: [5, 11] }, { label: [13, 22] }] }],
|
|
activeSignature: 0,
|
|
activeParameter: 0,
|
|
});
|
|
const view = mount("<?php\npair", lspSignatureHelp(fakeBuffer(5), transport));
|
|
view.dispatch({ changes: { from: 10, insert: "(" }, selection: { anchor: 11 }, userEvent: "input.type" });
|
|
await vi.waitFor(() => {
|
|
expect(view.state.field(signatureField)).not.toBeNull();
|
|
});
|
|
expect(transport.calls[0]).toEqual({ command: "lsp.signatureHelp", params: { id: 5, position: { line: 1, column: 5 } } });
|
|
expect(view.dom.querySelector(".lt-assist-signature-active")?.textContent).toBe("int $a");
|
|
view.dispatch({ changes: { from: 11, insert: "1" }, selection: { anchor: 12 }, userEvent: "input.type" });
|
|
await vi.waitFor(() => {
|
|
expect(transport.calls).toHaveLength(2);
|
|
});
|
|
const escape = new KeyboardEvent("keydown", { key: "Escape", bubbles: true });
|
|
view.contentDOM.dispatchEvent(escape);
|
|
expect(view.state.field(signatureField)).toBeNull();
|
|
});
|
|
|
|
it("closes when the server has no signatures any more", async () => {
|
|
const transport = fakeTransport({ signatures: [], activeSignature: 0, activeParameter: 0 });
|
|
const view = mount("<?php\npair(1)", lspSignatureHelp(fakeBuffer(5), transport));
|
|
view.dispatch({ effects: setSignature.of({ pos: 11, help: pair }) });
|
|
expect(view.state.field(signatureField)).not.toBeNull();
|
|
view.dispatch({ selection: { anchor: 13 } });
|
|
await vi.waitFor(() => {
|
|
expect(view.state.field(signatureField)).toBeNull();
|
|
});
|
|
});
|
|
|
|
it("ignores typing that does not start an argument", async () => {
|
|
const transport = fakeTransport(null);
|
|
const view = mount("<?php\n", lspSignatureHelp(fakeBuffer(5), transport));
|
|
view.dispatch({ changes: { from: 6, insert: "x" }, userEvent: "input.type" });
|
|
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
expect(transport.calls).toHaveLength(0);
|
|
});
|
|
});
|
|
|
|
describe("assist commands", () => {
|
|
it("are enabled only with a PHP editor and show quick documentation at the cursor", async () => {
|
|
expect(assistEnabled()).toBe(false);
|
|
const transport = fakeTransport({ contents: "**Foo** docs" });
|
|
const view = mount("<?php\nnew Foo();", assistExtensions("a.php", fakeBuffer(9), transport));
|
|
view.dispatch({ selection: { anchor: 11 } });
|
|
expect(assistEnabled()).toBe(true);
|
|
await quickDocumentation();
|
|
expect(transport.calls[0]).toEqual({ command: "lsp.hover", params: { id: 9, position: { line: 1, column: 5 } } });
|
|
expect(view.dom.querySelector(".lt-assist-hover strong")?.textContent).toBe("Foo");
|
|
view.dispatch({ selection: { anchor: 12 } });
|
|
expect(view.dom.querySelector(".lt-assist-hover")).toBeNull();
|
|
view.destroy();
|
|
views = views.filter((v) => v !== view);
|
|
expect(assistEnabled()).toBe(false);
|
|
});
|
|
});
|