Skip to content

Commit

Permalink
analyzer: simplify, improve completion context detection
Browse files Browse the repository at this point in the history
detect compile time for loop as inside_loop
  • Loading branch information
ttytm committed Mar 31, 2024
1 parent b50fdc3 commit cae4a2c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/analyzer/psi/CompileTimeIfExpression.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ pub fn (n CompileTimeIfExpression) block() ?&Block {
}

pub fn (n CompileTimeIfExpression) else_branch() ?PsiElement {
return n.find_child_by_name('else_branch')
return n.find_child_by_type(.else_branch)?.last_child()
}
8 changes: 2 additions & 6 deletions src/analyzer/psi/IfExpression.v
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ pub fn (n IfExpression) var_definition() ?&VarDefinition {

pub fn (n IfExpression) block() ?&Block {
block := n.find_child_by_type(.block)?
if block is Block {
return block
}
return none
return if block is Block { block } else { none }
}

pub fn (n IfExpression) else_branch() ?PsiElement {
else_branch := n.find_child_by_type(.else_branch)?
return else_branch.last_child()
return n.find_child_by_type(.else_branch)?.last_child()
}
66 changes: 22 additions & 44 deletions src/server/completion/CompletionContext.v
Original file line number Diff line number Diff line change
Expand Up @@ -48,59 +48,37 @@ pub fn (mut c CompletionContext) compute() {

parent := c.element.parent() or { return }

if parent is psi.ImportName {
c.is_import_name = true
match parent.node.type_name {
.import_name { c.is_import_name = true }
.keyed_element { c.inside_struct_init_with_keys = true }
.type_reference_expression { c.is_type_reference = true }
.for_statement, .compile_time_for_statement { c.inside_loop = true }
else {}
}

if parent.node.type_name == .reference_expression {
if grand := parent.parent() {
// do not consider as reference_expression if it is inside an attribute
c.is_expression = grand !is psi.ValueAttribute

if grand.node.type_name == .element_list {
c.inside_struct_init_with_keys = true
}

if _ := grand.prev_sibling_of_type(.keyed_element) {
if grand := parent.parent() {
// Do not consider as reference_expression if it is inside an attribute.
if parent.node.type_name == .reference_expression {
c.is_expression = grand.node.type_name != .key_value_attribute
if grand.node.type_name == .element_list
|| grand.prev_sibling_of_type(.keyed_element) != none {
c.inside_struct_init_with_keys = true
}
}
}

if parent.node.type_name == .keyed_element {
c.inside_struct_init_with_keys = true
}

if parent.node.type_name == .type_reference_expression {
c.is_type_reference = true
}

if parent.node.type_name == .for_statement {
c.inside_loop = true
}

if grand := parent.parent() {
if grand.node.type_name == .simple_statement {
c.is_statement = true
}
if grand.node.type_name == .assert_statement {
c.is_assert_statement = true
}
if grand.node.type_name == .value_attribute {
c.is_attribute = true
}

if grand.node.type_name == .for_statement {
c.inside_loop = true
match grand.node.type_name {
.simple_statement { c.is_statement = true }
.assert_statement { c.is_assert_statement = true }
.value_attribute { c.is_attribute = true }
.for_statement, .compile_time_for_statement { c.inside_loop = true }
else {}
}

if grand_grand := grand.parent() {
if grand_grand.node.type_name == .source_file {
c.is_top_level = true
}

if grand_grand.node.type_name == .for_statement {
c.inside_loop = true
match grand_grand.node.type_name {
.source_file { c.is_top_level = true }
.for_statement, .compile_time_for_statement { c.inside_loop = true }
else {}
}
}
}
Expand Down

0 comments on commit cae4a2c

Please sign in to comment.