]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/rust-analyzer/src/handlers.rs
Rollup merge of #100389 - compiler-errors:return-type-suggestion-cycle, r=cjgillot
[rust.git] / src / tools / rust-analyzer / 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             message: d.message,
1324             related_information: None,
1325             tags: if d.unused { Some(vec![DiagnosticTag::UNNECESSARY]) } else { None },
1326             data: None,
1327         })
1328         .collect();
1329     Ok(diagnostics)
1330 }
1331
1332 pub(crate) fn handle_inlay_hints(
1333     snap: GlobalStateSnapshot,
1334     params: InlayHintParams,
1335 ) -> Result<Option<Vec<InlayHint>>> {
1336     let _p = profile::span("handle_inlay_hints");
1337     let document_uri = &params.text_document.uri;
1338     let file_id = from_proto::file_id(&snap, document_uri)?;
1339     let line_index = snap.file_line_index(file_id)?;
1340     let range = from_proto::file_range(
1341         &snap,
1342         TextDocumentIdentifier::new(document_uri.to_owned()),
1343         params.range,
1344     )?;
1345     let inlay_hints_config = snap.config.inlay_hints();
1346     Ok(Some(
1347         snap.analysis
1348             .inlay_hints(&inlay_hints_config, file_id, Some(range))?
1349             .into_iter()
1350             .map(|it| {
1351                 to_proto::inlay_hint(&snap, &line_index, inlay_hints_config.render_colons, it)
1352             })
1353             .collect(),
1354     ))
1355 }
1356
1357 pub(crate) fn handle_inlay_hints_resolve(
1358     snap: GlobalStateSnapshot,
1359     mut hint: InlayHint,
1360 ) -> Result<InlayHint> {
1361     let _p = profile::span("handle_inlay_hints_resolve");
1362     let data = match hint.data.take() {
1363         Some(it) => it,
1364         None => return Ok(hint),
1365     };
1366
1367     let resolve_data: lsp_ext::InlayHintResolveData = serde_json::from_value(data)?;
1368
1369     let file_range = from_proto::file_range(
1370         &snap,
1371         resolve_data.text_document,
1372         match resolve_data.position {
1373             PositionOrRange::Position(pos) => Range::new(pos, pos),
1374             PositionOrRange::Range(range) => range,
1375         },
1376     )?;
1377     let info = match snap.analysis.hover(&snap.config.hover(), file_range)? {
1378         None => return Ok(hint),
1379         Some(info) => info,
1380     };
1381
1382     let markup_kind =
1383         snap.config.hover().documentation.map_or(ide::HoverDocFormat::Markdown, |kind| kind);
1384
1385     // FIXME: hover actions?
1386     hint.tooltip = Some(lsp_types::InlayHintTooltip::MarkupContent(to_proto::markup_content(
1387         info.info.markup,
1388         markup_kind,
1389     )));
1390     Ok(hint)
1391 }
1392
1393 pub(crate) fn handle_call_hierarchy_prepare(
1394     snap: GlobalStateSnapshot,
1395     params: CallHierarchyPrepareParams,
1396 ) -> Result<Option<Vec<CallHierarchyItem>>> {
1397     let _p = profile::span("handle_call_hierarchy_prepare");
1398     let position = from_proto::file_position(&snap, params.text_document_position_params)?;
1399
1400     let nav_info = match snap.analysis.call_hierarchy(position)? {
1401         None => return Ok(None),
1402         Some(it) => it,
1403     };
1404
1405     let RangeInfo { range: _, info: navs } = nav_info;
1406     let res = navs
1407         .into_iter()
1408         .filter(|it| it.kind == Some(SymbolKind::Function))
1409         .map(|it| to_proto::call_hierarchy_item(&snap, it))
1410         .collect::<Result<Vec<_>>>()?;
1411
1412     Ok(Some(res))
1413 }
1414
1415 pub(crate) fn handle_call_hierarchy_incoming(
1416     snap: GlobalStateSnapshot,
1417     params: CallHierarchyIncomingCallsParams,
1418 ) -> Result<Option<Vec<CallHierarchyIncomingCall>>> {
1419     let _p = profile::span("handle_call_hierarchy_incoming");
1420     let item = params.item;
1421
1422     let doc = TextDocumentIdentifier::new(item.uri);
1423     let frange = from_proto::file_range(&snap, doc, item.selection_range)?;
1424     let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
1425
1426     let call_items = match snap.analysis.incoming_calls(fpos)? {
1427         None => return Ok(None),
1428         Some(it) => it,
1429     };
1430
1431     let mut res = vec![];
1432
1433     for call_item in call_items.into_iter() {
1434         let file_id = call_item.target.file_id;
1435         let line_index = snap.file_line_index(file_id)?;
1436         let item = to_proto::call_hierarchy_item(&snap, call_item.target)?;
1437         res.push(CallHierarchyIncomingCall {
1438             from: item,
1439             from_ranges: call_item
1440                 .ranges
1441                 .into_iter()
1442                 .map(|it| to_proto::range(&line_index, it))
1443                 .collect(),
1444         });
1445     }
1446
1447     Ok(Some(res))
1448 }
1449
1450 pub(crate) fn handle_call_hierarchy_outgoing(
1451     snap: GlobalStateSnapshot,
1452     params: CallHierarchyOutgoingCallsParams,
1453 ) -> Result<Option<Vec<CallHierarchyOutgoingCall>>> {
1454     let _p = profile::span("handle_call_hierarchy_outgoing");
1455     let item = params.item;
1456
1457     let doc = TextDocumentIdentifier::new(item.uri);
1458     let frange = from_proto::file_range(&snap, doc, item.selection_range)?;
1459     let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
1460
1461     let call_items = match snap.analysis.outgoing_calls(fpos)? {
1462         None => return Ok(None),
1463         Some(it) => it,
1464     };
1465
1466     let mut res = vec![];
1467
1468     for call_item in call_items.into_iter() {
1469         let file_id = call_item.target.file_id;
1470         let line_index = snap.file_line_index(file_id)?;
1471         let item = to_proto::call_hierarchy_item(&snap, call_item.target)?;
1472         res.push(CallHierarchyOutgoingCall {
1473             to: item,
1474             from_ranges: call_item
1475                 .ranges
1476                 .into_iter()
1477                 .map(|it| to_proto::range(&line_index, it))
1478                 .collect(),
1479         });
1480     }
1481
1482     Ok(Some(res))
1483 }
1484
1485 pub(crate) fn handle_semantic_tokens_full(
1486     snap: GlobalStateSnapshot,
1487     params: SemanticTokensParams,
1488 ) -> Result<Option<SemanticTokensResult>> {
1489     let _p = profile::span("handle_semantic_tokens_full");
1490
1491     let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
1492     let text = snap.analysis.file_text(file_id)?;
1493     let line_index = snap.file_line_index(file_id)?;
1494
1495     let highlights = snap.analysis.highlight(file_id)?;
1496     let highlight_strings = snap.config.highlighting_strings();
1497     let semantic_tokens =
1498         to_proto::semantic_tokens(&text, &line_index, highlights, highlight_strings);
1499
1500     // Unconditionally cache the tokens
1501     snap.semantic_tokens_cache.lock().insert(params.text_document.uri, semantic_tokens.clone());
1502
1503     Ok(Some(semantic_tokens.into()))
1504 }
1505
1506 pub(crate) fn handle_semantic_tokens_full_delta(
1507     snap: GlobalStateSnapshot,
1508     params: SemanticTokensDeltaParams,
1509 ) -> Result<Option<SemanticTokensFullDeltaResult>> {
1510     let _p = profile::span("handle_semantic_tokens_full_delta");
1511
1512     let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
1513     let text = snap.analysis.file_text(file_id)?;
1514     let line_index = snap.file_line_index(file_id)?;
1515
1516     let highlights = snap.analysis.highlight(file_id)?;
1517     let highlight_strings = snap.config.highlighting_strings();
1518     let semantic_tokens =
1519         to_proto::semantic_tokens(&text, &line_index, highlights, highlight_strings);
1520
1521     let mut cache = snap.semantic_tokens_cache.lock();
1522     let cached_tokens = cache.entry(params.text_document.uri).or_default();
1523
1524     if let Some(prev_id) = &cached_tokens.result_id {
1525         if *prev_id == params.previous_result_id {
1526             let delta = to_proto::semantic_token_delta(cached_tokens, &semantic_tokens);
1527             *cached_tokens = semantic_tokens;
1528             return Ok(Some(delta.into()));
1529         }
1530     }
1531
1532     *cached_tokens = semantic_tokens.clone();
1533
1534     Ok(Some(semantic_tokens.into()))
1535 }
1536
1537 pub(crate) fn handle_semantic_tokens_range(
1538     snap: GlobalStateSnapshot,
1539     params: SemanticTokensRangeParams,
1540 ) -> Result<Option<SemanticTokensRangeResult>> {
1541     let _p = profile::span("handle_semantic_tokens_range");
1542
1543     let frange = from_proto::file_range(&snap, params.text_document, params.range)?;
1544     let text = snap.analysis.file_text(frange.file_id)?;
1545     let line_index = snap.file_line_index(frange.file_id)?;
1546
1547     let highlights = snap.analysis.highlight_range(frange)?;
1548     let highlight_strings = snap.config.highlighting_strings();
1549     let semantic_tokens =
1550         to_proto::semantic_tokens(&text, &line_index, highlights, highlight_strings);
1551     Ok(Some(semantic_tokens.into()))
1552 }
1553
1554 pub(crate) fn handle_open_docs(
1555     snap: GlobalStateSnapshot,
1556     params: lsp_types::TextDocumentPositionParams,
1557 ) -> Result<Option<lsp_types::Url>> {
1558     let _p = profile::span("handle_open_docs");
1559     let position = from_proto::file_position(&snap, params)?;
1560
1561     let remote = snap.analysis.external_docs(position)?;
1562
1563     Ok(remote.and_then(|remote| Url::parse(&remote).ok()))
1564 }
1565
1566 pub(crate) fn handle_open_cargo_toml(
1567     snap: GlobalStateSnapshot,
1568     params: lsp_ext::OpenCargoTomlParams,
1569 ) -> Result<Option<lsp_types::GotoDefinitionResponse>> {
1570     let _p = profile::span("handle_open_cargo_toml");
1571     let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
1572
1573     let cargo_spec = match CargoTargetSpec::for_file(&snap, file_id)? {
1574         Some(it) => it,
1575         None => return Ok(None),
1576     };
1577
1578     let cargo_toml_url = to_proto::url_from_abs_path(&cargo_spec.cargo_toml);
1579     let res: lsp_types::GotoDefinitionResponse =
1580         Location::new(cargo_toml_url, Range::default()).into();
1581     Ok(Some(res))
1582 }
1583
1584 pub(crate) fn handle_move_item(
1585     snap: GlobalStateSnapshot,
1586     params: lsp_ext::MoveItemParams,
1587 ) -> Result<Vec<lsp_ext::SnippetTextEdit>> {
1588     let _p = profile::span("handle_move_item");
1589     let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
1590     let range = from_proto::file_range(&snap, params.text_document, params.range)?;
1591
1592     let direction = match params.direction {
1593         lsp_ext::MoveItemDirection::Up => ide::Direction::Up,
1594         lsp_ext::MoveItemDirection::Down => ide::Direction::Down,
1595     };
1596
1597     match snap.analysis.move_item(range, direction)? {
1598         Some(text_edit) => {
1599             let line_index = snap.file_line_index(file_id)?;
1600             Ok(to_proto::snippet_text_edit_vec(&line_index, true, text_edit))
1601         }
1602         None => Ok(vec![]),
1603     }
1604 }
1605
1606 fn to_command_link(command: lsp_types::Command, tooltip: String) -> lsp_ext::CommandLink {
1607     lsp_ext::CommandLink { tooltip: Some(tooltip), command }
1608 }
1609
1610 fn show_impl_command_link(
1611     snap: &GlobalStateSnapshot,
1612     position: &FilePosition,
1613 ) -> Option<lsp_ext::CommandLinkGroup> {
1614     if snap.config.hover_actions().implementations && snap.config.client_commands().show_reference {
1615         if let Some(nav_data) = snap.analysis.goto_implementation(*position).unwrap_or(None) {
1616             let uri = to_proto::url(snap, position.file_id);
1617             let line_index = snap.file_line_index(position.file_id).ok()?;
1618             let position = to_proto::position(&line_index, position.offset);
1619             let locations: Vec<_> = nav_data
1620                 .info
1621                 .into_iter()
1622                 .filter_map(|nav| to_proto::location_from_nav(snap, nav).ok())
1623                 .collect();
1624             let title = to_proto::implementation_title(locations.len());
1625             let command = to_proto::command::show_references(title, &uri, position, locations);
1626
1627             return Some(lsp_ext::CommandLinkGroup {
1628                 commands: vec![to_command_link(command, "Go to implementations".into())],
1629                 ..Default::default()
1630             });
1631         }
1632     }
1633     None
1634 }
1635
1636 fn show_ref_command_link(
1637     snap: &GlobalStateSnapshot,
1638     position: &FilePosition,
1639 ) -> Option<lsp_ext::CommandLinkGroup> {
1640     if snap.config.hover_actions().references && snap.config.client_commands().show_reference {
1641         if let Some(ref_search_res) = snap.analysis.find_all_refs(*position, None).unwrap_or(None) {
1642             let uri = to_proto::url(snap, position.file_id);
1643             let line_index = snap.file_line_index(position.file_id).ok()?;
1644             let position = to_proto::position(&line_index, position.offset);
1645             let locations: Vec<_> = ref_search_res
1646                 .into_iter()
1647                 .flat_map(|res| res.references)
1648                 .flat_map(|(file_id, ranges)| {
1649                     ranges.into_iter().filter_map(move |(range, _)| {
1650                         to_proto::location(snap, FileRange { file_id, range }).ok()
1651                     })
1652                 })
1653                 .collect();
1654             let title = to_proto::reference_title(locations.len());
1655             let command = to_proto::command::show_references(title, &uri, position, locations);
1656
1657             return Some(lsp_ext::CommandLinkGroup {
1658                 commands: vec![to_command_link(command, "Go to references".into())],
1659                 ..Default::default()
1660             });
1661         }
1662     }
1663     None
1664 }
1665
1666 fn runnable_action_links(
1667     snap: &GlobalStateSnapshot,
1668     runnable: Runnable,
1669 ) -> Option<lsp_ext::CommandLinkGroup> {
1670     let hover_actions_config = snap.config.hover_actions();
1671     if !hover_actions_config.runnable() {
1672         return None;
1673     }
1674
1675     let cargo_spec = CargoTargetSpec::for_file(snap, runnable.nav.file_id).ok()?;
1676     if should_skip_target(&runnable, cargo_spec.as_ref()) {
1677         return None;
1678     }
1679
1680     let client_commands_config = snap.config.client_commands();
1681     if !(client_commands_config.run_single || client_commands_config.debug_single) {
1682         return None;
1683     }
1684
1685     let title = runnable.title();
1686     let r = to_proto::runnable(snap, runnable).ok()?;
1687
1688     let mut group = lsp_ext::CommandLinkGroup::default();
1689
1690     if hover_actions_config.run && client_commands_config.run_single {
1691         let run_command = to_proto::command::run_single(&r, &title);
1692         group.commands.push(to_command_link(run_command, r.label.clone()));
1693     }
1694
1695     if hover_actions_config.debug && client_commands_config.debug_single {
1696         let dbg_command = to_proto::command::debug_single(&r);
1697         group.commands.push(to_command_link(dbg_command, r.label));
1698     }
1699
1700     Some(group)
1701 }
1702
1703 fn goto_type_action_links(
1704     snap: &GlobalStateSnapshot,
1705     nav_targets: &[HoverGotoTypeData],
1706 ) -> Option<lsp_ext::CommandLinkGroup> {
1707     if !snap.config.hover_actions().goto_type_def
1708         || nav_targets.is_empty()
1709         || !snap.config.client_commands().goto_location
1710     {
1711         return None;
1712     }
1713
1714     Some(lsp_ext::CommandLinkGroup {
1715         title: Some("Go to ".into()),
1716         commands: nav_targets
1717             .iter()
1718             .filter_map(|it| {
1719                 to_proto::command::goto_location(snap, &it.nav)
1720                     .map(|cmd| to_command_link(cmd, it.mod_path.clone()))
1721             })
1722             .collect(),
1723     })
1724 }
1725
1726 fn prepare_hover_actions(
1727     snap: &GlobalStateSnapshot,
1728     actions: &[HoverAction],
1729 ) -> Vec<lsp_ext::CommandLinkGroup> {
1730     actions
1731         .iter()
1732         .filter_map(|it| match it {
1733             HoverAction::Implementation(position) => show_impl_command_link(snap, position),
1734             HoverAction::Reference(position) => show_ref_command_link(snap, position),
1735             HoverAction::Runnable(r) => runnable_action_links(snap, r.clone()),
1736             HoverAction::GoToType(targets) => goto_type_action_links(snap, targets),
1737         })
1738         .collect()
1739 }
1740
1741 fn should_skip_target(runnable: &Runnable, cargo_spec: Option<&CargoTargetSpec>) -> bool {
1742     match runnable.kind {
1743         RunnableKind::Bin => {
1744             // Do not suggest binary run on other target than binary
1745             match &cargo_spec {
1746                 Some(spec) => !matches!(
1747                     spec.target_kind,
1748                     TargetKind::Bin | TargetKind::Example | TargetKind::Test
1749                 ),
1750                 None => true,
1751             }
1752         }
1753         _ => false,
1754     }
1755 }
1756
1757 fn run_rustfmt(
1758     snap: &GlobalStateSnapshot,
1759     text_document: TextDocumentIdentifier,
1760     range: Option<lsp_types::Range>,
1761 ) -> Result<Option<Vec<lsp_types::TextEdit>>> {
1762     let file_id = from_proto::file_id(snap, &text_document.uri)?;
1763     let file = snap.analysis.file_text(file_id)?;
1764     let crate_ids = snap.analysis.crate_for(file_id)?;
1765
1766     let line_index = snap.file_line_index(file_id)?;
1767
1768     let mut rustfmt = match snap.config.rustfmt() {
1769         RustfmtConfig::Rustfmt { extra_args, enable_range_formatting } => {
1770             let mut cmd = process::Command::new(toolchain::rustfmt());
1771             cmd.args(extra_args);
1772             // try to chdir to the file so we can respect `rustfmt.toml`
1773             // FIXME: use `rustfmt --config-path` once
1774             // https://github.com/rust-lang/rustfmt/issues/4660 gets fixed
1775             match text_document.uri.to_file_path() {
1776                 Ok(mut path) => {
1777                     // pop off file name
1778                     if path.pop() && path.is_dir() {
1779                         cmd.current_dir(path);
1780                     }
1781                 }
1782                 Err(_) => {
1783                     tracing::error!(
1784                         "Unable to get file path for {}, rustfmt.toml might be ignored",
1785                         text_document.uri
1786                     );
1787                 }
1788             }
1789             if let Some(&crate_id) = crate_ids.first() {
1790                 // Assume all crates are in the same edition
1791                 let edition = snap.analysis.crate_edition(crate_id)?;
1792                 cmd.arg("--edition");
1793                 cmd.arg(edition.to_string());
1794             }
1795
1796             if let Some(range) = range {
1797                 if !enable_range_formatting {
1798                     return Err(LspError::new(
1799                         ErrorCode::InvalidRequest as i32,
1800                         String::from(
1801                             "rustfmt range formatting is unstable. \
1802                             Opt-in by using a nightly build of rustfmt and setting \
1803                             `rustfmt.rangeFormatting.enable` to true in your LSP configuration",
1804                         ),
1805                     )
1806                     .into());
1807                 }
1808
1809                 let frange = from_proto::file_range(snap, text_document, range)?;
1810                 let start_line = line_index.index.line_col(frange.range.start()).line;
1811                 let end_line = line_index.index.line_col(frange.range.end()).line;
1812
1813                 cmd.arg("--unstable-features");
1814                 cmd.arg("--file-lines");
1815                 cmd.arg(
1816                     json!([{
1817                         "file": "stdin",
1818                         "range": [start_line, end_line]
1819                     }])
1820                     .to_string(),
1821                 );
1822             }
1823
1824             cmd
1825         }
1826         RustfmtConfig::CustomCommand { command, args } => {
1827             let mut cmd = process::Command::new(command);
1828             cmd.args(args);
1829             cmd
1830         }
1831     };
1832
1833     let mut rustfmt = rustfmt
1834         .stdin(Stdio::piped())
1835         .stdout(Stdio::piped())
1836         .stderr(Stdio::piped())
1837         .spawn()
1838         .context(format!("Failed to spawn {:?}", rustfmt))?;
1839
1840     rustfmt.stdin.as_mut().unwrap().write_all(file.as_bytes())?;
1841
1842     let output = rustfmt.wait_with_output()?;
1843     let captured_stdout = String::from_utf8(output.stdout)?;
1844     let captured_stderr = String::from_utf8(output.stderr).unwrap_or_default();
1845
1846     if !output.status.success() {
1847         let rustfmt_not_installed =
1848             captured_stderr.contains("not installed") || captured_stderr.contains("not available");
1849
1850         return match output.status.code() {
1851             Some(1) if !rustfmt_not_installed => {
1852                 // While `rustfmt` doesn't have a specific exit code for parse errors this is the
1853                 // likely cause exiting with 1. Most Language Servers swallow parse errors on
1854                 // formatting because otherwise an error is surfaced to the user on top of the
1855                 // syntax error diagnostics they're already receiving. This is especially jarring
1856                 // if they have format on save enabled.
1857                 tracing::info!("rustfmt exited with status 1, assuming parse error and ignoring");
1858                 Ok(None)
1859             }
1860             _ => {
1861                 // Something else happened - e.g. `rustfmt` is missing or caught a signal
1862                 Err(LspError::new(
1863                     -32900,
1864                     format!(
1865                         r#"rustfmt exited with:
1866                            Status: {}
1867                            stdout: {}
1868                            stderr: {}"#,
1869                         output.status, captured_stdout, captured_stderr,
1870                     ),
1871                 )
1872                 .into())
1873             }
1874         };
1875     }
1876
1877     let (new_text, new_line_endings) = LineEndings::normalize(captured_stdout);
1878
1879     if line_index.endings != new_line_endings {
1880         // If line endings are different, send the entire file.
1881         // Diffing would not work here, as the line endings might be the only
1882         // difference.
1883         Ok(Some(to_proto::text_edit_vec(
1884             &line_index,
1885             TextEdit::replace(TextRange::up_to(TextSize::of(&*file)), new_text),
1886         )))
1887     } else if *file == new_text {
1888         // The document is already formatted correctly -- no edits needed.
1889         Ok(None)
1890     } else {
1891         Ok(Some(to_proto::text_edit_vec(&line_index, diff(&file, &new_text))))
1892     }
1893 }