]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/main_loop.rs
Auto merge of #12271 - bitgaoshu:box_with_expec, r=flodiebold
[rust.git] / crates / rust-analyzer / src / main_loop.rs
1 //! The main loop of `rust-analyzer` responsible for dispatching LSP
2 //! requests/replies and notifications back to the client.
3 use std::{
4     fmt,
5     sync::Arc,
6     time::{Duration, Instant},
7 };
8
9 use always_assert::always;
10 use crossbeam_channel::{select, Receiver};
11 use ide_db::base_db::{SourceDatabaseExt, VfsPath};
12 use lsp_server::{Connection, Notification, Request};
13 use lsp_types::notification::Notification as _;
14 use vfs::{ChangeKind, FileId};
15
16 use crate::{
17     config::Config,
18     dispatch::{NotificationDispatcher, RequestDispatcher},
19     from_proto,
20     global_state::{file_id_to_url, url_to_file_id, GlobalState},
21     handlers, lsp_ext,
22     lsp_utils::{apply_document_changes, is_cancelled, notification_is, Progress},
23     mem_docs::DocumentData,
24     reload::{self, BuildDataProgress, ProjectWorkspaceProgress},
25     Result,
26 };
27
28 pub fn main_loop(config: Config, connection: Connection) -> Result<()> {
29     tracing::info!("initial config: {:#?}", config);
30
31     // Windows scheduler implements priority boosts: if thread waits for an
32     // event (like a condvar), and event fires, priority of the thread is
33     // temporary bumped. This optimization backfires in our case: each time the
34     // `main_loop` schedules a task to run on a threadpool, the worker threads
35     // gets a higher priority, and (on a machine with fewer cores) displaces the
36     // main loop! We work-around this by marking the main loop as a
37     // higher-priority thread.
38     //
39     // https://docs.microsoft.com/en-us/windows/win32/procthread/scheduling-priorities
40     // https://docs.microsoft.com/en-us/windows/win32/procthread/priority-boosts
41     // https://github.com/rust-analyzer/rust-analyzer/issues/2835
42     #[cfg(windows)]
43     unsafe {
44         use winapi::um::processthreadsapi::*;
45         let thread = GetCurrentThread();
46         let thread_priority_above_normal = 1;
47         SetThreadPriority(thread, thread_priority_above_normal);
48     }
49
50     GlobalState::new(connection.sender, config).run(connection.receiver)
51 }
52
53 enum Event {
54     Lsp(lsp_server::Message),
55     Task(Task),
56     Vfs(vfs::loader::Message),
57     Flycheck(flycheck::Message),
58 }
59
60 #[derive(Debug)]
61 pub(crate) enum Task {
62     Response(lsp_server::Response),
63     Diagnostics(Vec<(FileId, Vec<lsp_types::Diagnostic>)>),
64     PrimeCaches(PrimeCachesProgress),
65     FetchWorkspace(ProjectWorkspaceProgress),
66     FetchBuildData(BuildDataProgress),
67 }
68
69 #[derive(Debug)]
70 pub(crate) enum PrimeCachesProgress {
71     Begin,
72     Report(ide::ParallelPrimeCachesProgress),
73     End { cancelled: bool },
74 }
75
76 impl fmt::Debug for Event {
77     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78         let debug_verbose_not = |not: &Notification, f: &mut fmt::Formatter| {
79             f.debug_struct("Notification").field("method", &not.method).finish()
80         };
81
82         match self {
83             Event::Lsp(lsp_server::Message::Notification(not)) => {
84                 if notification_is::<lsp_types::notification::DidOpenTextDocument>(not)
85                     || notification_is::<lsp_types::notification::DidChangeTextDocument>(not)
86                 {
87                     return debug_verbose_not(not, f);
88                 }
89             }
90             Event::Task(Task::Response(resp)) => {
91                 return f
92                     .debug_struct("Response")
93                     .field("id", &resp.id)
94                     .field("error", &resp.error)
95                     .finish();
96             }
97             _ => (),
98         }
99         match self {
100             Event::Lsp(it) => fmt::Debug::fmt(it, f),
101             Event::Task(it) => fmt::Debug::fmt(it, f),
102             Event::Vfs(it) => fmt::Debug::fmt(it, f),
103             Event::Flycheck(it) => fmt::Debug::fmt(it, f),
104         }
105     }
106 }
107
108 impl GlobalState {
109     fn run(mut self, inbox: Receiver<lsp_server::Message>) -> Result<()> {
110         if self.config.linked_projects().is_empty()
111             && self.config.detached_files().is_empty()
112             && self.config.notifications().cargo_toml_not_found
113         {
114             self.show_and_log_error("rust-analyzer failed to discover workspace".to_string(), None);
115         };
116
117         if self.config.did_save_text_document_dynamic_registration() {
118             let save_registration_options = lsp_types::TextDocumentSaveRegistrationOptions {
119                 include_text: Some(false),
120                 text_document_registration_options: lsp_types::TextDocumentRegistrationOptions {
121                     document_selector: Some(vec![
122                         lsp_types::DocumentFilter {
123                             language: None,
124                             scheme: None,
125                             pattern: Some("**/*.rs".into()),
126                         },
127                         lsp_types::DocumentFilter {
128                             language: None,
129                             scheme: None,
130                             pattern: Some("**/Cargo.toml".into()),
131                         },
132                         lsp_types::DocumentFilter {
133                             language: None,
134                             scheme: None,
135                             pattern: Some("**/Cargo.lock".into()),
136                         },
137                     ]),
138                 },
139             };
140
141             let registration = lsp_types::Registration {
142                 id: "textDocument/didSave".to_string(),
143                 method: "textDocument/didSave".to_string(),
144                 register_options: Some(serde_json::to_value(save_registration_options).unwrap()),
145             };
146             self.send_request::<lsp_types::request::RegisterCapability>(
147                 lsp_types::RegistrationParams { registrations: vec![registration] },
148                 |_, _| (),
149             );
150         }
151
152         self.fetch_workspaces_queue.request_op("startup".to_string());
153         if let Some(cause) = self.fetch_workspaces_queue.should_start_op() {
154             self.fetch_workspaces(cause);
155         }
156
157         while let Some(event) = self.next_event(&inbox) {
158             if let Event::Lsp(lsp_server::Message::Notification(not)) = &event {
159                 if not.method == lsp_types::notification::Exit::METHOD {
160                     return Ok(());
161                 }
162             }
163             self.handle_event(event)?
164         }
165
166         Err("client exited without proper shutdown sequence".into())
167     }
168
169     fn next_event(&self, inbox: &Receiver<lsp_server::Message>) -> Option<Event> {
170         select! {
171             recv(inbox) -> msg =>
172                 msg.ok().map(Event::Lsp),
173
174             recv(self.task_pool.receiver) -> task =>
175                 Some(Event::Task(task.unwrap())),
176
177             recv(self.loader.receiver) -> task =>
178                 Some(Event::Vfs(task.unwrap())),
179
180             recv(self.flycheck_receiver) -> task =>
181                 Some(Event::Flycheck(task.unwrap())),
182         }
183     }
184
185     fn handle_event(&mut self, event: Event) -> Result<()> {
186         let loop_start = Instant::now();
187         // NOTE: don't count blocking select! call as a loop-turn time
188         let _p = profile::span("GlobalState::handle_event");
189
190         tracing::debug!("handle_event({:?})", event);
191         let task_queue_len = self.task_pool.handle.len();
192         if task_queue_len > 0 {
193             tracing::info!("task queue len: {}", task_queue_len);
194         }
195
196         let was_quiescent = self.is_quiescent();
197         match event {
198             Event::Lsp(msg) => match msg {
199                 lsp_server::Message::Request(req) => self.on_request(loop_start, req)?,
200                 lsp_server::Message::Notification(not) => {
201                     self.on_notification(not)?;
202                 }
203                 lsp_server::Message::Response(resp) => self.complete_request(resp),
204             },
205             Event::Task(mut task) => {
206                 let _p = profile::span("GlobalState::handle_event/task");
207                 let mut prime_caches_progress = Vec::new();
208                 loop {
209                     match task {
210                         Task::Response(response) => self.respond(response),
211                         Task::Diagnostics(diagnostics_per_file) => {
212                             for (file_id, diagnostics) in diagnostics_per_file {
213                                 self.diagnostics.set_native_diagnostics(file_id, diagnostics)
214                             }
215                         }
216                         Task::PrimeCaches(progress) => match progress {
217                             PrimeCachesProgress::Begin => prime_caches_progress.push(progress),
218                             PrimeCachesProgress::Report(_) => {
219                                 match prime_caches_progress.last_mut() {
220                                     Some(last @ PrimeCachesProgress::Report(_)) => {
221                                         // Coalesce subsequent update events.
222                                         *last = progress;
223                                     }
224                                     _ => prime_caches_progress.push(progress),
225                                 }
226                             }
227                             PrimeCachesProgress::End { .. } => prime_caches_progress.push(progress),
228                         },
229                         Task::FetchWorkspace(progress) => {
230                             let (state, msg) = match progress {
231                                 ProjectWorkspaceProgress::Begin => (Progress::Begin, None),
232                                 ProjectWorkspaceProgress::Report(msg) => {
233                                     (Progress::Report, Some(msg))
234                                 }
235                                 ProjectWorkspaceProgress::End(workspaces) => {
236                                     self.fetch_workspaces_queue.op_completed(workspaces);
237
238                                     let old = Arc::clone(&self.workspaces);
239                                     self.switch_workspaces("fetched workspace".to_string());
240                                     let workspaces_updated = !Arc::ptr_eq(&old, &self.workspaces);
241
242                                     if self.config.run_build_scripts() && workspaces_updated {
243                                         self.fetch_build_data_queue
244                                             .request_op(format!("workspace updated"));
245                                     }
246
247                                     (Progress::End, None)
248                                 }
249                             };
250
251                             self.report_progress("Fetching", state, msg, None);
252                         }
253                         Task::FetchBuildData(progress) => {
254                             let (state, msg) = match progress {
255                                 BuildDataProgress::Begin => (Some(Progress::Begin), None),
256                                 BuildDataProgress::Report(msg) => {
257                                     (Some(Progress::Report), Some(msg))
258                                 }
259                                 BuildDataProgress::End(build_data_result) => {
260                                     self.fetch_build_data_queue.op_completed(build_data_result);
261
262                                     self.switch_workspaces("fetched build data".to_string());
263
264                                     (Some(Progress::End), None)
265                                 }
266                             };
267
268                             if let Some(state) = state {
269                                 self.report_progress("Loading", state, msg, None);
270                             }
271                         }
272                     }
273
274                     // Coalesce multiple task events into one loop turn
275                     task = match self.task_pool.receiver.try_recv() {
276                         Ok(task) => task,
277                         Err(_) => break,
278                     };
279                 }
280
281                 for progress in prime_caches_progress {
282                     let (state, message, fraction);
283                     match progress {
284                         PrimeCachesProgress::Begin => {
285                             state = Progress::Begin;
286                             message = None;
287                             fraction = 0.0;
288                         }
289                         PrimeCachesProgress::Report(report) => {
290                             state = Progress::Report;
291
292                             message = match &report.crates_currently_indexing[..] {
293                                 [crate_name] => Some(format!(
294                                     "{}/{} ({})",
295                                     report.crates_done, report.crates_total, crate_name
296                                 )),
297                                 [crate_name, rest @ ..] => Some(format!(
298                                     "{}/{} ({} + {} more)",
299                                     report.crates_done,
300                                     report.crates_total,
301                                     crate_name,
302                                     rest.len()
303                                 )),
304                                 _ => None,
305                             };
306
307                             fraction = Progress::fraction(report.crates_done, report.crates_total);
308                         }
309                         PrimeCachesProgress::End { cancelled } => {
310                             state = Progress::End;
311                             message = None;
312                             fraction = 1.0;
313
314                             self.prime_caches_queue.op_completed(());
315                             if cancelled {
316                                 self.prime_caches_queue
317                                     .request_op("restart after cancellation".to_string());
318                             }
319                         }
320                     };
321
322                     self.report_progress("Indexing", state, message, Some(fraction));
323                 }
324             }
325             Event::Vfs(mut task) => {
326                 let _p = profile::span("GlobalState::handle_event/vfs");
327                 loop {
328                     match task {
329                         vfs::loader::Message::Loaded { files } => {
330                             let vfs = &mut self.vfs.write().0;
331                             for (path, contents) in files {
332                                 let path = VfsPath::from(path);
333                                 if !self.mem_docs.contains(&path) {
334                                     vfs.set_file_contents(path, contents);
335                                 }
336                             }
337                         }
338                         vfs::loader::Message::Progress { n_total, n_done, config_version } => {
339                             always!(config_version <= self.vfs_config_version);
340
341                             self.vfs_progress_config_version = config_version;
342                             self.vfs_progress_n_total = n_total;
343                             self.vfs_progress_n_done = n_done;
344
345                             let state = if n_done == 0 {
346                                 Progress::Begin
347                             } else if n_done < n_total {
348                                 Progress::Report
349                             } else {
350                                 assert_eq!(n_done, n_total);
351                                 Progress::End
352                             };
353                             self.report_progress(
354                                 "Roots Scanned",
355                                 state,
356                                 Some(format!("{}/{}", n_done, n_total)),
357                                 Some(Progress::fraction(n_done, n_total)),
358                             )
359                         }
360                     }
361                     // Coalesce many VFS event into a single loop turn
362                     task = match self.loader.receiver.try_recv() {
363                         Ok(task) => task,
364                         Err(_) => break,
365                     }
366                 }
367             }
368             Event::Flycheck(mut task) => {
369                 let _p = profile::span("GlobalState::handle_event/flycheck");
370                 loop {
371                     match task {
372                         flycheck::Message::AddDiagnostic { workspace_root, diagnostic } => {
373                             let snap = self.snapshot();
374                             let diagnostics =
375                                 crate::diagnostics::to_proto::map_rust_diagnostic_to_lsp(
376                                     &self.config.diagnostics_map(),
377                                     &diagnostic,
378                                     &workspace_root,
379                                     &snap,
380                                 );
381                             for diag in diagnostics {
382                                 match url_to_file_id(&self.vfs.read().0, &diag.url) {
383                                     Ok(file_id) => self.diagnostics.add_check_diagnostic(
384                                         file_id,
385                                         diag.diagnostic,
386                                         diag.fix,
387                                     ),
388                                     Err(err) => {
389                                         tracing::error!(
390                                             "File with cargo diagnostic not found in VFS: {}",
391                                             err
392                                         );
393                                     }
394                                 };
395                             }
396                         }
397
398                         flycheck::Message::Progress { id, progress } => {
399                             let (state, message) = match progress {
400                                 flycheck::Progress::DidStart => {
401                                     self.diagnostics.clear_check();
402                                     (Progress::Begin, None)
403                                 }
404                                 flycheck::Progress::DidCheckCrate(target) => {
405                                     (Progress::Report, Some(target))
406                                 }
407                                 flycheck::Progress::DidCancel => (Progress::End, None),
408                                 flycheck::Progress::DidFinish(result) => {
409                                     if let Err(err) = result {
410                                         self.show_and_log_error(
411                                             "cargo check failed".to_string(),
412                                             Some(err.to_string()),
413                                         );
414                                     }
415                                     (Progress::End, None)
416                                 }
417                             };
418
419                             // When we're running multiple flychecks, we have to include a disambiguator in
420                             // the title, or the editor complains. Note that this is a user-facing string.
421                             let title = if self.flycheck.len() == 1 {
422                                 match self.config.flycheck() {
423                                     Some(config) => format!("{}", config),
424                                     None => "cargo check".to_string(),
425                                 }
426                             } else {
427                                 format!("cargo check (#{})", id + 1)
428                             };
429                             self.report_progress(&title, state, message, None);
430                         }
431                     }
432                     // Coalesce many flycheck updates into a single loop turn
433                     task = match self.flycheck_receiver.try_recv() {
434                         Ok(task) => task,
435                         Err(_) => break,
436                     }
437                 }
438             }
439         }
440
441         let state_changed = self.process_changes();
442         let memdocs_added_or_removed = self.mem_docs.take_changes();
443
444         if self.is_quiescent() {
445             if !was_quiescent {
446                 for flycheck in &self.flycheck {
447                     flycheck.update();
448                 }
449                 if self.config.prefill_caches() {
450                     self.prime_caches_queue.request_op("became quiescent".to_string());
451                 }
452             }
453
454             if !was_quiescent || state_changed {
455                 // Refresh semantic tokens if the client supports it.
456                 if self.config.semantic_tokens_refresh() {
457                     self.semantic_tokens_cache.lock().clear();
458                     self.send_request::<lsp_types::request::SemanticTokensRefresh>((), |_, _| ());
459                 }
460
461                 // Refresh code lens if the client supports it.
462                 if self.config.code_lens_refresh() {
463                     self.send_request::<lsp_types::request::CodeLensRefresh>((), |_, _| ());
464                 }
465             }
466
467             if !was_quiescent || state_changed || memdocs_added_or_removed {
468                 if self.config.publish_diagnostics() {
469                     self.update_diagnostics()
470                 }
471             }
472         }
473
474         if let Some(diagnostic_changes) = self.diagnostics.take_changes() {
475             for file_id in diagnostic_changes {
476                 let db = self.analysis_host.raw_database();
477                 let source_root = db.file_source_root(file_id);
478                 if db.source_root(source_root).is_library {
479                     // Only publish diagnostics for files in the workspace, not from crates.io deps
480                     // or the sysroot.
481                     // While theoretically these should never have errors, we have quite a few false
482                     // positives particularly in the stdlib, and those diagnostics would stay around
483                     // forever if we emitted them here.
484                     continue;
485                 }
486
487                 let url = file_id_to_url(&self.vfs.read().0, file_id);
488                 let diagnostics = self.diagnostics.diagnostics_for(file_id).cloned().collect();
489                 let version = from_proto::vfs_path(&url)
490                     .map(|path| self.mem_docs.get(&path).map(|it| it.version))
491                     .unwrap_or_default();
492
493                 self.send_notification::<lsp_types::notification::PublishDiagnostics>(
494                     lsp_types::PublishDiagnosticsParams { uri: url, diagnostics, version },
495                 );
496             }
497         }
498
499         if self.config.cargo_autoreload() {
500             if let Some(cause) = self.fetch_workspaces_queue.should_start_op() {
501                 self.fetch_workspaces(cause);
502             }
503         }
504
505         if !self.fetch_workspaces_queue.op_in_progress() {
506             if let Some(cause) = self.fetch_build_data_queue.should_start_op() {
507                 self.fetch_build_data(cause);
508             }
509         }
510
511         if let Some(cause) = self.prime_caches_queue.should_start_op() {
512             tracing::debug!(%cause, "will prime caches");
513             let num_worker_threads = self.config.prime_caches_num_threads();
514
515             self.task_pool.handle.spawn_with_sender({
516                 let analysis = self.snapshot().analysis;
517                 move |sender| {
518                     sender.send(Task::PrimeCaches(PrimeCachesProgress::Begin)).unwrap();
519                     let res = analysis.parallel_prime_caches(num_worker_threads, |progress| {
520                         let report = PrimeCachesProgress::Report(progress);
521                         sender.send(Task::PrimeCaches(report)).unwrap();
522                     });
523                     sender
524                         .send(Task::PrimeCaches(PrimeCachesProgress::End {
525                             cancelled: res.is_err(),
526                         }))
527                         .unwrap();
528                 }
529             });
530         }
531
532         let status = self.current_status();
533         if self.last_reported_status.as_ref() != Some(&status) {
534             self.last_reported_status = Some(status.clone());
535
536             if let (lsp_ext::Health::Error, Some(message)) = (status.health, &status.message) {
537                 self.show_message(lsp_types::MessageType::ERROR, message.clone());
538             }
539
540             if self.config.server_status_notification() {
541                 self.send_notification::<lsp_ext::ServerStatusNotification>(status);
542             }
543         }
544
545         let loop_duration = loop_start.elapsed();
546         if loop_duration > Duration::from_millis(100) && was_quiescent {
547             tracing::warn!("overly long loop turn: {:?}", loop_duration);
548             self.poke_rust_analyzer_developer(format!(
549                 "overly long loop turn: {:?}",
550                 loop_duration
551             ));
552         }
553         Ok(())
554     }
555
556     fn on_request(&mut self, request_received: Instant, req: Request) -> Result<()> {
557         self.register_request(&req, request_received);
558
559         if self.shutdown_requested {
560             self.respond(lsp_server::Response::new_err(
561                 req.id,
562                 lsp_server::ErrorCode::InvalidRequest as i32,
563                 "Shutdown already requested.".to_owned(),
564             ));
565
566             return Ok(());
567         }
568
569         // Avoid flashing a bunch of unresolved references during initial load.
570         if self.workspaces.is_empty() && !self.is_quiescent() {
571             self.respond(lsp_server::Response::new_err(
572                 req.id,
573                 lsp_server::ErrorCode::ContentModified as i32,
574                 "waiting for cargo metadata or cargo check".to_owned(),
575             ));
576             return Ok(());
577         }
578
579         RequestDispatcher { req: Some(req), global_state: self }
580             .on_sync_mut::<lsp_types::request::Shutdown>(|s, ()| {
581                 s.shutdown_requested = true;
582                 Ok(())
583             })?
584             .on_sync_mut::<lsp_ext::ReloadWorkspace>(handlers::handle_workspace_reload)?
585             .on_sync_mut::<lsp_ext::MemoryUsage>(handlers::handle_memory_usage)?
586             .on_sync_mut::<lsp_ext::ShuffleCrateGraph>(handlers::handle_shuffle_crate_graph)?
587             .on_sync::<lsp_ext::JoinLines>(handlers::handle_join_lines)?
588             .on_sync::<lsp_ext::OnEnter>(handlers::handle_on_enter)?
589             .on_sync::<lsp_types::request::SelectionRangeRequest>(handlers::handle_selection_range)?
590             .on_sync::<lsp_ext::MatchingBrace>(handlers::handle_matching_brace)?
591             .on::<lsp_ext::AnalyzerStatus>(handlers::handle_analyzer_status)
592             .on::<lsp_ext::SyntaxTree>(handlers::handle_syntax_tree)
593             .on::<lsp_ext::ViewHir>(handlers::handle_view_hir)
594             .on::<lsp_ext::ViewFileText>(handlers::handle_view_file_text)
595             .on::<lsp_ext::ViewCrateGraph>(handlers::handle_view_crate_graph)
596             .on::<lsp_ext::ViewItemTree>(handlers::handle_view_item_tree)
597             .on::<lsp_ext::ExpandMacro>(handlers::handle_expand_macro)
598             .on::<lsp_ext::ParentModule>(handlers::handle_parent_module)
599             .on::<lsp_ext::Runnables>(handlers::handle_runnables)
600             .on::<lsp_ext::RelatedTests>(handlers::handle_related_tests)
601             .on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action)
602             .on::<lsp_ext::CodeActionResolveRequest>(handlers::handle_code_action_resolve)
603             .on::<lsp_ext::HoverRequest>(handlers::handle_hover)
604             .on::<lsp_ext::ExternalDocs>(handlers::handle_open_docs)
605             .on::<lsp_ext::OpenCargoToml>(handlers::handle_open_cargo_toml)
606             .on::<lsp_ext::MoveItem>(handlers::handle_move_item)
607             .on::<lsp_ext::WorkspaceSymbol>(handlers::handle_workspace_symbol)
608             .on::<lsp_types::request::OnTypeFormatting>(handlers::handle_on_type_formatting)
609             .on::<lsp_types::request::DocumentSymbolRequest>(handlers::handle_document_symbol)
610             .on::<lsp_types::request::GotoDefinition>(handlers::handle_goto_definition)
611             .on::<lsp_types::request::GotoDeclaration>(handlers::handle_goto_declaration)
612             .on::<lsp_types::request::GotoImplementation>(handlers::handle_goto_implementation)
613             .on::<lsp_types::request::GotoTypeDefinition>(handlers::handle_goto_type_definition)
614             .on::<lsp_types::request::InlayHintRequest>(handlers::handle_inlay_hints)
615             .on::<lsp_types::request::Completion>(handlers::handle_completion)
616             .on::<lsp_types::request::ResolveCompletionItem>(handlers::handle_completion_resolve)
617             .on::<lsp_types::request::CodeLensRequest>(handlers::handle_code_lens)
618             .on::<lsp_types::request::CodeLensResolve>(handlers::handle_code_lens_resolve)
619             .on::<lsp_types::request::FoldingRangeRequest>(handlers::handle_folding_range)
620             .on::<lsp_types::request::SignatureHelpRequest>(handlers::handle_signature_help)
621             .on::<lsp_types::request::PrepareRenameRequest>(handlers::handle_prepare_rename)
622             .on::<lsp_types::request::Rename>(handlers::handle_rename)
623             .on::<lsp_types::request::References>(handlers::handle_references)
624             .on::<lsp_types::request::Formatting>(handlers::handle_formatting)
625             .on::<lsp_types::request::RangeFormatting>(handlers::handle_range_formatting)
626             .on::<lsp_types::request::DocumentHighlightRequest>(handlers::handle_document_highlight)
627             .on::<lsp_types::request::CallHierarchyPrepare>(handlers::handle_call_hierarchy_prepare)
628             .on::<lsp_types::request::CallHierarchyIncomingCalls>(
629                 handlers::handle_call_hierarchy_incoming,
630             )
631             .on::<lsp_types::request::CallHierarchyOutgoingCalls>(
632                 handlers::handle_call_hierarchy_outgoing,
633             )
634             .on::<lsp_types::request::SemanticTokensFullRequest>(
635                 handlers::handle_semantic_tokens_full,
636             )
637             .on::<lsp_types::request::SemanticTokensFullDeltaRequest>(
638                 handlers::handle_semantic_tokens_full_delta,
639             )
640             .on::<lsp_types::request::SemanticTokensRangeRequest>(
641                 handlers::handle_semantic_tokens_range,
642             )
643             .on::<lsp_types::request::WillRenameFiles>(handlers::handle_will_rename_files)
644             .on::<lsp_ext::Ssr>(handlers::handle_ssr)
645             .finish();
646         Ok(())
647     }
648     fn on_notification(&mut self, not: Notification) -> Result<()> {
649         NotificationDispatcher { not: Some(not), global_state: self }
650             .on::<lsp_types::notification::Cancel>(|this, params| {
651                 let id: lsp_server::RequestId = match params.id {
652                     lsp_types::NumberOrString::Number(id) => id.into(),
653                     lsp_types::NumberOrString::String(id) => id.into(),
654                 };
655                 this.cancel(id);
656                 Ok(())
657             })?
658             .on::<lsp_types::notification::WorkDoneProgressCancel>(|_this, _params| {
659                 // Just ignore this. It is OK to continue sending progress
660                 // notifications for this token, as the client can't know when
661                 // we accepted notification.
662                 Ok(())
663             })?
664             .on::<lsp_types::notification::DidOpenTextDocument>(|this, params| {
665                 if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) {
666                     if this
667                         .mem_docs
668                         .insert(path.clone(), DocumentData::new(params.text_document.version))
669                         .is_err()
670                     {
671                         tracing::error!("duplicate DidOpenTextDocument: {}", path)
672                     }
673                     this.vfs
674                         .write()
675                         .0
676                         .set_file_contents(path, Some(params.text_document.text.into_bytes()));
677                 }
678                 Ok(())
679             })?
680             .on::<lsp_types::notification::DidChangeTextDocument>(|this, params| {
681                 if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) {
682                     match this.mem_docs.get_mut(&path) {
683                         Some(doc) => {
684                             // The version passed in DidChangeTextDocument is the version after all edits are applied
685                             // so we should apply it before the vfs is notified.
686                             doc.version = params.text_document.version;
687                         }
688                         None => {
689                             tracing::error!("unexpected DidChangeTextDocument: {}; send DidOpenTextDocument first", path);
690                             return Ok(());
691                         }
692                     };
693
694                     let vfs = &mut this.vfs.write().0;
695                     let file_id = vfs.file_id(&path).unwrap();
696                     let mut text = String::from_utf8(vfs.file_contents(file_id).to_vec()).unwrap();
697                     apply_document_changes(&mut text, params.content_changes);
698
699                     vfs.set_file_contents(path, Some(text.into_bytes()));
700                 }
701                 Ok(())
702             })?
703             .on::<lsp_types::notification::DidCloseTextDocument>(|this, params| {
704                 if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) {
705                     if this.mem_docs.remove(&path).is_err() {
706                         tracing::error!("orphan DidCloseTextDocument: {}", path);
707                     }
708
709                     this.semantic_tokens_cache.lock().remove(&params.text_document.uri);
710
711                     if let Some(path) = path.as_path() {
712                         this.loader.handle.invalidate(path.to_path_buf());
713                     }
714                 }
715                 Ok(())
716             })?
717             .on::<lsp_types::notification::DidSaveTextDocument>(|this, params| {
718                 for flycheck in &this.flycheck {
719                     flycheck.update();
720                 }
721                 if let Ok(abs_path) = from_proto::abs_path(&params.text_document.uri) {
722                     if reload::should_refresh_for_change(&abs_path, ChangeKind::Modify) {
723                         this.fetch_workspaces_queue.request_op(format!("DidSaveTextDocument {}", abs_path.display()));
724                     }
725                 }
726                 Ok(())
727             })?
728             .on::<lsp_types::notification::DidChangeConfiguration>(|this, _params| {
729                 // As stated in https://github.com/microsoft/language-server-protocol/issues/676,
730                 // this notification's parameters should be ignored and the actual config queried separately.
731                 this.send_request::<lsp_types::request::WorkspaceConfiguration>(
732                     lsp_types::ConfigurationParams {
733                         items: vec![lsp_types::ConfigurationItem {
734                             scope_uri: None,
735                             section: Some("rust-analyzer".to_string()),
736                         }],
737                     },
738                     |this, resp| {
739                         tracing::debug!("config update response: '{:?}", resp);
740                         let lsp_server::Response { error, result, .. } = resp;
741
742                         match (error, result) {
743                             (Some(err), _) => {
744                                 tracing::error!("failed to fetch the server settings: {:?}", err)
745                             }
746                             (None, Some(mut configs)) => {
747                                 if let Some(json) = configs.get_mut(0) {
748                                     // Note that json can be null according to the spec if the client can't
749                                     // provide a configuration. This is handled in Config::update below.
750                                     let mut config = Config::clone(&*this.config);
751                                     if let Err(error) = config.update(json.take()) {
752                                         this.show_message(lsp_types::MessageType::WARNING, error.to_string());
753                                     }
754                                     this.update_configuration(config);
755                                 }
756                             }
757                             (None, None) => tracing::error!(
758                                 "received empty server settings response from the client"
759                             ),
760                         }
761                     },
762                 );
763
764                 Ok(())
765             })?
766             .on::<lsp_types::notification::DidChangeWatchedFiles>(|this, params| {
767                 for change in params.changes {
768                     if let Ok(path) = from_proto::abs_path(&change.uri) {
769                         this.loader.handle.invalidate(path);
770                     }
771                 }
772                 Ok(())
773             })?
774             .finish();
775         Ok(())
776     }
777
778     fn update_diagnostics(&mut self) {
779         let subscriptions = self
780             .mem_docs
781             .iter()
782             .map(|path| self.vfs.read().0.file_id(path).unwrap())
783             .collect::<Vec<_>>();
784
785         tracing::trace!("updating notifications for {:?}", subscriptions);
786
787         let snapshot = self.snapshot();
788         self.task_pool.handle.spawn(move || {
789             let diagnostics = subscriptions
790                 .into_iter()
791                 .filter_map(|file_id| {
792                     handlers::publish_diagnostics(&snapshot, file_id)
793                         .map_err(|err| {
794                             if !is_cancelled(&*err) {
795                                 tracing::error!("failed to compute diagnostics: {:?}", err);
796                             }
797                         })
798                         .ok()
799                         .map(|diags| (file_id, diags))
800                 })
801                 .collect::<Vec<_>>();
802             Task::Diagnostics(diagnostics)
803         })
804     }
805 }