From ed47a6bc317e04b859e8d1e782fb502fef3ad101 Mon Sep 17 00:00:00 2001 From: z80 Date: Sat, 14 Dec 2024 22:18:07 -0500 Subject: [PATCH] fix: find top-level node when adding new line also add test for struct member completion --- tests/test_completions.py | 35 +++++++++++++++++++++++++++++++++++ vyper_lsp/ast.py | 9 ++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/tests/test_completions.py b/tests/test_completions.py index 1138987..020c4be 100644 --- a/tests/test_completions.py +++ b/tests/test_completions.py @@ -41,6 +41,41 @@ def baz(): assert "foo" in [c.label for c in completions.items] +def test_completions_struct_members(ast): + src = """ +struct Foo: + bar: uint256 + baz: uint256 + +@internal +def foo(): + return + +@external +def bar(): + self.foo() + +@external +def baz(): + g: Foo = Foo(bar=12, baz=13) +""" + ast.build_ast(src) + + src += """ + g. +""" + doc = Document(uri="", source=src) + pos = Position(line=17, character=6) + context = CompletionContext(trigger_character=".", trigger_kind=2) + params = CompletionParams( + text_document=TextDocumentIdentifier(uri=doc.uri), position=pos, context=context + ) + + analyzer = CompletionHandler(ast) + completions = analyzer._get_completions_in_doc(doc, params) + assert len(completions.items) == 2 + + def test_completions_enum_variant(ast): src = """ flag Foo: diff --git a/vyper_lsp/ast.py b/vyper_lsp/ast.py index 3173f41..0c64dc0 100644 --- a/vyper_lsp/ast.py +++ b/vyper_lsp/ast.py @@ -334,10 +334,17 @@ def find_nodes_referencing_struct(self, struct: str): return return_nodes def find_top_level_node_at_pos(self, pos: Position) -> Optional[VyperNode]: - for node in self.get_top_level_nodes(): + nodes = self.get_top_level_nodes() + for node in nodes: if node.lineno <= pos.line and pos.line <= node.end_lineno: return node + # return node with highest lineno if no node found + if nodes: + # sort + nodes.sort(key=lambda x: x.lineno, reverse=True) + return nodes[0] + return None def find_nodes_referencing_symbol(self, symbol: str):