Skip to content

Commit

Permalink
Fix completion error
Browse files Browse the repository at this point in the history
  • Loading branch information
RedCMD committed Nov 27, 2024
1 parent a3d3fce commit 1213a22
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/Providers/CompletionItemProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import { getScopes } from "../themeScopeColors";
import { UNICODE_PROPERTIES } from "../UNICODE_PROPERTIES";
import { unicode_property_data } from "../unicode_property_data";
import { SyntaxNode } from 'web-tree-sitter';
import { getPackageJSON } from '../extension';
import { getPackageJSON, sleep } from '../extension';

type CompletionItem = vscode.CompletionItem & { type?: string; };

const triggerCharacterSets: { [key: string]: string[]; } = {
schema: ['"', ':'],
schema: ['"'],
schema_new: [':'],
scopeName: ['"', '.'],
name: ['"'],
include: ['"', '#', '.', '$'],
new_scope: ['"', ' '],
scope: ['.', '$'],
scope_new: ['"', ' '],
regex: ['{', '^',/* '\\', '(', '?', '<', '\'' */],
};
export const triggerCharacters = Object.values(triggerCharacterSets).flat();
Expand Down Expand Up @@ -53,6 +54,7 @@ export const CompletionItemProvider: vscode.CompletionItemProvider = {
async provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, context: vscode.CompletionContext): Promise<vscode.CompletionList<vscode.CompletionItem>> {
// vscode.window.showInformationMessage(JSON.stringify("Completions"));
// const start = performance.now();
await sleep(50); // partially avoids race condition with reparseTextDocument()

const trees = getTrees(document);
const tree = trees.jsonTree;
Expand All @@ -61,14 +63,14 @@ export const CompletionItemProvider: vscode.CompletionItemProvider = {

const cursorQuery = `;scm
(schema (value) @schema)
(schema) @new_schema
(schema) @schema_new
(scopeName (value) @scopeName)
(name_display (value) @name)
(include (value) @include)
(name (value) @new_scope)
(name (value (scope) @scope))
(contentName (value) @new_scope)
(name (value) @scope_new)
(contentName (value (scope) @scope))
(contentName (value) @scope_new)
(regex) @regex
`;
const cursorCapture = queryNode(rootNode, cursorQuery, point);
Expand All @@ -82,16 +84,16 @@ export const CompletionItemProvider: vscode.CompletionItemProvider = {
}
}
const cursorNode = cursorCapture.node;
const cursorRange = toRange(cursorNode);
const cursorRange = cursorName.endsWith("_new") ? new vscode.Range(position, position) : toRange(cursorNode);
const completionItems: CompletionItem[] = [];

switch (cursorName) {
case 'new_schema':
case 'schema':
case 'schema_new':
const schema = "https://raw.githubusercontent.com/RedCMD/TmLanguage-Syntax-Highlighter/main/vscode.tmLanguage.schema.json";
completionItems.push({
label: cursorName == 'schema' ? schema : `"${schema}"`,
range: cursorName == 'schema' ? cursorRange : new vscode.Range(position, cursorRange.end),
range: cursorRange,
kind: vscode.CompletionItemKind.Reference,
documentation: "Schema for VSCode's TextMate JSON grammars",
sortText: ' ',
Expand Down Expand Up @@ -310,7 +312,7 @@ export const CompletionItemProvider: vscode.CompletionItemProvider = {
}
break;
case 'scope':
case 'new_scope':
case 'scope_new':
const themeScopes = await getScopes();
for (const key in themeScopes) {
const scope = themeScopes[key];
Expand All @@ -330,7 +332,7 @@ export const CompletionItemProvider: vscode.CompletionItemProvider = {
// detail: scope.foreground,
description: scope.name,
},
range: cursorName == 'scope' ? cursorRange : null,
range: cursorRange,
kind: vscode.CompletionItemKind.Color,
detail: scope.foreground || scope.background,
documentation: documentation,
Expand Down Expand Up @@ -362,7 +364,7 @@ export const CompletionItemProvider: vscode.CompletionItemProvider = {
label: scope,
description: "VSCode Debug Tokens",
},
range: cursorName == 'scope' ? cursorRange : null,
range: cursorRange,
kind: vscode.CompletionItemKind.Color,
detail: settings.foreground || settings.background,
sortText: `~${scope}`,
Expand All @@ -388,7 +390,7 @@ export const CompletionItemProvider: vscode.CompletionItemProvider = {
}
completionItems.push({
label: scope,
range: cursorName == 'scope' ? cursorRange : null,
range: cursorRange,
kind: vscode.CompletionItemKind.Text,
sortText: ` ${scope}`,
});
Expand Down

0 comments on commit 1213a22

Please sign in to comment.