Skip to content

Commit ed47a6b

Browse files
committed
fix: find top-level node when adding new line
also add test for struct member completion
1 parent b102a14 commit ed47a6b

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

tests/test_completions.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,41 @@ def baz():
4141
assert "foo" in [c.label for c in completions.items]
4242

4343

44+
def test_completions_struct_members(ast):
45+
src = """
46+
struct Foo:
47+
bar: uint256
48+
baz: uint256
49+
50+
@internal
51+
def foo():
52+
return
53+
54+
@external
55+
def bar():
56+
self.foo()
57+
58+
@external
59+
def baz():
60+
g: Foo = Foo(bar=12, baz=13)
61+
"""
62+
ast.build_ast(src)
63+
64+
src += """
65+
g.
66+
"""
67+
doc = Document(uri="<inline source code>", source=src)
68+
pos = Position(line=17, character=6)
69+
context = CompletionContext(trigger_character=".", trigger_kind=2)
70+
params = CompletionParams(
71+
text_document=TextDocumentIdentifier(uri=doc.uri), position=pos, context=context
72+
)
73+
74+
analyzer = CompletionHandler(ast)
75+
completions = analyzer._get_completions_in_doc(doc, params)
76+
assert len(completions.items) == 2
77+
78+
4479
def test_completions_enum_variant(ast):
4580
src = """
4681
flag Foo:

vyper_lsp/ast.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,17 @@ def find_nodes_referencing_struct(self, struct: str):
334334
return return_nodes
335335

336336
def find_top_level_node_at_pos(self, pos: Position) -> Optional[VyperNode]:
337-
for node in self.get_top_level_nodes():
337+
nodes = self.get_top_level_nodes()
338+
for node in nodes:
338339
if node.lineno <= pos.line and pos.line <= node.end_lineno:
339340
return node
340341

342+
# return node with highest lineno if no node found
343+
if nodes:
344+
# sort
345+
nodes.sort(key=lambda x: x.lineno, reverse=True)
346+
return nodes[0]
347+
341348
return None
342349

343350
def find_nodes_referencing_symbol(self, symbol: str):

0 commit comments

Comments
 (0)