Skip to content

Commit

Permalink
Use asynchronous logic for help completions (#1489)
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmholt authored Aug 24, 2018
1 parent ae4d15e commit 0e1ad8d
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions src/features/HelpCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class HelpCompletionFeature implements IFeature {
}
}

public onEvent(changeEvent: TextDocumentChangeEvent): void {
public async onEvent(changeEvent: TextDocumentChangeEvent): Promise<void> {
if (!(changeEvent && changeEvent.contentChanges)) {
this.log.writeWarning(`<${HelpCompletionFeature.name}>: ` +
`Bad TextDocumentChangeEvent message: ${JSON.stringify(changeEvent)}`);
Expand All @@ -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();
}
}
}
Expand Down Expand Up @@ -156,36 +157,37 @@ class HelpCompletionProvider {
this.triggerFinderHelpComment.reset();
}

public complete(): Thenable<void> {
public async complete(): Promise<void> {
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 {
Expand Down

0 comments on commit 0e1ad8d

Please sign in to comment.