Skip to content

Commit 5fb2fb9

Browse files
authored
[ty] Add completion kind to playground (#19251)
1 parent 801f69a commit 5fb2fb9

File tree

2 files changed

+124
-1
lines changed

2 files changed

+124
-1
lines changed

crates/ty_wasm/src/lib.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ impl Workspace {
309309
Ok(completions
310310
.into_iter()
311311
.map(|completion| Completion {
312+
kind: completion.kind(&self.db).map(CompletionKind::from),
312313
name: completion.name.into(),
313314
})
314315
.collect())
@@ -666,6 +667,69 @@ pub struct Hover {
666667
pub struct Completion {
667668
#[wasm_bindgen(getter_with_clone)]
668669
pub name: String,
670+
pub kind: Option<CompletionKind>,
671+
}
672+
673+
#[wasm_bindgen]
674+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
675+
pub enum CompletionKind {
676+
Text,
677+
Method,
678+
Function,
679+
Constructor,
680+
Field,
681+
Variable,
682+
Class,
683+
Interface,
684+
Module,
685+
Property,
686+
Unit,
687+
Value,
688+
Enum,
689+
Keyword,
690+
Snippet,
691+
Color,
692+
File,
693+
Reference,
694+
Folder,
695+
EnumMember,
696+
Constant,
697+
Struct,
698+
Event,
699+
Operator,
700+
TypeParameter,
701+
}
702+
703+
impl From<ty_python_semantic::CompletionKind> for CompletionKind {
704+
fn from(value: ty_python_semantic::CompletionKind) -> Self {
705+
match value {
706+
ty_python_semantic::CompletionKind::Text => Self::Text,
707+
ty_python_semantic::CompletionKind::Method => Self::Method,
708+
ty_python_semantic::CompletionKind::Function => Self::Function,
709+
ty_python_semantic::CompletionKind::Constructor => Self::Constructor,
710+
ty_python_semantic::CompletionKind::Field => Self::Field,
711+
ty_python_semantic::CompletionKind::Variable => Self::Variable,
712+
ty_python_semantic::CompletionKind::Class => Self::Class,
713+
ty_python_semantic::CompletionKind::Interface => Self::Interface,
714+
ty_python_semantic::CompletionKind::Module => Self::Module,
715+
ty_python_semantic::CompletionKind::Property => Self::Property,
716+
ty_python_semantic::CompletionKind::Unit => Self::Unit,
717+
ty_python_semantic::CompletionKind::Value => Self::Value,
718+
ty_python_semantic::CompletionKind::Enum => Self::Enum,
719+
ty_python_semantic::CompletionKind::Keyword => Self::Keyword,
720+
ty_python_semantic::CompletionKind::Snippet => Self::Snippet,
721+
ty_python_semantic::CompletionKind::Color => Self::Color,
722+
ty_python_semantic::CompletionKind::File => Self::File,
723+
ty_python_semantic::CompletionKind::Reference => Self::Reference,
724+
ty_python_semantic::CompletionKind::Folder => Self::Folder,
725+
ty_python_semantic::CompletionKind::EnumMember => Self::EnumMember,
726+
ty_python_semantic::CompletionKind::Constant => Self::Constant,
727+
ty_python_semantic::CompletionKind::Struct => Self::Struct,
728+
ty_python_semantic::CompletionKind::Event => Self::Event,
729+
ty_python_semantic::CompletionKind::Operator => Self::Operator,
730+
ty_python_semantic::CompletionKind::TypeParameter => Self::TypeParameter,
731+
}
732+
}
669733
}
670734

671735
#[wasm_bindgen]

playground/ty/src/Editor/Editor.tsx

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
SemanticToken,
2424
Severity,
2525
type Workspace,
26+
CompletionKind,
2627
} from "ty_wasm";
2728
import { FileId, ReadonlyFiles } from "../Playground";
2829
import { isPythonFile } from "./Files";
@@ -276,7 +277,10 @@ class PlaygroundServer
276277
suggestions: completions.map((completion, i) => ({
277278
label: completion.name,
278279
sortText: String(i).padStart(digitsLength, "0"),
279-
kind: CompletionItemKind.Variable,
280+
kind:
281+
completion.kind == null
282+
? CompletionItemKind.Variable
283+
: mapCompletionKind(completion.kind),
280284
insertText: completion.name,
281285
// TODO(micha): It's unclear why this field is required for monaco but not VS Code.
282286
// and omitting it works just fine? The LSP doesn't expose this information right now
@@ -616,3 +620,58 @@ function generateMonacoTokens(
616620

617621
return { data: Uint32Array.from(result) };
618622
}
623+
624+
function mapCompletionKind(kind: CompletionKind): CompletionItemKind {
625+
switch (kind) {
626+
case CompletionKind.Text:
627+
return CompletionItemKind.Text;
628+
case CompletionKind.Method:
629+
return CompletionItemKind.Method;
630+
case CompletionKind.Function:
631+
return CompletionItemKind.Function;
632+
case CompletionKind.Constructor:
633+
return CompletionItemKind.Constructor;
634+
case CompletionKind.Field:
635+
return CompletionItemKind.Field;
636+
case CompletionKind.Variable:
637+
return CompletionItemKind.Variable;
638+
case CompletionKind.Class:
639+
return CompletionItemKind.Class;
640+
case CompletionKind.Interface:
641+
return CompletionItemKind.Interface;
642+
case CompletionKind.Module:
643+
return CompletionItemKind.Module;
644+
case CompletionKind.Property:
645+
return CompletionItemKind.Property;
646+
case CompletionKind.Unit:
647+
return CompletionItemKind.Unit;
648+
case CompletionKind.Value:
649+
return CompletionItemKind.Value;
650+
case CompletionKind.Enum:
651+
return CompletionItemKind.Enum;
652+
case CompletionKind.Keyword:
653+
return CompletionItemKind.Keyword;
654+
case CompletionKind.Snippet:
655+
return CompletionItemKind.Snippet;
656+
case CompletionKind.Color:
657+
return CompletionItemKind.Color;
658+
case CompletionKind.File:
659+
return CompletionItemKind.File;
660+
case CompletionKind.Reference:
661+
return CompletionItemKind.Reference;
662+
case CompletionKind.Folder:
663+
return CompletionItemKind.Folder;
664+
case CompletionKind.EnumMember:
665+
return CompletionItemKind.EnumMember;
666+
case CompletionKind.Constant:
667+
return CompletionItemKind.Constant;
668+
case CompletionKind.Struct:
669+
return CompletionItemKind.Struct;
670+
case CompletionKind.Event:
671+
return CompletionItemKind.Event;
672+
case CompletionKind.Operator:
673+
return CompletionItemKind.Operator;
674+
case CompletionKind.TypeParameter:
675+
return CompletionItemKind.TypeParameter;
676+
}
677+
}

0 commit comments

Comments
 (0)