rust_analyzer/handlers/
notification.rs

1//! This module is responsible for implementing handlers for Language Server
2//! Protocol. This module specifically handles notifications.
3
4use std::{
5    ops::{Deref, Not as _},
6    panic::UnwindSafe,
7};
8
9use itertools::Itertools;
10use lsp_types::{
11    CancelParams, DidChangeConfigurationParams, DidChangeTextDocumentParams,
12    DidChangeWatchedFilesParams, DidChangeWorkspaceFoldersParams, DidCloseTextDocumentParams,
13    DidOpenTextDocumentParams, DidSaveTextDocumentParams, WorkDoneProgressCancelParams,
14};
15use paths::Utf8PathBuf;
16use triomphe::Arc;
17use vfs::{AbsPathBuf, ChangeKind, VfsPath};
18
19use crate::{
20    config::{Config, ConfigChange},
21    flycheck::{InvocationStrategy, Target},
22    global_state::{FetchWorkspaceRequest, GlobalState},
23    lsp::{from_proto, utils::apply_document_changes},
24    lsp_ext::{self, RunFlycheckParams},
25    mem_docs::DocumentData,
26    reload,
27    target_spec::TargetSpec,
28    try_default,
29};
30
31pub(crate) fn handle_cancel(state: &mut GlobalState, params: CancelParams) -> anyhow::Result<()> {
32    let id: lsp_server::RequestId = match params.id {
33        lsp_types::NumberOrString::Number(id) => id.into(),
34        lsp_types::NumberOrString::String(id) => id.into(),
35    };
36    state.cancel(id);
37    Ok(())
38}
39
40pub(crate) fn handle_work_done_progress_cancel(
41    state: &mut GlobalState,
42    params: WorkDoneProgressCancelParams,
43) -> anyhow::Result<()> {
44    if let lsp_types::NumberOrString::String(s) = &params.token
45        && let Some(id) = s.strip_prefix("rust-analyzer/flycheck/")
46        && let Ok(id) = id.parse::<u32>()
47        && let Some(flycheck) = state.flycheck.get(id as usize)
48    {
49        flycheck.cancel();
50    }
51
52    // Just ignore this. It is OK to continue sending progress
53    // notifications for this token, as the client can't know when
54    // we accepted notification.
55    Ok(())
56}
57
58pub(crate) fn handle_did_open_text_document(
59    state: &mut GlobalState,
60    params: DidOpenTextDocumentParams,
61) -> anyhow::Result<()> {
62    let _p = tracing::info_span!("handle_did_open_text_document").entered();
63
64    if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) {
65        let already_exists = state
66            .mem_docs
67            .insert(
68                path.clone(),
69                DocumentData::new(
70                    params.text_document.version,
71                    params.text_document.text.clone().into_bytes(),
72                ),
73            )
74            .is_err();
75        if already_exists {
76            tracing::error!("duplicate DidOpenTextDocument: {}", path);
77        }
78
79        if let Some(abs_path) = path.as_path()
80            && state.config.excluded().any(|excluded| abs_path.starts_with(&excluded))
81        {
82            tracing::trace!("opened excluded file {abs_path}");
83            state.vfs.write().0.insert_excluded_file(path);
84            return Ok(());
85        }
86
87        let contents = params.text_document.text.into_bytes();
88        state.vfs.write().0.set_file_contents(path, Some(contents));
89        if state.config.discover_workspace_config().is_some() {
90            tracing::debug!("queuing task");
91            let _ = state
92                .deferred_task_queue
93                .sender
94                .send(crate::main_loop::DeferredTask::CheckIfIndexed(params.text_document.uri));
95        }
96    }
97    Ok(())
98}
99
100pub(crate) fn handle_did_change_text_document(
101    state: &mut GlobalState,
102    params: DidChangeTextDocumentParams,
103) -> anyhow::Result<()> {
104    let _p = tracing::info_span!("handle_did_change_text_document").entered();
105
106    if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) {
107        let Some(DocumentData { version, data }) = state.mem_docs.get_mut(&path) else {
108            tracing::error!(?path, "unexpected DidChangeTextDocument");
109            return Ok(());
110        };
111        // The version passed in DidChangeTextDocument is the version after all edits are applied
112        // so we should apply it before the vfs is notified.
113        *version = params.text_document.version;
114
115        let new_contents = apply_document_changes(
116            state.config.negotiated_encoding(),
117            std::str::from_utf8(data).unwrap(),
118            params.content_changes,
119        )
120        .into_bytes();
121        if *data != new_contents {
122            data.clone_from(&new_contents);
123            state.vfs.write().0.set_file_contents(path, Some(new_contents));
124        }
125    }
126    Ok(())
127}
128
129pub(crate) fn handle_did_close_text_document(
130    state: &mut GlobalState,
131    params: DidCloseTextDocumentParams,
132) -> anyhow::Result<()> {
133    let _p = tracing::info_span!("handle_did_close_text_document").entered();
134
135    if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) {
136        if state.mem_docs.remove(&path).is_err() {
137            tracing::error!("orphan DidCloseTextDocument: {}", path);
138        }
139
140        // Clear diagnostics also for excluded files, just in case.
141        if let Some((file_id, _)) = state.vfs.read().0.file_id(&path) {
142            state.diagnostics.clear_native_for(file_id);
143        }
144
145        state.semantic_tokens_cache.lock().remove(&params.text_document.uri);
146
147        if let Some(path) = path.as_path() {
148            state.loader.handle.invalidate(path.to_path_buf());
149        }
150    }
151    Ok(())
152}
153
154pub(crate) fn handle_did_save_text_document(
155    state: &mut GlobalState,
156    params: DidSaveTextDocumentParams,
157) -> anyhow::Result<()> {
158    if let Ok(vfs_path) = from_proto::vfs_path(&params.text_document.uri) {
159        let snap = state.snapshot();
160        let file_id = try_default!(snap.vfs_path_to_file_id(&vfs_path)?);
161        let sr = snap.analysis.source_root_id(file_id)?;
162
163        if state.config.script_rebuild_on_save(Some(sr)) && state.build_deps_changed {
164            state.build_deps_changed = false;
165            state
166                .fetch_build_data_queue
167                .request_op("build_deps_changed - save notification".to_owned(), ());
168        }
169
170        // Re-fetch workspaces if a workspace related file has changed
171        if let Some(path) = vfs_path.as_path() {
172            let additional_files = &state
173                .config
174                .discover_workspace_config()
175                .map(|cfg| cfg.files_to_watch.iter().map(String::as_str).collect::<Vec<&str>>())
176                .unwrap_or_default();
177
178            // FIXME: We should move this check into a QueuedTask and do semantic resolution of
179            // the files. There is only so much we can tell syntactically from the path.
180            if reload::should_refresh_for_change(path, ChangeKind::Modify, additional_files) {
181                state.fetch_workspaces_queue.request_op(
182                    format!("workspace vfs file change saved {path}"),
183                    FetchWorkspaceRequest {
184                        path: Some(path.to_owned()),
185                        force_crate_graph_reload: false,
186                    },
187                );
188            } else if state.detached_files.contains(path) {
189                state.fetch_workspaces_queue.request_op(
190                    format!("detached file saved {path}"),
191                    FetchWorkspaceRequest {
192                        path: Some(path.to_owned()),
193                        force_crate_graph_reload: false,
194                    },
195                );
196            }
197        }
198
199        if !state.config.check_on_save(Some(sr)) || run_flycheck(state, vfs_path) {
200            return Ok(());
201        }
202    } else if state.config.check_on_save(None) && state.config.flycheck_workspace(None) {
203        // No specific flycheck was triggered, so let's trigger all of them.
204        for flycheck in state.flycheck.iter() {
205            flycheck.restart_workspace(None);
206        }
207    }
208
209    Ok(())
210}
211
212pub(crate) fn handle_did_change_configuration(
213    state: &mut GlobalState,
214    _params: DidChangeConfigurationParams,
215) -> anyhow::Result<()> {
216    // As stated in https://github.com/microsoft/language-server-protocol/issues/676,
217    // this notification's parameters should be ignored and the actual config queried separately.
218    state.send_request::<lsp_types::request::WorkspaceConfiguration>(
219        lsp_types::ConfigurationParams {
220            items: vec![lsp_types::ConfigurationItem {
221                scope_uri: None,
222                section: Some("rust-analyzer".to_owned()),
223            }],
224        },
225        |this, resp| {
226            tracing::debug!("config update response: '{:?}", resp);
227            let lsp_server::Response { error, result, .. } = resp;
228
229            match (error, result) {
230                (Some(err), _) => {
231                    tracing::error!("failed to fetch the server settings: {:?}", err)
232                }
233                (None, Some(mut configs)) => {
234                    if let Some(json) = configs.get_mut(0) {
235                        let config = Config::clone(&*this.config);
236                        let mut change = ConfigChange::default();
237                        change.change_client_config(json.take());
238
239                        let (config, e, _) = config.apply_change(change);
240                        this.config_errors = e.is_empty().not().then_some(e);
241
242                        // Client config changes necessitates .update_config method to be called.
243                        this.update_configuration(config);
244                    }
245                }
246                (None, None) => {
247                    tracing::error!("received empty server settings response from the client")
248                }
249            }
250        },
251    );
252
253    Ok(())
254}
255
256pub(crate) fn handle_did_change_workspace_folders(
257    state: &mut GlobalState,
258    params: DidChangeWorkspaceFoldersParams,
259) -> anyhow::Result<()> {
260    let config = Arc::make_mut(&mut state.config);
261
262    for workspace in params.event.removed {
263        let Ok(path) = workspace.uri.to_file_path() else { continue };
264        let Ok(path) = Utf8PathBuf::from_path_buf(path) else { continue };
265        let Ok(path) = AbsPathBuf::try_from(path) else { continue };
266        config.remove_workspace(&path);
267    }
268
269    let added = params
270        .event
271        .added
272        .into_iter()
273        .filter_map(|it| it.uri.to_file_path().ok())
274        .filter_map(|it| Utf8PathBuf::from_path_buf(it).ok())
275        .filter_map(|it| AbsPathBuf::try_from(it).ok());
276    config.add_workspaces(added);
277
278    if !config.has_linked_projects() && config.detached_files().is_empty() {
279        config.rediscover_workspaces();
280
281        let req = FetchWorkspaceRequest { path: None, force_crate_graph_reload: false };
282        state.fetch_workspaces_queue.request_op("client workspaces changed".to_owned(), req);
283    }
284
285    Ok(())
286}
287
288pub(crate) fn handle_did_change_watched_files(
289    state: &mut GlobalState,
290    params: DidChangeWatchedFilesParams,
291) -> anyhow::Result<()> {
292    for change in params.changes.iter().unique_by(|&it| &it.uri) {
293        if let Ok(path) = from_proto::abs_path(&change.uri) {
294            state.loader.handle.invalidate(path);
295        }
296    }
297    Ok(())
298}
299
300fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
301    let _p = tracing::info_span!("run_flycheck").entered();
302
303    let file_id = state.vfs.read().0.file_id(&vfs_path);
304    if let Some((file_id, vfs::FileExcluded::No)) = file_id {
305        let world = state.snapshot();
306        let invocation_strategy = state.config.flycheck(None).invocation_strategy();
307        let may_flycheck_workspace = state.config.flycheck_workspace(None);
308
309        let task: Box<dyn FnOnce() -> ide::Cancellable<()> + Send + UnwindSafe> =
310            match invocation_strategy {
311                InvocationStrategy::Once => {
312                    Box::new(move || {
313                        // FIXME: Because triomphe::Arc's auto UnwindSafe impl requires that the inner type
314                        // be UnwindSafe, and FlycheckHandle is not UnwindSafe, `word.flycheck` cannot
315                        // be captured directly. std::sync::Arc has an UnwindSafe impl that only requires
316                        // that the inner type be RefUnwindSafe, so if we were using that one we wouldn't
317                        // have this problem. Remove the line below when triomphe::Arc has an UnwindSafe impl
318                        // like std::sync::Arc's.
319                        let world = world;
320                        stdx::always!(
321                            world.flycheck.len() == 1,
322                            "should have exactly one flycheck handle when invocation strategy is once"
323                        );
324                        let saved_file = vfs_path.as_path().map(ToOwned::to_owned);
325                        world.flycheck[0].restart_workspace(saved_file);
326                        Ok(())
327                    })
328                }
329                InvocationStrategy::PerWorkspace => {
330                    Box::new(move || {
331                        let target = TargetSpec::for_file(&world, file_id)?.and_then(|it| {
332                            let tgt_kind = it.target_kind();
333                            let (tgt_name, root, package) = match it {
334                                TargetSpec::Cargo(c) => (c.target, c.workspace_root, c.package_id),
335                                _ => return None,
336                            };
337
338                            let tgt = match tgt_kind {
339                                project_model::TargetKind::Bin => Target::Bin(tgt_name),
340                                project_model::TargetKind::Example => Target::Example(tgt_name),
341                                project_model::TargetKind::Test => Target::Test(tgt_name),
342                                project_model::TargetKind::Bench => Target::Benchmark(tgt_name),
343                                _ => return Some((None, root, package)),
344                            };
345
346                            Some((Some(tgt), root, package))
347                        });
348                        tracing::debug!(?target, "flycheck target");
349                        // we have a specific non-library target, attempt to only check that target, nothing
350                        // else will be affected
351                        let mut package_workspace_idx = None;
352                        if let Some((target, root, package)) = target {
353                            // trigger a package check if we have a non-library target as that can't affect
354                            // anything else in the workspace OR if we're not allowed to check the workspace as
355                            // the user opted into package checks then
356                            let package_check_allowed = target.is_some() || !may_flycheck_workspace;
357                            if package_check_allowed {
358                                package_workspace_idx =
359                                    world.workspaces.iter().position(|ws| match &ws.kind {
360                                        project_model::ProjectWorkspaceKind::Cargo {
361                                            cargo,
362                                            ..
363                                        }
364                                        | project_model::ProjectWorkspaceKind::DetachedFile {
365                                            cargo: Some((cargo, _, _)),
366                                            ..
367                                        } => *cargo.workspace_root() == root,
368                                        _ => false,
369                                    });
370                                if let Some(idx) = package_workspace_idx {
371                                    let workspace_deps =
372                                        world.all_workspace_dependencies_for_package(&package);
373                                    world.flycheck[idx].restart_for_package(
374                                        package,
375                                        target,
376                                        workspace_deps,
377                                    );
378                                }
379                            }
380                        }
381
382                        if !may_flycheck_workspace {
383                            return Ok(());
384                        }
385
386                        // Trigger flychecks for all workspaces that depend on the saved file
387                        // Crates containing or depending on the saved file
388                        let crate_ids: Vec<_> = world
389                            .analysis
390                            .crates_for(file_id)?
391                            .into_iter()
392                            .flat_map(|id| world.analysis.transitive_rev_deps(id))
393                            .flatten()
394                            .unique()
395                            .collect();
396                        tracing::debug!(?crate_ids, "flycheck crate ids");
397                        let crate_root_paths: Vec<_> = crate_ids
398                            .iter()
399                            .filter_map(|&crate_id| {
400                                world
401                                    .analysis
402                                    .crate_root(crate_id)
403                                    .map(|file_id| {
404                                        world
405                                            .file_id_to_file_path(file_id)
406                                            .as_path()
407                                            .map(ToOwned::to_owned)
408                                    })
409                                    .transpose()
410                            })
411                            .collect::<ide::Cancellable<_>>()?;
412                        let crate_root_paths: Vec<_> =
413                            crate_root_paths.iter().map(Deref::deref).collect();
414                        tracing::debug!(?crate_root_paths, "flycheck crate roots");
415
416                        // Find all workspaces that have at least one target containing the saved file
417                        let workspace_ids =
418                            world.workspaces.iter().enumerate().filter(|&(idx, ws)| {
419                                let ws_contains_file = match &ws.kind {
420                                    project_model::ProjectWorkspaceKind::Cargo {
421                                        cargo, ..
422                                    }
423                                    | project_model::ProjectWorkspaceKind::DetachedFile {
424                                        cargo: Some((cargo, _, _)),
425                                        ..
426                                    } => cargo.packages().any(|pkg| {
427                                        cargo[pkg].targets.iter().any(|&it| {
428                                            crate_root_paths.contains(&cargo[it].root.as_path())
429                                        })
430                                    }),
431                                    project_model::ProjectWorkspaceKind::Json(project) => {
432                                        project.crates().any(|(_, krate)| {
433                                            crate_root_paths.contains(&krate.root_module.as_path())
434                                        })
435                                    }
436                                    project_model::ProjectWorkspaceKind::DetachedFile {
437                                        ..
438                                    } => false,
439                                };
440                                let is_pkg_ws = match package_workspace_idx {
441                                    Some(pkg_idx) => pkg_idx == idx,
442                                    None => false,
443                                };
444                                ws_contains_file && !is_pkg_ws
445                            });
446
447                        let saved_file = vfs_path.as_path().map(ToOwned::to_owned);
448                        let mut workspace_check_triggered = false;
449                        // Find and trigger corresponding flychecks
450                        'flychecks: for flycheck in world.flycheck.iter() {
451                            for (id, _) in workspace_ids.clone() {
452                                if id == flycheck.id() {
453                                    workspace_check_triggered = true;
454                                    flycheck.restart_workspace(saved_file.clone());
455                                    continue 'flychecks;
456                                }
457                            }
458                        }
459
460                        // No specific flycheck was triggered, so let's trigger all of them.
461                        if !workspace_check_triggered && package_workspace_idx.is_none() {
462                            for flycheck in world.flycheck.iter() {
463                                flycheck.restart_workspace(saved_file.clone());
464                            }
465                        }
466                        Ok(())
467                    })
468                }
469            };
470
471        state.task_pool.handle.spawn_with_sender(stdx::thread::ThreadIntent::Worker, move |_| {
472            if let Err(e) = std::panic::catch_unwind(task) {
473                tracing::error!("flycheck task panicked: {e:?}")
474            }
475        });
476        true
477    } else {
478        false
479    }
480}
481
482pub(crate) fn handle_cancel_flycheck(state: &mut GlobalState, _: ()) -> anyhow::Result<()> {
483    let _p = tracing::info_span!("handle_cancel_flycheck").entered();
484    state.flycheck.iter().for_each(|flycheck| flycheck.cancel());
485    Ok(())
486}
487
488pub(crate) fn handle_clear_flycheck(state: &mut GlobalState, _: ()) -> anyhow::Result<()> {
489    let _p = tracing::info_span!("handle_clear_flycheck").entered();
490    state.diagnostics.clear_check_all();
491    Ok(())
492}
493
494pub(crate) fn handle_run_flycheck(
495    state: &mut GlobalState,
496    params: RunFlycheckParams,
497) -> anyhow::Result<()> {
498    let _p = tracing::info_span!("handle_run_flycheck").entered();
499    if let Some(text_document) = params.text_document
500        && let Ok(vfs_path) = from_proto::vfs_path(&text_document.uri)
501        && run_flycheck(state, vfs_path)
502    {
503        return Ok(());
504    }
505    // No specific flycheck was triggered, so let's trigger all of them.
506    if state.config.flycheck_workspace(None) {
507        for flycheck in state.flycheck.iter() {
508            flycheck.restart_workspace(None);
509        }
510    }
511    Ok(())
512}
513
514pub(crate) fn handle_abort_run_test(state: &mut GlobalState, _: ()) -> anyhow::Result<()> {
515    if state.test_run_session.take().is_some() {
516        state.send_notification::<lsp_ext::EndRunTest>(());
517    }
518    Ok(())
519}