From 0e1ad8d02cb8216e58268f8448ce449ed9cfb07d Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Fri, 24 Aug 2018 10:30:09 -0700 Subject: [PATCH] Use asynchronous logic for help completions (#1489) --- src/features/HelpCompletion.ts | 48 ++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/features/HelpCompletion.ts b/src/features/HelpCompletion.ts index 0892c8552c..5dcfb3bcd8 100644 --- a/src/features/HelpCompletion.ts +++ b/src/features/HelpCompletion.ts @@ -54,7 +54,7 @@ export class HelpCompletionFeature implements IFeature { } } - public onEvent(changeEvent: TextDocumentChangeEvent): void { + public async onEvent(changeEvent: TextDocumentChangeEvent): Promise { if (!(changeEvent && changeEvent.contentChanges)) { this.log.writeWarning(`<${HelpCompletionFeature.name}>: ` + `Bad TextDocumentChangeEvent message: ${JSON.stringify(changeEvent)}`); @@ -69,7 +69,8 @@ export class HelpCompletionFeature implements IFeature { // todo raise an event when trigger is found, and attach complete() to the event. if (this.helpCompletionProvider.triggerFound) { - this.helpCompletionProvider.complete().then(() => this.helpCompletionProvider.reset()); + await this.helpCompletionProvider.complete(); + await this.helpCompletionProvider.reset(); } } } @@ -156,36 +157,37 @@ class HelpCompletionProvider { this.triggerFinderHelpComment.reset(); } - public complete(): Thenable { + public async complete(): Promise { if (this.langClient === undefined) { return; } - const change = this.lastChangeText; const triggerStartPos = this.lastChangeRange.start; - const triggerEndPos = this.lastChangeRange.end; const doc = this.lastDocument; - return this.langClient.sendRequest( - CommentHelpRequestType, - { - documentUri: doc.uri.toString(), - triggerPosition: triggerStartPos, - blockComment: this.settings.helpCompletion === Settings.HelpCompletion.BlockComment, - }).then((result) => { - if (result == null || result.content == null) { - return; - } + const result = await this.langClient.sendRequest(CommentHelpRequestType, { + documentUri: doc.uri.toString(), + triggerPosition: triggerStartPos, + blockComment: this.settings.helpCompletion === Settings.HelpCompletion.BlockComment, + }); + + if (!(result && result.content)) { + return; + } + + const replaceRange = new Range(triggerStartPos.translate(0, -1), triggerStartPos.translate(0, 1)); + + // TODO add indentation level to the help content + // Trim leading whitespace (used by the rule for indentation) as VSCode takes care of the indentation. + // Trim the last empty line and join the strings. + const lines: string[] = result.content; + const text = lines + .map((x) => (x as any).trimLeft()) + .join(this.getEOL(doc.eol)); - // todo add indentation level to the help content - const editor = window.activeTextEditor; - const replaceRange = new Range(triggerStartPos.translate(0, -1), triggerStartPos.translate(0, 1)); + const snippetString = new SnippetString(text); - // Trim leading whitespace (used by the rule for indentation) as VSCode takes care of the indentation. - // Trim the last empty line and join the strings. - const text = result.content.map((x) => x.trimLeft()).slice(0, -1).join(this.getEOL(doc.eol)); - editor.insertSnippet(new SnippetString(text), replaceRange); - }); + window.activeTextEditor.insertSnippet(snippetString, replaceRange); } private getEOL(eol: EndOfLine): string {