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