[ty] Make auto-import work in the playground

It turned out that we weren't quite funneling the new completion data
all the way through.

I followed the docs for [`CompletionItem`] for the Monaco editor. It's
similar, but not identical, to the LSP protocol specification.

[`CompletionItem`]: https://microsoft.github.io/monaco-editor/typedoc/interfaces/languages.CompletionItem.html
This commit is contained in:
Andrew Gallant 2025-09-19 10:59:43 -04:00 committed by Andrew Gallant
parent 8eeca023d6
commit 3bf4dae452
2 changed files with 20 additions and 6 deletions

View File

@ -439,7 +439,7 @@ impl Workspace {
name: comp.name.into(), name: comp.name.into(),
kind, kind,
detail: type_display, detail: type_display,
description: comp.module_name.map(ToString::to_string), module_name: comp.module_name.map(ToString::to_string),
insert_text: comp.insert.map(String::from), insert_text: comp.insert.map(String::from),
additional_text_edits: import_edit.map(|edit| vec![edit]), additional_text_edits: import_edit.map(|edit| vec![edit]),
documentation: comp documentation: comp
@ -942,7 +942,7 @@ pub struct Completion {
#[wasm_bindgen(getter_with_clone)] #[wasm_bindgen(getter_with_clone)]
pub detail: Option<String>, pub detail: Option<String>,
#[wasm_bindgen(getter_with_clone)] #[wasm_bindgen(getter_with_clone)]
pub description: Option<String>, pub module_name: Option<String>,
} }
#[wasm_bindgen] #[wasm_bindgen]
@ -1010,9 +1010,9 @@ impl From<ty_ide::CompletionKind> for CompletionKind {
#[wasm_bindgen] #[wasm_bindgen]
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct TextEdit { pub struct TextEdit {
range: Range, pub range: Range,
#[wasm_bindgen(getter_with_clone)] #[wasm_bindgen(getter_with_clone)]
new_text: String, pub new_text: String,
} }
#[wasm_bindgen] #[wasm_bindgen]

View File

@ -28,6 +28,7 @@ import {
DocumentHighlight, DocumentHighlight,
DocumentHighlightKind, DocumentHighlightKind,
InlayHintKind, InlayHintKind,
TextEdit,
} from "ty_wasm"; } from "ty_wasm";
import { FileId, ReadonlyFiles } from "../Playground"; import { FileId, ReadonlyFiles } from "../Playground";
import { isPythonFile } from "./Files"; import { isPythonFile } from "./Files";
@ -313,13 +314,26 @@ class PlaygroundServer
return { return {
suggestions: completions.map((completion, i) => ({ suggestions: completions.map((completion, i) => ({
label: completion.name, label: {
label: completion.name,
detail:
completion.module_name == null
? undefined
: ` (import ${completion.module_name})`,
description: completion.detail ?? undefined,
},
sortText: String(i).padStart(digitsLength, "0"), sortText: String(i).padStart(digitsLength, "0"),
kind: kind:
completion.kind == null completion.kind == null
? CompletionItemKind.Variable ? CompletionItemKind.Variable
: mapCompletionKind(completion.kind), : mapCompletionKind(completion.kind),
insertText: completion.name, insertText: completion.insert_text ?? completion.name,
additionalTextEdits: completion.additional_text_edits?.map(
(edit: TextEdit) => ({
range: tyRangeToMonacoRange(edit.range),
text: edit.new_text,
}),
),
documentation: completion.documentation, documentation: completion.documentation,
detail: completion.detail, detail: completion.detail,
// TODO(micha): It's unclear why this field is required for monaco but not VS Code. // TODO(micha): It's unclear why this field is required for monaco but not VS Code.