From 5008551247b35f71d96c8ff2f7b0c81096801b8e Mon Sep 17 00:00:00 2001 From: dinhlongnguyen Date: Tue, 15 Nov 2022 23:37:43 +0700 Subject: [PATCH] Variable autocomplete (#81) (#82) * Variable autocomplete * update per fred --- gui/src/gui/completion.ts | 17 +++++++++++++---- gui/src/gui/context.ts | 4 +++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/gui/src/gui/completion.ts b/gui/src/gui/completion.ts index 33a91af..7b98dec 100644 --- a/gui/src/gui/completion.ts +++ b/gui/src/gui/completion.ts @@ -1,10 +1,12 @@ import { CancellationToken, + commands, CompletionItem, CompletionItemKind, CompletionItemProvider, MarkdownString, Position, + SymbolInformation, TextDocument, } from "vscode"; import { defaultElementList, defaultElementProperties } from "./constant"; @@ -12,15 +14,22 @@ import { defaultElementList, defaultElementProperties } from "./constant"; const RE_LINE = /<(([\|]{1})([^\|]*)){1,2}/; export class GuiCompletionItemProvider implements CompletionItemProvider { - public provideCompletionItems( + public async provideCompletionItems( document: TextDocument, position: Position, token: CancellationToken - ): Thenable { + ): Promise { const line = document.lineAt(position).text; const linePrefix = line.slice(0, position.character); + if ((document.fileName.endsWith(".py") || document.languageId === "python") && linePrefix.endsWith("{")) { + const symbols = (await commands.executeCommand( + "vscode.executeDocumentSymbolProvider", + document.uri + )) as SymbolInformation[]; + return symbols.map((v) => new CompletionItem(v.name, CompletionItemKind.Property)); + } let completionList: CompletionItem[] = []; - let foundElements = defaultElementList.reduce((p: string[], c: string) => { + const foundElements = defaultElementList.reduce((p: string[], c: string) => { linePrefix.includes(`|${c}`) && p.push(c); return p; }, []); @@ -39,6 +48,6 @@ export class GuiCompletionItemProvider implements CompletionItemProvider { return completionItem; }); } - return Promise.resolve(completionList); + return completionList; } } diff --git a/gui/src/gui/context.ts b/gui/src/gui/context.ts index 848fe46..20cbac5 100644 --- a/gui/src/gui/context.ts +++ b/gui/src/gui/context.ts @@ -44,7 +44,9 @@ export class GuiContext { context.subscriptions.push( languages.registerCompletionItemProvider(markdownFilter, new GuiCompletionItemProvider(), "|") ); - context.subscriptions.push(languages.registerCompletionItemProvider(pythonFilter, new GuiCompletionItemProvider(), "|")); + context.subscriptions.push( + languages.registerCompletionItemProvider(pythonFilter, new GuiCompletionItemProvider(), "|", "{") + ); } private registerGenerateElementCommand(context: ExtensionContext): void {