From 012a7e57b9744bd8a40bdfe45ddb56954d559117 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 25 Jun 2020 00:35:22 +0200 Subject: Reduce visibility --- crates/rust-analyzer/src/global_state.rs | 10 ++++----- crates/rust-analyzer/src/lib.rs | 22 ++++++++++++++++++- crates/rust-analyzer/src/main_loop.rs | 37 +++----------------------------- 3 files changed, 29 insertions(+), 40 deletions(-) diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index ad5f94e87..0b42b88ac 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -167,7 +167,7 @@ impl GlobalState { res } - pub fn update_configuration(&mut self, config: Config) { + pub(crate) fn update_configuration(&mut self, config: Config) { self.analysis_host.update_lru_capacity(config.lru_capacity); if config.check != self.config.check { self.flycheck = @@ -177,7 +177,7 @@ impl GlobalState { self.config = config; } - pub fn process_changes(&mut self) -> bool { + pub(crate) fn process_changes(&mut self) -> bool { let change = { let mut change = AnalysisChange::new(); let (vfs, line_endings_map) = &mut *self.vfs.write(); @@ -215,7 +215,7 @@ impl GlobalState { true } - pub fn snapshot(&self) -> GlobalStateSnapshot { + pub(crate) fn snapshot(&self) -> GlobalStateSnapshot { GlobalStateSnapshot { config: self.config.clone(), workspaces: Arc::clone(&self.workspaces), @@ -226,11 +226,11 @@ impl GlobalState { } } - pub fn maybe_collect_garbage(&mut self) { + pub(crate) fn maybe_collect_garbage(&mut self) { self.analysis_host.maybe_collect_garbage() } - pub fn collect_garbage(&mut self) { + pub(crate) fn collect_garbage(&mut self) { self.analysis_host.collect_garbage() } diff --git a/crates/rust-analyzer/src/lib.rs b/crates/rust-analyzer/src/lib.rs index 9757a16a3..d6cd04303 100644 --- a/crates/rust-analyzer/src/lib.rs +++ b/crates/rust-analyzer/src/lib.rs @@ -37,12 +37,32 @@ use serde::de::DeserializeOwned; pub type Result> = std::result::Result; pub use crate::{ caps::server_capabilities, - main_loop::LspError, main_loop::{main_loop, show_message}, }; +use std::fmt; pub fn from_json(what: &'static str, json: serde_json::Value) -> Result { let res = T::deserialize(&json) .map_err(|e| format!("Failed to deserialize {}: {}; {}", what, e, json))?; Ok(res) } + +#[derive(Debug)] +struct LspError { + code: i32, + message: String, +} + +impl LspError { + fn new(code: i32, message: String) -> LspError { + LspError { code, message } + } +} + +impl fmt::Display for LspError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Language Server request failed with {}. ({})", self.code, self.message) + } +} + +impl std::error::Error for LspError {} diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 02e188b02..1787e8c16 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -1,9 +1,7 @@ //! The main loop of `rust-analyzer` responsible for dispatching LSP //! requests/replies and notifications back to the client. use std::{ - env, - error::Error, - fmt, + env, fmt, ops::Range, panic, sync::Arc, @@ -28,31 +26,9 @@ use crate::{ global_state::{file_id_to_url, GlobalState, GlobalStateSnapshot, Status}, handlers, lsp_ext, request_metrics::RequestMetrics, - Result, + LspError, Result, }; -#[derive(Debug)] -pub struct LspError { - pub code: i32, - pub message: String, -} - -impl LspError { - pub const UNKNOWN_FILE: i32 = -32900; - - pub fn new(code: i32, message: String) -> LspError { - LspError { code, message } - } -} - -impl fmt::Display for LspError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Language Server request failed with {}. ({})", self.code, self.message) - } -} - -impl Error for LspError {} - pub fn main_loop(config: Config, connection: Connection) -> Result<()> { log::info!("initial config: {:#?}", config); @@ -848,14 +824,7 @@ where let response = match result { Ok(resp) => Response::new_ok(id, &resp), Err(e) => match e.downcast::() { - Ok(lsp_error) => { - if lsp_error.code == LspError::UNKNOWN_FILE { - // Work-around for https://github.com/rust-analyzer/rust-analyzer/issues/1521 - Response::new_ok(id, ()) - } else { - Response::new_err(id, lsp_error.code, lsp_error.message) - } - } + Ok(lsp_error) => Response::new_err(id, lsp_error.code, lsp_error.message), Err(e) => { if is_canceled(&e) { Response::new_err( -- cgit v1.2.3