From b2b50c62889b4edc5920c83cb913b8c12051ebd2 Mon Sep 17 00:00:00 2001 From: James Westman Date: Sat, 30 Oct 2021 21:25:15 -0500 Subject: [PATCH] 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. --- gtkblueprinttool/completions.py | 6 ++++-- gtkblueprinttool/lsp_utils.py | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/gtkblueprinttool/completions.py b/gtkblueprinttool/completions.py index 34f6336..825b21f 100644 --- a/gtkblueprinttool/completions.py +++ b/gtkblueprinttool/completions.py @@ -41,6 +41,10 @@ def complete(ast_node: ast.AstNode, tokens: T.List[Token], idx: int) -> T.Iterat if token.start < idx <= token.end: 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 while len(prev_tokens) < 5 and token_idx >= 0: token = tokens[token_idx] @@ -80,8 +84,6 @@ class Completer: if not any_match: return - print("completions", match_variables, self.func) - if self.ast_type is not None: while ast_node is not None and not isinstance(ast_node, self.ast_type): ast_node = ast_node.parent diff --git a/gtkblueprinttool/lsp_utils.py b/gtkblueprinttool/lsp_utils.py index 1b4648e..d2b83b9 100644 --- a/gtkblueprinttool/lsp_utils.py +++ b/gtkblueprinttool/lsp_utils.py @@ -83,7 +83,7 @@ class Completion: insert_text = self.snippet insert_text_format = InsertTextFormat.Snippet - return { + result = { "label": self.label, "kind": self.kind, "tags": [CompletionItemTag.Deprecated] if self.deprecated else None, @@ -93,3 +93,4 @@ class Completion: "insertText": insert_text, "insertTextFormat": insert_text_format, } + return { k: v for k, v in result.items() if v is not None }