Extracting methods

This commit is contained in:
Matt Bierner
2018-05-29 13:39:48 -07:00
parent 5efcb23d96
commit 5c46e0d66f

View File

@@ -71,14 +71,22 @@ export default class JsDocCompletionProvider implements CompletionItemProvider {
return [];
}
// Only show the JSdoc completion when the everything before the cursor is whitespace
// or could be the opening of a comment
const line = document.lineAt(position.line).text;
const prefix = line.slice(0, position.character);
if (prefix.match(/^\s*$|\/\*\*\s*$|^\s*\/\*\*+\s*$/) === null) {
if (!this.isValidCursorPosition(document, position)) {
return [];
}
if (!await this.isCommentableLocation(file, position, token)) {
return [];
}
return [new JsDocCompletionItem(document, position, enableJsDocCompletions)];
}
private async isCommentableLocation(
file: string,
position: Position,
token: CancellationToken
): Promise<boolean> {
const args: Proto.FileRequestArgs = {
file
};
@@ -86,8 +94,9 @@ export default class JsDocCompletionProvider implements CompletionItemProvider {
this.client.execute('navtree', args, token),
new Promise<Proto.NavTreeResponse>((resolve) => setTimeout(resolve, 250))
]);
if (!response || !response.body) {
return [];
return false;
}
const body = response.body;
@@ -104,11 +113,15 @@ export default class JsDocCompletionProvider implements CompletionItemProvider {
return tree.childItems ? tree.childItems.some(matchesPosition) : false;
}
if (!matchesPosition(body)) {
return [];
}
return matchesPosition(body);
}
return [new JsDocCompletionItem(document, position, enableJsDocCompletions)];
private isValidCursorPosition(document: TextDocument, position: Position): boolean {
// Only show the JSdoc completion when the everything before the cursor is whitespace
// or could be the opening of a comment
const line = document.lineAt(position.line).text;
const prefix = line.slice(0, position.character);
return prefix.match(/^\s*$|\/\*\*\s*$|^\s*\/\*\*+\s*$/) !== null;
}
public resolveCompletionItem(item: CompletionItem, _token: CancellationToken) {