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