diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index e7eeb60eea..9a75cb2ab8 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -20,7 +20,7 @@ use crate::{ diagnostics::{CheckFixes, DiagnosticCollection}, from_proto, line_endings::LineEndings, - main_loop::request_metrics::{LatestRequests, RequestMetrics}, + request_metrics::{LatestRequests, RequestMetrics}, to_proto::url_from_abs_path, Result, }; @@ -46,32 +46,32 @@ fn create_flycheck(workspaces: &[ProjectWorkspace], config: &FlycheckConfig) -> /// snapshot of the file systems, and `analysis_host`, which stores our /// incremental salsa database. #[derive(Debug)] -pub struct GlobalState { - pub config: Config, - pub workspaces: Arc>, - pub analysis_host: AnalysisHost, - pub loader: Box, - pub task_receiver: Receiver, - pub flycheck: Option, - pub diagnostics: DiagnosticCollection, - pub proc_macro_client: ProcMacroClient, +pub(crate) struct GlobalState { + pub(crate) config: Config, + pub(crate) workspaces: Arc>, + pub(crate) analysis_host: AnalysisHost, + pub(crate) loader: Box, + pub(crate) task_receiver: Receiver, + pub(crate) flycheck: Option, + pub(crate) diagnostics: DiagnosticCollection, + pub(crate) proc_macro_client: ProcMacroClient, pub(crate) vfs: Arc)>>, pub(crate) latest_requests: Arc>, source_root_config: SourceRootConfig, } /// An immutable snapshot of the world's state at a point in time. -pub struct GlobalStateSnapshot { - pub config: Config, - pub workspaces: Arc>, - pub analysis: Analysis, - pub check_fixes: CheckFixes, +pub(crate) struct GlobalStateSnapshot { + pub(crate) config: Config, + pub(crate) workspaces: Arc>, + pub(crate) analysis: Analysis, + pub(crate) check_fixes: CheckFixes, pub(crate) latest_requests: Arc>, vfs: Arc)>>, } impl GlobalState { - pub fn new( + pub(crate) fn new( workspaces: Vec, lru_capacity: Option, config: Config, @@ -241,7 +241,7 @@ impl GlobalStateSnapshot { self.vfs.read().1[&id] } - pub fn anchored_path(&self, file_id: FileId, path: &str) -> Url { + pub(crate) fn anchored_path(&self, file_id: FileId, path: &str) -> Url { let mut base = self.vfs.read().0.file_path(file_id); base.pop(); let path = base.join(path); @@ -264,7 +264,7 @@ impl GlobalStateSnapshot { }) } - pub fn status(&self) -> String { + pub(crate) fn status(&self) -> String { let mut buf = String::new(); if self.workspaces.is_empty() { buf.push_str("no workspaces\n") @@ -349,7 +349,7 @@ pub(crate) struct SourceRootConfig { } impl SourceRootConfig { - pub fn partition(&self, vfs: &vfs::Vfs) -> Vec { + pub(crate) fn partition(&self, vfs: &vfs::Vfs) -> Vec { self.fsc .partition(vfs) .into_iter() diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/handlers.rs similarity index 96% rename from crates/rust-analyzer/src/main_loop/handlers.rs rename to crates/rust-analyzer/src/handlers.rs index a1e2432cff..b38755b79d 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -38,7 +38,7 @@ use crate::{ to_proto, LspError, Result, }; -pub fn handle_analyzer_status(snap: GlobalStateSnapshot, _: ()) -> Result { +pub(crate) fn handle_analyzer_status(snap: GlobalStateSnapshot, _: ()) -> Result { let _p = profile("handle_analyzer_status"); let mut buf = snap.status(); format_to!(buf, "\n\nrequests:\n"); @@ -50,7 +50,7 @@ pub fn handle_analyzer_status(snap: GlobalStateSnapshot, _: ()) -> Result Result { @@ -62,7 +62,7 @@ pub fn handle_syntax_tree( Ok(res) } -pub fn handle_expand_macro( +pub(crate) fn handle_expand_macro( snap: GlobalStateSnapshot, params: lsp_ext::ExpandMacroParams, ) -> Result> { @@ -75,7 +75,7 @@ pub fn handle_expand_macro( Ok(res.map(|it| lsp_ext::ExpandedMacro { name: it.name, expansion: it.expansion })) } -pub fn handle_selection_range( +pub(crate) fn handle_selection_range( snap: GlobalStateSnapshot, params: lsp_types::SelectionRangeParams, ) -> Result>> { @@ -118,7 +118,7 @@ pub fn handle_selection_range( Ok(Some(res?)) } -pub fn handle_matching_brace( +pub(crate) fn handle_matching_brace( snap: GlobalStateSnapshot, params: lsp_ext::MatchingBraceParams, ) -> Result> { @@ -140,7 +140,7 @@ pub fn handle_matching_brace( Ok(res) } -pub fn handle_join_lines( +pub(crate) fn handle_join_lines( snap: GlobalStateSnapshot, params: lsp_ext::JoinLinesParams, ) -> Result> { @@ -163,7 +163,7 @@ pub fn handle_join_lines( Ok(res) } -pub fn handle_on_enter( +pub(crate) fn handle_on_enter( snap: GlobalStateSnapshot, params: lsp_types::TextDocumentPositionParams, ) -> Result>> { @@ -180,7 +180,7 @@ pub fn handle_on_enter( } // Don't forget to add new trigger characters to `ServerCapabilities` in `caps.rs`. -pub fn handle_on_type_formatting( +pub(crate) fn handle_on_type_formatting( snap: GlobalStateSnapshot, params: lsp_types::DocumentOnTypeFormattingParams, ) -> Result>> { @@ -219,7 +219,7 @@ pub fn handle_on_type_formatting( Ok(Some(change)) } -pub fn handle_document_symbol( +pub(crate) fn handle_document_symbol( snap: GlobalStateSnapshot, params: lsp_types::DocumentSymbolParams, ) -> Result> { @@ -287,7 +287,7 @@ pub fn handle_document_symbol( } } -pub fn handle_workspace_symbol( +pub(crate) fn handle_workspace_symbol( snap: GlobalStateSnapshot, params: lsp_types::WorkspaceSymbolParams, ) -> Result>> { @@ -331,7 +331,7 @@ pub fn handle_workspace_symbol( } } -pub fn handle_goto_definition( +pub(crate) fn handle_goto_definition( snap: GlobalStateSnapshot, params: lsp_types::GotoDefinitionParams, ) -> Result> { @@ -346,7 +346,7 @@ pub fn handle_goto_definition( Ok(Some(res)) } -pub fn handle_goto_implementation( +pub(crate) fn handle_goto_implementation( snap: GlobalStateSnapshot, params: lsp_types::request::GotoImplementationParams, ) -> Result> { @@ -361,7 +361,7 @@ pub fn handle_goto_implementation( Ok(Some(res)) } -pub fn handle_goto_type_definition( +pub(crate) fn handle_goto_type_definition( snap: GlobalStateSnapshot, params: lsp_types::request::GotoTypeDefinitionParams, ) -> Result> { @@ -376,7 +376,7 @@ pub fn handle_goto_type_definition( Ok(Some(res)) } -pub fn handle_parent_module( +pub(crate) fn handle_parent_module( snap: GlobalStateSnapshot, params: lsp_types::TextDocumentPositionParams, ) -> Result> { @@ -387,7 +387,7 @@ pub fn handle_parent_module( Ok(Some(res)) } -pub fn handle_runnables( +pub(crate) fn handle_runnables( snap: GlobalStateSnapshot, params: lsp_ext::RunnablesParams, ) -> Result> { @@ -446,7 +446,7 @@ pub fn handle_runnables( Ok(res) } -pub fn handle_completion( +pub(crate) fn handle_completion( snap: GlobalStateSnapshot, params: lsp_types::CompletionParams, ) -> Result> { @@ -488,7 +488,7 @@ pub fn handle_completion( Ok(Some(items.into())) } -pub fn handle_folding_range( +pub(crate) fn handle_folding_range( snap: GlobalStateSnapshot, params: FoldingRangeParams, ) -> Result>> { @@ -505,7 +505,7 @@ pub fn handle_folding_range( Ok(Some(res)) } -pub fn handle_signature_help( +pub(crate) fn handle_signature_help( snap: GlobalStateSnapshot, params: lsp_types::SignatureHelpParams, ) -> Result> { @@ -529,7 +529,7 @@ pub fn handle_signature_help( })) } -pub fn handle_hover( +pub(crate) fn handle_hover( snap: GlobalStateSnapshot, params: lsp_types::HoverParams, ) -> Result> { @@ -555,7 +555,7 @@ pub fn handle_hover( Ok(Some(hover)) } -pub fn handle_prepare_rename( +pub(crate) fn handle_prepare_rename( snap: GlobalStateSnapshot, params: lsp_types::TextDocumentPositionParams, ) -> Result> { @@ -573,7 +573,7 @@ pub fn handle_prepare_rename( Ok(Some(PrepareRenameResponse::Range(range))) } -pub fn handle_rename( +pub(crate) fn handle_rename( snap: GlobalStateSnapshot, params: RenameParams, ) -> Result> { @@ -597,7 +597,7 @@ pub fn handle_rename( Ok(Some(workspace_edit)) } -pub fn handle_references( +pub(crate) fn handle_references( snap: GlobalStateSnapshot, params: lsp_types::ReferenceParams, ) -> Result>> { @@ -624,7 +624,7 @@ pub fn handle_references( Ok(Some(locations)) } -pub fn handle_formatting( +pub(crate) fn handle_formatting( snap: GlobalStateSnapshot, params: DocumentFormattingParams, ) -> Result>> { @@ -739,7 +739,7 @@ fn handle_fixes( Ok(()) } -pub fn handle_code_action( +pub(crate) fn handle_code_action( snap: GlobalStateSnapshot, params: lsp_types::CodeActionParams, ) -> Result>> { @@ -774,7 +774,7 @@ pub fn handle_code_action( Ok(Some(res)) } -pub fn handle_resolve_code_action( +pub(crate) fn handle_resolve_code_action( snap: GlobalStateSnapshot, params: lsp_ext::ResolveCodeActionParams, ) -> Result> { @@ -792,7 +792,7 @@ pub fn handle_resolve_code_action( Ok(to_proto::resolved_code_action(&snap, assist.clone())?.edit) } -pub fn handle_code_lens( +pub(crate) fn handle_code_lens( snap: GlobalStateSnapshot, params: lsp_types::CodeLensParams, ) -> Result>> { @@ -873,7 +873,7 @@ enum CodeLensResolveData { Impls(lsp_types::request::GotoImplementationParams), } -pub fn handle_code_lens_resolve( +pub(crate) fn handle_code_lens_resolve( snap: GlobalStateSnapshot, code_lens: CodeLens, ) -> Result { @@ -910,7 +910,7 @@ pub fn handle_code_lens_resolve( } } -pub fn handle_document_highlight( +pub(crate) fn handle_document_highlight( snap: GlobalStateSnapshot, params: lsp_types::DocumentHighlightParams, ) -> Result>> { @@ -937,7 +937,7 @@ pub fn handle_document_highlight( Ok(Some(res)) } -pub fn handle_ssr( +pub(crate) fn handle_ssr( snap: GlobalStateSnapshot, params: lsp_ext::SsrParams, ) -> Result { @@ -947,7 +947,10 @@ pub fn handle_ssr( to_proto::workspace_edit(&snap, source_change) } -pub fn publish_diagnostics(snap: &GlobalStateSnapshot, file_id: FileId) -> Result { +pub(crate) fn publish_diagnostics( + snap: &GlobalStateSnapshot, + file_id: FileId, +) -> Result { let _p = profile("publish_diagnostics"); let line_index = snap.analysis().file_line_index(file_id)?; let diagnostics: Vec = snap @@ -967,7 +970,7 @@ pub fn publish_diagnostics(snap: &GlobalStateSnapshot, file_id: FileId) -> Resul Ok(DiagnosticTask::SetNative(file_id, diagnostics)) } -pub fn handle_inlay_hints( +pub(crate) fn handle_inlay_hints( snap: GlobalStateSnapshot, params: InlayHintsParams, ) -> Result> { @@ -982,7 +985,7 @@ pub fn handle_inlay_hints( .collect()) } -pub fn handle_call_hierarchy_prepare( +pub(crate) fn handle_call_hierarchy_prepare( snap: GlobalStateSnapshot, params: CallHierarchyPrepareParams, ) -> Result>> { @@ -1004,7 +1007,7 @@ pub fn handle_call_hierarchy_prepare( Ok(Some(res)) } -pub fn handle_call_hierarchy_incoming( +pub(crate) fn handle_call_hierarchy_incoming( snap: GlobalStateSnapshot, params: CallHierarchyIncomingCallsParams, ) -> Result>> { @@ -1039,7 +1042,7 @@ pub fn handle_call_hierarchy_incoming( Ok(Some(res)) } -pub fn handle_call_hierarchy_outgoing( +pub(crate) fn handle_call_hierarchy_outgoing( snap: GlobalStateSnapshot, params: CallHierarchyOutgoingCallsParams, ) -> Result>> { @@ -1074,7 +1077,7 @@ pub fn handle_call_hierarchy_outgoing( Ok(Some(res)) } -pub fn handle_semantic_tokens( +pub(crate) fn handle_semantic_tokens( snap: GlobalStateSnapshot, params: SemanticTokensParams, ) -> Result> { @@ -1089,7 +1092,7 @@ pub fn handle_semantic_tokens( Ok(Some(semantic_tokens.into())) } -pub fn handle_semantic_tokens_range( +pub(crate) fn handle_semantic_tokens_range( snap: GlobalStateSnapshot, params: SemanticTokensRangeParams, ) -> Result> { diff --git a/crates/rust-analyzer/src/lib.rs b/crates/rust-analyzer/src/lib.rs index b38067079f..9757a16a38 100644 --- a/crates/rust-analyzer/src/lib.rs +++ b/crates/rust-analyzer/src/lib.rs @@ -17,18 +17,20 @@ macro_rules! eprintln { ($($tt:tt)*) => { stdx::eprintln!($($tt)*) }; } +mod global_state; +mod main_loop; +mod handlers; mod caps; mod cargo_target_spec; mod to_proto; mod from_proto; -mod main_loop; +mod semantic_tokens; mod markdown; +mod diagnostics; +mod line_endings; +mod request_metrics; pub mod lsp_ext; pub mod config; -mod global_state; -mod diagnostics; -mod semantic_tokens; -mod line_endings; use serde::de::DeserializeOwned; diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index b55d45cd37..c8819c3b00 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -1,9 +1,5 @@ //! The main loop of `rust-analyzer` responsible for dispatching LSP //! requests/replies and notifications back to the client. - -mod handlers; -pub(crate) mod request_metrics; - use std::{ env, error::Error, @@ -19,6 +15,7 @@ use lsp_server::{ Connection, ErrorCode, Message, Notification, ReqQueue, Request, RequestId, Response, }; use lsp_types::{request::Request as _, NumberOrString, TextDocumentContentChangeEvent}; +use ra_db::VfsPath; use ra_flycheck::CheckTask; use ra_ide::{Canceled, FileId, LineIndex}; use ra_prof::profile; @@ -32,11 +29,10 @@ use crate::{ diagnostics::DiagnosticTask, from_proto, global_state::{file_id_to_url, GlobalState, GlobalStateSnapshot}, - lsp_ext, - main_loop::request_metrics::RequestMetrics, + handlers, lsp_ext, + request_metrics::RequestMetrics, Result, }; -use ra_db::VfsPath; #[derive(Debug)] pub struct LspError { diff --git a/crates/rust-analyzer/src/main_loop/request_metrics.rs b/crates/rust-analyzer/src/request_metrics.rs similarity index 100% rename from crates/rust-analyzer/src/main_loop/request_metrics.rs rename to crates/rust-analyzer/src/request_metrics.rs