Reduce visibility

This commit is contained in:
Aleksey Kladov 2020-06-25 00:35:22 +02:00
parent 19b063e055
commit 012a7e57b9
3 changed files with 29 additions and 40 deletions

View File

@ -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()
}

View File

@ -37,12 +37,32 @@ use serde::de::DeserializeOwned;
pub type Result<T, E = Box<dyn std::error::Error + Send + Sync>> = std::result::Result<T, E>;
pub use crate::{
caps::server_capabilities,
main_loop::LspError,
main_loop::{main_loop, show_message},
};
use std::fmt;
pub fn from_json<T: DeserializeOwned>(what: &'static str, json: serde_json::Value) -> Result<T> {
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 {}

View File

@ -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::<LspError>() {
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(