From 55040b779867345e3fb441b2665fb1345357721c Mon Sep 17 00:00:00 2001 From: "xiarui.xr" Date: Mon, 9 Oct 2023 14:12:50 +0800 Subject: [PATCH] chore: format code Signed-off-by: xiarui.xr --- kclvm/tools/src/LSP/src/find_refs.rs | 38 +++++++++++++++++++--------- kclvm/tools/src/LSP/src/main_loop.rs | 6 ++++- kclvm/tools/src/LSP/src/state.rs | 15 ++++++----- kclvm/tools/src/LSP/src/tests.rs | 6 ++--- 4 files changed, 43 insertions(+), 22 deletions(-) diff --git a/kclvm/tools/src/LSP/src/find_refs.rs b/kclvm/tools/src/LSP/src/find_refs.rs index 52fddf571..21a207406 100644 --- a/kclvm/tools/src/LSP/src/find_refs.rs +++ b/kclvm/tools/src/LSP/src/find_refs.rs @@ -19,7 +19,8 @@ pub(crate) fn find_refs 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 = locs.into_iter() + let matched_locs: Vec = locs + .into_iter() .filter(|ref_loc| { // from location to real def // return if the real def location matches the def_loc @@ -33,13 +34,11 @@ pub(crate) fn find_refs 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 { @@ -62,11 +61,11 @@ pub(crate) fn find_refs 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); @@ -87,7 +86,10 @@ mod tests { } fn setup_word_index_map(root: &str) -> HashMap>> { - 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] @@ -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"), @@ -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"), diff --git a/kclvm/tools/src/LSP/src/main_loop.rs b/kclvm/tools/src/LSP/src/main_loop.rs index d3ea01386..b0f8b16c1 100644 --- a/kclvm/tools/src/LSP/src/main_loop.rs +++ b/kclvm/tools/src/LSP/src/main_loop.rs @@ -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) } diff --git a/kclvm/tools/src/LSP/src/state.rs b/kclvm/tools/src/LSP/src/state.rs index c51365826..cccf948ad 100644 --- a/kclvm/tools/src/LSP/src/state.rs +++ b/kclvm/tools/src/LSP/src/state.rs @@ -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}; @@ -88,14 +87,18 @@ pub(crate) struct LanguageServerSnapshot { #[allow(unused)] impl LanguageServerState { - pub fn new(sender: Sender, config: Config, initialize_params: InitializeParams) -> Self { + pub fn new( + sender: Sender, + config: Config, + initialize_params: InitializeParams, + ) -> Self { let (task_sender, task_receiver) = unbounded::(); let (vfs_sender, receiver) = unbounded::(); 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; - + // build word index for all the workspace folders // todo: async let mut word_index_map = HashMap::new(); @@ -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, } } diff --git a/kclvm/tools/src/LSP/src/tests.rs b/kclvm/tools/src/LSP/src/tests.rs index d8cc6b53c..57c5f06a3 100644 --- a/kclvm/tools/src/LSP/src/tests.rs +++ b/kclvm/tools/src/LSP/src/tests.rs @@ -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; @@ -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; @@ -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(), }]);