From e0b4791b01b5a081eb3a18a3808cd32f6afc0c47 Mon Sep 17 00:00:00 2001 From: He1pa <56333845+He1pa@users.noreply.github.com> Date: Wed, 26 Jun 2024 11:53:29 +0800 Subject: [PATCH] fix: fix lambda expr scope range in advanced resolver (#1439) * fix: fix lambda expr scope range Signed-off-by: he1pa <18012015693@163.com> * update ut Signed-off-by: he1pa <18012015693@163.com> --------- Signed-off-by: he1pa <18012015693@163.com> --- kclvm/sema/src/advanced_resolver/node.rs | 1 + kclvm/tools/src/LSP/src/completion.rs | 39 +++++++++++++++++++ ...e_server__completion__tests__lambda_1.snap | 5 +++ .../lambda/lambda_1/lambda_1.k | 9 +++++ 4 files changed, 54 insertions(+) create mode 100644 kclvm/tools/src/LSP/src/snapshots/kcl_language_server__completion__tests__lambda_1.snap create mode 100644 kclvm/tools/src/LSP/src/test_data/completion_test/lambda/lambda_1/lambda_1.k diff --git a/kclvm/sema/src/advanced_resolver/node.rs b/kclvm/sema/src/advanced_resolver/node.rs index da2d82521..6df4640a1 100644 --- a/kclvm/sema/src/advanced_resolver/node.rs +++ b/kclvm/sema/src/advanced_resolver/node.rs @@ -846,6 +846,7 @@ impl<'ctx> AdvancedResolver<'ctx> { | ast::Expr::Schema(_) | ast::Expr::ConfigIfEntry(_) | ast::Expr::Quant(_) + | ast::Expr::Lambda(_) ) { let (start, end) = expr.get_span_pos(); self.ctx.start_pos = start; diff --git a/kclvm/tools/src/LSP/src/completion.rs b/kclvm/tools/src/LSP/src/completion.rs index 8cb74d1e2..0e6939892 100644 --- a/kclvm/tools/src/LSP/src/completion.rs +++ b/kclvm/tools/src/LSP/src/completion.rs @@ -1824,4 +1824,43 @@ mod tests { CompletionResponse::List(_) => panic!("test failed"), } } + + #[macro_export] + macro_rules! completion_label_test_snapshot { + ($name:ident, $file:expr, $line:expr, $column: expr, $trigger: expr) => { + #[test] + fn $name() { + insta::assert_snapshot!(format!("{:?}", { + let (file, program, _, gs) = compile_test_file($file); + + let pos = KCLPos { + filename: file.clone(), + line: $line, + column: Some($column), + }; + let tool = toolchain::default(); + + let mut got = completion($trigger, &program, &pos, &gs, &tool).unwrap(); + + match &mut got { + CompletionResponse::Array(arr) => { + let mut labels: Vec = + arr.iter().map(|item| item.label.clone()).collect(); + labels.sort(); + labels + } + CompletionResponse::List(_) => panic!("test failed"), + } + })); + } + }; + } + + completion_label_test_snapshot!( + lambda_1, + "src/test_data/completion_test/lambda/lambda_1/lambda_1.k", + 8, + 5, + None + ); } diff --git a/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__completion__tests__lambda_1.snap b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__completion__tests__lambda_1.snap new file mode 100644 index 000000000..31b3c212e --- /dev/null +++ b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__completion__tests__lambda_1.snap @@ -0,0 +1,5 @@ +--- +source: tools/src/LSP/src/completion.rs +expression: "format!(\"{:?}\", labels)" +--- +["abs(…)", "all_true(…)", "any_true(…)", "bin(…)", "bool(…)", "case", "cases", "dict(…)", "float(…)", "func1", "hex(…)", "int(…)", "isunique(…)", "len(…)", "list(…)", "max(…)", "min(…)", "multiplyof(…)", "oct(…)", "option(…)", "ord(…)", "pow(…)", "print(…)", "range(…)", "round(…)", "sorted(…)", "str(…)", "sum(…)", "typeof(…)", "zip(…)"] diff --git a/kclvm/tools/src/LSP/src/test_data/completion_test/lambda/lambda_1/lambda_1.k b/kclvm/tools/src/LSP/src/test_data/completion_test/lambda/lambda_1/lambda_1.k new file mode 100644 index 000000000..5874be935 --- /dev/null +++ b/kclvm/tools/src/LSP/src/test_data/completion_test/lambda/lambda_1/lambda_1.k @@ -0,0 +1,9 @@ +cases: [str] = ["a"] + +func1 = lambda elems: [any], func: (any) -> (any) { + +} + +func1(cases, lambda case: str { + +})