2019-11-22 07:33:08 +00:00
|
|
|
//! Implementation of the LSP for rust-analyzer.
|
|
|
|
//!
|
2020-02-18 11:11:32 +00:00
|
|
|
//! This crate takes Rust-specific analysis results from ra_ide and translates
|
|
|
|
//! into LSP types.
|
2019-11-22 07:33:08 +00:00
|
|
|
//!
|
|
|
|
//! It also is the root of all state. `world` module defines the bulk of the
|
|
|
|
//! state, and `main_loop` module defines the rules for modifying it.
|
2020-02-18 11:11:32 +00:00
|
|
|
//!
|
|
|
|
//! The `cli` submodule implements some batch-processing analysis, primarily as
|
|
|
|
//! a debugging aid.
|
2019-06-26 06:12:46 +00:00
|
|
|
#![recursion_limit = "512"]
|
2019-11-22 07:33:08 +00:00
|
|
|
|
2020-02-17 18:03:03 +00:00
|
|
|
pub mod cli;
|
|
|
|
|
2019-11-22 07:33:08 +00:00
|
|
|
#[allow(unused)]
|
2020-04-06 14:58:16 +00:00
|
|
|
macro_rules! eprintln {
|
|
|
|
($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
|
2019-11-22 07:33:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 16:57:30 +00:00
|
|
|
mod global_state;
|
2020-06-25 21:44:58 +00:00
|
|
|
mod reload;
|
2020-06-24 16:57:30 +00:00
|
|
|
mod main_loop;
|
2020-06-25 15:22:18 +00:00
|
|
|
mod dispatch;
|
2020-06-24 16:57:30 +00:00
|
|
|
mod handlers;
|
2018-09-01 15:16:08 +00:00
|
|
|
mod caps;
|
2019-01-12 18:00:58 +00:00
|
|
|
mod cargo_target_spec;
|
2020-05-10 11:55:24 +00:00
|
|
|
mod to_proto;
|
|
|
|
mod from_proto;
|
2020-06-24 16:57:30 +00:00
|
|
|
mod semantic_tokens;
|
2019-01-30 02:39:09 +00:00
|
|
|
mod markdown;
|
2020-01-31 18:23:25 +00:00
|
|
|
mod diagnostics;
|
2020-06-11 09:04:09 +00:00
|
|
|
mod line_endings;
|
2020-06-24 16:57:30 +00:00
|
|
|
mod request_metrics;
|
2020-06-25 06:01:03 +00:00
|
|
|
mod lsp_utils;
|
2020-06-25 13:35:42 +00:00
|
|
|
mod thread_pool;
|
2020-06-24 16:57:30 +00:00
|
|
|
pub mod lsp_ext;
|
|
|
|
pub mod config;
|
2018-09-01 15:16:08 +00:00
|
|
|
|
2020-02-11 08:46:45 +00:00
|
|
|
use serde::de::DeserializeOwned;
|
|
|
|
|
2020-06-20 21:08:01 +00:00
|
|
|
pub type Result<T, E = Box<dyn std::error::Error + Send + Sync>> = std::result::Result<T, E>;
|
2020-06-26 14:33:57 +00:00
|
|
|
pub use crate::{caps::server_capabilities, main_loop::main_loop};
|
2020-07-15 10:14:51 +00:00
|
|
|
use ra_ide::AnalysisHost;
|
2020-06-24 22:35:22 +00:00
|
|
|
use std::fmt;
|
2020-07-15 10:14:51 +00:00
|
|
|
use vfs::Vfs;
|
2020-02-11 08:46:45 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2020-06-24 22:35:22 +00:00
|
|
|
|
|
|
|
#[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 {}
|
2020-07-15 10:14:51 +00:00
|
|
|
|
|
|
|
fn print_memory_usage(mut host: AnalysisHost, vfs: Vfs) {
|
|
|
|
let mut mem = host.per_query_memory_usage();
|
|
|
|
|
|
|
|
let before = ra_prof::memory_usage();
|
|
|
|
drop(vfs);
|
|
|
|
let vfs = before.allocated - ra_prof::memory_usage().allocated;
|
|
|
|
mem.push(("VFS".into(), vfs));
|
|
|
|
|
|
|
|
let before = ra_prof::memory_usage();
|
|
|
|
drop(host);
|
|
|
|
mem.push(("Unaccounted".into(), before.allocated - ra_prof::memory_usage().allocated));
|
|
|
|
|
|
|
|
mem.push(("Remaining".into(), ra_prof::memory_usage().allocated));
|
|
|
|
|
|
|
|
for (name, bytes) in mem {
|
|
|
|
println!("{:>8} {}", bytes, name);
|
|
|
|
}
|
|
|
|
}
|