From 6ad6d406498e8bc9402820f9ca4d914bc342786c Mon Sep 17 00:00:00 2001 From: Xiaok29 <1526783667@qq.com> Date: Tue, 21 Nov 2023 11:58:48 +0800 Subject: [PATCH 1/5] arch: migrate lsp find_refs to new sema model Signed-off-by: Xiaok29 <1526783667@qq.com> --- kclvm/tools/src/LSP/src/find_refs.rs | 60 ++++++++++++---------------- kclvm/tools/src/LSP/src/request.rs | 2 + 2 files changed, 28 insertions(+), 34 deletions(-) diff --git a/kclvm/tools/src/LSP/src/find_refs.rs b/kclvm/tools/src/LSP/src/find_refs.rs index 5cd34a062..5acea12c7 100644 --- a/kclvm/tools/src/LSP/src/find_refs.rs +++ b/kclvm/tools/src/LSP/src/find_refs.rs @@ -1,10 +1,11 @@ use crate::from_lsp::kcl_pos; -use crate::goto_def::{find_def, goto_definition}; +use crate::goto_def::{find_def, goto_definition, find_def_with_gs}; use crate::to_lsp::lsp_location; use crate::util::{parse_param_and_compile, Param}; use anyhow; use kclvm_ast::ast::{Program, Stmt}; use kclvm_error::Position as KCLPos; +use kclvm_sema::core::global_state::GlobalState; use kclvm_sema::resolver::scope::ProgramScope; use lsp_types::{Location, Url}; use parking_lot::RwLock; @@ -13,46 +14,37 @@ use std::collections::HashMap; use std::sync::Arc; pub(crate) fn find_refs Result<(), anyhow::Error>>( - program: &Program, + _program: &Program, kcl_pos: &KCLPos, include_declaration: bool, - prog_scope: &ProgramScope, + _prog_scope: &ProgramScope, word_index_map: Arc>>>>, vfs: Option>>, logger: F, + gs: &GlobalState, ) -> Result, String> { - // find the definition at the position - let def = match program.pos_to_stmt(kcl_pos) { - Some(node) => match node.node { - Stmt::Import(_) => None, - _ => find_def(node.clone(), kcl_pos, prog_scope), + let def = find_def_with_gs(kcl_pos, &gs, true); + match def { + Some(def_ref) => match gs.get_symbols().get_symbol(def_ref) { + Some(obj) => { + let (start, end) = obj.get_range(); + // find all the refs of the def + if let Some(def_loc) = lsp_location(start.filename.clone(), &start, &end) { + Ok(find_refs_from_def( + vfs, + word_index_map, + def_loc, + obj.get_name(), + include_declaration, + logger, + )) + } else { + Err(format!("Invalid file path: {0}", start.filename)) + } + }, + None => Err(String::from("Found more than one definitions, reference not supported")), }, - None => None, - }; - if def.is_none() { - return Err(String::from( - "Definition item not found, result in no reference", - )); - } - let def = def.unwrap(); - if def.get_positions().len() > 1 { - return Err(String::from( - "Found more than one definitions, reference not supported", - )); - } - let (start, end) = def.get_positions().iter().next().unwrap().clone(); - // find all the refs of the def - if let Some(def_loc) = lsp_location(start.filename.clone(), &start, &end) { - Ok(find_refs_from_def( - vfs, - word_index_map, - def_loc, - def.get_name(), - include_declaration, - logger, - )) - } else { - Err(format!("Invalid file path: {0}", start.filename)) + None => Err(String::from("Definition item not found, result in no reference")), } } diff --git a/kclvm/tools/src/LSP/src/request.rs b/kclvm/tools/src/LSP/src/request.rs index 26d9f421c..62b5466ab 100644 --- a/kclvm/tools/src/LSP/src/request.rs +++ b/kclvm/tools/src/LSP/src/request.rs @@ -178,6 +178,7 @@ pub(crate) fn handle_reference( snapshot.word_index_map.clone(), Some(snapshot.vfs.clone()), log, + &db.gs ) { core::result::Result::Ok(locations) => Ok(Some(locations)), Err(msg) => { @@ -284,6 +285,7 @@ pub(crate) fn handle_rename( snapshot.word_index_map.clone(), Some(snapshot.vfs.clone()), log, + &db.gs, ); match references { Result::Ok(locations) => { From f8f4663d1c3ba4887fb05c806cb364a18899c5b3 Mon Sep 17 00:00:00 2001 From: Xiaok29 <1526783667@qq.com> Date: Tue, 21 Nov 2023 13:15:55 +0800 Subject: [PATCH 2/5] style:format find_refs function Signed-off-by: Xiaok29 <1526783667@qq.com> --- kclvm/tools/src/LSP/src/find_refs.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/kclvm/tools/src/LSP/src/find_refs.rs b/kclvm/tools/src/LSP/src/find_refs.rs index 5acea12c7..476b7f9a5 100644 --- a/kclvm/tools/src/LSP/src/find_refs.rs +++ b/kclvm/tools/src/LSP/src/find_refs.rs @@ -1,5 +1,5 @@ use crate::from_lsp::kcl_pos; -use crate::goto_def::{find_def, goto_definition, find_def_with_gs}; +use crate::goto_def::{find_def, find_def_with_gs, goto_definition}; use crate::to_lsp::lsp_location; use crate::util::{parse_param_and_compile, Param}; use anyhow; @@ -23,7 +23,7 @@ pub(crate) fn find_refs Result<(), anyhow::Error>>( logger: F, gs: &GlobalState, ) -> Result, String> { - let def = find_def_with_gs(kcl_pos, &gs, true); + let def = find_def_with_gs(kcl_pos, &gs, true); match def { Some(def_ref) => match gs.get_symbols().get_symbol(def_ref) { Some(obj) => { @@ -41,10 +41,14 @@ pub(crate) fn find_refs Result<(), anyhow::Error>>( } else { Err(format!("Invalid file path: {0}", start.filename)) } - }, - None => Err(String::from("Found more than one definitions, reference not supported")), + } + None => Err(String::from( + "Found more than one definitions, reference not supported", + )), }, - None => Err(String::from("Definition item not found, result in no reference")), + None => Err(String::from( + "Definition item not found, result in no reference", + )), } } From 26cc6ee3e79cec83cc6ab62dd0e0a0eff7310f59 Mon Sep 17 00:00:00 2001 From: Xiaok29 <1526783667@qq.com> Date: Tue, 21 Nov 2023 13:29:26 +0800 Subject: [PATCH 3/5] style:format handle_reference function Signed-off-by: Xiaok29 <1526783667@qq.com> --- kclvm/tools/src/LSP/src/request.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kclvm/tools/src/LSP/src/request.rs b/kclvm/tools/src/LSP/src/request.rs index 62b5466ab..428c7cb5a 100644 --- a/kclvm/tools/src/LSP/src/request.rs +++ b/kclvm/tools/src/LSP/src/request.rs @@ -178,7 +178,7 @@ pub(crate) fn handle_reference( snapshot.word_index_map.clone(), Some(snapshot.vfs.clone()), log, - &db.gs + &db.gs, ) { core::result::Result::Ok(locations) => Ok(Some(locations)), Err(msg) => { From a0cf3dfbd0d1fcda883779c2d5fba6eeb63c67db Mon Sep 17 00:00:00 2001 From: Xiaok29 <1526783667@qq.com> Date: Tue, 21 Nov 2023 14:51:37 +0800 Subject: [PATCH 4/5] migrate lsp find_refs_from_def to new sema model Signed-off-by: Xiaok29 <1526783667@qq.com> --- kclvm/tools/src/LSP/src/find_refs.rs | 33 +++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/kclvm/tools/src/LSP/src/find_refs.rs b/kclvm/tools/src/LSP/src/find_refs.rs index 476b7f9a5..90aab3883 100644 --- a/kclvm/tools/src/LSP/src/find_refs.rs +++ b/kclvm/tools/src/LSP/src/find_refs.rs @@ -1,9 +1,9 @@ use crate::from_lsp::kcl_pos; -use crate::goto_def::{find_def, find_def_with_gs, goto_definition}; +use crate::goto_def::{find_def_with_gs, goto_definition_with_gs}; use crate::to_lsp::lsp_location; use crate::util::{parse_param_and_compile, Param}; use anyhow; -use kclvm_ast::ast::{Program, Stmt}; +use kclvm_ast::ast::Program; use kclvm_error::Position as KCLPos; use kclvm_sema::core::global_state::GlobalState; use kclvm_sema::resolver::scope::ProgramScope; @@ -14,10 +14,10 @@ use std::collections::HashMap; use std::sync::Arc; pub(crate) fn find_refs Result<(), anyhow::Error>>( - _program: &Program, + program: &Program, kcl_pos: &KCLPos, include_declaration: bool, - _prog_scope: &ProgramScope, + prog_scope: &ProgramScope, word_index_map: Arc>>>>, vfs: Option>>, logger: F, @@ -37,6 +37,7 @@ pub(crate) fn find_refs Result<(), anyhow::Error>>( obj.get_name(), include_declaration, logger, + gs, )) } else { Err(format!("Invalid file path: {0}", start.filename)) @@ -59,6 +60,7 @@ pub(crate) fn find_refs_from_def Result<(), anyhow::Error>>( name: String, include_declaration: bool, logger: F, + gs: &GlobalState, ) -> Vec { let mut ref_locations = vec![]; for (_, word_index) in &mut *word_index_map.write() { @@ -81,7 +83,9 @@ pub(crate) fn find_refs_from_def Result<(), anyhow::Error>>( return false; } // find def from the ref_pos - if let Some(real_def) = goto_definition(&prog, &ref_pos, &scope) { + if let Some(real_def) = + goto_definition_with_gs(&prog, &ref_pos, &scope, gs) + { match real_def { lsp_types::GotoDefinitionResponse::Scalar(real_def_loc) => { real_def_loc == def_loc @@ -109,6 +113,7 @@ pub(crate) fn find_refs_from_def Result<(), anyhow::Error>>( #[cfg(test)] mod tests { use super::find_refs_from_def; + use crate::tests::compile_test_file; use crate::util::build_word_index; use lsp_types::{Location, Position, Range, Url}; use parking_lot::RwLock; @@ -138,6 +143,10 @@ mod tests { let mut path = root.clone(); path.push("src/test_data/find_refs_test/main.k"); let path = path.to_str().unwrap(); + + let (_file, _program, _prog_scope, _, gs) = + compile_test_file("src/test_data/find_refs_test/main.k"); + match lsp_types::Url::from_file_path(path) { Ok(url) => { let def_loc = Location { @@ -186,6 +195,7 @@ mod tests { "a".to_string(), true, logger, + &gs, ), ); } @@ -199,6 +209,10 @@ mod tests { let mut path = root.clone(); path.push("src/test_data/find_refs_test/main.k"); let path = path.to_str().unwrap(); + + let (_file, _program, _prog_scope, _, gs) = + compile_test_file("src/test_data/find_refs_test/main.k"); + match lsp_types::Url::from_file_path(path) { Ok(url) => { let def_loc = Location { @@ -240,6 +254,7 @@ mod tests { "a".to_string(), false, logger, + &gs, ), ); } @@ -253,6 +268,10 @@ mod tests { let mut path = root.clone(); path.push("src/test_data/find_refs_test/main.k"); let path = path.to_str().unwrap(); + + let (_file, _program, _prog_scope, _, gs) = + compile_test_file("src/test_data/find_refs_test/main.k"); + match lsp_types::Url::from_file_path(path) { Ok(url) => { let def_loc = Location { @@ -294,6 +313,7 @@ mod tests { "Name".to_string(), true, logger, + &gs, ), ); } @@ -307,6 +327,8 @@ mod tests { let mut path = root.clone(); path.push("src/test_data/find_refs_test/main.k"); let path = path.to_str().unwrap(); + let (_file, _program, _prog_scope, _, gs) = + compile_test_file("src/test_data/find_refs_test/main.k"); match lsp_types::Url::from_file_path(path) { Ok(url) => { let def_loc = Location { @@ -341,6 +363,7 @@ mod tests { "name".to_string(), true, logger, + &gs, ), ); } From bd15e2b6ab9689b767f0ff4f479834c1bc2edb2b Mon Sep 17 00:00:00 2001 From: Xiaok29 <1526783667@qq.com> Date: Wed, 22 Nov 2023 12:20:57 +0800 Subject: [PATCH 5/5] fix:remove gs parameter in def_find_refs_from function Signed-off-by: Xiaok29 <1526783667@qq.com> --- kclvm/tools/src/LSP/src/find_refs.rs | 28 ++++------------------------ 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/kclvm/tools/src/LSP/src/find_refs.rs b/kclvm/tools/src/LSP/src/find_refs.rs index 90aab3883..1145dadbc 100644 --- a/kclvm/tools/src/LSP/src/find_refs.rs +++ b/kclvm/tools/src/LSP/src/find_refs.rs @@ -37,7 +37,6 @@ pub(crate) fn find_refs Result<(), anyhow::Error>>( obj.get_name(), include_declaration, logger, - gs, )) } else { Err(format!("Invalid file path: {0}", start.filename)) @@ -60,7 +59,6 @@ pub(crate) fn find_refs_from_def Result<(), anyhow::Error>>( name: String, include_declaration: bool, logger: F, - gs: &GlobalState, ) -> Vec { let mut ref_locations = vec![]; for (_, word_index) in &mut *word_index_map.write() { @@ -77,14 +75,14 @@ pub(crate) fn find_refs_from_def Result<(), anyhow::Error>>( }, vfs.clone(), ) { - Ok((prog, scope, _, _gs)) => { + Ok((prog, scope, _, gs)) => { let ref_pos = kcl_pos(&file_path, ref_loc.range.start); if *ref_loc == def_loc && !include_declaration { return false; } // find def from the ref_pos if let Some(real_def) = - goto_definition_with_gs(&prog, &ref_pos, &scope, gs) + goto_definition_with_gs(&prog, &ref_pos, &scope, &gs) { match real_def { lsp_types::GotoDefinitionResponse::Scalar(real_def_loc) => { @@ -113,7 +111,6 @@ pub(crate) fn find_refs_from_def Result<(), anyhow::Error>>( #[cfg(test)] mod tests { use super::find_refs_from_def; - use crate::tests::compile_test_file; use crate::util::build_word_index; use lsp_types::{Location, Position, Range, Url}; use parking_lot::RwLock; @@ -144,9 +141,6 @@ mod tests { path.push("src/test_data/find_refs_test/main.k"); let path = path.to_str().unwrap(); - let (_file, _program, _prog_scope, _, gs) = - compile_test_file("src/test_data/find_refs_test/main.k"); - match lsp_types::Url::from_file_path(path) { Ok(url) => { let def_loc = Location { @@ -195,7 +189,6 @@ mod tests { "a".to_string(), true, logger, - &gs, ), ); } @@ -209,10 +202,6 @@ mod tests { let mut path = root.clone(); path.push("src/test_data/find_refs_test/main.k"); let path = path.to_str().unwrap(); - - let (_file, _program, _prog_scope, _, gs) = - compile_test_file("src/test_data/find_refs_test/main.k"); - match lsp_types::Url::from_file_path(path) { Ok(url) => { let def_loc = Location { @@ -254,7 +243,6 @@ mod tests { "a".to_string(), false, logger, - &gs, ), ); } @@ -268,17 +256,13 @@ mod tests { let mut path = root.clone(); path.push("src/test_data/find_refs_test/main.k"); let path = path.to_str().unwrap(); - - let (_file, _program, _prog_scope, _, gs) = - compile_test_file("src/test_data/find_refs_test/main.k"); - match lsp_types::Url::from_file_path(path) { Ok(url) => { let def_loc = Location { uri: url.clone(), range: Range { - start: Position::new(4, 0), - end: Position::new(7, 0), + start: Position::new(4, 7), + end: Position::new(4, 11), }, }; let expect = vec![ @@ -313,7 +297,6 @@ mod tests { "Name".to_string(), true, logger, - &gs, ), ); } @@ -327,8 +310,6 @@ mod tests { let mut path = root.clone(); path.push("src/test_data/find_refs_test/main.k"); let path = path.to_str().unwrap(); - let (_file, _program, _prog_scope, _, gs) = - compile_test_file("src/test_data/find_refs_test/main.k"); match lsp_types::Url::from_file_path(path) { Ok(url) => { let def_loc = Location { @@ -363,7 +344,6 @@ mod tests { "name".to_string(), true, logger, - &gs, ), ); }