]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/lib.rs
Add rustc-perf to metrics
[rust.git] / crates / rust-analyzer / src / lib.rs
1 //! Implementation of the LSP for rust-analyzer.
2 //!
3 //! This crate takes Rust-specific analysis results from ra_ide and translates
4 //! into LSP types.
5 //!
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.
8 //!
9 //! The `cli` submodule implements some batch-processing analysis, primarily as
10 //! a debugging aid.
11 #![recursion_limit = "512"]
12
13 pub mod cli;
14
15 #[allow(unused)]
16 macro_rules! eprintln {
17     ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
18 }
19
20 mod global_state;
21 mod reload;
22 mod main_loop;
23 mod dispatch;
24 mod handlers;
25 mod caps;
26 mod cargo_target_spec;
27 mod to_proto;
28 mod from_proto;
29 mod semantic_tokens;
30 mod markdown;
31 mod diagnostics;
32 mod line_endings;
33 mod request_metrics;
34 mod lsp_utils;
35 mod thread_pool;
36 mod document;
37 pub mod lsp_ext;
38 pub mod config;
39
40 use serde::de::DeserializeOwned;
41
42 pub type Result<T, E = Box<dyn std::error::Error + Send + Sync>> = std::result::Result<T, E>;
43 pub use crate::{caps::server_capabilities, main_loop::main_loop};
44 use ra_ide::AnalysisHost;
45 use std::fmt;
46 use vfs::Vfs;
47
48 pub fn from_json<T: DeserializeOwned>(what: &'static str, json: serde_json::Value) -> Result<T> {
49     let res = T::deserialize(&json)
50         .map_err(|e| format!("Failed to deserialize {}: {}; {}", what, e, json))?;
51     Ok(res)
52 }
53
54 #[derive(Debug)]
55 struct LspError {
56     code: i32,
57     message: String,
58 }
59
60 impl LspError {
61     fn new(code: i32, message: String) -> LspError {
62         LspError { code, message }
63     }
64 }
65
66 impl fmt::Display for LspError {
67     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68         write!(f, "Language Server request failed with {}. ({})", self.code, self.message)
69     }
70 }
71
72 impl std::error::Error for LspError {}
73
74 fn print_memory_usage(mut host: AnalysisHost, vfs: Vfs) {
75     let mut mem = host.per_query_memory_usage();
76
77     let before = ra_prof::memory_usage();
78     drop(vfs);
79     let vfs = before.allocated - ra_prof::memory_usage().allocated;
80     mem.push(("VFS".into(), vfs));
81
82     let before = ra_prof::memory_usage();
83     drop(host);
84     mem.push(("Unaccounted".into(), before.allocated - ra_prof::memory_usage().allocated));
85
86     mem.push(("Remaining".into(), ra_prof::memory_usage().allocated));
87
88     for (name, bytes) in mem {
89         eprintln!("{:>8} {}", bytes, name);
90     }
91 }