Skip to content

Commit

Permalink
feat: add simple auto-complete for keywords and built-in types
Browse files Browse the repository at this point in the history
  • Loading branch information
badeend committed Sep 3, 2023
1 parent aa65be0 commit 7bb2014
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,71 @@
import * as vscode from 'vscode';

const builtinTypes = [
"u8",
"u16",
"u32",
"u64",
"s8",
"s16",
"s32",
"s64",
"float32",
"float64",
"char",
"bool",
"string",
"tuple",
"list",
"option",
"result",
"borrow",
];

const keywords = [
"_",
"as",
"constructor",
"enum",
"export",
"flags",
"func",
"import",
"include",
"interface",
"package",
"record",
"resource",
"static",
"type",
"union",
"use",
"variant",
"with",
"world",
];

const staticCompletions = new vscode.CompletionList([
...keywords.map(keyword => new vscode.CompletionItem(keyword, vscode.CompletionItemKind.Keyword)),
...builtinTypes.map(type => new vscode.CompletionItem(type, vscode.CompletionItemKind.Struct)),
], /* isIncomplete: */ true);

export function activate(context: vscode.ExtensionContext) {

// VSCode does not support having both its built-in suggestions _and_ a custom completion provider.
// But, without a proper LSP, we do want both!
// Registering the provider directly on "wit" would disable VSCode's built-in suggestions.
// The workaround is to register it for all languages (`*`) and then dynamically check the document type.
const provider = vscode.languages.registerCompletionItemProvider('*', {
provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, context: vscode.CompletionContext) {
if (document.languageId !== 'wit') {
return;
}

return staticCompletions;
}
});

context.subscriptions.push(provider);
}

export function deactivate() {
Expand Down

0 comments on commit 7bb2014

Please sign in to comment.