Skip to content

Commit

Permalink
chore: format code
Browse files Browse the repository at this point in the history
Signed-off-by: xiarui.xr <[email protected]>
  • Loading branch information
amyXia1994 committed Oct 12, 2023
1 parent c1261ea commit 55040b7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
38 changes: 26 additions & 12 deletions kclvm/tools/src/LSP/src/find_refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ pub(crate) fn find_refs<F: Fn(String) -> Result<(), anyhow::Error>>(

for (_, word_index) in word_index_map {
if let Some(locs) = word_index.get(name.as_str()).cloned() {
let matched_locs: Vec<Location> = locs.into_iter()
let matched_locs: Vec<Location> = locs
.into_iter()
.filter(|ref_loc| {
// from location to real def
// return if the real def location matches the def_loc
Expand All @@ -33,13 +34,11 @@ pub(crate) fn find_refs<F: Fn(String) -> Result<(), anyhow::Error>>(
Ok((prog, scope, _)) => {
let ref_pos = kcl_pos(&file_path, ref_loc.range.start);
// find def from the ref_pos
if let Some(real_def) =
goto_definition(&prog, &ref_pos, &scope)
{
if let Some(real_def) = goto_definition(&prog, &ref_pos, &scope) {
match real_def {
lsp_types::GotoDefinitionResponse::Scalar(
real_def_loc,
) => real_def_loc == def_loc,
lsp_types::GotoDefinitionResponse::Scalar(real_def_loc) => {
real_def_loc == def_loc
}
_ => false,
}
} else {
Expand All @@ -62,11 +61,11 @@ pub(crate) fn find_refs<F: Fn(String) -> Result<(), anyhow::Error>>(
#[cfg(test)]
mod tests {
use super::find_refs;
use crate::util::build_word_index;
use lsp_types::{Location, Position, Range};
use std::collections::HashMap;
use std::ops::Index;
use std::path::PathBuf;
use std::collections::HashMap;
use crate::util::build_word_index;

fn logger(msg: String) -> Result<(), anyhow::Error> {
println!("{}", msg);
Expand All @@ -87,7 +86,10 @@ mod tests {
}

fn setup_word_index_map(root: &str) -> HashMap<String, HashMap<String, Vec<Location>>> {
HashMap::from([("default".to_string(), build_word_index(root.to_string()).unwrap())])
HashMap::from([(
"default".to_string(),
build_word_index(root.to_string()).unwrap(),
)])
}

#[test]
Expand Down Expand Up @@ -137,7 +139,13 @@ mod tests {
];
check_locations_match(
expect,
find_refs(setup_word_index_map(path), def_loc, "a".to_string(), path.to_string(), logger),
find_refs(
setup_word_index_map(path),
def_loc,
"a".to_string(),
path.to_string(),
logger,
),
);
}
Err(_) => assert!(false, "file not found"),
Expand Down Expand Up @@ -184,7 +192,13 @@ mod tests {
];
check_locations_match(
expect,
find_refs(setup_word_index_map(path), def_loc, "Name".to_string(), path.to_string(), logger),
find_refs(
setup_word_index_map(path),
def_loc,
"Name".to_string(),
path.to_string(),
logger,
),
);
}
Err(_) => assert!(false, "file not found"),
Expand Down
6 changes: 5 additions & 1 deletion kclvm/tools/src/LSP/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ use lsp_types::InitializeParams;

#[allow(dead_code)]
/// Runs the main loop of the language server. This will receive requests and handle them.
pub(crate) fn main_loop(connection: Connection, config: Config, initialize_params: InitializeParams) -> anyhow::Result<()> {
pub(crate) fn main_loop(
connection: Connection,
config: Config,
initialize_params: InitializeParams,
) -> anyhow::Result<()> {
LanguageServerState::new(connection.sender, config, initialize_params).run(connection.receiver)
}

Expand Down
15 changes: 9 additions & 6 deletions kclvm/tools/src/LSP/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ use crate::analysis::Analysis;
use crate::config::Config;
use crate::db::AnalysisDatabase;
use crate::to_lsp::{kcl_diag_to_lsp_diags, url};
use crate::util::{get_file_name, parse_param_and_compile, to_json, Param, build_word_index};
use crate::util::{build_word_index, get_file_name, parse_param_and_compile, to_json, Param};
use crossbeam_channel::{select, unbounded, Receiver, Sender};
use indexmap::IndexSet;
use lsp_server::{ReqQueue, Response};
use lsp_types::{
notification::{Notification, PublishDiagnostics},
Diagnostic, Location, PublishDiagnosticsParams,
InitializeParams,
Diagnostic, InitializeParams, Location, PublishDiagnosticsParams,
};
use parking_lot::RwLock;
use ra_ap_vfs::{FileId, Vfs};
Expand Down Expand Up @@ -88,14 +87,18 @@ pub(crate) struct LanguageServerSnapshot {

#[allow(unused)]
impl LanguageServerState {
pub fn new(sender: Sender<lsp_server::Message>, config: Config, initialize_params: InitializeParams) -> Self {
pub fn new(
sender: Sender<lsp_server::Message>,
config: Config,
initialize_params: InitializeParams,
) -> Self {
let (task_sender, task_receiver) = unbounded::<Task>();

let (vfs_sender, receiver) = unbounded::<ra_ap_vfs::loader::Message>();
let handle: NotifyHandle =
ra_ap_vfs::loader::Handle::spawn(Box::new(move |msg| vfs_sender.send(msg).unwrap()));
let handle = Box::new(handle) as Box<dyn ra_ap_vfs::loader::Handle>;

// build word index for all the workspace folders
// todo: async
let mut word_index_map = HashMap::new();
Expand Down Expand Up @@ -124,7 +127,7 @@ impl LanguageServerState {
analysis: Analysis::default(),
opened_files: IndexSet::new(),
vfs_handle: handle,
word_index_map: word_index_map,
word_index_map,
}
}

Expand Down
6 changes: 3 additions & 3 deletions kclvm/tools/src/LSP/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use lsp_types::GotoDefinitionResponse;
use lsp_types::Hover;
use lsp_types::HoverContents;
use lsp_types::HoverParams;
use lsp_types::InitializeParams;
use lsp_types::MarkedString;
use lsp_types::PublishDiagnosticsParams;
use lsp_types::ReferenceContext;
Expand All @@ -23,9 +24,8 @@ use lsp_types::TextDocumentIdentifier;
use lsp_types::TextDocumentItem;
use lsp_types::TextDocumentPositionParams;
use lsp_types::TextEdit;
use lsp_types::InitializeParams;
use lsp_types::WorkspaceFolder;
use lsp_types::Url;
use lsp_types::WorkspaceFolder;

use serde::Serialize;
use std::cell::Cell;
Expand Down Expand Up @@ -1318,7 +1318,7 @@ fn test_find_refs() {
let path = path.to_str().unwrap();
let src = std::fs::read_to_string(path.clone()).unwrap();
let mut initialize_params = InitializeParams::default();
initialize_params.workspace_folders = Some(vec![WorkspaceFolder{
initialize_params.workspace_folders = Some(vec![WorkspaceFolder {
uri: Url::from_file_path(root.clone()).unwrap(),
name: "test".to_string(),
}]);
Expand Down

0 comments on commit 55040b7

Please sign in to comment.