Skip to content

Commit

Permalink
fix: find top-level node when adding new line
Browse files Browse the repository at this point in the history
also add test for struct member completion
  • Loading branch information
z80dev committed Dec 15, 2024
1 parent b102a14 commit ed47a6b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
35 changes: 35 additions & 0 deletions tests/test_completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="<inline source code>", 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:
Expand Down
9 changes: 8 additions & 1 deletion vyper_lsp/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit ed47a6b

Please sign in to comment.