]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/lib.rs
Merge pull request #5447 from jethrogb/gitattributes
[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 pub mod lsp_ext;
37 pub mod config;
38
39 use serde::de::DeserializeOwned;
40
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;
44 use std::fmt;
45 use vfs::Vfs;
46
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))?;
50     Ok(res)
51 }
52
53 #[derive(Debug)]
54 struct LspError {
55     code: i32,
56     message: String,
57 }
58
59 impl LspError {
60     fn new(code: i32, message: String) -> LspError {
61         LspError { code, message }
62     }
63 }
64
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)
68     }
69 }
70
71 impl std::error::Error for LspError {}
72
73 fn print_memory_usage(mut host: AnalysisHost, vfs: Vfs) {
74     let mut mem = host.per_query_memory_usage();
75
76     let before = ra_prof::memory_usage();
77     drop(vfs);
78     let vfs = before.allocated - ra_prof::memory_usage().allocated;
79     mem.push(("VFS".into(), vfs));
80
81     let before = ra_prof::memory_usage();
82     drop(host);
83     mem.push(("Unaccounted".into(), before.allocated - ra_prof::memory_usage().allocated));
84
85     mem.push(("Remaining".into(), ra_prof::memory_usage().allocated));
86
87     for (name, bytes) in mem {
88         println!("{:>8} {}", bytes, name);
89     }
90 }