From 67e8c280fe8e28fe7512d9a202cb67423c533a17 Mon Sep 17 00:00:00 2001 From: He1pa <56333845+He1pa@users.noreply.github.com> Date: Tue, 28 May 2024 17:21:05 +0800 Subject: [PATCH] fix: fix lsp goto def of system pkg. (#1376) fix: fix lsp goto def of system pkg. refactor some unit test Signed-off-by: he1pa <18012015693@163.com> --- kclvm/sema/src/core/package.rs | 8 ++++- kclvm/sema/src/namer/mod.rs | 7 +++- kclvm/tools/src/LSP/src/goto_def.rs | 34 ++++++++++++++++++- ...goto_def__tests__goto_system_pkg_test.snap | 5 +++ .../goto_def_test/system_pkg/goto_sys_pkg.k | 3 ++ kclvm/tools/src/LSP/src/util.rs | 2 +- 6 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 kclvm/tools/src/LSP/src/snapshots/kcl_language_server__goto_def__tests__goto_system_pkg_test.snap create mode 100644 kclvm/tools/src/LSP/src/test_data/goto_def_test/system_pkg/goto_sys_pkg.k diff --git a/kclvm/sema/src/core/package.rs b/kclvm/sema/src/core/package.rs index b0de5be90..3854fa9d4 100644 --- a/kclvm/sema/src/core/package.rs +++ b/kclvm/sema/src/core/package.rs @@ -45,14 +45,16 @@ pub struct PackageInfo { pub(crate) fully_qualified_name: String, pub(crate) pkg_filepath: String, pub(crate) kfile_paths: IndexSet, + pub(crate) is_system: bool, } impl PackageInfo { - pub fn new(fully_qualified_name: String, pkg_filepath: String) -> Self { + pub fn new(fully_qualified_name: String, pkg_filepath: String, is_system: bool) -> Self { Self { fully_qualified_name, pkg_filepath, kfile_paths: IndexSet::default(), + is_system, } } @@ -63,6 +65,10 @@ impl PackageInfo { pub fn get_pkg_filepath(&self) -> &String { &self.pkg_filepath } + + pub fn is_system(&self) -> bool { + self.is_system + } } #[allow(unused)] #[derive(Debug, Clone)] diff --git a/kclvm/sema/src/namer/mod.rs b/kclvm/sema/src/namer/mod.rs index 965d02670..8f2d14a1d 100644 --- a/kclvm/sema/src/namer/mod.rs +++ b/kclvm/sema/src/namer/mod.rs @@ -102,8 +102,13 @@ impl<'ctx> Namer<'ctx> { namer.ctx.current_package_info = Some(PackageInfo::new( BUILTIN_SYMBOL_PKG_PATH.to_string(), "".to_string(), + true, )); namer.init_builtin_symbols(); + namer + .gs + .get_packages_mut() + .add_package(namer.ctx.current_package_info.take().unwrap()); for (name, modules) in namer.ctx.program.pkgs.iter() { { @@ -130,7 +135,7 @@ impl<'ctx> Namer<'ctx> { namer.ctx.owner_symbols.push(symbol_ref); namer.ctx.current_package_info = - Some(PackageInfo::new(name.to_string(), real_path)); + Some(PackageInfo::new(name.to_string(), real_path, false)); } for module in modules.iter() { diff --git a/kclvm/tools/src/LSP/src/goto_def.rs b/kclvm/tools/src/LSP/src/goto_def.rs index 79c69031b..3fa145b50 100644 --- a/kclvm/tools/src/LSP/src/goto_def.rs +++ b/kclvm/tools/src/LSP/src/goto_def.rs @@ -25,7 +25,13 @@ pub(crate) fn goto_def( Some(def_ref) => match gs.get_symbols().get_symbol(def_ref) { Some(def) => match def_ref.get_kind() { kclvm_sema::core::symbol::SymbolKind::Package => { - let pkg_info = gs.get_packages().get_package_info(&def.get_name()).unwrap(); + let pkg_info = match gs.get_packages().get_package_info(&def.get_name()) { + Some(pkg_info) => pkg_info, + None => return None, + }; + if pkg_info.is_system() { + return None; + } for file in pkg_info.get_kfile_paths() { let dummy_pos = KCLPos { filename: file.clone(), @@ -688,4 +694,30 @@ mod tests { let res = goto_def(&pos, &gs); compare_goto_res(res, (&file, 94, 11, 94, 12)); } + + #[macro_export] + macro_rules! goto_def_test_snapshot { + ($name:ident, $file:expr, $line:expr, $column: 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), + }; + goto_def(&pos, &gs) + })); + } + }; + } + + goto_def_test_snapshot!( + goto_system_pkg_test, + "src/test_data/goto_def_test/goto_def.k", + 3, + 1 + ); } diff --git a/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__goto_def__tests__goto_system_pkg_test.snap b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__goto_def__tests__goto_system_pkg_test.snap new file mode 100644 index 000000000..1fddab521 --- /dev/null +++ b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__goto_def__tests__goto_system_pkg_test.snap @@ -0,0 +1,5 @@ +--- +source: tools/src/LSP/src/goto_def.rs +expression: "format!(\"{:?}\", res)" +--- +None diff --git a/kclvm/tools/src/LSP/src/test_data/goto_def_test/system_pkg/goto_sys_pkg.k b/kclvm/tools/src/LSP/src/test_data/goto_def_test/system_pkg/goto_sys_pkg.k new file mode 100644 index 000000000..e0beed2c9 --- /dev/null +++ b/kclvm/tools/src/LSP/src/test_data/goto_def_test/system_pkg/goto_sys_pkg.k @@ -0,0 +1,3 @@ +import yaml + +yaml.decode("") \ No newline at end of file diff --git a/kclvm/tools/src/LSP/src/util.rs b/kclvm/tools/src/LSP/src/util.rs index d23feb627..aba2493ea 100644 --- a/kclvm/tools/src/LSP/src/util.rs +++ b/kclvm/tools/src/LSP/src/util.rs @@ -143,7 +143,7 @@ pub(crate) fn compile_with_params( let gs = Namer::find_symbols(&program, gs); match AdvancedResolver::resolve_program(&program, gs, prog_scope.node_ty_map.clone()) { Ok(gs) => (diags, Ok((program, gs))), - Err(e) => (diags, Err(anyhow::anyhow!("Parse failed: {:?}", e))), + Err(e) => (diags, Err(anyhow::anyhow!("Resolve failed: {:?}", e))), } }