Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arch: migrate lsp find_refs to new sema model #899

Merged
merged 6 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Peefy marked this conversation as resolved.
Show resolved Hide resolved
_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);
Peefy marked this conversation as resolved.
Show resolved Hide resolved
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
Loading