Skip to content

Commit

Permalink
arch: migrate lsp find_refs to new sema model
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaok29 <1526783667@qq.com>
  • Loading branch information
XiaoK29 committed Nov 21, 2023
1 parent 0f7bf2d commit 6ad6d40
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
60 changes: 26 additions & 34 deletions kclvm/tools/src/LSP/src/find_refs.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,46 +14,37 @@ use std::collections::HashMap;
use std::sync::Arc;

pub(crate) fn find_refs<F: Fn(String) -> Result<(), anyhow::Error>>(
program: &Program,
_program: &Program,
kcl_pos: &KCLPos,
include_declaration: bool,
prog_scope: &ProgramScope,
_prog_scope: &ProgramScope,
word_index_map: Arc<RwLock<HashMap<Url, HashMap<String, Vec<Location>>>>>,
vfs: Option<Arc<RwLock<Vfs>>>,
logger: F,
gs: &GlobalState,
) -> Result<Vec<Location>, 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")),
}
}

Expand Down
2 changes: 2 additions & 0 deletions kclvm/tools/src/LSP/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 6ad6d40

Please sign in to comment.