Skip to main content

ra_ap_rust_analyzer/
lib.rs

1//! Implementation of the LSP for rust-analyzer.
2//!
3//! This crate takes Rust-specific analysis results from 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
12#![cfg_attr(feature = "in-rust-tree", feature(rustc_private))]
13
14#[cfg(feature = "in-rust-tree")]
15extern crate rustc_driver as _;
16
17extern crate ra_ap_rustc_type_ir as rustc_type_ir;
18
19/*
20    If you bump this, grep for `FIXME(MINIMUM_SUPPORTED_TOOLCHAIN_VERSION)` to check for old support code we can drop
21*/
22/// Any toolchain less than this version will likely not work with rust-analyzer built from this revision.
23pub const MINIMUM_SUPPORTED_TOOLCHAIN_VERSION: semver::Version = semver::Version {
24    major: 1,
25    minor: 78,
26    patch: 0,
27    pre: semver::Prerelease::EMPTY,
28    build: semver::BuildMetadata::EMPTY,
29};
30
31pub mod cli;
32
33mod command;
34mod diagnostics;
35mod discover;
36mod flycheck;
37mod line_index;
38mod main_loop;
39mod mem_docs;
40mod op_queue;
41mod reload;
42mod target_spec;
43mod task_pool;
44mod test_runner;
45mod version;
46
47mod handlers {
48    pub(crate) mod dispatch;
49    pub(crate) mod notification;
50    pub(crate) mod request;
51}
52
53pub mod tracing {
54    pub mod config;
55    pub mod json;
56    pub use config::Config;
57    pub mod hprof;
58}
59
60pub mod config;
61mod global_state;
62pub mod lsp;
63
64use self::lsp::ext as lsp_ext;
65
66#[cfg(test)]
67mod integrated_benchmarks;
68
69use serde::de::DeserializeOwned;
70
71pub use crate::{
72    lsp::capabilities::server_capabilities, main_loop::main_loop, reload::ws_to_crate_graph,
73    version::version,
74};
75
76pub fn from_json<T: DeserializeOwned>(what: &str, json: &serde_json::Value) -> anyhow::Result<T> {
77    serde_json::from_value(json.clone())
78        .map_err(|e| anyhow::format_err!("Failed to deserialize {what}: {e}; {json}"))
79}
80
81#[doc(hidden)]
82macro_rules! try_default_ {
83    ($it:expr $(,)?) => {
84        match $it {
85            Some(it) => it,
86            None => return Ok(Default::default()),
87        }
88    };
89}
90pub(crate) use try_default_ as try_default;
91
92#[cfg(feature = "dhat")]
93#[global_allocator]
94static ALLOC: dhat::Alloc = dhat::Alloc;
95
96#[cfg(feature = "dhat")]
97static DHAT_PROFILER: std::sync::Mutex<Option<dhat::Profiler>> = std::sync::Mutex::new(None);