/** * Tests of the completion source (M3.4, `assist/completion.ts`): Lutra's * trigger rules, the query it sends (UTF-16 columns, trigger characters, * waiting for the buffer sync), local filtering only for complete lists, * and applying items: text edits, text typed after the request, imports * as additional edits, CRLF documents and CRLF insert texts. */ import { CompletionContext, type Completion, type CompletionResult } from "@codemirror/autocomplete"; import { php } from "@codemirror/lang-php"; import { ensureSyntaxTree } from "@codemirror/language"; import { EditorState, Text } from "@codemirror/state"; import { EditorView } from "@codemirror/view"; import { afterEach, describe, expect, it } from "vitest"; import { completionChanges, completionTrigger, lspCompletionSource, toOptions, } from "../../../src/components/editor/assist/completion"; import { kindIcon } from "../../../src/components/editor/assist/icons"; import { assistExtensions } from "../../../src/components/editor/assist"; import { toCompletionList, type CompletionItem } 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 PHP editor state with the cursor at `|` in `text`. */ function stateAt(text: string): { state: EditorState; pos: number } { const pos = text.indexOf("|"); const doc = text.slice(0, pos) + text.slice(pos + 1); const state = EditorState.create({ doc, extensions: [php()], selection: { anchor: pos } }); ensureSyntaxTree(state, state.doc.length, 5000); return { state, pos }; } /** A complete item with defaults. */ function item(over: Partial): CompletionItem { return { label: "x", kind: "text", insertText: over.label ?? "x", ...over }; } /** Applies a plan to a document and returns the text and cursor. */ function applyPlan(doc: string, plan: ReturnType): { text: string; cursor: number } { const state = EditorState.create({ doc }); const changes = state.changes(plan.changes); return { text: changes.apply(state.doc).toString(), cursor: changes.mapPos(plan.mainEnd, 1) }; } describe("completionTrigger", () => { it("triggers after member and static access, variables and namespaces", () => { expect(completionTrigger("$a->", false)).toEqual({ back: 0, character: ">" }); expect(completionTrigger("$a?->", false)).toEqual({ back: 0, character: ">" }); expect(completionTrigger("Foo::", false)).toEqual({ back: 0, character: ":" }); expect(completionTrigger("echo $", false)).toEqual({ back: 1, character: "$" }); expect(completionTrigger("new App\\", false)).toEqual({ back: 0, character: "\\" }); }); it("triggers on identifiers and variables being typed", () => { expect(completionTrigger("$a->fo", false)).toEqual({ back: 2 }); expect(completionTrigger("echo $na", false)).toEqual({ back: 3 }); expect(completionTrigger("strl", false)).toEqual({ back: 4 }); expect(completionTrigger("$รค", false)).toEqual({ back: 2 }); }); it("does not trigger on numbers, operators or spaces unless asked", () => { expect(completionTrigger("x = 12", false)).toBeNull(); expect(completionTrigger("x = 1e5", false)).toBeNull(); expect(completionTrigger("a - ", false)).toBeNull(); expect(completionTrigger("a -", false)).toBeNull(); expect(completionTrigger("", false)).toBeNull(); expect(completionTrigger("a - ", true)).toEqual({ back: 0 }); expect(completionTrigger("12", true)).toEqual({ back: 0 }); }); }); describe("toCompletionList", () => { it("drops malformed items and defaults the rest", () => { const list = toCompletionList({ incomplete: true, items: [ { label: "a", kind: "nonsense" }, { label: "" }, "junk", { label: "b", kind: "class", insertText: "B", textEdit: { range: { start: {}, end: {} }, newText: "x" } }, { label: "c", additionalTextEdits: [{ range: { start: { line: 0, column: 0 }, end: { line: 0, column: 0 } }, newText: "u" }, null] }, ], }); expect(list.incomplete).toBe(true); expect(list.items.map((i) => i.label)).toEqual(["a", "b", "c"]); expect(list.items[0]).toEqual({ label: "a", kind: "text", insertText: "a" }); expect(list.items[1]?.textEdit).toBeUndefined(); expect(list.items[2]?.additionalTextEdits).toHaveLength(1); expect(toCompletionList(null)).toEqual({ items: [], incomplete: false }); expect(toCompletionList({ items: "no" })).toEqual({ items: [], incomplete: false }); }); }); describe("completionChanges", () => { it("applies a text edit and the typing since the request", () => { const doc = "fo"; const requestDoc = Text.of(doc.split("\n")); const requestPos = doc.length; const format = item({ label: "format", insertText: "format(", textEdit: { range: { start: { line: 1, column: 4 }, end: { line: 1, column: 6 } }, newText: "format(" }, }); expect(applyPlan(doc, completionChanges(requestDoc, requestPos, format, 10, 12, doc.length))).toEqual({ text: "format(", cursor: 17, }); const typedMore = doc + "rm"; expect(applyPlan(typedMore, completionChanges(requestDoc, requestPos, format, 10, 14, typedMore.length))).toEqual({ text: "format(", cursor: 17, }); }); it("replaces the typed word with insertText without a text edit", () => { const doc = " { const doc = " { const engineText = "ru\r\n}\r\n"; const lines = engineText.split("\r\n"); const requestDoc = Text.of(lines); const state = EditorState.create({ doc: requestDoc }); const line = state.doc.line(3); const requestPos = line.to; const run = item({ label: "run", textEdit: { range: { start: { line: 2, column: 11 }, end: { line: 2, column: 13 } }, newText: "run(\r\n 1\r\n)" }, }); const plan = completionChanges(requestDoc, requestPos, run, requestPos - 2, requestPos, state.doc.length); const changes = state.changes(plan.changes); const after = changes.apply(state.doc); expect(after.lines).toBe(7); expect(after.line(3).text).toBe(" $this->run("); expect(after.line(4).text).toBe(" 1"); expect(after.line(5).text).toBe(")"); expect(after.sliceString(0)).not.toContain("\r"); }); it("clamps edits that point past the document to its last line", () => { const doc = " { it("asks with UTF-16 columns after the sync is idle and maps the items", async () => { const transport = fakeTransport({ items: [ { label: "format", kind: "method", detail: "format(): string", documentation: "**doc**", insertText: "format(" }, { label: "$value", kind: "variable", insertText: "$value" }, ], incomplete: false, }); const buffer = fakeBuffer(7); const { state, pos } = stateAt("fo|"); const source = lspCompletionSource(buffer, transport); const result = (await source(new CompletionContext(state, pos, false))) as CompletionResult; expect(buffer.idleCalls).toBe(1); expect(transport.calls).toEqual([ { command: "lsp.completion", params: { id: 7, position: { line: 1, column: 7 } } }, ]); expect(result.from).toBe(pos - 2); expect(result.validFor).toBeInstanceOf(RegExp); expect(result.options.map((o) => o.label)).toEqual(["format", "$value"]); expect(result.options[0]?.detail).toBe("format(): string"); expect(result.options[0]?.type).toBe("method"); expect((result.options[0]?.boost ?? 0) > (result.options[1]?.boost ?? 0)).toBe(true); const info = result.options[0]?.info; expect(typeof info).toBe("function"); }); it("sends the trigger character and asks again for incomplete lists", async () => { const transport = fakeTransport({ items: [{ label: "run", kind: "method", insertText: "run" }], incomplete: true }); const { state, pos } = stateAt("|"); const result = (await lspCompletionSource(fakeBuffer(1), transport)(new CompletionContext(state, pos, false))) as CompletionResult; expect(transport.calls[0]?.params).toMatchObject({ triggerCharacter: ">" }); expect(result.from).toBe(pos); expect(result.validFor).toBeUndefined(); }); it("stays quiet in comments and strings unless asked", async () => { const transport = fakeTransport({ items: [{ label: "x", kind: "text", insertText: "x" }], incomplete: false }); const source = lspCompletionSource(fakeBuffer(1), transport); const comment = stateAt(" { const { state, pos } = stateAt(" { view.dispatch({ changes: { from: pos, insert: "o" } }); }); const transport = fakeTransport({ items: [{ label: "foo", kind: "function", insertText: "foo" }], incomplete: false }); const context = new CompletionContext(state, pos, false, view); expect(await lspCompletionSource(buffer, transport)(context)).toBeNull(); expect(transport.calls).toHaveLength(0); const failing = fakeTransport(null, { code: "failed", message: "no PHP language server is running" }); expect(await lspCompletionSource(fakeBuffer(1), failing)(new CompletionContext(state, pos, false))).toBeNull(); }); it("applies an option through the view, with the import", () => { const doc = " { it("is only added for PHP files of a buffer", () => { expect(assistExtensions("a.txt", fakeBuffer(1))).toEqual([]); expect(assistExtensions("a.php", undefined)).toEqual([]); expect(assistExtensions("src/A.php", fakeBuffer(1))).not.toEqual([]); }); it("renders a Lucide icon with the kind's catalog name", () => { const icon = kindIcon("class"); expect(icon.title).toBe("Class"); expect(icon.querySelector("svg")).not.toBeNull(); expect(kindIcon("nonsense").className).toContain("lt-assist-kind-text"); }); });