Skip to content

Commit 0e1ad8d

Browse files
authored
Use asynchronous logic for help completions (#1489)
1 parent ae4d15e commit 0e1ad8d

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

src/features/HelpCompletion.ts

+25-23
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class HelpCompletionFeature implements IFeature {
5454
}
5555
}
5656

57-
public onEvent(changeEvent: TextDocumentChangeEvent): void {
57+
public async onEvent(changeEvent: TextDocumentChangeEvent): Promise<void> {
5858
if (!(changeEvent && changeEvent.contentChanges)) {
5959
this.log.writeWarning(`<${HelpCompletionFeature.name}>: ` +
6060
`Bad TextDocumentChangeEvent message: ${JSON.stringify(changeEvent)}`);
@@ -69,7 +69,8 @@ export class HelpCompletionFeature implements IFeature {
6969

7070
// todo raise an event when trigger is found, and attach complete() to the event.
7171
if (this.helpCompletionProvider.triggerFound) {
72-
this.helpCompletionProvider.complete().then(() => this.helpCompletionProvider.reset());
72+
await this.helpCompletionProvider.complete();
73+
await this.helpCompletionProvider.reset();
7374
}
7475
}
7576
}
@@ -156,36 +157,37 @@ class HelpCompletionProvider {
156157
this.triggerFinderHelpComment.reset();
157158
}
158159

159-
public complete(): Thenable<void> {
160+
public async complete(): Promise<void> {
160161
if (this.langClient === undefined) {
161162
return;
162163
}
163164

164-
const change = this.lastChangeText;
165165
const triggerStartPos = this.lastChangeRange.start;
166-
const triggerEndPos = this.lastChangeRange.end;
167166
const doc = this.lastDocument;
168167

169-
return this.langClient.sendRequest(
170-
CommentHelpRequestType,
171-
{
172-
documentUri: doc.uri.toString(),
173-
triggerPosition: triggerStartPos,
174-
blockComment: this.settings.helpCompletion === Settings.HelpCompletion.BlockComment,
175-
}).then((result) => {
176-
if (result == null || result.content == null) {
177-
return;
178-
}
168+
const result = await this.langClient.sendRequest(CommentHelpRequestType, {
169+
documentUri: doc.uri.toString(),
170+
triggerPosition: triggerStartPos,
171+
blockComment: this.settings.helpCompletion === Settings.HelpCompletion.BlockComment,
172+
});
173+
174+
if (!(result && result.content)) {
175+
return;
176+
}
177+
178+
const replaceRange = new Range(triggerStartPos.translate(0, -1), triggerStartPos.translate(0, 1));
179+
180+
// TODO add indentation level to the help content
181+
// Trim leading whitespace (used by the rule for indentation) as VSCode takes care of the indentation.
182+
// Trim the last empty line and join the strings.
183+
const lines: string[] = result.content;
184+
const text = lines
185+
.map((x) => (x as any).trimLeft())
186+
.join(this.getEOL(doc.eol));
179187

180-
// todo add indentation level to the help content
181-
const editor = window.activeTextEditor;
182-
const replaceRange = new Range(triggerStartPos.translate(0, -1), triggerStartPos.translate(0, 1));
188+
const snippetString = new SnippetString(text);
183189

184-
// Trim leading whitespace (used by the rule for indentation) as VSCode takes care of the indentation.
185-
// Trim the last empty line and join the strings.
186-
const text = result.content.map((x) => x.trimLeft()).slice(0, -1).join(this.getEOL(doc.eol));
187-
editor.insertSnippet(new SnippetString(text), replaceRange);
188-
});
190+
window.activeTextEditor.insertSnippet(snippetString, replaceRange);
189191
}
190192

191193
private getEOL(eol: EndOfLine): string {

0 commit comments

Comments
 (0)