]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/handlers.rs
Merge #10938
[rust.git] / crates / rust-analyzer / src / handlers.rs
1 //! This module is responsible for implementing handlers for Language Server
2 //! Protocol. The majority of requests are fulfilled by calling into the
3 //! `ide` crate.
4
5 use std::{
6     io::Write as _,
7     process::{self, Stdio},
8 };
9
10 use anyhow::Context;
11 use ide::{
12     AnnotationConfig, AssistKind, AssistResolveStrategy, FileId, FilePosition, FileRange,
13     HoverAction, HoverGotoTypeData, Query, RangeInfo, Runnable, RunnableKind, SingleResolve,
14     SourceChange, TextEdit,
15 };
16 use ide_db::SymbolKind;
17 use itertools::Itertools;
18 use lsp_server::ErrorCode;
19 use lsp_types::{
20     CallHierarchyIncomingCall, CallHierarchyIncomingCallsParams, CallHierarchyItem,
21     CallHierarchyOutgoingCall, CallHierarchyOutgoingCallsParams, CallHierarchyPrepareParams,
22     CodeLens, CompletionItem, Diagnostic, DiagnosticTag, DocumentFormattingParams, FoldingRange,
23     FoldingRangeParams, HoverContents, Location, LocationLink, NumberOrString, Position,
24     PrepareRenameResponse, Range, RenameParams, SemanticTokensDeltaParams,
25     SemanticTokensFullDeltaResult, SemanticTokensParams, SemanticTokensRangeParams,
26     SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation, SymbolTag,
27     TextDocumentIdentifier, Url, WorkspaceEdit,
28 };
29 use project_model::{ManifestPath, ProjectWorkspace, TargetKind};
30 use serde_json::json;
31 use stdx::{format_to, never};
32 use syntax::{algo, ast, AstNode, TextRange, TextSize, T};
33 use vfs::AbsPathBuf;
34
35 use crate::{
36     cargo_target_spec::CargoTargetSpec,
37     config::RustfmtConfig,
38     diff::diff,
39     from_proto,
40     global_state::{GlobalState, GlobalStateSnapshot},
41     line_index::LineEndings,
42     lsp_ext::{
43         self, InlayHint, InlayHintsParams, PositionOrRange, ViewCrateGraphParams,
44         WorkspaceSymbolParams,
45     },
46     lsp_utils::{all_edits_are_disjoint, invalid_params_error},
47     to_proto, LspError, Result,
48 };
49
50 pub(crate) fn handle_analyzer_status(
51     snap: GlobalStateSnapshot,
52     params: lsp_ext::AnalyzerStatusParams,
53 ) -> Result<String> {
54     let _p = profile::span("handle_analyzer_status");
55
56     let mut buf = String::new();
57
58     let mut file_id = None;
59     if let Some(tdi) = params.text_document {
60         match from_proto::file_id(&snap, &tdi.uri) {
61             Ok(it) => file_id = Some(it),
62             Err(_) => format_to!(buf, "file {} not found in vfs", tdi.uri),
63         }
64     }
65
66     if snap.workspaces.is_empty() {
67         buf.push_str("No workspaces\n")
68     } else {
69         buf.push_str("Workspaces:\n");
70         format_to!(
71             buf,
72             "Loaded {:?} packages across {} workspace{}.\n",
73             snap.workspaces.iter().map(|w| w.n_packages()).sum::<usize>(),
74             snap.workspaces.len(),
75             if snap.workspaces.len() == 1 { "" } else { "s" }
76         );
77     }
78     buf.push_str("\nAnalysis:\n");
79     buf.push_str(
80         &snap
81             .analysis
82             .status(file_id)
83             .unwrap_or_else(|_| "Analysis retrieval was cancelled".to_owned()),
84     );
85     Ok(buf)
86 }
87
88 pub(crate) fn handle_memory_usage(state: &mut GlobalState, _: ()) -> Result<String> {
89     let _p = profile::span("handle_memory_usage");
90     let mut mem = state.analysis_host.per_query_memory_usage();
91     mem.push(("Remaining".into(), profile::memory_usage().allocated));
92
93     let mut out = String::new();
94     for (name, bytes) in mem {
95         format_to!(out, "{:>8} {}\n", bytes, name);
96     }
97     Ok(out)
98 }
99
100 pub(crate) fn handle_syntax_tree(
101     snap: GlobalStateSnapshot,
102     params: lsp_ext::SyntaxTreeParams,
103 ) -> Result<String> {
104     let _p = profile::span("handle_syntax_tree");
105     let id = from_proto::file_id(&snap, &params.text_document.uri)?;
106     let line_index = snap.file_line_index(id)?;
107     let text_range = params.range.map(|r| from_proto::text_range(&line_index, r));
108     let res = snap.analysis.syntax_tree(id, text_range)?;
109     Ok(res)
110 }
111
112 pub(crate) fn handle_view_hir(
113     snap: GlobalStateSnapshot,
114     params: lsp_types::TextDocumentPositionParams,
115 ) -> Result<String> {
116     let _p = profile::span("handle_view_hir");
117     let position = from_proto::file_position(&snap, params)?;
118     let res = snap.analysis.view_hir(position)?;
119     Ok(res)
120 }
121
122 pub(crate) fn handle_view_item_tree(
123     snap: GlobalStateSnapshot,
124     params: lsp_ext::ViewItemTreeParams,
125 ) -> Result<String> {
126     let _p = profile::span("handle_view_item_tree");
127     let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
128     let res = snap.analysis.view_item_tree(file_id)?;
129     Ok(res)
130 }
131
132 pub(crate) fn handle_view_crate_graph(
133     snap: GlobalStateSnapshot,
134     params: ViewCrateGraphParams,
135 ) -> Result<String> {
136     let _p = profile::span("handle_view_crate_graph");
137     let dot = snap.analysis.view_crate_graph(params.full)??;
138     Ok(dot)
139 }
140
141 pub(crate) fn handle_expand_macro(
142     snap: GlobalStateSnapshot,
143     params: lsp_ext::ExpandMacroParams,
144 ) -> Result<Option<lsp_ext::ExpandedMacro>> {
145     let _p = profile::span("handle_expand_macro");
146     let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
147     let line_index = snap.file_line_index(file_id)?;
148     let offset = from_proto::offset(&line_index, params.position);
149
150     let res = snap.analysis.expand_macro(FilePosition { file_id, offset })?;
151     Ok(res.map(|it| lsp_ext::ExpandedMacro { name: it.name, expansion: it.expansion }))
152 }
153
154 pub(crate) fn handle_selection_range(
155     snap: GlobalStateSnapshot,
156     params: lsp_types::SelectionRangeParams,
157 ) -> Result<Option<Vec<lsp_types::SelectionRange>>> {
158     let _p = profile::span("handle_selection_range");
159     let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
160     let line_index = snap.file_line_index(file_id)?;
161     let res: Result<Vec<lsp_types::SelectionRange>> = params
162         .positions
163         .into_iter()
164         .map(|position| {
165             let offset = from_proto::offset(&line_index, position);
166             let mut ranges = Vec::new();
167             {
168                 let mut range = TextRange::new(offset, offset);
169                 loop {
170                     ranges.push(range);
171                     let frange = FileRange { file_id, range };
172                     let next = snap.analysis.extend_selection(frange)?;
173                     if next == range {
174                         break;
175                     } else {
176                         range = next
177                     }
178                 }
179             }
180             let mut range = lsp_types::SelectionRange {
181                 range: to_proto::range(&line_index, *ranges.last().unwrap()),
182                 parent: None,
183             };
184             for &r in ranges.iter().rev().skip(1) {
185                 range = lsp_types::SelectionRange {
186                     range: to_proto::range(&line_index, r),
187                     parent: Some(Box::new(range)),
188                 }
189             }
190             Ok(range)
191         })
192         .collect();
193
194     Ok(Some(res?))
195 }
196
197 pub(crate) fn handle_matching_brace(
198     snap: GlobalStateSnapshot,
199     params: lsp_ext::MatchingBraceParams,
200 ) -> Result<Vec<Position>> {
201     let _p = profile::span("handle_matching_brace");
202     let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
203     let line_index = snap.file_line_index(file_id)?;
204     let res = params
205         .positions
206         .into_iter()
207         .map(|position| {
208             let offset = from_proto::offset(&line_index, position);
209             let offset = match snap.analysis.matching_brace(FilePosition { file_id, offset }) {
210                 Ok(Some(matching_brace_offset)) => matching_brace_offset,
211                 Err(_) | Ok(None) => offset,
212             };
213             to_proto::position(&line_index, offset)
214         })
215         .collect();
216     Ok(res)
217 }
218
219 pub(crate) fn handle_join_lines(
220     snap: GlobalStateSnapshot,
221     params: lsp_ext::JoinLinesParams,
222 ) -> Result<Vec<lsp_types::TextEdit>> {
223     let _p = profile::span("handle_join_lines");
224
225     let config = snap.config.join_lines();
226     let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
227     let line_index = snap.file_line_index(file_id)?;
228
229     let mut res = TextEdit::default();
230     for range in params.ranges {
231         let range = from_proto::text_range(&line_index, range);
232         let edit = snap.analysis.join_lines(&config, FileRange { file_id, range })?;
233         match res.union(edit) {
234             Ok(()) => (),
235             Err(_edit) => {
236                 // just ignore overlapping edits
237             }
238         }
239     }
240
241     Ok(to_proto::text_edit_vec(&line_index, res))
242 }
243
244 pub(crate) fn handle_on_enter(
245     snap: GlobalStateSnapshot,
246     params: lsp_types::TextDocumentPositionParams,
247 ) -> Result<Option<Vec<lsp_ext::SnippetTextEdit>>> {
248     let _p = profile::span("handle_on_enter");
249     let position = from_proto::file_position(&snap, params)?;
250     let edit = match snap.analysis.on_enter(position)? {
251         None => return Ok(None),
252         Some(it) => it,
253     };
254     let line_index = snap.file_line_index(position.file_id)?;
255     let edit = to_proto::snippet_text_edit_vec(&line_index, true, edit);
256     Ok(Some(edit))
257 }
258
259 pub(crate) fn handle_on_type_formatting(
260     snap: GlobalStateSnapshot,
261     params: lsp_types::DocumentOnTypeFormattingParams,
262 ) -> Result<Option<Vec<lsp_types::TextEdit>>> {
263     let _p = profile::span("handle_on_type_formatting");
264     let mut position = from_proto::file_position(&snap, params.text_document_position)?;
265     let line_index = snap.file_line_index(position.file_id)?;
266
267     // in `ide`, the `on_type` invariant is that
268     // `text.char_at(position) == typed_char`.
269     position.offset -= TextSize::of('.');
270     let char_typed = params.ch.chars().next().unwrap_or('\0');
271
272     let text = snap.analysis.file_text(position.file_id)?;
273     if !text[usize::from(position.offset)..].starts_with(char_typed) {
274         // Add `always!` here once VS Code bug is fixed:
275         //   https://github.com/rust-analyzer/rust-analyzer/issues/10002
276         return Ok(None);
277     }
278
279     // We have an assist that inserts ` ` after typing `->` in `fn foo() ->{`,
280     // but it requires precise cursor positioning to work, and one can't
281     // position the cursor with on_type formatting. So, let's just toggle this
282     // feature off here, hoping that we'll enable it one day, ðŸ˜¿.
283     if char_typed == '>' {
284         return Ok(None);
285     }
286
287     let edit = snap.analysis.on_char_typed(position, char_typed)?;
288     let edit = match edit {
289         Some(it) => it,
290         None => return Ok(None),
291     };
292
293     // This should be a single-file edit
294     let (_, edit) = edit.source_file_edits.into_iter().next().unwrap();
295
296     let change = to_proto::text_edit_vec(&line_index, edit);
297     Ok(Some(change))
298 }
299
300 pub(crate) fn handle_document_symbol(
301     snap: GlobalStateSnapshot,
302     params: lsp_types::DocumentSymbolParams,
303 ) -> Result<Option<lsp_types::DocumentSymbolResponse>> {
304     let _p = profile::span("handle_document_symbol");
305     let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
306     let line_index = snap.file_line_index(file_id)?;
307
308     let mut parents: Vec<(lsp_types::DocumentSymbol, Option<usize>)> = Vec::new();
309
310     for symbol in snap.analysis.file_structure(file_id)? {
311         let mut tags = Vec::new();
312         if symbol.deprecated {
313             tags.push(SymbolTag::DEPRECATED)
314         };
315
316         #[allow(deprecated)]
317         let doc_symbol = lsp_types::DocumentSymbol {
318             name: symbol.label,
319             detail: symbol.detail,
320             kind: to_proto::structure_node_kind(symbol.kind),
321             tags: Some(tags),
322             deprecated: Some(symbol.deprecated),
323             range: to_proto::range(&line_index, symbol.node_range),
324             selection_range: to_proto::range(&line_index, symbol.navigation_range),
325             children: None,
326         };
327         parents.push((doc_symbol, symbol.parent));
328     }
329
330     // Builds hierarchy from a flat list, in reverse order (so that indices
331     // makes sense)
332     let document_symbols = {
333         let mut acc = Vec::new();
334         while let Some((mut node, parent_idx)) = parents.pop() {
335             if let Some(children) = &mut node.children {
336                 children.reverse();
337             }
338             let parent = match parent_idx {
339                 None => &mut acc,
340                 Some(i) => parents[i].0.children.get_or_insert_with(Vec::new),
341             };
342             parent.push(node);
343         }
344         acc.reverse();
345         acc
346     };
347
348     let res = if snap.config.hierarchical_symbols() {
349         document_symbols.into()
350     } else {
351         let url = to_proto::url(&snap, file_id);
352         let mut symbol_information = Vec::<SymbolInformation>::new();
353         for symbol in document_symbols {
354             flatten_document_symbol(&symbol, None, &url, &mut symbol_information);
355         }
356         symbol_information.into()
357     };
358     return Ok(Some(res));
359
360     fn flatten_document_symbol(
361         symbol: &lsp_types::DocumentSymbol,
362         container_name: Option<String>,
363         url: &Url,
364         res: &mut Vec<SymbolInformation>,
365     ) {
366         let mut tags = Vec::new();
367
368         #[allow(deprecated)]
369         if let Some(true) = symbol.deprecated {
370             tags.push(SymbolTag::DEPRECATED)
371         }
372
373         #[allow(deprecated)]
374         res.push(SymbolInformation {
375             name: symbol.name.clone(),
376             kind: symbol.kind,
377             tags: Some(tags),
378             deprecated: symbol.deprecated,
379             location: Location::new(url.clone(), symbol.range),
380             container_name,
381         });
382
383         for child in symbol.children.iter().flatten() {
384             flatten_document_symbol(child, Some(symbol.name.clone()), url, res);
385         }
386     }
387 }
388
389 pub(crate) fn handle_workspace_symbol(
390     snap: GlobalStateSnapshot,
391     params: WorkspaceSymbolParams,
392 ) -> Result<Option<Vec<SymbolInformation>>> {
393     let _p = profile::span("handle_workspace_symbol");
394
395     let (all_symbols, libs) = decide_search_scope_and_kind(&params, &snap);
396
397     let query = {
398         let query: String = params.query.chars().filter(|&c| c != '#' && c != '*').collect();
399         let mut q = Query::new(query);
400         if !all_symbols {
401             q.only_types();
402         }
403         if libs {
404             q.libs();
405         }
406         q.limit(128);
407         q
408     };
409     let mut res = exec_query(&snap, query)?;
410     if res.is_empty() && !all_symbols {
411         let mut query = Query::new(params.query);
412         query.limit(128);
413         res = exec_query(&snap, query)?;
414     }
415
416     return Ok(Some(res));
417
418     fn decide_search_scope_and_kind(
419         params: &WorkspaceSymbolParams,
420         snap: &GlobalStateSnapshot,
421     ) -> (bool, bool) {
422         // Support old-style parsing of markers in the query.
423         let mut all_symbols = params.query.contains('#');
424         let mut libs = params.query.contains('*');
425
426         let config = snap.config.workspace_symbol();
427
428         // If no explicit marker was set, check request params. If that's also empty
429         // use global config.
430         if !all_symbols {
431             let search_kind = match params.search_kind {
432                 Some(ref search_kind) => search_kind,
433                 None => &config.search_kind,
434             };
435             all_symbols = match search_kind {
436                 lsp_ext::WorkspaceSymbolSearchKind::OnlyTypes => false,
437                 lsp_ext::WorkspaceSymbolSearchKind::AllSymbols => true,
438             }
439         }
440
441         if !libs {
442             let search_scope = match params.search_scope {
443                 Some(ref search_scope) => search_scope,
444                 None => &config.search_scope,
445             };
446             libs = match search_scope {
447                 lsp_ext::WorkspaceSymbolSearchScope::Workspace => false,
448                 lsp_ext::WorkspaceSymbolSearchScope::WorkspaceAndDependencies => true,
449             }
450         }
451
452         (all_symbols, libs)
453     }
454
455     fn exec_query(snap: &GlobalStateSnapshot, query: Query) -> Result<Vec<SymbolInformation>> {
456         let mut res = Vec::new();
457         for nav in snap.analysis.symbol_search(query)? {
458             let container_name = nav.container_name.as_ref().map(|v| v.to_string());
459
460             #[allow(deprecated)]
461             let info = SymbolInformation {
462                 name: nav.name.to_string(),
463                 kind: nav
464                     .kind
465                     .map(to_proto::symbol_kind)
466                     .unwrap_or(lsp_types::SymbolKind::VARIABLE),
467                 tags: None,
468                 location: to_proto::location_from_nav(snap, nav)?,
469                 container_name,
470                 deprecated: None,
471             };
472             res.push(info);
473         }
474         Ok(res)
475     }
476 }
477
478 pub(crate) fn handle_will_rename_files(
479     snap: GlobalStateSnapshot,
480     params: lsp_types::RenameFilesParams,
481 ) -> Result<Option<lsp_types::WorkspaceEdit>> {
482     let _p = profile::span("handle_will_rename_files");
483
484     let source_changes: Vec<SourceChange> = params
485         .files
486         .into_iter()
487         .filter_map(|file_rename| {
488             let from = Url::parse(&file_rename.old_uri).ok()?;
489             let to = Url::parse(&file_rename.new_uri).ok()?;
490
491             let from_path = from.to_file_path().ok()?;
492             let to_path = to.to_file_path().ok()?;
493
494             // Limit to single-level moves for now.
495             match (from_path.parent(), to_path.parent()) {
496                 (Some(p1), Some(p2)) if p1 == p2 => {
497                     if from_path.is_dir() {
498                         // add '/' to end of url -- from `file://path/to/folder` to `file://path/to/folder/`
499                         let mut old_folder_name = from_path.file_stem()?.to_str()?.to_string();
500                         old_folder_name.push('/');
501                         let from_with_trailing_slash = from.join(&old_folder_name).ok()?;
502
503                         let imitate_from_url = from_with_trailing_slash.join("mod.rs").ok()?;
504                         let new_file_name = to_path.file_name()?.to_str()?;
505                         Some((
506                             snap.url_to_file_id(&imitate_from_url).ok()?,
507                             new_file_name.to_string(),
508                         ))
509                     } else {
510                         let old_name = from_path.file_stem()?.to_str()?;
511                         let new_name = to_path.file_stem()?.to_str()?;
512                         match (old_name, new_name) {
513                             ("mod", _) => None,
514                             (_, "mod") => None,
515                             _ => Some((snap.url_to_file_id(&from).ok()?, new_name.to_string())),
516                         }
517                     }
518                 }
519                 _ => None,
520             }
521         })
522         .filter_map(|(file_id, new_name)| {
523             snap.analysis.will_rename_file(file_id, &new_name).ok()?
524         })
525         .collect();
526
527     // Drop file system edits since we're just renaming things on the same level
528     let mut source_changes = source_changes.into_iter();
529     let mut source_change = source_changes.next().unwrap_or_default();
530     source_change.file_system_edits.clear();
531     // no collect here because we want to merge text edits on same file ids
532     source_change.extend(source_changes.map(|it| it.source_file_edits).flatten());
533     if source_change.source_file_edits.is_empty() {
534         Ok(None)
535     } else {
536         to_proto::workspace_edit(&snap, source_change).map(Some)
537     }
538 }
539
540 pub(crate) fn handle_goto_definition(
541     snap: GlobalStateSnapshot,
542     params: lsp_types::GotoDefinitionParams,
543 ) -> Result<Option<lsp_types::GotoDefinitionResponse>> {
544     let _p = profile::span("handle_goto_definition");
545     let position = from_proto::file_position(&snap, params.text_document_position_params)?;
546     let nav_info = match snap.analysis.goto_definition(position)? {
547         None => return Ok(None),
548         Some(it) => it,
549     };
550     let src = FileRange { file_id: position.file_id, range: nav_info.range };
551     let res = to_proto::goto_definition_response(&snap, Some(src), nav_info.info)?;
552     Ok(Some(res))
553 }
554
555 pub(crate) fn handle_goto_declaration(
556     snap: GlobalStateSnapshot,
557     params: lsp_types::request::GotoDeclarationParams,
558 ) -> Result<Option<lsp_types::request::GotoDeclarationResponse>> {
559     let _p = profile::span("handle_goto_declaration");
560     let position = from_proto::file_position(&snap, params.text_document_position_params.clone())?;
561     let nav_info = match snap.analysis.goto_declaration(position)? {
562         None => return handle_goto_definition(snap, params),
563         Some(it) => it,
564     };
565     let src = FileRange { file_id: position.file_id, range: nav_info.range };
566     let res = to_proto::goto_definition_response(&snap, Some(src), nav_info.info)?;
567     Ok(Some(res))
568 }
569
570 pub(crate) fn handle_goto_implementation(
571     snap: GlobalStateSnapshot,
572     params: lsp_types::request::GotoImplementationParams,
573 ) -> Result<Option<lsp_types::request::GotoImplementationResponse>> {
574     let _p = profile::span("handle_goto_implementation");
575     let position = from_proto::file_position(&snap, params.text_document_position_params)?;
576     let nav_info = match snap.analysis.goto_implementation(position)? {
577         None => return Ok(None),
578         Some(it) => it,
579     };
580     let src = FileRange { file_id: position.file_id, range: nav_info.range };
581     let res = to_proto::goto_definition_response(&snap, Some(src), nav_info.info)?;
582     Ok(Some(res))
583 }
584
585 pub(crate) fn handle_goto_type_definition(
586     snap: GlobalStateSnapshot,
587     params: lsp_types::request::GotoTypeDefinitionParams,
588 ) -> Result<Option<lsp_types::request::GotoTypeDefinitionResponse>> {
589     let _p = profile::span("handle_goto_type_definition");
590     let position = from_proto::file_position(&snap, params.text_document_position_params)?;
591     let nav_info = match snap.analysis.goto_type_definition(position)? {
592         None => return Ok(None),
593         Some(it) => it,
594     };
595     let src = FileRange { file_id: position.file_id, range: nav_info.range };
596     let res = to_proto::goto_definition_response(&snap, Some(src), nav_info.info)?;
597     Ok(Some(res))
598 }
599
600 pub(crate) fn handle_parent_module(
601     snap: GlobalStateSnapshot,
602     params: lsp_types::TextDocumentPositionParams,
603 ) -> Result<Option<lsp_types::GotoDefinitionResponse>> {
604     let _p = profile::span("handle_parent_module");
605     if let Ok(file_path) = &params.text_document.uri.to_file_path() {
606         if file_path.file_name().unwrap_or_default() == "Cargo.toml" {
607             // search workspaces for parent packages or fallback to workspace root
608             let abs_path_buf = match AbsPathBuf::try_from(file_path.to_path_buf()).ok() {
609                 Some(abs_path_buf) => abs_path_buf,
610                 None => return Ok(None),
611             };
612
613             let manifest_path = match ManifestPath::try_from(abs_path_buf).ok() {
614                 Some(manifest_path) => manifest_path,
615                 None => return Ok(None),
616             };
617
618             let links: Vec<LocationLink> = snap
619                 .workspaces
620                 .iter()
621                 .filter_map(|ws| match ws {
622                     ProjectWorkspace::Cargo { cargo, .. } => cargo.parent_manifests(&manifest_path),
623                     _ => None,
624                 })
625                 .flatten()
626                 .map(|parent_manifest_path| LocationLink {
627                     origin_selection_range: None,
628                     target_uri: to_proto::url_from_abs_path(&parent_manifest_path),
629                     target_range: Range::default(),
630                     target_selection_range: Range::default(),
631                 })
632                 .collect::<_>();
633             return Ok(Some(links.into()));
634         }
635
636         // check if invoked at the crate root
637         let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
638         let crate_id = match snap.analysis.crate_for(file_id)?.first() {
639             Some(&crate_id) => crate_id,
640             None => return Ok(None),
641         };
642         let cargo_spec = match CargoTargetSpec::for_file(&snap, file_id)? {
643             Some(it) => it,
644             None => return Ok(None),
645         };
646
647         if snap.analysis.crate_root(crate_id)? == file_id {
648             let cargo_toml_url = to_proto::url_from_abs_path(&cargo_spec.cargo_toml);
649             let res = vec![LocationLink {
650                 origin_selection_range: None,
651                 target_uri: cargo_toml_url,
652                 target_range: Range::default(),
653                 target_selection_range: Range::default(),
654             }]
655             .into();
656             return Ok(Some(res));
657         }
658     }
659
660     // locate parent module by semantics
661     let position = from_proto::file_position(&snap, params)?;
662     let navs = snap.analysis.parent_module(position)?;
663     let res = to_proto::goto_definition_response(&snap, None, navs)?;
664     Ok(Some(res))
665 }
666
667 pub(crate) fn handle_runnables(
668     snap: GlobalStateSnapshot,
669     params: lsp_ext::RunnablesParams,
670 ) -> Result<Vec<lsp_ext::Runnable>> {
671     let _p = profile::span("handle_runnables");
672     let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
673     let line_index = snap.file_line_index(file_id)?;
674     let offset = params.position.map(|it| from_proto::offset(&line_index, it));
675     let cargo_spec = CargoTargetSpec::for_file(&snap, file_id)?;
676
677     let expect_test = match offset {
678         Some(offset) => {
679             let source_file = snap.analysis.parse(file_id)?;
680             algo::find_node_at_offset::<ast::MacroCall>(source_file.syntax(), offset)
681                 .and_then(|it| it.path()?.segment()?.name_ref())
682                 .map_or(false, |it| it.text() == "expect" || it.text() == "expect_file")
683         }
684         None => false,
685     };
686
687     let mut res = Vec::new();
688     for runnable in snap.analysis.runnables(file_id)? {
689         if let Some(offset) = offset {
690             if !runnable.nav.full_range.contains_inclusive(offset) {
691                 continue;
692             }
693         }
694         if should_skip_target(&runnable, cargo_spec.as_ref()) {
695             continue;
696         }
697         let mut runnable = to_proto::runnable(&snap, runnable)?;
698         if expect_test {
699             runnable.label = format!("{} + expect", runnable.label);
700             runnable.args.expect_test = Some(true);
701         }
702         res.push(runnable);
703     }
704
705     // Add `cargo check` and `cargo test` for all targets of the whole package
706     let config = snap.config.runnables();
707     match cargo_spec {
708         Some(spec) => {
709             for cmd in ["check", "test"] {
710                 res.push(lsp_ext::Runnable {
711                     label: format!("cargo {} -p {} --all-targets", cmd, spec.package),
712                     location: None,
713                     kind: lsp_ext::RunnableKind::Cargo,
714                     args: lsp_ext::CargoRunnable {
715                         workspace_root: Some(spec.workspace_root.clone().into()),
716                         override_cargo: config.override_cargo.clone(),
717                         cargo_args: vec![
718                             cmd.to_string(),
719                             "--package".to_string(),
720                             spec.package.clone(),
721                             "--all-targets".to_string(),
722                         ],
723                         cargo_extra_args: config.cargo_extra_args.clone(),
724                         executable_args: Vec::new(),
725                         expect_test: None,
726                     },
727                 })
728             }
729         }
730         None => {
731             if !snap.config.linked_projects().is_empty()
732                 || !snap
733                     .config
734                     .discovered_projects
735                     .as_ref()
736                     .map(|projects| projects.is_empty())
737                     .unwrap_or(true)
738             {
739                 res.push(lsp_ext::Runnable {
740                     label: "cargo check --workspace".to_string(),
741                     location: None,
742                     kind: lsp_ext::RunnableKind::Cargo,
743                     args: lsp_ext::CargoRunnable {
744                         workspace_root: None,
745                         override_cargo: config.override_cargo,
746                         cargo_args: vec!["check".to_string(), "--workspace".to_string()],
747                         cargo_extra_args: config.cargo_extra_args,
748                         executable_args: Vec::new(),
749                         expect_test: None,
750                     },
751                 });
752             }
753         }
754     }
755     Ok(res)
756 }
757
758 pub(crate) fn handle_related_tests(
759     snap: GlobalStateSnapshot,
760     params: lsp_types::TextDocumentPositionParams,
761 ) -> Result<Vec<lsp_ext::TestInfo>> {
762     let _p = profile::span("handle_related_tests");
763     let position = from_proto::file_position(&snap, params)?;
764
765     let tests = snap.analysis.related_tests(position, None)?;
766     let mut res = Vec::new();
767     for it in tests {
768         if let Ok(runnable) = to_proto::runnable(&snap, it) {
769             res.push(lsp_ext::TestInfo { runnable })
770         }
771     }
772
773     Ok(res)
774 }
775
776 pub(crate) fn handle_completion(
777     snap: GlobalStateSnapshot,
778     params: lsp_types::CompletionParams,
779 ) -> Result<Option<lsp_types::CompletionResponse>> {
780     let _p = profile::span("handle_completion");
781     let text_document_position = params.text_document_position.clone();
782     let position = from_proto::file_position(&snap, params.text_document_position)?;
783     let completion_triggered_after_single_colon = {
784         let mut res = false;
785         if let Some(ctx) = params.context {
786             if ctx.trigger_character.as_deref() == Some(":") {
787                 let source_file = snap.analysis.parse(position.file_id)?;
788                 let left_token =
789                     source_file.syntax().token_at_offset(position.offset).left_biased();
790                 match left_token {
791                     Some(left_token) => res = left_token.kind() == T![:],
792                     None => res = true,
793                 }
794             }
795         }
796         res
797     };
798     if completion_triggered_after_single_colon {
799         return Ok(None);
800     }
801
802     let completion_config = &snap.config.completion();
803     let items = match snap.analysis.completions(completion_config, position)? {
804         None => return Ok(None),
805         Some(items) => items,
806     };
807     let line_index = snap.file_line_index(position.file_id)?;
808
809     let items =
810         to_proto::completion_items(&snap.config, &line_index, text_document_position, items);
811
812     let completion_list = lsp_types::CompletionList { is_incomplete: true, items };
813     Ok(Some(completion_list.into()))
814 }
815
816 pub(crate) fn handle_completion_resolve(
817     snap: GlobalStateSnapshot,
818     mut original_completion: CompletionItem,
819 ) -> Result<CompletionItem> {
820     let _p = profile::span("handle_completion_resolve");
821
822     if !all_edits_are_disjoint(&original_completion, &[]) {
823         return Err(invalid_params_error(
824             "Received a completion with overlapping edits, this is not LSP-compliant".to_string(),
825         )
826         .into());
827     }
828
829     let data = match original_completion.data.take() {
830         Some(it) => it,
831         None => return Ok(original_completion),
832     };
833
834     let resolve_data: lsp_ext::CompletionResolveData = serde_json::from_value(data)?;
835
836     let file_id = from_proto::file_id(&snap, &resolve_data.position.text_document.uri)?;
837     let line_index = snap.file_line_index(file_id)?;
838     let offset = from_proto::offset(&line_index, resolve_data.position.position);
839
840     let additional_edits = snap
841         .analysis
842         .resolve_completion_edits(
843             &snap.config.completion(),
844             FilePosition { file_id, offset },
845             resolve_data
846                 .imports
847                 .into_iter()
848                 .map(|import| (import.full_import_path, import.imported_name)),
849         )?
850         .into_iter()
851         .flat_map(|edit| edit.into_iter().map(|indel| to_proto::text_edit(&line_index, indel)))
852         .collect_vec();
853
854     if !all_edits_are_disjoint(&original_completion, &additional_edits) {
855         return Err(LspError::new(
856             ErrorCode::InternalError as i32,
857             "Import edit overlaps with the original completion edits, this is not LSP-compliant"
858                 .into(),
859         )
860         .into());
861     }
862
863     if let Some(original_additional_edits) = original_completion.additional_text_edits.as_mut() {
864         original_additional_edits.extend(additional_edits.into_iter())
865     } else {
866         original_completion.additional_text_edits = Some(additional_edits);
867     }
868
869     Ok(original_completion)
870 }
871
872 pub(crate) fn handle_folding_range(
873     snap: GlobalStateSnapshot,
874     params: FoldingRangeParams,
875 ) -> Result<Option<Vec<FoldingRange>>> {
876     let _p = profile::span("handle_folding_range");
877     let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
878     let folds = snap.analysis.folding_ranges(file_id)?;
879     let text = snap.analysis.file_text(file_id)?;
880     let line_index = snap.file_line_index(file_id)?;
881     let line_folding_only = snap.config.line_folding_only();
882     let res = folds
883         .into_iter()
884         .map(|it| to_proto::folding_range(&*text, &line_index, line_folding_only, it))
885         .collect();
886     Ok(Some(res))
887 }
888
889 pub(crate) fn handle_signature_help(
890     snap: GlobalStateSnapshot,
891     params: lsp_types::SignatureHelpParams,
892 ) -> Result<Option<lsp_types::SignatureHelp>> {
893     let _p = profile::span("handle_signature_help");
894     let position = from_proto::file_position(&snap, params.text_document_position_params)?;
895     let call_info = match snap.analysis.call_info(position)? {
896         Some(it) => it,
897         None => return Ok(None),
898     };
899     let concise = !snap.config.call_info_full();
900     let res =
901         to_proto::signature_help(call_info, concise, snap.config.signature_help_label_offsets());
902     Ok(Some(res))
903 }
904
905 pub(crate) fn handle_hover(
906     snap: GlobalStateSnapshot,
907     params: lsp_ext::HoverParams,
908 ) -> Result<Option<lsp_ext::Hover>> {
909     let _p = profile::span("handle_hover");
910     let range = match params.position {
911         PositionOrRange::Position(position) => Range::new(position, position),
912         PositionOrRange::Range(range) => range,
913     };
914
915     let file_range = from_proto::file_range(&snap, params.text_document, range)?;
916     let info = match snap.analysis.hover(&snap.config.hover(), file_range)? {
917         None => return Ok(None),
918         Some(info) => info,
919     };
920
921     let line_index = snap.file_line_index(file_range.file_id)?;
922     let range = to_proto::range(&line_index, info.range);
923     let markup_kind =
924         snap.config.hover().documentation.map_or(ide::HoverDocFormat::Markdown, |kind| kind);
925     let hover = lsp_ext::Hover {
926         hover: lsp_types::Hover {
927             contents: HoverContents::Markup(to_proto::markup_content(
928                 info.info.markup,
929                 markup_kind,
930             )),
931             range: Some(range),
932         },
933         actions: if snap.config.hover_actions().none() {
934             Vec::new()
935         } else {
936             prepare_hover_actions(&snap, &info.info.actions)
937         },
938     };
939
940     Ok(Some(hover))
941 }
942
943 pub(crate) fn handle_prepare_rename(
944     snap: GlobalStateSnapshot,
945     params: lsp_types::TextDocumentPositionParams,
946 ) -> Result<Option<PrepareRenameResponse>> {
947     let _p = profile::span("handle_prepare_rename");
948     let position = from_proto::file_position(&snap, params)?;
949
950     let change = snap.analysis.prepare_rename(position)?.map_err(to_proto::rename_error)?;
951
952     let line_index = snap.file_line_index(position.file_id)?;
953     let range = to_proto::range(&line_index, change.range);
954     Ok(Some(PrepareRenameResponse::Range(range)))
955 }
956
957 pub(crate) fn handle_rename(
958     snap: GlobalStateSnapshot,
959     params: RenameParams,
960 ) -> Result<Option<WorkspaceEdit>> {
961     let _p = profile::span("handle_rename");
962     let position = from_proto::file_position(&snap, params.text_document_position)?;
963
964     let mut change =
965         snap.analysis.rename(position, &*params.new_name)?.map_err(to_proto::rename_error)?;
966
967     // this is kind of a hack to prevent double edits from happening when moving files
968     // When a module gets renamed by renaming the mod declaration this causes the file to move
969     // which in turn will trigger a WillRenameFiles request to the server for which we reply with a
970     // a second identical set of renames, the client will then apply both edits causing incorrect edits
971     // with this we only emit source_file_edits in the WillRenameFiles response which will do the rename instead
972     // See https://github.com/microsoft/vscode-languageserver-node/issues/752 for more info
973     if !change.file_system_edits.is_empty() && snap.config.will_rename() {
974         change.source_file_edits.clear();
975     }
976     let workspace_edit = to_proto::workspace_edit(&snap, change)?;
977     Ok(Some(workspace_edit))
978 }
979
980 pub(crate) fn handle_references(
981     snap: GlobalStateSnapshot,
982     params: lsp_types::ReferenceParams,
983 ) -> Result<Option<Vec<Location>>> {
984     let _p = profile::span("handle_references");
985     let position = from_proto::file_position(&snap, params.text_document_position)?;
986
987     let refs = match snap.analysis.find_all_refs(position, None)? {
988         None => return Ok(None),
989         Some(refs) => refs,
990     };
991
992     let include_declaration = params.context.include_declaration;
993     let locations = refs
994         .into_iter()
995         .flat_map(|refs| {
996             let decl = if include_declaration {
997                 refs.declaration.map(|decl| FileRange {
998                     file_id: decl.nav.file_id,
999                     range: decl.nav.focus_or_full_range(),
1000                 })
1001             } else {
1002                 None
1003             };
1004             refs.references
1005                 .into_iter()
1006                 .flat_map(|(file_id, refs)| {
1007                     refs.into_iter().map(move |(range, _)| FileRange { file_id, range })
1008                 })
1009                 .chain(decl)
1010         })
1011         .filter_map(|frange| to_proto::location(&snap, frange).ok())
1012         .collect();
1013
1014     Ok(Some(locations))
1015 }
1016
1017 pub(crate) fn handle_formatting(
1018     snap: GlobalStateSnapshot,
1019     params: DocumentFormattingParams,
1020 ) -> Result<Option<Vec<lsp_types::TextEdit>>> {
1021     let _p = profile::span("handle_formatting");
1022
1023     run_rustfmt(&snap, params.text_document, None)
1024 }
1025
1026 pub(crate) fn handle_range_formatting(
1027     snap: GlobalStateSnapshot,
1028     params: lsp_types::DocumentRangeFormattingParams,
1029 ) -> Result<Option<Vec<lsp_types::TextEdit>>> {
1030     let _p = profile::span("handle_range_formatting");
1031
1032     run_rustfmt(&snap, params.text_document, Some(params.range))
1033 }
1034
1035 pub(crate) fn handle_code_action(
1036     snap: GlobalStateSnapshot,
1037     params: lsp_types::CodeActionParams,
1038 ) -> Result<Option<Vec<lsp_ext::CodeAction>>> {
1039     let _p = profile::span("handle_code_action");
1040
1041     if !snap.config.code_action_literals() {
1042         // We intentionally don't support command-based actions, as those either
1043         // require either custom client-code or server-initiated edits. Server
1044         // initiated edits break causality, so we avoid those.
1045         return Ok(None);
1046     }
1047
1048     let line_index =
1049         snap.file_line_index(from_proto::file_id(&snap, &params.text_document.uri)?)?;
1050     let frange = from_proto::file_range(&snap, params.text_document.clone(), params.range)?;
1051
1052     let mut assists_config = snap.config.assist();
1053     assists_config.allowed = params
1054         .context
1055         .only
1056         .clone()
1057         .map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect());
1058
1059     let mut res: Vec<lsp_ext::CodeAction> = Vec::new();
1060
1061     let code_action_resolve_cap = snap.config.code_action_resolve();
1062     let resolve = if code_action_resolve_cap {
1063         AssistResolveStrategy::None
1064     } else {
1065         AssistResolveStrategy::All
1066     };
1067     let assists = snap.analysis.assists_with_fixes(
1068         &assists_config,
1069         &snap.config.diagnostics(),
1070         resolve,
1071         frange,
1072     )?;
1073     for (index, assist) in assists.into_iter().enumerate() {
1074         let resolve_data =
1075             if code_action_resolve_cap { Some((index, params.clone())) } else { None };
1076         let code_action = to_proto::code_action(&snap, assist, resolve_data)?;
1077         res.push(code_action)
1078     }
1079
1080     // Fixes from `cargo check`.
1081     for fix in snap.check_fixes.get(&frange.file_id).into_iter().flatten() {
1082         // FIXME: this mapping is awkward and shouldn't exist. Refactor
1083         // `snap.check_fixes` to not convert to LSP prematurely.
1084         let intersect_fix_range = fix
1085             .ranges
1086             .iter()
1087             .copied()
1088             .map(|range| from_proto::text_range(&line_index, range))
1089             .any(|fix_range| fix_range.intersect(frange.range).is_some());
1090         if intersect_fix_range {
1091             res.push(fix.action.clone());
1092         }
1093     }
1094
1095     Ok(Some(res))
1096 }
1097
1098 pub(crate) fn handle_code_action_resolve(
1099     snap: GlobalStateSnapshot,
1100     mut code_action: lsp_ext::CodeAction,
1101 ) -> Result<lsp_ext::CodeAction> {
1102     let _p = profile::span("handle_code_action_resolve");
1103     let params = match code_action.data.take() {
1104         Some(it) => it,
1105         None => return Err(invalid_params_error("code action without data".to_string()).into()),
1106     };
1107
1108     let file_id = from_proto::file_id(&snap, &params.code_action_params.text_document.uri)?;
1109     let line_index = snap.file_line_index(file_id)?;
1110     let range = from_proto::text_range(&line_index, params.code_action_params.range);
1111     let frange = FileRange { file_id, range };
1112
1113     let mut assists_config = snap.config.assist();
1114     assists_config.allowed = params
1115         .code_action_params
1116         .context
1117         .only
1118         .map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect());
1119
1120     let (assist_index, assist_resolve) = match parse_action_id(&params.id) {
1121         Ok(parsed_data) => parsed_data,
1122         Err(e) => {
1123             return Err(invalid_params_error(format!(
1124                 "Failed to parse action id string '{}': {}",
1125                 params.id, e
1126             ))
1127             .into())
1128         }
1129     };
1130
1131     let expected_assist_id = assist_resolve.assist_id.clone();
1132     let expected_kind = assist_resolve.assist_kind;
1133
1134     let assists = snap.analysis.assists_with_fixes(
1135         &assists_config,
1136         &snap.config.diagnostics(),
1137         AssistResolveStrategy::Single(assist_resolve),
1138         frange,
1139     )?;
1140
1141     let assist = match assists.get(assist_index) {
1142         Some(assist) => assist,
1143         None => return Err(invalid_params_error(format!(
1144             "Failed to find the assist for index {} provided by the resolve request. Resolve request assist id: {}",
1145             assist_index, params.id,
1146         ))
1147         .into())
1148     };
1149     if assist.id.0 != expected_assist_id || assist.id.1 != expected_kind {
1150         return Err(invalid_params_error(format!(
1151             "Mismatching assist at index {} for the resolve parameters given. Resolve request assist id: {}, actual id: {:?}.",
1152             assist_index, params.id, assist.id
1153         ))
1154         .into());
1155     }
1156     let edit = to_proto::code_action(&snap, assist.clone(), None)?.edit;
1157     code_action.edit = edit;
1158     Ok(code_action)
1159 }
1160
1161 fn parse_action_id(action_id: &str) -> Result<(usize, SingleResolve), String> {
1162     let id_parts = action_id.split(':').collect_vec();
1163     match id_parts.as_slice() {
1164         [assist_id_string, assist_kind_string, index_string] => {
1165             let assist_kind: AssistKind = assist_kind_string.parse()?;
1166             let index: usize = match index_string.parse() {
1167                 Ok(index) => index,
1168                 Err(e) => return Err(format!("Incorrect index string: {}", e)),
1169             };
1170             Ok((index, SingleResolve { assist_id: assist_id_string.to_string(), assist_kind }))
1171         }
1172         _ => Err("Action id contains incorrect number of segments".to_string()),
1173     }
1174 }
1175
1176 pub(crate) fn handle_code_lens(
1177     snap: GlobalStateSnapshot,
1178     params: lsp_types::CodeLensParams,
1179 ) -> Result<Option<Vec<CodeLens>>> {
1180     let _p = profile::span("handle_code_lens");
1181
1182     let lens_config = snap.config.lens();
1183     if lens_config.none() {
1184         // early return before any db query!
1185         return Ok(Some(Vec::default()));
1186     }
1187
1188     let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
1189     let cargo_target_spec = CargoTargetSpec::for_file(&snap, file_id)?;
1190
1191     let annotations = snap.analysis.annotations(
1192         &AnnotationConfig {
1193             binary_target: cargo_target_spec
1194                 .map(|spec| {
1195                     matches!(
1196                         spec.target_kind,
1197                         TargetKind::Bin | TargetKind::Example | TargetKind::Test
1198                     )
1199                 })
1200                 .unwrap_or(false),
1201             annotate_runnables: lens_config.runnable(),
1202             annotate_impls: lens_config.implementations,
1203             annotate_references: lens_config.refs,
1204             annotate_method_references: lens_config.method_refs,
1205             annotate_enum_variant_references: lens_config.enum_variant_refs,
1206         },
1207         file_id,
1208     )?;
1209
1210     let mut res = Vec::new();
1211     for a in annotations {
1212         to_proto::code_lens(&mut res, &snap, a)?;
1213     }
1214
1215     Ok(Some(res))
1216 }
1217
1218 pub(crate) fn handle_code_lens_resolve(
1219     snap: GlobalStateSnapshot,
1220     code_lens: CodeLens,
1221 ) -> Result<CodeLens> {
1222     let annotation = from_proto::annotation(&snap, code_lens.clone())?;
1223     let annotation = snap.analysis.resolve_annotation(annotation)?;
1224
1225     let mut acc = Vec::new();
1226     to_proto::code_lens(&mut acc, &snap, annotation)?;
1227
1228     let res = match acc.pop() {
1229         Some(it) if acc.is_empty() => it,
1230         _ => {
1231             never!();
1232             code_lens
1233         }
1234     };
1235
1236     Ok(res)
1237 }
1238
1239 pub(crate) fn handle_document_highlight(
1240     snap: GlobalStateSnapshot,
1241     params: lsp_types::DocumentHighlightParams,
1242 ) -> Result<Option<Vec<lsp_types::DocumentHighlight>>> {
1243     let _p = profile::span("handle_document_highlight");
1244     let position = from_proto::file_position(&snap, params.text_document_position_params)?;
1245     let line_index = snap.file_line_index(position.file_id)?;
1246
1247     let refs = match snap.analysis.highlight_related(snap.config.highlight_related(), position)? {
1248         None => return Ok(None),
1249         Some(refs) => refs,
1250     };
1251     let res = refs
1252         .into_iter()
1253         .map(|ide::HighlightedRange { range, category }| lsp_types::DocumentHighlight {
1254             range: to_proto::range(&line_index, range),
1255             kind: category.map(to_proto::document_highlight_kind),
1256         })
1257         .collect();
1258     Ok(Some(res))
1259 }
1260
1261 pub(crate) fn handle_ssr(
1262     snap: GlobalStateSnapshot,
1263     params: lsp_ext::SsrParams,
1264 ) -> Result<lsp_types::WorkspaceEdit> {
1265     let _p = profile::span("handle_ssr");
1266     let selections = params
1267         .selections
1268         .iter()
1269         .map(|range| from_proto::file_range(&snap, params.position.text_document.clone(), *range))
1270         .collect::<Result<Vec<_>, _>>()?;
1271     let position = from_proto::file_position(&snap, params.position)?;
1272     let source_change = snap.analysis.structural_search_replace(
1273         &params.query,
1274         params.parse_only,
1275         position,
1276         selections,
1277     )??;
1278     to_proto::workspace_edit(&snap, source_change)
1279 }
1280
1281 pub(crate) fn publish_diagnostics(
1282     snap: &GlobalStateSnapshot,
1283     file_id: FileId,
1284 ) -> Result<Vec<Diagnostic>> {
1285     let _p = profile::span("publish_diagnostics");
1286     let line_index = snap.file_line_index(file_id)?;
1287
1288     let diagnostics: Vec<Diagnostic> = snap
1289         .analysis
1290         .diagnostics(&snap.config.diagnostics(), AssistResolveStrategy::None, file_id)?
1291         .into_iter()
1292         .map(|d| Diagnostic {
1293             range: to_proto::range(&line_index, d.range),
1294             severity: Some(to_proto::diagnostic_severity(d.severity)),
1295             code: Some(NumberOrString::String(d.code.as_str().to_string())),
1296             code_description: Some(lsp_types::CodeDescription {
1297                 href: lsp_types::Url::parse(&format!(
1298                     "https://rust-analyzer.github.io/manual.html#{}",
1299                     d.code.as_str()
1300                 ))
1301                 .unwrap(),
1302             }),
1303             source: Some("rust-analyzer".to_string()),
1304             message: d.message,
1305             related_information: None,
1306             tags: if d.unused { Some(vec![DiagnosticTag::UNNECESSARY]) } else { None },
1307             data: None,
1308         })
1309         .collect();
1310     Ok(diagnostics)
1311 }
1312
1313 pub(crate) fn handle_inlay_hints(
1314     snap: GlobalStateSnapshot,
1315     params: InlayHintsParams,
1316 ) -> Result<Vec<InlayHint>> {
1317     let _p = profile::span("handle_inlay_hints");
1318     let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
1319     let line_index = snap.file_line_index(file_id)?;
1320     Ok(snap
1321         .analysis
1322         .inlay_hints(&snap.config.inlay_hints(), file_id)?
1323         .into_iter()
1324         .map(|it| to_proto::inlay_hint(&line_index, it))
1325         .collect())
1326 }
1327
1328 pub(crate) fn handle_call_hierarchy_prepare(
1329     snap: GlobalStateSnapshot,
1330     params: CallHierarchyPrepareParams,
1331 ) -> Result<Option<Vec<CallHierarchyItem>>> {
1332     let _p = profile::span("handle_call_hierarchy_prepare");
1333     let position = from_proto::file_position(&snap, params.text_document_position_params)?;
1334
1335     let nav_info = match snap.analysis.call_hierarchy(position)? {
1336         None => return Ok(None),
1337         Some(it) => it,
1338     };
1339
1340     let RangeInfo { range: _, info: navs } = nav_info;
1341     let res = navs
1342         .into_iter()
1343         .filter(|it| it.kind == Some(SymbolKind::Function))
1344         .map(|it| to_proto::call_hierarchy_item(&snap, it))
1345         .collect::<Result<Vec<_>>>()?;
1346
1347     Ok(Some(res))
1348 }
1349
1350 pub(crate) fn handle_call_hierarchy_incoming(
1351     snap: GlobalStateSnapshot,
1352     params: CallHierarchyIncomingCallsParams,
1353 ) -> Result<Option<Vec<CallHierarchyIncomingCall>>> {
1354     let _p = profile::span("handle_call_hierarchy_incoming");
1355     let item = params.item;
1356
1357     let doc = TextDocumentIdentifier::new(item.uri);
1358     let frange = from_proto::file_range(&snap, doc, item.selection_range)?;
1359     let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
1360
1361     let call_items = match snap.analysis.incoming_calls(fpos)? {
1362         None => return Ok(None),
1363         Some(it) => it,
1364     };
1365
1366     let mut res = vec![];
1367
1368     for call_item in call_items.into_iter() {
1369         let file_id = call_item.target.file_id;
1370         let line_index = snap.file_line_index(file_id)?;
1371         let item = to_proto::call_hierarchy_item(&snap, call_item.target)?;
1372         res.push(CallHierarchyIncomingCall {
1373             from: item,
1374             from_ranges: call_item
1375                 .ranges
1376                 .into_iter()
1377                 .map(|it| to_proto::range(&line_index, it))
1378                 .collect(),
1379         });
1380     }
1381
1382     Ok(Some(res))
1383 }
1384
1385 pub(crate) fn handle_call_hierarchy_outgoing(
1386     snap: GlobalStateSnapshot,
1387     params: CallHierarchyOutgoingCallsParams,
1388 ) -> Result<Option<Vec<CallHierarchyOutgoingCall>>> {
1389     let _p = profile::span("handle_call_hierarchy_outgoing");
1390     let item = params.item;
1391
1392     let doc = TextDocumentIdentifier::new(item.uri);
1393     let frange = from_proto::file_range(&snap, doc, item.selection_range)?;
1394     let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
1395
1396     let call_items = match snap.analysis.outgoing_calls(fpos)? {
1397         None => return Ok(None),
1398         Some(it) => it,
1399     };
1400
1401     let mut res = vec![];
1402
1403     for call_item in call_items.into_iter() {
1404         let file_id = call_item.target.file_id;
1405         let line_index = snap.file_line_index(file_id)?;
1406         let item = to_proto::call_hierarchy_item(&snap, call_item.target)?;
1407         res.push(CallHierarchyOutgoingCall {
1408             to: item,
1409             from_ranges: call_item
1410                 .ranges
1411                 .into_iter()
1412                 .map(|it| to_proto::range(&line_index, it))
1413                 .collect(),
1414         });
1415     }
1416
1417     Ok(Some(res))
1418 }
1419
1420 pub(crate) fn handle_semantic_tokens_full(
1421     snap: GlobalStateSnapshot,
1422     params: SemanticTokensParams,
1423 ) -> Result<Option<SemanticTokensResult>> {
1424     let _p = profile::span("handle_semantic_tokens_full");
1425
1426     let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
1427     let text = snap.analysis.file_text(file_id)?;
1428     let line_index = snap.file_line_index(file_id)?;
1429
1430     let highlights = snap.analysis.highlight(file_id)?;
1431     let highlight_strings = snap.config.highlighting_strings();
1432     let semantic_tokens =
1433         to_proto::semantic_tokens(&text, &line_index, highlights, highlight_strings);
1434
1435     // Unconditionally cache the tokens
1436     snap.semantic_tokens_cache.lock().insert(params.text_document.uri, semantic_tokens.clone());
1437
1438     Ok(Some(semantic_tokens.into()))
1439 }
1440
1441 pub(crate) fn handle_semantic_tokens_full_delta(
1442     snap: GlobalStateSnapshot,
1443     params: SemanticTokensDeltaParams,
1444 ) -> Result<Option<SemanticTokensFullDeltaResult>> {
1445     let _p = profile::span("handle_semantic_tokens_full_delta");
1446
1447     let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
1448     let text = snap.analysis.file_text(file_id)?;
1449     let line_index = snap.file_line_index(file_id)?;
1450
1451     let highlights = snap.analysis.highlight(file_id)?;
1452     let highlight_strings = snap.config.highlighting_strings();
1453     let semantic_tokens =
1454         to_proto::semantic_tokens(&text, &line_index, highlights, highlight_strings);
1455
1456     let mut cache = snap.semantic_tokens_cache.lock();
1457     let cached_tokens = cache.entry(params.text_document.uri).or_default();
1458
1459     if let Some(prev_id) = &cached_tokens.result_id {
1460         if *prev_id == params.previous_result_id {
1461             let delta = to_proto::semantic_token_delta(cached_tokens, &semantic_tokens);
1462             *cached_tokens = semantic_tokens;
1463             return Ok(Some(delta.into()));
1464         }
1465     }
1466
1467     *cached_tokens = semantic_tokens.clone();
1468
1469     Ok(Some(semantic_tokens.into()))
1470 }
1471
1472 pub(crate) fn handle_semantic_tokens_range(
1473     snap: GlobalStateSnapshot,
1474     params: SemanticTokensRangeParams,
1475 ) -> Result<Option<SemanticTokensRangeResult>> {
1476     let _p = profile::span("handle_semantic_tokens_range");
1477
1478     let frange = from_proto::file_range(&snap, params.text_document, params.range)?;
1479     let text = snap.analysis.file_text(frange.file_id)?;
1480     let line_index = snap.file_line_index(frange.file_id)?;
1481
1482     let highlights = snap.analysis.highlight_range(frange)?;
1483     let highlight_strings = snap.config.highlighting_strings();
1484     let semantic_tokens =
1485         to_proto::semantic_tokens(&text, &line_index, highlights, highlight_strings);
1486     Ok(Some(semantic_tokens.into()))
1487 }
1488
1489 pub(crate) fn handle_open_docs(
1490     snap: GlobalStateSnapshot,
1491     params: lsp_types::TextDocumentPositionParams,
1492 ) -> Result<Option<lsp_types::Url>> {
1493     let _p = profile::span("handle_open_docs");
1494     let position = from_proto::file_position(&snap, params)?;
1495
1496     let remote = snap.analysis.external_docs(position)?;
1497
1498     Ok(remote.and_then(|remote| Url::parse(&remote).ok()))
1499 }
1500
1501 pub(crate) fn handle_open_cargo_toml(
1502     snap: GlobalStateSnapshot,
1503     params: lsp_ext::OpenCargoTomlParams,
1504 ) -> Result<Option<lsp_types::GotoDefinitionResponse>> {
1505     let _p = profile::span("handle_open_cargo_toml");
1506     let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
1507
1508     let cargo_spec = match CargoTargetSpec::for_file(&snap, file_id)? {
1509         Some(it) => it,
1510         None => return Ok(None),
1511     };
1512
1513     let cargo_toml_url = to_proto::url_from_abs_path(&cargo_spec.cargo_toml);
1514     let res: lsp_types::GotoDefinitionResponse =
1515         Location::new(cargo_toml_url, Range::default()).into();
1516     Ok(Some(res))
1517 }
1518
1519 pub(crate) fn handle_move_item(
1520     snap: GlobalStateSnapshot,
1521     params: lsp_ext::MoveItemParams,
1522 ) -> Result<Vec<lsp_ext::SnippetTextEdit>> {
1523     let _p = profile::span("handle_move_item");
1524     let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
1525     let range = from_proto::file_range(&snap, params.text_document, params.range)?;
1526
1527     let direction = match params.direction {
1528         lsp_ext::MoveItemDirection::Up => ide::Direction::Up,
1529         lsp_ext::MoveItemDirection::Down => ide::Direction::Down,
1530     };
1531
1532     match snap.analysis.move_item(range, direction)? {
1533         Some(text_edit) => {
1534             let line_index = snap.file_line_index(file_id)?;
1535             Ok(to_proto::snippet_text_edit_vec(&line_index, true, text_edit))
1536         }
1537         None => Ok(vec![]),
1538     }
1539 }
1540
1541 fn to_command_link(command: lsp_types::Command, tooltip: String) -> lsp_ext::CommandLink {
1542     lsp_ext::CommandLink { tooltip: Some(tooltip), command }
1543 }
1544
1545 fn show_impl_command_link(
1546     snap: &GlobalStateSnapshot,
1547     position: &FilePosition,
1548 ) -> Option<lsp_ext::CommandLinkGroup> {
1549     if snap.config.hover_actions().implementations && snap.config.client_commands().show_reference {
1550         if let Some(nav_data) = snap.analysis.goto_implementation(*position).unwrap_or(None) {
1551             let uri = to_proto::url(snap, position.file_id);
1552             let line_index = snap.file_line_index(position.file_id).ok()?;
1553             let position = to_proto::position(&line_index, position.offset);
1554             let locations: Vec<_> = nav_data
1555                 .info
1556                 .into_iter()
1557                 .filter_map(|nav| to_proto::location_from_nav(snap, nav).ok())
1558                 .collect();
1559             let title = to_proto::implementation_title(locations.len());
1560             let command = to_proto::command::show_references(title, &uri, position, locations);
1561
1562             return Some(lsp_ext::CommandLinkGroup {
1563                 commands: vec![to_command_link(command, "Go to implementations".into())],
1564                 ..Default::default()
1565             });
1566         }
1567     }
1568     None
1569 }
1570
1571 fn show_ref_command_link(
1572     snap: &GlobalStateSnapshot,
1573     position: &FilePosition,
1574 ) -> Option<lsp_ext::CommandLinkGroup> {
1575     if snap.config.hover_actions().references && snap.config.client_commands().show_reference {
1576         if let Some(ref_search_res) = snap.analysis.find_all_refs(*position, None).unwrap_or(None) {
1577             let uri = to_proto::url(snap, position.file_id);
1578             let line_index = snap.file_line_index(position.file_id).ok()?;
1579             let position = to_proto::position(&line_index, position.offset);
1580             let locations: Vec<_> = ref_search_res
1581                 .into_iter()
1582                 .flat_map(|res| res.references)
1583                 .flat_map(|(file_id, ranges)| {
1584                     ranges.into_iter().filter_map(move |(range, _)| {
1585                         to_proto::location(snap, FileRange { file_id, range }).ok()
1586                     })
1587                 })
1588                 .collect();
1589             let title = to_proto::reference_title(locations.len());
1590             let command = to_proto::command::show_references(title, &uri, position, locations);
1591
1592             return Some(lsp_ext::CommandLinkGroup {
1593                 commands: vec![to_command_link(command, "Go to references".into())],
1594                 ..Default::default()
1595             });
1596         }
1597     }
1598     None
1599 }
1600
1601 fn runnable_action_links(
1602     snap: &GlobalStateSnapshot,
1603     runnable: Runnable,
1604 ) -> Option<lsp_ext::CommandLinkGroup> {
1605     let hover_actions_config = snap.config.hover_actions();
1606     if !hover_actions_config.runnable() {
1607         return None;
1608     }
1609
1610     let cargo_spec = CargoTargetSpec::for_file(snap, runnable.nav.file_id).ok()?;
1611     if should_skip_target(&runnable, cargo_spec.as_ref()) {
1612         return None;
1613     }
1614
1615     let client_commands_config = snap.config.client_commands();
1616     if !(client_commands_config.run_single || client_commands_config.debug_single) {
1617         return None;
1618     }
1619
1620     let title = runnable.title();
1621     let r = to_proto::runnable(snap, runnable).ok()?;
1622
1623     let mut group = lsp_ext::CommandLinkGroup::default();
1624
1625     if hover_actions_config.run && client_commands_config.run_single {
1626         let run_command = to_proto::command::run_single(&r, &title);
1627         group.commands.push(to_command_link(run_command, r.label.clone()));
1628     }
1629
1630     if hover_actions_config.debug && client_commands_config.debug_single {
1631         let dbg_command = to_proto::command::debug_single(&r);
1632         group.commands.push(to_command_link(dbg_command, r.label));
1633     }
1634
1635     Some(group)
1636 }
1637
1638 fn goto_type_action_links(
1639     snap: &GlobalStateSnapshot,
1640     nav_targets: &[HoverGotoTypeData],
1641 ) -> Option<lsp_ext::CommandLinkGroup> {
1642     if !snap.config.hover_actions().goto_type_def
1643         || nav_targets.is_empty()
1644         || !snap.config.client_commands().goto_location
1645     {
1646         return None;
1647     }
1648
1649     Some(lsp_ext::CommandLinkGroup {
1650         title: Some("Go to ".into()),
1651         commands: nav_targets
1652             .iter()
1653             .filter_map(|it| {
1654                 to_proto::command::goto_location(snap, &it.nav)
1655                     .map(|cmd| to_command_link(cmd, it.mod_path.clone()))
1656             })
1657             .collect(),
1658     })
1659 }
1660
1661 fn prepare_hover_actions(
1662     snap: &GlobalStateSnapshot,
1663     actions: &[HoverAction],
1664 ) -> Vec<lsp_ext::CommandLinkGroup> {
1665     actions
1666         .iter()
1667         .filter_map(|it| match it {
1668             HoverAction::Implementation(position) => show_impl_command_link(snap, position),
1669             HoverAction::Reference(position) => show_ref_command_link(snap, position),
1670             HoverAction::Runnable(r) => runnable_action_links(snap, r.clone()),
1671             HoverAction::GoToType(targets) => goto_type_action_links(snap, targets),
1672         })
1673         .collect()
1674 }
1675
1676 fn should_skip_target(runnable: &Runnable, cargo_spec: Option<&CargoTargetSpec>) -> bool {
1677     match runnable.kind {
1678         RunnableKind::Bin => {
1679             // Do not suggest binary run on other target than binary
1680             match &cargo_spec {
1681                 Some(spec) => !matches!(
1682                     spec.target_kind,
1683                     TargetKind::Bin | TargetKind::Example | TargetKind::Test
1684                 ),
1685                 None => true,
1686             }
1687         }
1688         _ => false,
1689     }
1690 }
1691
1692 fn run_rustfmt(
1693     snap: &GlobalStateSnapshot,
1694     text_document: TextDocumentIdentifier,
1695     range: Option<lsp_types::Range>,
1696 ) -> Result<Option<Vec<lsp_types::TextEdit>>> {
1697     let file_id = from_proto::file_id(snap, &text_document.uri)?;
1698     let file = snap.analysis.file_text(file_id)?;
1699     let crate_ids = snap.analysis.crate_for(file_id)?;
1700
1701     let line_index = snap.file_line_index(file_id)?;
1702
1703     let mut rustfmt = match snap.config.rustfmt() {
1704         RustfmtConfig::Rustfmt { extra_args, enable_range_formatting } => {
1705             let mut cmd = process::Command::new(toolchain::rustfmt());
1706             cmd.args(extra_args);
1707             // try to chdir to the file so we can respect `rustfmt.toml`
1708             // FIXME: use `rustfmt --config-path` once
1709             // https://github.com/rust-lang/rustfmt/issues/4660 gets fixed
1710             match text_document.uri.to_file_path() {
1711                 Ok(mut path) => {
1712                     // pop off file name
1713                     if path.pop() && path.is_dir() {
1714                         cmd.current_dir(path);
1715                     }
1716                 }
1717                 Err(_) => {
1718                     tracing::error!(
1719                         "Unable to get file path for {}, rustfmt.toml might be ignored",
1720                         text_document.uri
1721                     );
1722                 }
1723             }
1724             if let Some(&crate_id) = crate_ids.first() {
1725                 // Assume all crates are in the same edition
1726                 let edition = snap.analysis.crate_edition(crate_id)?;
1727                 cmd.arg("--edition");
1728                 cmd.arg(edition.to_string());
1729             }
1730
1731             if let Some(range) = range {
1732                 if !enable_range_formatting {
1733                     return Err(LspError::new(
1734                         ErrorCode::InvalidRequest as i32,
1735                         String::from(
1736                             "rustfmt range formatting is unstable. \
1737                             Opt-in by using a nightly build of rustfmt and setting \
1738                             `rustfmt.enableRangeFormatting` to true in your LSP configuration",
1739                         ),
1740                     )
1741                     .into());
1742                 }
1743
1744                 let frange = from_proto::file_range(snap, text_document, range)?;
1745                 let start_line = line_index.index.line_col(frange.range.start()).line;
1746                 let end_line = line_index.index.line_col(frange.range.end()).line;
1747
1748                 cmd.arg("--unstable-features");
1749                 cmd.arg("--file-lines");
1750                 cmd.arg(
1751                     json!([{
1752                         "file": "stdin",
1753                         "range": [start_line, end_line]
1754                     }])
1755                     .to_string(),
1756                 );
1757             }
1758
1759             cmd
1760         }
1761         RustfmtConfig::CustomCommand { command, args } => {
1762             let mut cmd = process::Command::new(command);
1763             cmd.args(args);
1764             cmd
1765         }
1766     };
1767
1768     let mut rustfmt = rustfmt
1769         .stdin(Stdio::piped())
1770         .stdout(Stdio::piped())
1771         .stderr(Stdio::piped())
1772         .spawn()
1773         .context(format!("Failed to spawn {:?}", rustfmt))?;
1774
1775     rustfmt.stdin.as_mut().unwrap().write_all(file.as_bytes())?;
1776
1777     let output = rustfmt.wait_with_output()?;
1778     let captured_stdout = String::from_utf8(output.stdout)?;
1779     let captured_stderr = String::from_utf8(output.stderr).unwrap_or_default();
1780
1781     if !output.status.success() {
1782         let rustfmt_not_installed =
1783             captured_stderr.contains("not installed") || captured_stderr.contains("not available");
1784
1785         return match output.status.code() {
1786             Some(1) if !rustfmt_not_installed => {
1787                 // While `rustfmt` doesn't have a specific exit code for parse errors this is the
1788                 // likely cause exiting with 1. Most Language Servers swallow parse errors on
1789                 // formatting because otherwise an error is surfaced to the user on top of the
1790                 // syntax error diagnostics they're already receiving. This is especially jarring
1791                 // if they have format on save enabled.
1792                 tracing::info!("rustfmt exited with status 1, assuming parse error and ignoring");
1793                 Ok(None)
1794             }
1795             _ => {
1796                 // Something else happened - e.g. `rustfmt` is missing or caught a signal
1797                 Err(LspError::new(
1798                     -32900,
1799                     format!(
1800                         r#"rustfmt exited with:
1801                            Status: {}
1802                            stdout: {}
1803                            stderr: {}"#,
1804                         output.status, captured_stdout, captured_stderr,
1805                     ),
1806                 )
1807                 .into())
1808             }
1809         };
1810     }
1811
1812     let (new_text, new_line_endings) = LineEndings::normalize(captured_stdout);
1813
1814     if line_index.endings != new_line_endings {
1815         // If line endings are different, send the entire file.
1816         // Diffing would not work here, as the line endings might be the only
1817         // difference.
1818         Ok(Some(to_proto::text_edit_vec(
1819             &line_index,
1820             TextEdit::replace(TextRange::up_to(TextSize::of(&*file)), new_text),
1821         )))
1822     } else if *file == new_text {
1823         // The document is already formatted correctly -- no edits needed.
1824         Ok(None)
1825     } else {
1826         Ok(Some(to_proto::text_edit_vec(&line_index, diff(&file, &new_text))))
1827     }
1828 }