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 callable completion results from parameters (#2419)

l0ng-ai6b70e1ea0817
diff --git a/src/editor/lsp/completion.test.ts b/src/editor/lsp/completion.test.ts
index 36a375d44..406f50427 100644
--- a/src/editor/lsp/completion.test.ts
+++ b/src/editor/lsp/completion.test.ts
@@ -136,16 +136,46 @@ describe("completion label presentation", () => {
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"],
+ ["Add", "method", "() error", "func() error", null, "()", "error"],
[
"MakeArray",
"function",
"(capacity int) *LRUArray",
"func(capacity int) *LRUArray",
null,
- "(capacity int) *LRUArray",
- "func(capacity int) *LRUArray",
+ "(capacity int)",
+ "*LRUArray",
+ ],
+ ["Format", "method", "(layout string) string", "func(layout string) string", null, "(layout string)", "string"],
+ [
+ "Lookup",
+ "function",
+ "(x int) (string, error)",
+ "func(x int) (string, error)",
+ null,
+ "(x int)",
+ "(string, error)",
+ ],
+ ["Identity", "function", "[T any](x T) T", "func[T any](x T) T", null, "[T any](x T)", "T"],
+ [
+ "Apply",
+ "function",
+ "[T interface{ ~[]byte | ~[2]byte }](f func(T) (T, error), xs []T) (T, error)",
+ "func[T interface{ ~[]byte | ~[2]byte }](f func(T) (T, error), xs []T) (T, error)",
+ null,
+ "[T interface{ ~[]byte | ~[2]byte }](f func(T) (T, error), xs []T)",
+ "(T, error)",
],
+ ["Run", "function", "()", "func()", null, "()", "func()"],
+ ["broken", "function", "(f func() error", "func(f func() error", null, "(f func() error", "func(f func() error"],
+ ["callback", "field", " func() error", "func() error", null, null, "func() error"],
+ ["pair", "field", " (int, error)", "(int, error)", null, null, "(int, error)"],
+ ["repeat", "function", "(x: number)", null, "number", "(x: number)", "number"],
+ ["len", "method", "(…)", "fn len(&self) -> usize", "fn(…) -> usize", "(…)", "fn(…) -> usize"],
+ ["print", "function", null, "(value: object, /) -> None", null, null, "(value: object, /) -> None"],
+ ["size", "method", "() const", "size_t", null, "()", "size_t"],
+ ["Other", "function", "() error", "func(x int) error", null, "()", "func(x int) error"],
+ ["Named", "function", "() error", "func() error", "description", "()", "description"],
["fmt", "module", ' "fmt"', '"fmt"', null, null, '"fmt"'],
["count", "field", "int", null, null, null, "int"],
["count", "field", null, "int", null, null, "int"],
@@ -153,6 +183,27 @@ describe("completion label presentation", () => {
["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[]"],
+ [
+ "map",
+ "method",
+ "<T extends (x: number) => number>(value: T)",
+ null,
+ "T[]",
+ "<T extends (x: number) => number>(value: T)",
+ "T[]",
+ ],
+ ["literal", "function", '(value: ")" | "(")', null, "void", '(value: ")" | "(")', "void"],
+ ["literal", "function", '<T extends ">">(value: T)', null, "T", '<T extends ">">(value: T)', "T"],
+ ["literal", "function", "(value: ')' | '(')", null, "void", "(value: ')' | '(')", "void"],
+ [
+ "borrow",
+ "function",
+ "<'a>(value: &'a str)",
+ "fn borrow<'a>(value: &'a str)",
+ "&'a str",
+ "<'a>(value: &'a str)",
+ "&'a str",
+ ],
["str", "function", "(value: object)", null, "str", "(value: object)", "str"],
["println!", "function", "(…)", null, "std", "(…)", "std"],
["size", "property", "number", "(property) size: number", "number", null, "number"],
diff --git a/src/editor/lsp/completionItems.ts b/src/editor/lsp/completionItems.ts
index e4ed7d086..123a9964f 100644
--- a/src/editor/lsp/completionItems.ts
+++ b/src/editor/lsp/completionItems.ts
@@ -115,16 +115,49 @@ export function importSource(item: Pick<CompletionItem, "additionalEdits">): str
type LabelPresentation = Partial<Pick<CompletionItem, "labelDetail" | "labelDescription" | "kind">>;
+/** End of a balanced parameter or type-parameter list, including nested parentheses and brackets. */
+function listEnd(text: string, start: number): number | null {
+ const close = ({ "(": ")", "[": "]", "<": ">" } as Record<string, string>)[text[start]!];
+ if (!close) return null;
+ let depth = 1;
+ let quote: string | null = null;
+ for (let i = start + 1; i < text.length; i++) {
+ if (quote) {
+ if (text[i] === "\\" && quote !== "`") i++;
+ else if (text[i] === quote) quote = null;
+ continue;
+ }
+ // A Rust lifetime is not a quoted literal.
+ if (text[i] === "'" && !/^'(?:\\.|[^'\\])*'(?![\p{L}\p{N}_])/u.test(text.slice(i))) continue;
+ if (text[i] === '"' || text[i] === "'" || text[i] === "`") {
+ quote = text[i]!;
+ continue;
+ }
+ if (text[i] === text[start]) depth++;
+ else if (text[i] === close && !(close === ">" && text[i - 1] === "=") && --depth === 0) return i + 1;
+ }
+ return null;
+}
+
/** 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;
+ if (!callable || !suffix) return null;
+ let start = suffix.length - suffix.trimStart().length;
+ if (suffix[start] === "<" || suffix[start] === "[") {
+ const end = listEnd(suffix, start);
+ if (end === null) return null;
+ start = end;
+ }
+ if (suffix[start] !== "(") return null;
+ return suffix.slice(0, listEnd(suffix, start) ?? suffix.length);
}
/**
* Text of the right-aligned type column: the label description (`void`, `String`), else the
* non-signature label detail or detail, with the import source when it lacks one.
+ * A repeated `func` signature contributes only its result after the parameter suffix.
* 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.
@@ -132,8 +165,16 @@ function signatureSuffix(item: LabelPresentation): string | null {
export function typeColumnText(
item: Pick<CompletionItem, "detail" | "additionalEdits"> & LabelPresentation,
): string | null {
- const labelType = signatureSuffix(item) ? null : item.labelDetail?.trim();
- const detail = (item.labelDescription?.trim() || labelType || item.detail)?.replace(/\s+/g, " ") || null;
+ const suffix = signatureSuffix(item);
+ const labelType = suffix ? null : item.labelDetail?.trim();
+ const result = suffix && item.labelDetail?.slice(suffix.length).trim();
+ const repeated = item.detail
+ ?.match(/^func\s*(.*)$/s)?.[1]
+ ?.replace(/\s+/g, " ")
+ .trim();
+ const returnType = result && repeated === item.labelDetail?.replace(/\s+/g, " ").trim() ? result : null;
+ const detail =
+ (item.labelDescription?.trim() || labelType || returnType || 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})`;