Skip to content

Commit

Permalink
bump: update highlight/completion syntax to v0.5.0+ (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
amyXia1994 authored Aug 1, 2023
1 parent 442a44b commit ce105a2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 135 deletions.
189 changes: 56 additions & 133 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,141 +86,64 @@ function startLanguageServerWith(language_server_path: string) {
}

function autoCompletionItems(): vscode.CompletionItem[] {
// a completion item that inserts its text as snippet,
// the `insertText`-property is a `SnippetString` which will be
// honored by the editor.
const importAsCompletion = new vscode.CompletionItem('import as');
importAsCompletion.insertText = new vscode.SnippetString('import ${1} as ');

const mixinCompletion = new vscode.CompletionItem('mixin');
mixinCompletion.insertText = new vscode.SnippetString('mixin [\n ${1}\n]');

const checkCompletion = new vscode.CompletionItem('check');
checkCompletion.insertText = new vscode.SnippetString('check:\n ');

const schemaCompletion = new vscode.CompletionItem('schema');
schemaCompletion.insertText = new vscode.SnippetString('schema ${1}:\n ');
// schemaCompletion.command = { command: 'editor.action.triggerSuggest', title: 'Re-trigger completions...' };

const schemaRelaxedCompletion = new vscode.CompletionItem('schema relaxed');
schemaRelaxedCompletion.insertText = new vscode.SnippetString('schema relaxed ${1}:\n ');
// schemaRelaxedCompletion.command = { command: 'editor.action.triggerSuggest', title: 'Re-trigger completions...' };

const strValueCompletion = new vscode.CompletionItem('str = ${value}');
strValueCompletion.insertText = new vscode.SnippetString('str = "${1}"');
// strValueCompletion.command = { command: 'editor.action.triggerSuggest', title: 'Re-trigger completions...' };

const boolTrueCompletion = new vscode.CompletionItem('bool = True');
boolTrueCompletion.insertText = new vscode.SnippetString('bool = True\n');
// boolTrueCompletion.command = { command: 'editor.action.triggerSuggest', title: 'Re-trigger completions...' };

const boolFalseCompletion = new vscode.CompletionItem('bool = False');
boolFalseCompletion.insertText = new vscode.SnippetString('bool = False\n');
// boolFalseCompletion.command = { command: 'editor.action.triggerSuggest', title: 'Re-trigger completions...' };

const intValueCompletion = new vscode.CompletionItem('int = ${value}');
intValueCompletion.insertText = new vscode.SnippetString('int = ${1}');
// intValueCompletion.command = { command: 'editor.action.triggerSuggest', title: 'Re-trigger completions...' };

const floatValueCompletion = new vscode.CompletionItem('float = ${value}');
floatValueCompletion.insertText = new vscode.SnippetString('float = ${1}');
// floatValueCompletion.command = { command: 'editor.action.triggerSuggest', title: 'Re-trigger completions...' };


// a completion item that retriggers IntelliSense when being accepted, Also, the `insertText` is set so that
// a space is inserted after `import`
const importCompletion = new vscode.CompletionItem('import');
importCompletion.kind = vscode.CompletionItemKind.Keyword;
importCompletion.insertText = 'import ';

const assertCompletion = new vscode.CompletionItem('assert');
assertCompletion.kind = vscode.CompletionItemKind.Keyword;
assertCompletion.insertText = 'assert ';

const ifCompletion = new vscode.CompletionItem('if');
ifCompletion.kind = vscode.CompletionItemKind.Keyword;
ifCompletion.insertText = 'if ';

const finalCompletion = new vscode.CompletionItem('final');
finalCompletion.kind = vscode.CompletionItemKind.Keyword;
finalCompletion.insertText = 'final ';

const orCompletion = new vscode.CompletionItem('or');
orCompletion.kind = vscode.CompletionItemKind.Keyword;
orCompletion.insertText = 'or ';

const andCompletion = new vscode.CompletionItem('and');
andCompletion.kind = vscode.CompletionItemKind.Keyword;
andCompletion.insertText = 'and ';

const notCompletion = new vscode.CompletionItem('not');
notCompletion.kind = vscode.CompletionItemKind.Keyword;
notCompletion.insertText = 'not ';

const inCompletion = new vscode.CompletionItem('in');
inCompletion.kind = vscode.CompletionItemKind.Keyword;
inCompletion.insertText = 'in ';

const isCompletion = new vscode.CompletionItem('is');
isCompletion.kind = vscode.CompletionItemKind.Keyword;
isCompletion.insertText = 'is ';

const strCompletion = new vscode.CompletionItem('str');
strCompletion.kind = vscode.CompletionItemKind.Keyword;
strCompletion.insertText = 'str\n';

const boolCompletion = new vscode.CompletionItem('bool');
boolCompletion.kind = vscode.CompletionItemKind.Keyword;
boolCompletion.insertText = 'bool\n';

const floatCompletion = new vscode.CompletionItem('float');
floatCompletion.kind = vscode.CompletionItemKind.Keyword;
floatCompletion.insertText = 'str\n';

const intCompletion = new vscode.CompletionItem('int');
intCompletion.kind = vscode.CompletionItemKind.Keyword;
intCompletion.insertText = 'int\n';

const trueCompletion = new vscode.CompletionItem('True');
trueCompletion.kind = vscode.CompletionItemKind.Keyword;
trueCompletion.insertText = 'True';

const falseCompletion = new vscode.CompletionItem('False');
falseCompletion.kind = vscode.CompletionItemKind.Keyword;
falseCompletion.insertText = 'False';

const noneCompletion = new vscode.CompletionItem('None');
noneCompletion.kind = vscode.CompletionItemKind.Keyword;
noneCompletion.insertText = 'None';
const snippetCompletions: {label: string, text: string}[] = [
{label: 'import as', text: 'import ${1:path} as ${2:alias}'},
{label: 'check statement', text: 'check:\n '},
{label: 'schema def', text: 'schema ${1:Name}:\n '},
{label: 'schema def for protocol', text: 'schema ${1:Name} for ${2:Name}Protocol:\n '},
{label: 'protocol def', text: 'protocol ${1:Name}Protocol:\n '},
{label: 'rule def', text: 'rule ${1:Name}Rule:\n '},
{label: 'mixin def', text: 'mixin ${1:Name}Mixin for ${2:Name}Protocol:\n '},
{label: 'mixin list', text: 'mixin [\n ${1:Name}\n]'},
{label: 'str = ${value}', text: 'str = "${1:value}"'},
{label: 'bool = True', text: 'bool = True\n'},
{label: 'bool = False', text: 'bool = False\n'},
{label: 'int = ${value}', text: 'int = ${1:value}'},
{label: 'float = ${value}', text: 'float = ${1:value}'},
];

const KeywordCompletions: {label: string, text: string}[] = [
{label: 'import', text: 'import '},
{label: 'assert', text: 'assert '},
{label: 'if', text: 'if '},
{label: 'else', text: 'else '},
{label: 'elif', text: 'elif '},
{label: 'for', text: 'for '},
{label: 'or', text: 'or '},
{label: 'and', text: 'and '},
{label: 'not', text: 'not '},
{label: 'in', text: 'in '},
{label: 'is', text: 'is '},
{label: 'all', text: 'all'},
{label: 'any', text: 'any'},
{label: 'map', text: 'map'},
{label: 'filter', text: 'filter'},
{label: 'lambda', text: 'lambda '},
{label: 'str', text: 'str'},
{label: 'bool', text: 'bool'},
{label: 'float', text: 'float'},
{label: 'int', text: 'int'},
{label: 'True', text: 'True'},
{label: 'False', text: 'False'},
{label: 'None', text: 'None'},
{label: 'Undefined', text: 'Undefined'},
];

// return all completion items as array
return [
importAsCompletion,
mixinCompletion,
checkCompletion,
schemaRelaxedCompletion,
strValueCompletion,
boolTrueCompletion,
boolFalseCompletion,
intValueCompletion,
floatValueCompletion,
importCompletion,
schemaCompletion,
assertCompletion,
ifCompletion,
finalCompletion,
orCompletion,
andCompletion,
notCompletion,
inCompletion,
isCompletion,
strCompletion,
boolCompletion,
floatCompletion,
intCompletion,
trueCompletion,
falseCompletion,
noneCompletion
...snippetCompletions.map(({label, text}) => createSnippetCompletion(label, vscode.CompletionItemKind.Snippet, text)),
...KeywordCompletions.map(({label, text}) => createSnippetCompletion(label, vscode.CompletionItemKind.Keyword, text)),
];
}

function createSnippetCompletion(label: string, kind: vscode.CompletionItemKind, insertText: string): vscode.CompletionItem {
const completion = new vscode.CompletionItem(label);
completion.kind = kind;
switch (kind) {
case vscode.CompletionItemKind.Snippet:
completion.insertText = new vscode.SnippetString(insertText);
return completion;
default:
completion.insertText = insertText;
return completion;
}
}
4 changes: 2 additions & 2 deletions syntaxes/KCL.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"keywords": {
"patterns": [{
"name": "keyword.control.KCL",
"match": "\\b(as|assert|if|elif|else|lambda|for|import|schema|protocol|rule|mixin|relaxed|check|and|in|is|not|or|all|any|map|filter|type)\\b"
"match": "\\b(as|assert|if|elif|else|lambda|for|import|schema|protocol|rule|mixin|check|and|in|is|not|or|all|any|map|filter)\\b"
}]
},
"types": {
Expand All @@ -40,7 +40,7 @@
"builtin-literals": {
"patterns": [{
"name": "constant.language.KCL",
"match": "\\b(True|False|None)\\b"
"match": "\\b(True|False|None|Undefined)\\b"
}]
},
"single-quoted-strings": {
Expand Down

0 comments on commit ce105a2

Please sign in to comment.