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.
TypeScript code-editorcross-platformdapdatabase-clientdebuggerdesktop-appdeveloper-toolsgithttp-clientideintellijlsprusttauriterminal
fix(editor): show a completion's type, not its whole signature, in the right column (#2367)
ntbang09010b70ede663dd
diff --git a/crates/ide-host/src/ops/lsp.rs b/crates/ide-host/src/ops/lsp.rsindex 8c37732e9..e632087f7 100644--- a/crates/ide-host/src/ops/lsp.rs+++ b/crates/ide-host/src/ops/lsp.rs@@ -197,6 +197,7 @@ fn plugin_completion(item: ide_plugin::CompletionItem) -> CompletionItem {CompletionItem {label: item.label,label_detail: None,+ label_description: None,detail: item.detail,kind,documentation: item.documentation.map(|value| Markup { kind: "markdown".into(), value }),diff --git a/crates/ide-lsp/src/convert.rs b/crates/ide-lsp/src/convert.rsindex 08d4e579a..9fdc609e4 100644--- a/crates/ide-lsp/src/convert.rs+++ b/crates/ide-lsp/src/convert.rs@@ -301,6 +301,7 @@ pub fn completion_item(conv: &Conv, path: &Path, item: lsp::CompletionItem, servlet mut out = CompletionItem {label: item.label.clone(),label_detail: None,+ label_description: None,detail: None,kind: None,documentation: None,@@ -349,6 +350,9 @@ pub fn overlay_completion(conv: &Conv, path: &Path, out: &mut CompletionItem, itif let Some(d) = label_details.and_then(|d| d.detail) {out.label_detail = Some(d);}+ if let Some(d) = description.clone() {+ out.label_description = Some(d);+ }if let Some(d) = detail.or(description) {out.detail = Some(d);}@@ -425,10 +429,12 @@ pub fn lsp_completion_item(text: &DocText, enc: Encoding, item: &CompletionItem)});lsp::CompletionItem {label: item.label.clone(),- label_details: item- .label_detail- .clone()- .map(|detail| lsp::CompletionItemLabelDetails { detail: Some(detail), description: None }),+ label_details: (item.label_detail.is_some() || item.label_description.is_some()).then(|| {+ lsp::CompletionItemLabelDetails {+ detail: item.label_detail.clone(),+ description: item.label_description.clone(),+ }+ }),kind: item.kind.map(lsp_completion_kind),detail: item.detail.clone(),documentation: item.documentation.as_ref().map(lsp_documentation),@@ -1209,7 +1215,8 @@ mod tests {assert_eq!(a.insert_text, "println!($0)");assert!(a.is_snippet);assert_eq!(a.label_detail.as_deref(), Some("(…)"));- assert_eq!(a.detail.as_deref(), Some("std"));+ assert_eq!(a.label_description.as_deref(), Some("std"));+ assert_eq!(a.detail.as_deref(), Some("std"), "no `detail` from the server: the description stands in");assert_eq!(a.kind, Some(CompletionKind::Function));assert_eq!(a.replace_range, Some(Range::new(p(0, 4), p(0, 6))));assert_eq!(a.replace_range_tab, Some(Range::new(p(0, 4), p(0, 9))));@@ -1225,6 +1232,9 @@ mod tests {let back = lsp_completion_item(&text, Encoding::Utf16, &a);assert_eq!(back.data, Some(json!({"k": 1})));assert!(matches!(back.text_edit, Some(lsp::CompletionTextEdit::InsertAndReplace(_))));+ let details = back.label_details.expect("label details survive the resolve round trip");+ assert_eq!(details.detail.as_deref(), Some("(…)"));+ assert_eq!(details.description.as_deref(), Some("std"));}#[test]@@ -1255,6 +1265,10 @@ mod tests {let item = completion_item(&Conv::utf16(), Path::new("/Main.java"), items[0].clone(), ServerId(1));assert_eq!(item.replace_range, Some(Range::new(p(2, 19), p(2, end))));assert_eq!(item.replace_range_tab, None);+ // The list shows `print (Object obj)` and `void`; the long signature is only for the docs panel.+ assert_eq!(item.label_detail.as_deref(), Some("(Object obj)"));+ assert_eq!(item.label_description.as_deref(), Some("void"));+ assert_eq!(item.detail.as_deref(), Some("PrintStream.print(Object obj) : void"));assert_eq!(item.filter_text.as_deref(), Some("print(Object obj) : void"));assert_eq!(item.insert_text, "print(${0});");assert!(item.is_snippet);diff --git a/crates/ide-lsp/src/types.rs b/crates/ide-lsp/src/types.rsindex 9c629632c..042651bd9 100644--- a/crates/ide-lsp/src/types.rs+++ b/crates/ide-lsp/src/types.rs@@ -49,7 +49,11 @@ pub struct CompletionItem {pub label: String,/// e.g. `(req: CreateOrderRequest)` shown right after the label.pub label_detail: Option<String>,- /// e.g. return type, shown right-aligned.+ /// `labelDetails.description`: a return type or module path, shown right-aligned. Servers+ /// such as JDT.LS also send the whole signature as `detail`, which would repeat the label.+ pub label_description: Option<String>,+ /// The signature or type when the server sent no `label_description`; the documentation panel+ /// shows it either way.pub detail: Option<String>,pub kind: Option<CompletionKind>,pub documentation: Option<Markup>,diff --git a/crates/ide-lsp/tests/mock_server.rs b/crates/ide-lsp/tests/mock_server.rsindex 357c618c7..713427c2e 100644--- a/crates/ide-lsp/tests/mock_server.rs+++ b/crates/ide-lsp/tests/mock_server.rs@@ -1301,7 +1301,7 @@ async fn completion_mapping_merge_and_resolve() {"isIncomplete": false,"itemDefaults": { "data": { "from": "defaults" } },"items": [{- "label": "bar", "kind": 2, "detail": "fn() -> u8", "labelDetails": { "detail": "()" },+ "label": "bar", "kind": 2, "detail": "fn() -> u8", "labelDetails": { "detail": "()", "description": "u8" },"textEdit": { "newText": "bar()$0", "insert": {"start":{"line":0,"character":12},"end":{"line":0,"character":14}},"replace": {"start":{"line":0,"character":12},"end":{"line":0,"character":15}} },"insertTextFormat": 2, "sortText": "0001"@@ -1322,6 +1322,7 @@ async fn completion_mapping_merge_and_resolve() {assert_eq!(bar.insert_text, "bar()$0");assert!(bar.is_snippet);assert_eq!(bar.label_detail.as_deref(), Some("()"));+ assert_eq!(bar.label_description.as_deref(), Some("u8"));assert_eq!(bar.replace_range, Some(rng(0, 12, 0, 14)));assert_eq!(bar.replace_range_tab, Some(rng(0, 12, 0, 15)));assert_eq!(bar.data, Some(json!({ "from": "defaults" })));@@ -1337,9 +1338,13 @@ async fn completion_mapping_merge_and_resolve() {assert_eq!(resolved.documentation.as_ref().unwrap().value, "**docs**");assert_eq!(resolved.additional_edits[0].new_text, "use foo;\n");assert_eq!(resolved.insert_text, "bar()$0", "fields absent from the resolve reply are kept");+ assert_eq!(resolved.label_detail.as_deref(), Some("()"));+ assert_eq!(resolved.label_description.as_deref(), Some("u8"));+ assert_eq!(resolved.detail.as_deref(), Some("fn() -> u8"));assert_eq!(resolved.replace_range_tab, Some(rng(0, 12, 0, 15)));let sent = &a.requested("completionItem/resolve")[0];assert_eq!(sent["data"], json!({ "from": "defaults" }));+ assert_eq!(sent["labelDetails"], json!({ "detail": "()", "description": "u8" }));assert_eq!(sent["textEdit"]["replace"]["end"]["character"], 15);// Server b has no resolve support: the item comes back unchanged.diff --git a/src/bindings.ts b/src/bindings.tsindex 244352866..ab1dc840d 100644--- a/src/bindings.ts+++ b/src/bindings.ts@@ -2690,7 +2690,15 @@ export type CompletionItem = {label: string,/** e.g. `(req: CreateOrderRequest)` shown right after the label. */labelDetail: string | null,- /** e.g. return type, shown right-aligned. */+ /**+ * `labelDetails.description`: a return type or module path, shown right-aligned. Servers+ * such as JDT.LS also send the whole signature as `detail`, which would repeat the label.+ */+ labelDescription: string | null,+ /**+ * The signature or type when the server sent no `label_description`; the documentation panel+ * shows it either way.+ */detail: string | null,kind: CompletionKind | null,documentation: Markup | null,diff --git a/src/editor/lsp/completion.test.ts b/src/editor/lsp/completion.test.tsindex 5416bbcbc..71e4e0f50 100644--- a/src/editor/lsp/completion.test.ts+++ b/src/editor/lsp/completion.test.ts@@ -37,6 +37,7 @@ function item(over: Partial<CompletionItem> = {}): CompletionItem {return {label: "create",labelDetail: null,+ labelDescription: null,detail: null,kind: "method",documentation: null,@@ -486,6 +487,43 @@ describe("typeColumnText", () => {expect(typeColumnText({ detail: null, additionalEdits: edit('import { a } from "./money";\n') })).toBe("./money");});+ it("shows the type, not the repeated signature, when the server sends both (JDT.LS `print`)", () => {+ expect(+ typeColumnText({+ labelDescription: "void",+ detail: "PrintStream.print(Object obj) : void",+ additionalEdits: [],+ }),+ ).toBe("void");+ expect(typeColumnText({ labelDescription: "String", detail: "Person.name : String", additionalEdits: [] })).toBe(+ "String",+ );+ });++ it("falls back to the detail when the server sends no description, and names the import source", () => {+ expect(typeColumnText({ labelDescription: null, detail: "Order", additionalEdits: [] })).toBe("Order");+ expect(+ typeColumnText({+ labelDescription: "Auto-import",+ detail: "Auto import from './money'",+ additionalEdits: edit('import { a } from "./money";\n'),+ }),+ ).toBe("./money");+ });++ it.each([+ ["TypeScript", "(method) String.toUpperCase(): string"],+ ["Python", "(value: object) -> str"],+ ["Rust", "fn len(&self) -> usize"],+ ["Go", "func() error"],+ ])("keeps %s details unchanged without label details", (_language, detail) => {+ expect(typeColumnText(item({ detail }))).toBe(detail);+ });++ it.each(["", " \n\t "])("falls back to the signature for a blank description (%j)", (labelDescription) => {+ expect(typeColumnText(item({ labelDescription, detail: "Order" }))).toBe("Order");+ });+it("keeps a detail that already says where", () => {expect(typeColumnText({ detail: "java.util.ArrayList", additionalEdits: edit("import java.util.ArrayList;\n") }),diff --git a/src/editor/lsp/completionItems.ts b/src/editor/lsp/completionItems.tsindex 402a3ba54..a1f865e72 100644--- a/src/editor/lsp/completionItems.ts+++ b/src/editor/lsp/completionItems.ts@@ -78,6 +78,7 @@ export function withoutDuplicates(items: readonly CompletionItem[]): CompletionIit.server,it.label,it.labelDetail,+ it.labelDescription,it.detail,it.kind,it.insertText,@@ -111,9 +112,16 @@ export function importSource(item: Pick<CompletionItem, "additionalEdits">): strreturn null;}-/** Text of the right-aligned type column: the detail, with the import source when it lacks one. */-export function typeColumnText(item: Pick<CompletionItem, "detail" | "additionalEdits">): string | null {- const detail = item.detail?.replace(/\s+/g, " ") || 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`+ * (`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 },+): string | null {+ const detail = (item.labelDescription?.trim() || 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})`;diff --git a/src/workbench/perfProbe.ts b/src/workbench/perfProbe.tsindex f53a42f75..a2e8d9138 100644--- a/src/workbench/perfProbe.ts+++ b/src/workbench/perfProbe.ts@@ -60,6 +60,7 @@ function completionItem(i: number): CompletionItem {return {label,labelDetail: kind === "method" || kind === "function" ? "(id: string, options?: Options)" : null,+ labelDescription: null,detail: kind === "class" || kind === "interface" ? qualified(label) : RESULT,kind,documentation: null,