1 //! Implementation of the LSP for rust-analyzer.
3 //! This crate takes Rust-specific analysis results from ra_ide and translates
6 //! It also is the root of all state. `world` module defines the bulk of the
7 //! state, and `main_loop` module defines the rules for modifying it.
9 //! The `cli` submodule implements some batch-processing analysis, primarily as
11 #![recursion_limit = "512"]
16 macro_rules! eprintln {
17 ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
26 mod cargo_target_spec;
39 use serde::de::DeserializeOwned;
41 pub type Result<T, E = Box<dyn std::error::Error + Send + Sync>> = std::result::Result<T, E>;
42 pub use crate::{caps::server_capabilities, main_loop::main_loop};
43 use ra_ide::AnalysisHost;
47 pub fn from_json<T: DeserializeOwned>(what: &'static str, json: serde_json::Value) -> Result<T> {
48 let res = T::deserialize(&json)
49 .map_err(|e| format!("Failed to deserialize {}: {}; {}", what, e, json))?;
60 fn new(code: i32, message: String) -> LspError {
61 LspError { code, message }
65 impl fmt::Display for LspError {
66 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
67 write!(f, "Language Server request failed with {}. ({})", self.code, self.message)
71 impl std::error::Error for LspError {}
73 fn print_memory_usage(mut host: AnalysisHost, vfs: Vfs) {
74 let mut mem = host.per_query_memory_usage();
76 let before = ra_prof::memory_usage();
78 let vfs = before.allocated - ra_prof::memory_usage().allocated;
79 mem.push(("VFS".into(), vfs));
81 let before = ra_prof::memory_usage();
83 mem.push(("Unaccounted".into(), before.allocated - ra_prof::memory_usage().allocated));
85 mem.push(("Remaining".into(), ra_prof::memory_usage().allocated));
87 for (name, bytes) in mem {
88 println!("{:>8} {}", bytes, name);