From cae4a2c641a0668fba36df5410ffaa25864ba2e7 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Mon, 1 Apr 2024 01:21:15 +0200 Subject: [PATCH] analyzer: simplify, improve completion context detection detect compile time for loop as inside_loop --- src/analyzer/psi/CompileTimeIfExpression.v | 2 +- src/analyzer/psi/IfExpression.v | 8 +-- src/server/completion/CompletionContext.v | 66 ++++++++-------------- 3 files changed, 25 insertions(+), 51 deletions(-) diff --git a/src/analyzer/psi/CompileTimeIfExpression.v b/src/analyzer/psi/CompileTimeIfExpression.v index 3fac59d0..32c405e2 100644 --- a/src/analyzer/psi/CompileTimeIfExpression.v +++ b/src/analyzer/psi/CompileTimeIfExpression.v @@ -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() } diff --git a/src/analyzer/psi/IfExpression.v b/src/analyzer/psi/IfExpression.v index 515c6c95..ddd831fc 100644 --- a/src/analyzer/psi/IfExpression.v +++ b/src/analyzer/psi/IfExpression.v @@ -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() } diff --git a/src/server/completion/CompletionContext.v b/src/server/completion/CompletionContext.v index 38d54494..4518d74c 100644 --- a/src/server/completion/CompletionContext.v +++ b/src/server/completion/CompletionContext.v @@ -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 {} } } }