1 //! The context or environment in which the language server functions. In our
2 //! server implementation this is know as the `WorldState`.
3 //!
4 //! Each tick provides an immutable snapshot of the state as `WorldSnapshot`.
5 
6 use std::{sync::Arc, time::Instant};
7 
8 use crossbeam_channel::{unbounded, Receiver, Sender};
9 use flycheck::FlycheckHandle;
10 use ide::{Analysis, AnalysisHost, Cancellable, Change, FileId};
11 use ide_db::base_db::{CrateId, FileLoader, SourceDatabase};
12 use lsp_types::{SemanticTokens, Url};
13 use parking_lot::{Mutex, RwLock};
14 use proc_macro_api::ProcMacroServer;
15 use project_model::{CargoWorkspace, ProjectWorkspace, Target, WorkspaceBuildScripts};
16 use rustc_hash::FxHashMap;
17 use vfs::AnchoredPathBuf;
18 
19 use crate::{
20     config::Config,
21     diagnostics::{CheckFixes, DiagnosticCollection},
22     from_proto,
23     line_index::{LineEndings, LineIndex},
24     lsp_ext,
25     main_loop::Task,
26     mem_docs::MemDocs,
27     op_queue::OpQueue,
28     reload::{self, SourceRootConfig},
29     thread_pool::TaskPool,
30     to_proto::url_from_abs_path,
31     Result,
32 };
33 
34 // Enforces drop order
35 pub(crate) struct Handle<H, C> {
36     pub(crate) handle: H,
37     pub(crate) receiver: C,
38 }
39 
40 pub(crate) type ReqHandler = fn(&mut GlobalState, lsp_server::Response);
41 pub(crate) type ReqQueue = lsp_server::ReqQueue<(String, Instant), ReqHandler>;
42 
43 /// `GlobalState` is the primary mutable state of the language server
44 ///
45 /// The most interesting components are `vfs`, which stores a consistent
46 /// snapshot of the file systems, and `analysis_host`, which stores our
47 /// incremental salsa database.
48 ///
49 /// Note that this struct has more than on impl in various modules!
50 pub(crate) struct GlobalState {
51     sender: Sender<lsp_server::Message>,
52     req_queue: ReqQueue,
53     pub(crate) task_pool: Handle<TaskPool<Task>, Receiver<Task>>,
54     pub(crate) loader: Handle<Box<dyn vfs::loader::Handle>, Receiver<vfs::loader::Message>>,
55     pub(crate) config: Arc<Config>,
56     pub(crate) analysis_host: AnalysisHost,
57     pub(crate) diagnostics: DiagnosticCollection,
58     pub(crate) mem_docs: MemDocs,
59     pub(crate) semantic_tokens_cache: Arc<Mutex<FxHashMap<Url, SemanticTokens>>>,
60     pub(crate) shutdown_requested: bool,
61     pub(crate) proc_macro_changed: bool,
62     pub(crate) last_reported_status: Option<lsp_ext::ServerStatusParams>,
63     pub(crate) source_root_config: SourceRootConfig,
64     pub(crate) proc_macro_client: Option<ProcMacroServer>,
65 
66     pub(crate) flycheck: Vec<FlycheckHandle>,
67     pub(crate) flycheck_sender: Sender<flycheck::Message>,
68     pub(crate) flycheck_receiver: Receiver<flycheck::Message>,
69 
70     pub(crate) vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>,
71     pub(crate) vfs_config_version: u32,
72     pub(crate) vfs_progress_config_version: u32,
73     pub(crate) vfs_progress_n_total: usize,
74     pub(crate) vfs_progress_n_done: usize,
75 
76     /// `workspaces` field stores the data we actually use, while the `OpQueue`
77     /// stores the result of the last fetch.
78     ///
79     /// If the fetch (partially) fails, we do not update the current value.
80     ///
81     /// The handling of build data is subtle. We fetch workspace in two phases:
82     ///
83     /// *First*, we run `cargo metadata`, which gives us fast results for
84     /// initial analysis.
85     ///
86     /// *Second*, we run `cargo check` which runs build scripts and compiles
87     /// proc macros.
88     ///
89     /// We need both for the precise analysis, but we want rust-analyzer to be
90     /// at least partially available just after the first phase. That's because
91     /// first phase is much faster, and is much less likely to fail.
92     ///
93     /// This creates a complication -- by the time the second phase completes,
94     /// the results of the fist phase could be invalid. That is, while we run
95     /// `cargo check`, the user edits `Cargo.toml`, we notice this, and the new
96     /// `cargo metadata` completes before `cargo check`.
97     ///
98     /// An additional complication is that we want to avoid needless work. When
99     /// the user just adds comments or whitespace to Cargo.toml, we do not want
100     /// to invalidate any salsa caches.
101     pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>,
102     pub(crate) fetch_workspaces_queue: OpQueue<Vec<anyhow::Result<ProjectWorkspace>>>,
103     pub(crate) fetch_build_data_queue:
104         OpQueue<(Arc<Vec<ProjectWorkspace>>, Vec<anyhow::Result<WorkspaceBuildScripts>>)>,
105 
106     pub(crate) prime_caches_queue: OpQueue<()>,
107 }
108 
109 /// An immutable snapshot of the world's state at a point in time.
110 pub(crate) struct GlobalStateSnapshot {
111     pub(crate) config: Arc<Config>,
112     pub(crate) analysis: Analysis,
113     pub(crate) check_fixes: CheckFixes,
114     mem_docs: MemDocs,
115     pub(crate) semantic_tokens_cache: Arc<Mutex<FxHashMap<Url, SemanticTokens>>>,
116     vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>,
117     pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>,
118 }
119 
120 impl std::panic::UnwindSafe for GlobalStateSnapshot {}
121 
122 impl GlobalState {
new(sender: Sender<lsp_server::Message>, config: Config) -> GlobalState123     pub(crate) fn new(sender: Sender<lsp_server::Message>, config: Config) -> GlobalState {
124         let loader = {
125             let (sender, receiver) = unbounded::<vfs::loader::Message>();
126             let handle: vfs_notify::NotifyHandle =
127                 vfs::loader::Handle::spawn(Box::new(move |msg| sender.send(msg).unwrap()));
128             let handle = Box::new(handle) as Box<dyn vfs::loader::Handle>;
129             Handle { handle, receiver }
130         };
131 
132         let task_pool = {
133             let (sender, receiver) = unbounded();
134             let handle = TaskPool::new(sender);
135             Handle { handle, receiver }
136         };
137 
138         let analysis_host = AnalysisHost::new(config.lru_capacity());
139         let (flycheck_sender, flycheck_receiver) = unbounded();
140         let mut this = GlobalState {
141             sender,
142             req_queue: ReqQueue::default(),
143             task_pool,
144             loader,
145             config: Arc::new(config.clone()),
146             analysis_host,
147             diagnostics: Default::default(),
148             mem_docs: MemDocs::default(),
149             semantic_tokens_cache: Arc::new(Default::default()),
150             shutdown_requested: false,
151             proc_macro_changed: false,
152             last_reported_status: None,
153             source_root_config: SourceRootConfig::default(),
154             proc_macro_client: None,
155 
156             flycheck: Vec::new(),
157             flycheck_sender,
158             flycheck_receiver,
159 
160             vfs: Arc::new(RwLock::new((vfs::Vfs::default(), FxHashMap::default()))),
161             vfs_config_version: 0,
162             vfs_progress_config_version: 0,
163             vfs_progress_n_total: 0,
164             vfs_progress_n_done: 0,
165 
166             workspaces: Arc::new(Vec::new()),
167             fetch_workspaces_queue: OpQueue::default(),
168             prime_caches_queue: OpQueue::default(),
169 
170             fetch_build_data_queue: OpQueue::default(),
171         };
172         // Apply any required database inputs from the config.
173         this.update_configuration(config);
174         this
175     }
176 
process_changes(&mut self) -> bool177     pub(crate) fn process_changes(&mut self) -> bool {
178         let _p = profile::span("GlobalState::process_changes");
179         let mut fs_changes = Vec::new();
180         // A file was added or deleted
181         let mut has_structure_changes = false;
182 
183         let change = {
184             let mut change = Change::new();
185             let (vfs, line_endings_map) = &mut *self.vfs.write();
186             let changed_files = vfs.take_changes();
187             if changed_files.is_empty() {
188                 return false;
189             }
190 
191             for file in changed_files {
192                 if !file.is_created_or_deleted() {
193                     let crates = self.analysis_host.raw_database().relevant_crates(file.file_id);
194                     let crate_graph = self.analysis_host.raw_database().crate_graph();
195 
196                     if crates.iter().any(|&krate| !crate_graph[krate].proc_macro.is_empty()) {
197                         self.proc_macro_changed = true;
198                     }
199                 }
200 
201                 if let Some(path) = vfs.file_path(file.file_id).as_path() {
202                     let path = path.to_path_buf();
203                     if reload::should_refresh_for_change(&path, file.change_kind) {
204                         self.fetch_workspaces_queue.request_op();
205                     }
206                     fs_changes.push((path, file.change_kind));
207                     if file.is_created_or_deleted() {
208                         has_structure_changes = true;
209                     }
210                 }
211 
212                 let text = if file.exists() {
213                     let bytes = vfs.file_contents(file.file_id).to_vec();
214                     match String::from_utf8(bytes).ok() {
215                         Some(text) => {
216                             let (text, line_endings) = LineEndings::normalize(text);
217                             line_endings_map.insert(file.file_id, line_endings);
218                             Some(Arc::new(text))
219                         }
220                         None => None,
221                     }
222                 } else {
223                     None
224                 };
225                 change.change_file(file.file_id, text);
226             }
227             if has_structure_changes {
228                 let roots = self.source_root_config.partition(vfs);
229                 change.set_roots(roots);
230             }
231             change
232         };
233 
234         self.analysis_host.apply_change(change);
235         true
236     }
237 
snapshot(&self) -> GlobalStateSnapshot238     pub(crate) fn snapshot(&self) -> GlobalStateSnapshot {
239         GlobalStateSnapshot {
240             config: Arc::clone(&self.config),
241             workspaces: Arc::clone(&self.workspaces),
242             analysis: self.analysis_host.analysis(),
243             vfs: Arc::clone(&self.vfs),
244             check_fixes: Arc::clone(&self.diagnostics.check_fixes),
245             mem_docs: self.mem_docs.clone(),
246             semantic_tokens_cache: Arc::clone(&self.semantic_tokens_cache),
247         }
248     }
249 
send_request<R: lsp_types::request::Request>( &mut self, params: R::Params, handler: ReqHandler, )250     pub(crate) fn send_request<R: lsp_types::request::Request>(
251         &mut self,
252         params: R::Params,
253         handler: ReqHandler,
254     ) {
255         let request = self.req_queue.outgoing.register(R::METHOD.to_string(), params, handler);
256         self.send(request.into());
257     }
complete_request(&mut self, response: lsp_server::Response)258     pub(crate) fn complete_request(&mut self, response: lsp_server::Response) {
259         let handler = self.req_queue.outgoing.complete(response.id.clone());
260         handler(self, response)
261     }
262 
send_notification<N: lsp_types::notification::Notification>( &mut self, params: N::Params, )263     pub(crate) fn send_notification<N: lsp_types::notification::Notification>(
264         &mut self,
265         params: N::Params,
266     ) {
267         let not = lsp_server::Notification::new(N::METHOD.to_string(), params);
268         self.send(not.into());
269     }
270 
register_request( &mut self, request: &lsp_server::Request, request_received: Instant, )271     pub(crate) fn register_request(
272         &mut self,
273         request: &lsp_server::Request,
274         request_received: Instant,
275     ) {
276         self.req_queue
277             .incoming
278             .register(request.id.clone(), (request.method.clone(), request_received));
279     }
respond(&mut self, response: lsp_server::Response)280     pub(crate) fn respond(&mut self, response: lsp_server::Response) {
281         if let Some((method, start)) = self.req_queue.incoming.complete(response.id.clone()) {
282             if let Some(err) = &response.error {
283                 if err.message.starts_with("server panicked") {
284                     self.poke_rust_analyzer_developer(format!("{}, check the log", err.message))
285                 }
286             }
287 
288             let duration = start.elapsed();
289             tracing::info!("handled {} - ({}) in {:0.2?}", method, response.id, duration);
290             self.send(response.into());
291         }
292     }
cancel(&mut self, request_id: lsp_server::RequestId)293     pub(crate) fn cancel(&mut self, request_id: lsp_server::RequestId) {
294         if let Some(response) = self.req_queue.incoming.cancel(request_id) {
295             self.send(response.into());
296         }
297     }
298 
send(&mut self, message: lsp_server::Message)299     fn send(&mut self, message: lsp_server::Message) {
300         self.sender.send(message).unwrap()
301     }
302 }
303 
304 impl Drop for GlobalState {
drop(&mut self)305     fn drop(&mut self) {
306         self.analysis_host.request_cancellation()
307     }
308 }
309 
310 impl GlobalStateSnapshot {
url_to_file_id(&self, url: &Url) -> Result<FileId>311     pub(crate) fn url_to_file_id(&self, url: &Url) -> Result<FileId> {
312         url_to_file_id(&self.vfs.read().0, url)
313     }
314 
file_id_to_url(&self, id: FileId) -> Url315     pub(crate) fn file_id_to_url(&self, id: FileId) -> Url {
316         file_id_to_url(&self.vfs.read().0, id)
317     }
318 
file_line_index(&self, file_id: FileId) -> Cancellable<LineIndex>319     pub(crate) fn file_line_index(&self, file_id: FileId) -> Cancellable<LineIndex> {
320         let endings = self.vfs.read().1[&file_id];
321         let index = self.analysis.file_line_index(file_id)?;
322         let res = LineIndex { index, endings, encoding: self.config.offset_encoding() };
323         Ok(res)
324     }
325 
url_file_version(&self, url: &Url) -> Option<i32>326     pub(crate) fn url_file_version(&self, url: &Url) -> Option<i32> {
327         let path = from_proto::vfs_path(url).ok()?;
328         Some(self.mem_docs.get(&path)?.version)
329     }
330 
anchored_path(&self, path: &AnchoredPathBuf) -> Url331     pub(crate) fn anchored_path(&self, path: &AnchoredPathBuf) -> Url {
332         let mut base = self.vfs.read().0.file_path(path.anchor);
333         base.pop();
334         let path = base.join(&path.path).unwrap();
335         let path = path.as_path().unwrap();
336         url_from_abs_path(path)
337     }
338 
cargo_target_for_crate_root( &self, crate_id: CrateId, ) -> Option<(&CargoWorkspace, Target)>339     pub(crate) fn cargo_target_for_crate_root(
340         &self,
341         crate_id: CrateId,
342     ) -> Option<(&CargoWorkspace, Target)> {
343         let file_id = self.analysis.crate_root(crate_id).ok()?;
344         let path = self.vfs.read().0.file_path(file_id);
345         let path = path.as_path()?;
346         self.workspaces.iter().find_map(|ws| match ws {
347             ProjectWorkspace::Cargo { cargo, .. } => {
348                 cargo.target_by_root(path).map(|it| (cargo, it))
349             }
350             ProjectWorkspace::Json { .. } => None,
351             ProjectWorkspace::DetachedFiles { .. } => None,
352         })
353     }
354 }
355 
file_id_to_url(vfs: &vfs::Vfs, id: FileId) -> Url356 pub(crate) fn file_id_to_url(vfs: &vfs::Vfs, id: FileId) -> Url {
357     let path = vfs.file_path(id);
358     let path = path.as_path().unwrap();
359     url_from_abs_path(path)
360 }
361 
url_to_file_id(vfs: &vfs::Vfs, url: &Url) -> Result<FileId>362 pub(crate) fn url_to_file_id(vfs: &vfs::Vfs, url: &Url) -> Result<FileId> {
363     let path = from_proto::vfs_path(url)?;
364     let res = vfs.file_id(&path).ok_or_else(|| format!("file not found: {}", path))?;
365     Ok(res)
366 }
367