Improve completions

If the completion cursor is in the middle of an identifier, start the
completion matching before that token. GNOME Builder does this before
sending us a cursor position, but VS Code does not.
This commit is contained in:
James Westman 2021-10-30 21:25:15 -05:00
parent 54237d7976
commit b2b50c6288
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
2 changed files with 6 additions and 3 deletions

View file

@ -41,6 +41,10 @@ def complete(ast_node: ast.AstNode, tokens: T.List[Token], idx: int) -> T.Iterat
if token.start < idx <= token.end: if token.start < idx <= token.end:
token_idx = i token_idx = i
# if the current token is an identifier, move to the token before it
if tokens[token_idx].type == TokenType.IDENT:
token_idx -= 1
# collect the 5 previous non-skipped tokens # collect the 5 previous non-skipped tokens
while len(prev_tokens) < 5 and token_idx >= 0: while len(prev_tokens) < 5 and token_idx >= 0:
token = tokens[token_idx] token = tokens[token_idx]
@ -80,8 +84,6 @@ class Completer:
if not any_match: if not any_match:
return return
print("completions", match_variables, self.func)
if self.ast_type is not None: if self.ast_type is not None:
while ast_node is not None and not isinstance(ast_node, self.ast_type): while ast_node is not None and not isinstance(ast_node, self.ast_type):
ast_node = ast_node.parent ast_node = ast_node.parent

View file

@ -83,7 +83,7 @@ class Completion:
insert_text = self.snippet insert_text = self.snippet
insert_text_format = InsertTextFormat.Snippet insert_text_format = InsertTextFormat.Snippet
return { result = {
"label": self.label, "label": self.label,
"kind": self.kind, "kind": self.kind,
"tags": [CompletionItemTag.Deprecated] if self.deprecated else None, "tags": [CompletionItemTag.Deprecated] if self.deprecated else None,
@ -93,3 +93,4 @@ class Completion:
"insertText": insert_text, "insertText": insert_text,
"insertTextFormat": insert_text_format, "insertTextFormat": insert_text_format,
} }
return { k: v for k, v in result.items() if v is not None }