[ty] Add completion kind to playground (#19251)

This commit is contained in:
Micha Reiser 2025-07-10 09:41:59 +02:00 committed by GitHub
parent 801f69a7b4
commit 5fb2fb916b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 124 additions and 1 deletions

View file

@ -309,6 +309,7 @@ impl Workspace {
Ok(completions
.into_iter()
.map(|completion| Completion {
kind: completion.kind(&self.db).map(CompletionKind::from),
name: completion.name.into(),
})
.collect())
@ -666,6 +667,69 @@ pub struct Hover {
pub struct Completion {
#[wasm_bindgen(getter_with_clone)]
pub name: String,
pub kind: Option<CompletionKind>,
}
#[wasm_bindgen]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompletionKind {
Text,
Method,
Function,
Constructor,
Field,
Variable,
Class,
Interface,
Module,
Property,
Unit,
Value,
Enum,
Keyword,
Snippet,
Color,
File,
Reference,
Folder,
EnumMember,
Constant,
Struct,
Event,
Operator,
TypeParameter,
}
impl From<ty_python_semantic::CompletionKind> for CompletionKind {
fn from(value: ty_python_semantic::CompletionKind) -> Self {
match value {
ty_python_semantic::CompletionKind::Text => Self::Text,
ty_python_semantic::CompletionKind::Method => Self::Method,
ty_python_semantic::CompletionKind::Function => Self::Function,
ty_python_semantic::CompletionKind::Constructor => Self::Constructor,
ty_python_semantic::CompletionKind::Field => Self::Field,
ty_python_semantic::CompletionKind::Variable => Self::Variable,
ty_python_semantic::CompletionKind::Class => Self::Class,
ty_python_semantic::CompletionKind::Interface => Self::Interface,
ty_python_semantic::CompletionKind::Module => Self::Module,
ty_python_semantic::CompletionKind::Property => Self::Property,
ty_python_semantic::CompletionKind::Unit => Self::Unit,
ty_python_semantic::CompletionKind::Value => Self::Value,
ty_python_semantic::CompletionKind::Enum => Self::Enum,
ty_python_semantic::CompletionKind::Keyword => Self::Keyword,
ty_python_semantic::CompletionKind::Snippet => Self::Snippet,
ty_python_semantic::CompletionKind::Color => Self::Color,
ty_python_semantic::CompletionKind::File => Self::File,
ty_python_semantic::CompletionKind::Reference => Self::Reference,
ty_python_semantic::CompletionKind::Folder => Self::Folder,
ty_python_semantic::CompletionKind::EnumMember => Self::EnumMember,
ty_python_semantic::CompletionKind::Constant => Self::Constant,
ty_python_semantic::CompletionKind::Struct => Self::Struct,
ty_python_semantic::CompletionKind::Event => Self::Event,
ty_python_semantic::CompletionKind::Operator => Self::Operator,
ty_python_semantic::CompletionKind::TypeParameter => Self::TypeParameter,
}
}
}
#[wasm_bindgen]

View file

@ -23,6 +23,7 @@ import {
SemanticToken,
Severity,
type Workspace,
CompletionKind,
} from "ty_wasm";
import { FileId, ReadonlyFiles } from "../Playground";
import { isPythonFile } from "./Files";
@ -276,7 +277,10 @@ class PlaygroundServer
suggestions: completions.map((completion, i) => ({
label: completion.name,
sortText: String(i).padStart(digitsLength, "0"),
kind: CompletionItemKind.Variable,
kind:
completion.kind == null
? CompletionItemKind.Variable
: mapCompletionKind(completion.kind),
insertText: completion.name,
// TODO(micha): It's unclear why this field is required for monaco but not VS Code.
// and omitting it works just fine? The LSP doesn't expose this information right now
@ -616,3 +620,58 @@ function generateMonacoTokens(
return { data: Uint32Array.from(result) };
}
function mapCompletionKind(kind: CompletionKind): CompletionItemKind {
switch (kind) {
case CompletionKind.Text:
return CompletionItemKind.Text;
case CompletionKind.Method:
return CompletionItemKind.Method;
case CompletionKind.Function:
return CompletionItemKind.Function;
case CompletionKind.Constructor:
return CompletionItemKind.Constructor;
case CompletionKind.Field:
return CompletionItemKind.Field;
case CompletionKind.Variable:
return CompletionItemKind.Variable;
case CompletionKind.Class:
return CompletionItemKind.Class;
case CompletionKind.Interface:
return CompletionItemKind.Interface;
case CompletionKind.Module:
return CompletionItemKind.Module;
case CompletionKind.Property:
return CompletionItemKind.Property;
case CompletionKind.Unit:
return CompletionItemKind.Unit;
case CompletionKind.Value:
return CompletionItemKind.Value;
case CompletionKind.Enum:
return CompletionItemKind.Enum;
case CompletionKind.Keyword:
return CompletionItemKind.Keyword;
case CompletionKind.Snippet:
return CompletionItemKind.Snippet;
case CompletionKind.Color:
return CompletionItemKind.Color;
case CompletionKind.File:
return CompletionItemKind.File;
case CompletionKind.Reference:
return CompletionItemKind.Reference;
case CompletionKind.Folder:
return CompletionItemKind.Folder;
case CompletionKind.EnumMember:
return CompletionItemKind.EnumMember;
case CompletionKind.Constant:
return CompletionItemKind.Constant;
case CompletionKind.Struct:
return CompletionItemKind.Struct;
case CompletionKind.Event:
return CompletionItemKind.Event;
case CompletionKind.Operator:
return CompletionItemKind.Operator;
case CompletionKind.TypeParameter:
return CompletionItemKind.TypeParameter;
}
}