Skip to content

The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.

fix(editor): separate completion names from field types (#2402)

l0ng-aie3f41a22846a
diff --git a/e2e/codeInsight.spec.ts b/e2e/codeInsight.spec.ts
index 1ddcd4796..ee4082b60 100644
--- a/e2e/codeInsight.spec.ts
+++ b/e2e/codeInsight.spec.ts
@@ -33,7 +33,7 @@ test("completion lists the server's items and inserts the chosen one", async ({
await page.evaluate(() => {
const item = (label: string, kind: string, detail: string, insertText: string) => ({
label,
- labelDetail: null,
+ labelDetail: kind === "property" ? ` ${detail}` : "()",
detail,
kind,
documentation: null,
@@ -64,6 +64,12 @@ test("completion lists the server's items and inserts the chosen one", async ({
const popup = page.locator(".cm-tooltip-autocomplete");
await expect(popup.getByText("total", { exact: true })).toBeVisible();
await expect(popup.getByText("toDto", { exact: true })).toBeVisible();
+ const field = popup.locator("li").filter({ has: page.locator(".cm-completionLabel", { hasText: /^total$/ }) });
+ await expect(field.locator(".cm-completionDetail")).toHaveCount(0);
+ await expect(field.locator(".cm-lsp-type")).toHaveText("BigDecimal");
+ const method = popup.locator("li").filter({ has: page.locator(".cm-completionLabel", { hasText: /^toDto$/ }) });
+ await expect(method.locator(".cm-completionDetail")).toHaveText("()");
+ await expect(method.locator(".cm-lsp-type")).toHaveText("OrderDto");
await page.keyboard.type("toD");
await expect(popup.getByText("total", { exact: true })).toBeHidden();
await page.keyboard.press("Enter");
diff --git a/src/editor/lsp/completion.test.ts b/src/editor/lsp/completion.test.ts
index 71e4e0f50..36a375d44 100644
--- a/src/editor/lsp/completion.test.ts
+++ b/src/editor/lsp/completion.test.ts
@@ -131,6 +131,45 @@ describe("toCmCompletion", () => {
});
});
+describe("completion label presentation", () => {
+ // gopls v0.24.0: labelDetails.detail includes a leading space for field types; description is absent.
+ it.each([
+ ["Cap", "field", " int", "int", null, null, "int"],
+ ["Array", "field", " []any", "[]any", null, null, "[]any"],
+ ["Add", "method", "() error", "func() error", null, "() error", "func() error"],
+ [
+ "MakeArray",
+ "function",
+ "(capacity int) *LRUArray",
+ "func(capacity int) *LRUArray",
+ null,
+ "(capacity int) *LRUArray",
+ "func(capacity int) *LRUArray",
+ ],
+ ["fmt", "module", ' "fmt"', '"fmt"', null, null, '"fmt"'],
+ ["count", "field", "int", null, null, null, "int"],
+ ["count", "field", null, "int", null, null, "int"],
+ ["pair", "field", "(i32, i32)", null, null, null, "(i32, i32)"],
+ ["callback", "property", "(value: string) => void", null, null, null, "(value: string) => void"],
+ ["print", "method", "(Object obj)", "PrintStream.print(Object obj) : void", "void", "(Object obj)", "void"],
+ ["map", "method", "<T>(value: T)", null, "T[]", "<T>(value: T)", "T[]"],
+ ["str", "function", "(value: object)", null, "str", "(value: object)", "str"],
+ ["println!", "function", "(…)", null, "std", "(…)", "std"],
+ ["size", "property", "number", "(property) size: number", "number", null, "number"],
+ ["value", "variable", " ", "Order", " ", null, "Order"],
+ ] as const)(
+ "renders %s (%s) as name, signature suffix and type",
+ (label, kind, labelDetail, detail, labelDescription, suffix, type) => {
+ const source = item({ label, kind, labelDetail, detail, labelDescription });
+ const completion = toCmCompletion(source);
+ expect(completion.label).toBe(label);
+ expect(completion.detail ?? null).toBe(suffix);
+ expect(typeColumnText(source)).toBe(type);
+ expect(source.labelDetail).toBe(labelDetail);
+ },
+ );
+});
+
describe("completion helpers", () => {
it("detects trigger characters", () => {
expect(triggerCharacter("a.")).toBe(".");
diff --git a/src/editor/lsp/completionItems.ts b/src/editor/lsp/completionItems.ts
index a1f865e72..e4ed7d086 100644
--- a/src/editor/lsp/completionItems.ts
+++ b/src/editor/lsp/completionItems.ts
@@ -47,7 +47,8 @@ export function toCmCompletion(item: CompletionItem): Completion {
type: completionSymbolKind(item.kind),
};
if (filter) c.displayLabel = item.label;
- if (item.labelDetail) c.detail = item.labelDetail;
+ const suffix = signatureSuffix(item);
+ if (suffix) c.detail = suffix;
if (item.sortText) c.sortText = item.sortText;
if (item.preselect) c.boost = 99;
else if (item.deprecated) c.boost = -20;
@@ -112,16 +113,27 @@ export function importSource(item: Pick<CompletionItem, "additionalEdits">): str
return null;
}
+type LabelPresentation = Partial<Pick<CompletionItem, "labelDetail" | "labelDescription" | "kind">>;
+
+/** Only parameter suffixes belong beside the name; fields can have tuple or function types. */
+function signatureSuffix(item: LabelPresentation): string | null {
+ const suffix = item.labelDetail;
+ const callable = !item.kind || ["method", "function", "constructor"].includes(item.kind);
+ return callable && suffix && /^(?:<.*>)?\(/s.test(suffix.trimStart()) ? suffix : null;
+}
+
/**
* Text of the right-aligned type column: the label description (`void`, `String`), else the
- * detail, with the import source when it lacks one. JDT.LS sends the whole signature as `detail`
+ * non-signature label detail or detail, with the import source when it lacks one.
+ * JDT.LS sends the whole signature as `detail`
* (`PrintStream.print(Object obj) : void`), which would repeat the name and parameters already
* shown next to the label; the documentation panel still shows it.
*/
export function typeColumnText(
- item: Pick<CompletionItem, "detail" | "additionalEdits"> & { labelDescription?: string | null },
+ item: Pick<CompletionItem, "detail" | "additionalEdits"> & LabelPresentation,
): string | null {
- const detail = (item.labelDescription?.trim() || item.detail)?.replace(/\s+/g, " ") || null;
+ const labelType = signatureSuffix(item) ? null : item.labelDetail?.trim();
+ const detail = (item.labelDescription?.trim() || labelType || item.detail)?.replace(/\s+/g, " ") || null;
const source = importSource(item);
if (!source || detail?.includes(source)) return detail;
return !detail || /^auto[- ]?import$/i.test(detail) ? source : `${detail} (${source})`;