From a2345cd62f1af074aa4a5419372ebea051faf47c Mon Sep 17 00:00:00 2001 From: he1pa <18012015693@163.com> Date: Tue, 21 May 2024 18:50:45 +0800 Subject: [PATCH 1/2] feat: lsp hover for dict into schema expr key Signed-off-by: he1pa <18012015693@163.com> --- kclvm/Cargo.lock | 1 + kclvm/sema/src/advanced_resolver/node.rs | 16 +++++++++- kclvm/tools/src/LSP/Cargo.toml | 1 + kclvm/tools/src/LSP/src/hover.rs | 30 +++++++++++++++++++ ...r__hover__tests__dict_key_in_schema-2.snap | 5 ++++ ...r__hover__tests__dict_key_in_schema-3.snap | 5 ++++ ...ver__hover__tests__dict_key_in_schema.snap | 5 ++++ .../dict_key_in_schema/dict_key_in_schema.k | 14 +++++++++ 8 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 kclvm/tools/src/LSP/src/snapshots/kcl_language_server__hover__tests__dict_key_in_schema-2.snap create mode 100644 kclvm/tools/src/LSP/src/snapshots/kcl_language_server__hover__tests__dict_key_in_schema-3.snap create mode 100644 kclvm/tools/src/LSP/src/snapshots/kcl_language_server__hover__tests__dict_key_in_schema.snap create mode 100644 kclvm/tools/src/LSP/src/test_data/hover_test/dict_key_in_schema/dict_key_in_schema.k diff --git a/kclvm/Cargo.lock b/kclvm/Cargo.lock index 8ac7beda4..14dbfb7a2 100644 --- a/kclvm/Cargo.lock +++ b/kclvm/Cargo.lock @@ -1447,6 +1447,7 @@ dependencies = [ "env_logger", "im-rc", "indexmap 1.9.3", + "insta", "kclvm-ast", "kclvm-config", "kclvm-driver", diff --git a/kclvm/sema/src/advanced_resolver/node.rs b/kclvm/sema/src/advanced_resolver/node.rs index 7cf09ad0e..afc771145 100644 --- a/kclvm/sema/src/advanced_resolver/node.rs +++ b/kclvm/sema/src/advanced_resolver/node.rs @@ -93,6 +93,20 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { } self.ctx.maybe_def = true; self.walk_identifier_expr(target)?; + + if let Some(target_ty) = self.ctx.node_ty_map.get(&self.ctx.get_node_key(&target.id)) { + match &target_ty.kind { + TypeKind::Schema(_) => { + let schema_symbol = self + .gs + .get_symbols() + .get_type_symbol(&target_ty, self.get_current_module_info()) + .ok_or(anyhow!("schema_symbol not found"))?; + self.ctx.current_schema_symbol = Some(schema_symbol); + } + _ => {} + } + } self.ctx.maybe_def = false; } self.walk_type_expr(assign_stmt.ty.as_ref().map(|ty| ty.as_ref()))?; @@ -861,7 +875,7 @@ impl<'ctx> AdvancedResolver<'ctx> { } None => Ok(None), }, - some => some, + res => res, } } diff --git a/kclvm/tools/src/LSP/Cargo.toml b/kclvm/tools/src/LSP/Cargo.toml index 2bbb021bd..33a8bf3fa 100644 --- a/kclvm/tools/src/LSP/Cargo.toml +++ b/kclvm/tools/src/LSP/Cargo.toml @@ -44,6 +44,7 @@ serde_json = { version = "1.0", default-features = false } parking_lot = { version = "0.12.0", default-features = false } rustc-hash = { version = "1.1.0", default-features = false } proc_macro_crate = { path = "../../benches/proc_macro_crate" } +insta = "1.8.0" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] tokio = { version = "1.37.0", features = ["full"] } diff --git a/kclvm/tools/src/LSP/src/hover.rs b/kclvm/tools/src/LSP/src/hover.rs index 537b59b09..aff8456d8 100644 --- a/kclvm/tools/src/LSP/src/hover.rs +++ b/kclvm/tools/src/LSP/src/hover.rs @@ -610,4 +610,34 @@ mod tests { _ => unreachable!("test error"), } } + + #[test] + #[bench_test] + fn dict_key_in_schema() { + let (file, program, _, gs) = + compile_test_file("src/test_data/hover_test/dict_key_in_schema/dict_key_in_schema.k"); + let pos = KCLPos { + filename: file.clone(), + line: 5, + column: Some(5), + }; + let got = hover(&program, &pos, &gs).unwrap(); + insta::assert_snapshot!(format!("{:?}", got)); + + let pos = KCLPos { + filename: file.clone(), + line: 9, + column: Some(5), + }; + let got = hover(&program, &pos, &gs).unwrap(); + insta::assert_snapshot!(format!("{:?}", got)); + + let pos = KCLPos { + filename: file.clone(), + line: 13, + column: Some(5), + }; + let got = hover(&program, &pos, &gs).unwrap(); + insta::assert_snapshot!(format!("{:?}", got)); + } } diff --git a/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__hover__tests__dict_key_in_schema-2.snap b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__hover__tests__dict_key_in_schema-2.snap new file mode 100644 index 000000000..ffbe0c87c --- /dev/null +++ b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__hover__tests__dict_key_in_schema-2.snap @@ -0,0 +1,5 @@ +--- +source: tools/src/LSP/src/hover.rs +expression: "format!(\"{:?}\", got)" +--- +Hover { contents: Scalar(String("name: int")), range: None } diff --git a/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__hover__tests__dict_key_in_schema-3.snap b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__hover__tests__dict_key_in_schema-3.snap new file mode 100644 index 000000000..ffbe0c87c --- /dev/null +++ b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__hover__tests__dict_key_in_schema-3.snap @@ -0,0 +1,5 @@ +--- +source: tools/src/LSP/src/hover.rs +expression: "format!(\"{:?}\", got)" +--- +Hover { contents: Scalar(String("name: int")), range: None } diff --git a/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__hover__tests__dict_key_in_schema.snap b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__hover__tests__dict_key_in_schema.snap new file mode 100644 index 000000000..ffbe0c87c --- /dev/null +++ b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__hover__tests__dict_key_in_schema.snap @@ -0,0 +1,5 @@ +--- +source: tools/src/LSP/src/hover.rs +expression: "format!(\"{:?}\", got)" +--- +Hover { contents: Scalar(String("name: int")), range: None } diff --git a/kclvm/tools/src/LSP/src/test_data/hover_test/dict_key_in_schema/dict_key_in_schema.k b/kclvm/tools/src/LSP/src/test_data/hover_test/dict_key_in_schema/dict_key_in_schema.k new file mode 100644 index 000000000..2abb1da0f --- /dev/null +++ b/kclvm/tools/src/LSP/src/test_data/hover_test/dict_key_in_schema/dict_key_in_schema.k @@ -0,0 +1,14 @@ +schema Name: + name: int + +n1: Name = { + name = 1 +} + +n2 = Name{ + name: 1 +} + +n3: Name = Name{ + name: 1 +} \ No newline at end of file From 65541533d282ab9673c957a33666e4ce4824e474 Mon Sep 17 00:00:00 2001 From: he1pa <18012015693@163.com> Date: Tue, 21 May 2024 19:17:39 +0800 Subject: [PATCH 2/2] chore: move insta to dev-dependencies Signed-off-by: he1pa <18012015693@163.com> --- kclvm/tools/src/LSP/Cargo.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kclvm/tools/src/LSP/Cargo.toml b/kclvm/tools/src/LSP/Cargo.toml index 33a8bf3fa..168bb8afc 100644 --- a/kclvm/tools/src/LSP/Cargo.toml +++ b/kclvm/tools/src/LSP/Cargo.toml @@ -44,7 +44,9 @@ serde_json = { version = "1.0", default-features = false } parking_lot = { version = "0.12.0", default-features = false } rustc-hash = { version = "1.1.0", default-features = false } proc_macro_crate = { path = "../../benches/proc_macro_crate" } -insta = "1.8.0" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] tokio = { version = "1.37.0", features = ["full"] } + +[dev-dependencies] +insta = "1.8.0"