Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use asynchronous logic for help completions #1489

Merged
merged 1 commit into from
Aug 24, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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