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