1use std::{fs, io::Write as _, ops::Not, process::Stdio};
5
6use anyhow::Context;
7
8use base64::{Engine, prelude::BASE64_STANDARD};
9use ide::{
10 AssistKind, AssistResolveStrategy, Cancellable, CompletionFieldsToResolve,
11 CompletionItemImport, FilePosition, FileRange, FileStructureConfig, FindAllRefsConfig,
12 HoverAction, HoverGotoTypeData, InlayFieldsToResolve, Query, RangeInfo, Runnable, RunnableKind,
13 SingleResolve, SourceChange, TextEdit,
14};
15use ide_db::{FxHashMap, SymbolKind};
16use itertools::Itertools;
17use lsp_server::ErrorCode;
18use lsp_types::{
19 CallHierarchyIncomingCall, CallHierarchyIncomingCallsParams, CallHierarchyItem,
20 CallHierarchyOutgoingCall, CallHierarchyOutgoingCallsParams, CallHierarchyPrepareParams,
21 CodeLens, CompletionItem, FoldingRange, FoldingRangeParams, HoverContents, InlayHint,
22 InlayHintParams, Location, LocationLink, Position, PrepareRenameResponse, Range, RenameParams,
23 ResourceOp, ResourceOperationKind, SemanticTokensDeltaParams, SemanticTokensFullDeltaResult,
24 SemanticTokensParams, SemanticTokensRangeParams, SemanticTokensRangeResult,
25 SemanticTokensResult, SymbolInformation, SymbolTag, TextDocumentIdentifier, Url, WorkspaceEdit,
26};
27use paths::Utf8PathBuf;
28use project_model::{CargoWorkspace, ManifestPath, ProjectWorkspaceKind, TargetKind};
29use serde_json::json;
30use stdx::{format_to, never};
31use syntax::{TextRange, TextSize};
32use triomphe::Arc;
33use vfs::{AbsPath, AbsPathBuf, FileId, VfsPath};
34
35use crate::{
36 config::{
37 ClientCommandsConfig, Config, HoverActionsConfig, RustfmtConfig, WorkspaceSymbolConfig,
38 },
39 diagnostics::convert_diagnostic,
40 global_state::{FetchWorkspaceRequest, GlobalState, GlobalStateSnapshot},
41 line_index::LineEndings,
42 lsp::{
43 LspError, completion_item_hash,
44 ext::{
45 GetFailedObligationsParams, InternalTestingFetchConfigOption,
46 InternalTestingFetchConfigParams, InternalTestingFetchConfigResponse,
47 },
48 from_proto, to_proto,
49 utils::{all_edits_are_disjoint, invalid_params_error},
50 },
51 lsp_ext::{
52 self, CrateInfoResult, ExternalDocsPair, ExternalDocsResponse, FetchDependencyListParams,
53 FetchDependencyListResult, PositionOrRange, ViewCrateGraphParams, WorkspaceSymbolParams,
54 },
55 target_spec::{CargoTargetSpec, TargetSpec},
56 test_runner::{CargoTestHandle, TestTarget},
57 try_default,
58};
59
60pub(crate) fn handle_workspace_reload(state: &mut GlobalState, _: ()) -> anyhow::Result<()> {
61 state.proc_macro_clients = Arc::from_iter([]);
62 state.build_deps_changed = false;
63
64 let req = FetchWorkspaceRequest { path: None, force_crate_graph_reload: false };
65 state.fetch_workspaces_queue.request_op("reload workspace request".to_owned(), req);
66 Ok(())
67}
68
69pub(crate) fn handle_proc_macros_rebuild(state: &mut GlobalState, _: ()) -> anyhow::Result<()> {
70 state.proc_macro_clients = Arc::from_iter([]);
71 state.build_deps_changed = false;
72
73 state.fetch_build_data_queue.request_op("rebuild proc macros request".to_owned(), ());
74 Ok(())
75}
76
77pub(crate) fn handle_analyzer_status(
78 snap: GlobalStateSnapshot,
79 params: lsp_ext::AnalyzerStatusParams,
80) -> anyhow::Result<String> {
81 let _p = tracing::info_span!("handle_analyzer_status").entered();
82
83 let mut buf = String::new();
84
85 let mut file_id = None;
86 if let Some(tdi) = params.text_document {
87 match from_proto::file_id(&snap, &tdi.uri) {
88 Ok(Some(it)) => file_id = Some(it),
89 Ok(None) => {}
90 Err(_) => format_to!(buf, "file {} not found in vfs", tdi.uri),
91 }
92 }
93
94 if snap.workspaces.is_empty() {
95 buf.push_str("No workspaces\n")
96 } else {
97 buf.push_str("Workspaces:\n");
98 format_to!(
99 buf,
100 "Loaded {:?} packages across {} workspace{}.\n",
101 snap.workspaces.iter().map(|w| w.n_packages()).sum::<usize>(),
102 snap.workspaces.len(),
103 if snap.workspaces.len() == 1 { "" } else { "s" }
104 );
105
106 format_to!(
107 buf,
108 "Workspace root folders: {:?}",
109 snap.workspaces.iter().map(|ws| ws.manifest_or_root()).collect::<Vec<&AbsPath>>()
110 );
111 }
112 buf.push_str("\nAnalysis:\n");
113 buf.push_str(
114 &snap
115 .analysis
116 .status(file_id)
117 .unwrap_or_else(|_| "Analysis retrieval was cancelled".to_owned()),
118 );
119
120 buf.push_str("\nVersion: \n");
121 format_to!(buf, "{}", crate::version());
122
123 buf.push_str("\nConfiguration: \n");
124 format_to!(buf, "{:#?}", snap.config);
125
126 Ok(buf)
127}
128
129pub(crate) fn handle_memory_usage(_state: &mut GlobalState, _: ()) -> anyhow::Result<String> {
130 let _p = tracing::info_span!("handle_memory_usage").entered();
131
132 #[cfg(not(feature = "dhat"))]
133 {
134 Err(anyhow::anyhow!(
135 "Memory profiling is not enabled for this build of rust-analyzer.\n\n\
136 To build rust-analyzer with profiling support, pass `--features dhat --profile dev-rel` to `cargo build`
137 when building from source, or pass `--enable-profiling` to `cargo xtask`."
138 ))
139 }
140 #[cfg(feature = "dhat")]
141 {
142 if let Some(dhat_output_file) = _state.config.dhat_output_file() {
143 let mut profiler = crate::DHAT_PROFILER.lock().unwrap();
144 let old_profiler = profiler.take();
145 drop(old_profiler);
147 *profiler = Some(dhat::Profiler::builder().file_name(&dhat_output_file).build());
148 Ok(format!(
149 "Memory profile was saved successfully to {dhat_output_file}.\n\n\
150 See https://docs.rs/dhat/latest/dhat/#viewing for how to inspect the profile."
151 ))
152 } else {
153 Err(anyhow::anyhow!(
154 "Please set `rust-analyzer.profiling.memoryProfile` to the path where you want to save the profile."
155 ))
156 }
157 }
158}
159
160pub(crate) fn handle_view_syntax_tree(
161 snap: GlobalStateSnapshot,
162 params: lsp_ext::ViewSyntaxTreeParams,
163) -> anyhow::Result<String> {
164 let _p = tracing::info_span!("handle_view_syntax_tree").entered();
165 let id = try_default!(from_proto::file_id(&snap, ¶ms.text_document.uri)?);
166 let res = snap.analysis.view_syntax_tree(id)?;
167 Ok(res)
168}
169
170pub(crate) fn handle_view_hir(
171 snap: GlobalStateSnapshot,
172 params: lsp_types::TextDocumentPositionParams,
173) -> anyhow::Result<String> {
174 let _p = tracing::info_span!("handle_view_hir").entered();
175 let position = try_default!(from_proto::file_position(&snap, params)?);
176 let res = snap.analysis.view_hir(position)?;
177 Ok(res)
178}
179
180pub(crate) fn handle_view_mir(
181 snap: GlobalStateSnapshot,
182 params: lsp_types::TextDocumentPositionParams,
183) -> anyhow::Result<String> {
184 let _p = tracing::info_span!("handle_view_mir").entered();
185 let position = try_default!(from_proto::file_position(&snap, params)?);
186 let res = snap.analysis.view_mir(position)?;
187 Ok(res)
188}
189
190pub(crate) fn handle_interpret_function(
191 snap: GlobalStateSnapshot,
192 params: lsp_types::TextDocumentPositionParams,
193) -> anyhow::Result<String> {
194 let _p = tracing::info_span!("handle_interpret_function").entered();
195 let position = try_default!(from_proto::file_position(&snap, params)?);
196 let res = snap.analysis.interpret_function(position)?;
197 Ok(res)
198}
199
200pub(crate) fn handle_view_file_text(
201 snap: GlobalStateSnapshot,
202 params: lsp_types::TextDocumentIdentifier,
203) -> anyhow::Result<String> {
204 let file_id = try_default!(from_proto::file_id(&snap, ¶ms.uri)?);
205 Ok(snap.analysis.file_text(file_id)?.to_string())
206}
207
208pub(crate) fn handle_view_item_tree(
209 snap: GlobalStateSnapshot,
210 params: lsp_ext::ViewItemTreeParams,
211) -> anyhow::Result<String> {
212 let _p = tracing::info_span!("handle_view_item_tree").entered();
213 let file_id = try_default!(from_proto::file_id(&snap, ¶ms.text_document.uri)?);
214 let res = snap.analysis.view_item_tree(file_id)?;
215 Ok(res)
216}
217
218fn all_test_targets(cargo: &CargoWorkspace) -> impl Iterator<Item = TestTarget> {
229 cargo.packages().filter(|p| cargo[*p].is_member).flat_map(|p| {
230 let package = &cargo[p];
231 package.targets.iter().filter_map(|t| {
232 let target = &cargo[*t];
233 if target.kind == TargetKind::BuildScript {
234 None
235 } else {
236 Some(TestTarget {
237 package: package.name.clone(),
238 target: target.name.clone(),
239 kind: target.kind,
240 })
241 }
242 })
243 })
244}
245
246fn find_test_target(namespace_root: &str, cargo: &CargoWorkspace) -> Option<TestTarget> {
247 all_test_targets(cargo).find(|t| namespace_root == t.target.replace('-', "_"))
248}
249
250pub(crate) fn handle_run_test(
251 state: &mut GlobalState,
252 params: lsp_ext::RunTestParams,
253) -> anyhow::Result<()> {
254 if let Some(_session) = state.test_run_session.take() {
255 state.send_notification::<lsp_ext::EndRunTest>(());
256 }
257
258 let mut handles = vec![];
259 for ws in &*state.workspaces {
260 if let ProjectWorkspaceKind::Cargo { cargo, .. } = &ws.kind {
261 let tests = match params.include {
263 Some(ref include) => include
264 .iter()
265 .unique()
266 .filter_map(|test| {
267 let (root, remainder) = match test.split_once("::") {
268 Some((root, remainder)) => (root.to_owned(), Some(remainder)),
269 None => (test.clone(), None),
270 };
271 if let Some(target) = find_test_target(&root, cargo) {
272 Some((target, remainder))
273 } else {
274 tracing::error!("Test target not found for: {test}");
275 None
276 }
277 })
278 .collect_vec(),
279 None => all_test_targets(cargo).map(|target| (target, None)).collect(),
280 };
281
282 for (target, path) in tests {
283 let handle = CargoTestHandle::new(
284 path,
285 state.config.cargo_test_options(None),
286 cargo.workspace_root(),
287 Some(cargo.target_directory().as_ref()),
288 target,
289 state.test_run_sender.clone(),
290 )?;
291 handles.push(handle);
292 }
293 }
294 }
295 state.test_run_remaining_jobs = 2 * handles.len();
297 state.test_run_session = Some(handles);
298 Ok(())
299}
300
301pub(crate) fn handle_discover_test(
302 snap: GlobalStateSnapshot,
303 params: lsp_ext::DiscoverTestParams,
304) -> anyhow::Result<lsp_ext::DiscoverTestResults> {
305 let _p = tracing::info_span!("handle_discover_test").entered();
306 let (tests, scope) = match params.test_id {
307 Some(id) => {
308 let crate_id = id.split_once("::").map(|it| it.0).unwrap_or(&id);
309 (
310 snap.analysis.discover_tests_in_crate_by_test_id(crate_id)?,
311 Some(vec![crate_id.to_owned()]),
312 )
313 }
314 None => (snap.analysis.discover_test_roots()?, None),
315 };
316
317 Ok(lsp_ext::DiscoverTestResults {
318 tests: tests
319 .into_iter()
320 .filter_map(|t| {
321 let line_index = t.file.and_then(|f| snap.file_line_index(f).ok());
322 to_proto::test_item(&snap, t, line_index.as_ref())
323 })
324 .collect(),
325 scope,
326 scope_file: None,
327 })
328}
329
330pub(crate) fn handle_view_crate_graph(
331 snap: GlobalStateSnapshot,
332 params: ViewCrateGraphParams,
333) -> anyhow::Result<String> {
334 let _p = tracing::info_span!("handle_view_crate_graph").entered();
335 let dot = snap.analysis.view_crate_graph(params.full)?;
336 Ok(dot)
337}
338
339pub(crate) fn handle_expand_macro(
340 snap: GlobalStateSnapshot,
341 params: lsp_ext::ExpandMacroParams,
342) -> anyhow::Result<Option<lsp_ext::ExpandedMacro>> {
343 let _p = tracing::info_span!("handle_expand_macro").entered();
344 let file_id = try_default!(from_proto::file_id(&snap, ¶ms.text_document.uri)?);
345 let line_index = snap.file_line_index(file_id)?;
346 let offset = from_proto::offset(&line_index, params.position)?;
347
348 let res = snap.analysis.expand_macro(FilePosition { file_id, offset })?;
349 Ok(res.map(|it| lsp_ext::ExpandedMacro { name: it.name, expansion: it.expansion }))
350}
351
352pub(crate) fn handle_selection_range(
353 snap: GlobalStateSnapshot,
354 params: lsp_types::SelectionRangeParams,
355) -> anyhow::Result<Option<Vec<lsp_types::SelectionRange>>> {
356 let _p = tracing::info_span!("handle_selection_range").entered();
357 let file_id = try_default!(from_proto::file_id(&snap, ¶ms.text_document.uri)?);
358 let line_index = snap.file_line_index(file_id)?;
359 let res: anyhow::Result<Vec<lsp_types::SelectionRange>> = params
360 .positions
361 .into_iter()
362 .map(|position| {
363 let offset = from_proto::offset(&line_index, position)?;
364 let mut ranges = Vec::new();
365 {
366 let mut range = TextRange::new(offset, offset);
367 loop {
368 ranges.push(range);
369 let frange = FileRange { file_id, range };
370 let next = snap.analysis.extend_selection(frange)?;
371 if next == range {
372 break;
373 } else {
374 range = next
375 }
376 }
377 }
378 let mut range = lsp_types::SelectionRange {
379 range: to_proto::range(&line_index, *ranges.last().unwrap()),
380 parent: None,
381 };
382 for &r in ranges.iter().rev().skip(1) {
383 range = lsp_types::SelectionRange {
384 range: to_proto::range(&line_index, r),
385 parent: Some(Box::new(range)),
386 }
387 }
388 Ok(range)
389 })
390 .collect();
391
392 Ok(Some(res?))
393}
394
395pub(crate) fn handle_matching_brace(
396 snap: GlobalStateSnapshot,
397 params: lsp_ext::MatchingBraceParams,
398) -> anyhow::Result<Vec<Position>> {
399 let _p = tracing::info_span!("handle_matching_brace").entered();
400 let file_id = try_default!(from_proto::file_id(&snap, ¶ms.text_document.uri)?);
401 let line_index = snap.file_line_index(file_id)?;
402 params
403 .positions
404 .into_iter()
405 .map(|position| {
406 let offset = from_proto::offset(&line_index, position);
407 offset.map(|offset| {
408 let offset = match snap.analysis.matching_brace(FilePosition { file_id, offset }) {
409 Ok(Some(matching_brace_offset)) => matching_brace_offset,
410 Err(_) | Ok(None) => offset,
411 };
412 to_proto::position(&line_index, offset)
413 })
414 })
415 .collect()
416}
417
418pub(crate) fn handle_join_lines(
419 snap: GlobalStateSnapshot,
420 params: lsp_ext::JoinLinesParams,
421) -> anyhow::Result<Vec<lsp_types::TextEdit>> {
422 let _p = tracing::info_span!("handle_join_lines").entered();
423
424 let file_id = try_default!(from_proto::file_id(&snap, ¶ms.text_document.uri)?);
425 let config = snap.config.join_lines();
426 let line_index = snap.file_line_index(file_id)?;
427
428 let mut res = TextEdit::default();
429 for range in params.ranges {
430 let range = from_proto::text_range(&line_index, range)?;
431 let edit = snap.analysis.join_lines(&config, FileRange { file_id, range })?;
432 match res.union(edit) {
433 Ok(()) => (),
434 Err(_edit) => {
435 }
437 }
438 }
439
440 Ok(to_proto::text_edit_vec(&line_index, res))
441}
442
443pub(crate) fn handle_on_enter(
444 snap: GlobalStateSnapshot,
445 params: lsp_types::TextDocumentPositionParams,
446) -> anyhow::Result<Option<Vec<lsp_ext::SnippetTextEdit>>> {
447 let _p = tracing::info_span!("handle_on_enter").entered();
448 let position = try_default!(from_proto::file_position(&snap, params)?);
449 let edit = match snap.analysis.on_enter(position)? {
450 None => return Ok(None),
451 Some(it) => it,
452 };
453 let line_index = snap.file_line_index(position.file_id)?;
454 let edit = to_proto::snippet_text_edit_vec(
455 &line_index,
456 true,
457 edit,
458 snap.config.change_annotation_support(),
459 );
460 Ok(Some(edit))
461}
462
463pub(crate) fn handle_on_type_formatting(
464 snap: GlobalStateSnapshot,
465 params: lsp_types::DocumentOnTypeFormattingParams,
466) -> anyhow::Result<Option<Vec<lsp_ext::SnippetTextEdit>>> {
467 let _p = tracing::info_span!("handle_on_type_formatting").entered();
468 let char_typed = params.ch.chars().next().unwrap_or('\0');
469 if !snap.config.typing_trigger_chars().contains(char_typed) {
470 return Ok(None);
471 }
472
473 let mut position =
474 try_default!(from_proto::file_position(&snap, params.text_document_position)?);
475 let line_index = snap.file_line_index(position.file_id)?;
476
477 position.offset -= TextSize::of('.');
480
481 let text = snap.analysis.file_text(position.file_id)?;
482 if stdx::never!(!text[usize::from(position.offset)..].starts_with(char_typed)) {
483 return Ok(None);
484 }
485
486 let edit = snap.analysis.on_char_typed(position, char_typed)?;
487 let edit = match edit {
488 Some(it) => it,
489 None => return Ok(None),
490 };
491
492 let (_, (text_edit, snippet_edit)) = edit.source_file_edits.into_iter().next().unwrap();
494 stdx::always!(snippet_edit.is_none(), "on type formatting shouldn't use structured snippets");
495
496 let change = to_proto::snippet_text_edit_vec(
497 &line_index,
498 edit.is_snippet,
499 text_edit,
500 snap.config.change_annotation_support(),
501 );
502 Ok(Some(change))
503}
504
505pub(crate) fn empty_diagnostic_report() -> lsp_types::DocumentDiagnosticReportResult {
506 lsp_types::DocumentDiagnosticReportResult::Report(lsp_types::DocumentDiagnosticReport::Full(
507 lsp_types::RelatedFullDocumentDiagnosticReport {
508 related_documents: None,
509 full_document_diagnostic_report: lsp_types::FullDocumentDiagnosticReport {
510 result_id: Some("rust-analyzer".to_owned()),
511 items: vec![],
512 },
513 },
514 ))
515}
516
517pub(crate) fn handle_document_diagnostics(
518 snap: GlobalStateSnapshot,
519 params: lsp_types::DocumentDiagnosticParams,
520) -> anyhow::Result<lsp_types::DocumentDiagnosticReportResult> {
521 let file_id = match from_proto::file_id(&snap, ¶ms.text_document.uri)? {
522 Some(it) => it,
523 None => return Ok(empty_diagnostic_report()),
524 };
525 let source_root = snap.analysis.source_root_id(file_id)?;
526 if !snap.analysis.is_local_source_root(source_root)? {
527 return Ok(empty_diagnostic_report());
528 }
529 let source_root = snap.analysis.source_root_id(file_id)?;
530 let config = snap.config.diagnostics(Some(source_root));
531 if !config.enabled {
532 return Ok(empty_diagnostic_report());
533 }
534 let line_index = snap.file_line_index(file_id)?;
535 let supports_related = snap.config.text_document_diagnostic_related_document_support();
536
537 let mut related_documents = FxHashMap::default();
538 let diagnostics = snap
539 .analysis
540 .full_diagnostics(&config, AssistResolveStrategy::None, file_id)?
541 .into_iter()
542 .filter_map(|d| {
543 let file = d.range.file_id;
544 if file == file_id {
545 let diagnostic = convert_diagnostic(&line_index, d);
546 return Some(diagnostic);
547 }
548 if supports_related {
549 let (diagnostics, line_index) = related_documents
550 .entry(file)
551 .or_insert_with(|| (Vec::new(), snap.file_line_index(file).ok()));
552 let diagnostic = convert_diagnostic(line_index.as_mut()?, d);
553 diagnostics.push(diagnostic);
554 }
555 None
556 });
557 Ok(lsp_types::DocumentDiagnosticReportResult::Report(
558 lsp_types::DocumentDiagnosticReport::Full(lsp_types::RelatedFullDocumentDiagnosticReport {
559 full_document_diagnostic_report: lsp_types::FullDocumentDiagnosticReport {
560 result_id: Some("rust-analyzer".to_owned()),
561 items: diagnostics.collect(),
562 },
563 related_documents: related_documents.is_empty().not().then(|| {
564 related_documents
565 .into_iter()
566 .map(|(id, (items, _))| {
567 (
568 to_proto::url(&snap, id),
569 lsp_types::DocumentDiagnosticReportKind::Full(
570 lsp_types::FullDocumentDiagnosticReport {
571 result_id: Some("rust-analyzer".to_owned()),
572 items,
573 },
574 ),
575 )
576 })
577 .collect()
578 }),
579 }),
580 ))
581}
582
583pub(crate) fn handle_document_symbol(
584 snap: GlobalStateSnapshot,
585 params: lsp_types::DocumentSymbolParams,
586) -> anyhow::Result<Option<lsp_types::DocumentSymbolResponse>> {
587 let _p = tracing::info_span!("handle_document_symbol").entered();
588 let file_id = try_default!(from_proto::file_id(&snap, ¶ms.text_document.uri)?);
589 let line_index = snap.file_line_index(file_id)?;
590
591 let mut symbols: Vec<(lsp_types::DocumentSymbol, Option<usize>)> = Vec::new();
592
593 let config = snap.config.document_symbol(None);
594
595 let structure_nodes = snap.analysis.file_structure(
596 &FileStructureConfig { exclude_locals: config.search_exclude_locals },
597 file_id,
598 )?;
599
600 for node in structure_nodes {
601 let mut tags = Vec::new();
602 if node.deprecated {
603 tags.push(SymbolTag::DEPRECATED)
604 };
605
606 #[allow(deprecated)]
607 let symbol = lsp_types::DocumentSymbol {
608 name: node.label,
609 detail: node.detail,
610 kind: to_proto::structure_node_kind(node.kind),
611 tags: Some(tags),
612 deprecated: Some(node.deprecated),
613 range: to_proto::range(&line_index, node.node_range),
614 selection_range: to_proto::range(&line_index, node.navigation_range),
615 children: None,
616 };
617 symbols.push((symbol, node.parent));
618 }
619
620 let document_symbols = {
622 let mut acc = Vec::new();
623 while let Some((mut symbol, parent_idx)) = symbols.pop() {
624 if let Some(children) = &mut symbol.children {
625 children.reverse();
626 }
627 let parent = match parent_idx {
628 None => &mut acc,
629 Some(i) => symbols[i].0.children.get_or_insert_with(Vec::new),
630 };
631 parent.push(symbol);
632 }
633 acc.reverse();
634 acc
635 };
636
637 let res = if snap.config.hierarchical_symbols() {
638 document_symbols.into()
639 } else {
640 let url = to_proto::url(&snap, file_id);
641 let mut symbol_information = Vec::new();
642 for symbol in document_symbols {
643 flatten_document_symbol(&symbol, None, &url, &mut symbol_information);
644 }
645 symbol_information.into()
646 };
647 return Ok(Some(res));
648
649 fn flatten_document_symbol(
650 symbol: &lsp_types::DocumentSymbol,
651 container_name: Option<String>,
652 url: &Url,
653 res: &mut Vec<SymbolInformation>,
654 ) {
655 #[allow(deprecated)]
656 res.push(SymbolInformation {
657 name: symbol.name.clone(),
658 kind: symbol.kind,
659 tags: symbol.tags.clone(),
660 deprecated: symbol.deprecated,
661 location: Location::new(url.clone(), symbol.range),
662 container_name,
663 });
664
665 for child in symbol.children.iter().flatten() {
666 flatten_document_symbol(child, Some(symbol.name.clone()), url, res);
667 }
668 }
669}
670
671pub(crate) fn handle_workspace_symbol(
672 snap: GlobalStateSnapshot,
673 params: WorkspaceSymbolParams,
674) -> anyhow::Result<Option<lsp_types::WorkspaceSymbolResponse>> {
675 let _p = tracing::info_span!("handle_workspace_symbol").entered();
676
677 let config = snap.config.workspace_symbol(None);
678 let (all_symbols, libs) = decide_search_kind_and_scope(¶ms, &config);
679
680 let query = {
681 let query: String = params.query.chars().filter(|&c| c != '#' && c != '*').collect();
682 let mut q = Query::new(query);
683 if !all_symbols {
684 q.only_types();
685 }
686 if libs {
687 q.libs();
688 }
689 if config.search_exclude_imports {
690 q.exclude_imports();
691 }
692 q
693 };
694 let mut res = exec_query(&snap, query, config.search_limit)?;
695 if res.is_empty() && !all_symbols {
696 res = exec_query(&snap, Query::new(params.query), config.search_limit)?;
697 }
698
699 return Ok(Some(lsp_types::WorkspaceSymbolResponse::Nested(res)));
700
701 fn decide_search_kind_and_scope(
702 params: &WorkspaceSymbolParams,
703 config: &WorkspaceSymbolConfig,
704 ) -> (bool, bool) {
705 let mut all_symbols = params.query.contains('#');
707 let mut libs = params.query.contains('*');
708
709 if !all_symbols {
712 let search_kind = match params.search_kind {
713 Some(ref search_kind) => search_kind,
714 None => &config.search_kind,
715 };
716 all_symbols = match search_kind {
717 lsp_ext::WorkspaceSymbolSearchKind::OnlyTypes => false,
718 lsp_ext::WorkspaceSymbolSearchKind::AllSymbols => true,
719 }
720 }
721
722 if !libs {
723 let search_scope = match params.search_scope {
724 Some(ref search_scope) => search_scope,
725 None => &config.search_scope,
726 };
727 libs = match search_scope {
728 lsp_ext::WorkspaceSymbolSearchScope::Workspace => false,
729 lsp_ext::WorkspaceSymbolSearchScope::WorkspaceAndDependencies => true,
730 }
731 }
732
733 (all_symbols, libs)
734 }
735
736 fn exec_query(
737 snap: &GlobalStateSnapshot,
738 query: Query,
739 limit: usize,
740 ) -> anyhow::Result<Vec<lsp_types::WorkspaceSymbol>> {
741 let mut res = Vec::new();
742 for nav in snap.analysis.symbol_search(query, limit)? {
743 let container_name = nav.container_name.as_ref().map(|v| v.to_string());
744
745 let info = lsp_types::WorkspaceSymbol {
746 name: match &nav.alias {
747 Some(alias) => format!("{} (alias for {})", alias, nav.name),
748 None => format!("{}", nav.name),
749 },
750 kind: nav
751 .kind
752 .map(to_proto::symbol_kind)
753 .unwrap_or(lsp_types::SymbolKind::VARIABLE),
754 tags: None,
756 container_name,
757 location: lsp_types::OneOf::Left(to_proto::location_from_nav(snap, nav)?),
758 data: None,
759 };
760 res.push(info);
761 }
762 Ok(res)
763 }
764}
765
766pub(crate) fn handle_will_rename_files(
767 snap: GlobalStateSnapshot,
768 params: lsp_types::RenameFilesParams,
769) -> anyhow::Result<Option<lsp_types::WorkspaceEdit>> {
770 let _p = tracing::info_span!("handle_will_rename_files").entered();
771
772 let source_changes: Vec<SourceChange> = params
773 .files
774 .into_iter()
775 .filter_map(|file_rename| {
776 let from = Url::parse(&file_rename.old_uri).ok()?;
777 let to = Url::parse(&file_rename.new_uri).ok()?;
778
779 let from_path = from.to_file_path().ok()?;
780 let to_path = to.to_file_path().ok()?;
781
782 match (from_path.parent(), to_path.parent()) {
784 (Some(p1), Some(p2)) if p1 == p2 => {
785 if from_path.is_dir() {
786 let mut old_folder_name = from_path.file_stem()?.to_str()?.to_owned();
788 old_folder_name.push('/');
789 let from_with_trailing_slash = from.join(&old_folder_name).ok()?;
790
791 let imitate_from_url = from_with_trailing_slash.join("mod.rs").ok()?;
792 let new_file_name = to_path.file_name()?.to_str()?;
793 Some((
794 snap.url_to_file_id(&imitate_from_url).ok()?,
795 new_file_name.to_owned(),
796 ))
797 } else {
798 let old_name = from_path.file_stem()?.to_str()?;
799 let new_name = to_path.file_stem()?.to_str()?;
800 match (old_name, new_name) {
801 ("mod", _) => None,
802 (_, "mod") => None,
803 _ => Some((snap.url_to_file_id(&from).ok()?, new_name.to_owned())),
804 }
805 }
806 }
807 _ => None,
808 }
809 })
810 .filter_map(|(file_id, new_name)| {
811 let file_id = file_id?;
812 let source_root = snap.analysis.source_root_id(file_id).ok();
813 snap.analysis
814 .will_rename_file(file_id, &new_name, &snap.config.rename(source_root))
815 .ok()?
816 })
817 .collect();
818
819 let mut source_changes = source_changes.into_iter();
821 let mut source_change = source_changes.next().unwrap_or_default();
822 source_change.file_system_edits.clear();
823 source_change.extend(source_changes.flat_map(|it| it.source_file_edits));
825 if source_change.source_file_edits.is_empty() {
826 Ok(None)
827 } else {
828 Ok(Some(to_proto::workspace_edit(&snap, source_change)?))
829 }
830}
831
832pub(crate) fn handle_goto_definition(
833 snap: GlobalStateSnapshot,
834 params: lsp_types::GotoDefinitionParams,
835) -> anyhow::Result<Option<lsp_types::GotoDefinitionResponse>> {
836 let _p = tracing::info_span!("handle_goto_definition").entered();
837 let position =
838 try_default!(from_proto::file_position(&snap, params.text_document_position_params)?);
839 let config = snap.config.goto_definition(snap.minicore());
840 let nav_info = match snap.analysis.goto_definition(position, &config)? {
841 None => return Ok(None),
842 Some(it) => it,
843 };
844 let src = FileRange { file_id: position.file_id, range: nav_info.range };
845 let res = to_proto::goto_definition_response(&snap, Some(src), nav_info.info)?;
846 Ok(Some(res))
847}
848
849pub(crate) fn handle_goto_declaration(
850 snap: GlobalStateSnapshot,
851 params: lsp_types::request::GotoDeclarationParams,
852) -> anyhow::Result<Option<lsp_types::request::GotoDeclarationResponse>> {
853 let _p = tracing::info_span!("handle_goto_declaration").entered();
854 let position = try_default!(from_proto::file_position(
855 &snap,
856 params.text_document_position_params.clone()
857 )?);
858 let config = snap.config.goto_definition(snap.minicore());
859 let nav_info = match snap.analysis.goto_declaration(position, &config)? {
860 None => return handle_goto_definition(snap, params),
861 Some(it) => it,
862 };
863 let src = FileRange { file_id: position.file_id, range: nav_info.range };
864 let res = to_proto::goto_definition_response(&snap, Some(src), nav_info.info)?;
865 Ok(Some(res))
866}
867
868pub(crate) fn handle_goto_implementation(
869 snap: GlobalStateSnapshot,
870 params: lsp_types::request::GotoImplementationParams,
871) -> anyhow::Result<Option<lsp_types::request::GotoImplementationResponse>> {
872 let _p = tracing::info_span!("handle_goto_implementation").entered();
873 let position =
874 try_default!(from_proto::file_position(&snap, params.text_document_position_params)?);
875 let nav_info =
876 match snap.analysis.goto_implementation(&snap.config.goto_implementation(), position)? {
877 None => return Ok(None),
878 Some(it) => it,
879 };
880 let src = FileRange { file_id: position.file_id, range: nav_info.range };
881 let res = to_proto::goto_definition_response(&snap, Some(src), nav_info.info)?;
882 Ok(Some(res))
883}
884
885pub(crate) fn handle_goto_type_definition(
886 snap: GlobalStateSnapshot,
887 params: lsp_types::request::GotoTypeDefinitionParams,
888) -> anyhow::Result<Option<lsp_types::request::GotoTypeDefinitionResponse>> {
889 let _p = tracing::info_span!("handle_goto_type_definition").entered();
890 let position =
891 try_default!(from_proto::file_position(&snap, params.text_document_position_params)?);
892 let nav_info = match snap.analysis.goto_type_definition(position)? {
893 None => return Ok(None),
894 Some(it) => it,
895 };
896 let src = FileRange { file_id: position.file_id, range: nav_info.range };
897 let res = to_proto::goto_definition_response(&snap, Some(src), nav_info.info)?;
898 Ok(Some(res))
899}
900
901pub(crate) fn handle_parent_module(
902 snap: GlobalStateSnapshot,
903 params: lsp_types::TextDocumentPositionParams,
904) -> anyhow::Result<Option<lsp_types::GotoDefinitionResponse>> {
905 let _p = tracing::info_span!("handle_parent_module").entered();
906 if let Ok(file_path) = ¶ms.text_document.uri.to_file_path() {
907 if file_path.file_name().unwrap_or_default() == "Cargo.toml" {
908 let abs_path_buf = match Utf8PathBuf::from_path_buf(file_path.to_path_buf())
910 .ok()
911 .map(AbsPathBuf::try_from)
912 {
913 Some(Ok(abs_path_buf)) => abs_path_buf,
914 _ => return Ok(None),
915 };
916
917 let manifest_path = match ManifestPath::try_from(abs_path_buf).ok() {
918 Some(manifest_path) => manifest_path,
919 None => return Ok(None),
920 };
921
922 let links: Vec<LocationLink> = snap
923 .workspaces
924 .iter()
925 .filter_map(|ws| match &ws.kind {
926 ProjectWorkspaceKind::Cargo { cargo, .. }
927 | ProjectWorkspaceKind::DetachedFile { cargo: Some((cargo, _, _)), .. } => {
928 cargo.parent_manifests(&manifest_path)
929 }
930 _ => None,
931 })
932 .flatten()
933 .map(|parent_manifest_path| LocationLink {
934 origin_selection_range: None,
935 target_uri: to_proto::url_from_abs_path(&parent_manifest_path),
936 target_range: Range::default(),
937 target_selection_range: Range::default(),
938 })
939 .collect::<_>();
940 return Ok(Some(links.into()));
941 }
942
943 let file_id = try_default!(from_proto::file_id(&snap, ¶ms.text_document.uri)?);
945 let crate_id = match snap.analysis.crates_for(file_id)?.first() {
946 Some(&crate_id) => crate_id,
947 None => return Ok(None),
948 };
949 let cargo_spec = match TargetSpec::for_file(&snap, file_id)? {
950 Some(TargetSpec::Cargo(it)) => it,
951 Some(TargetSpec::ProjectJson(_)) | None => return Ok(None),
952 };
953
954 if snap.analysis.crate_root(crate_id)? == file_id {
955 let cargo_toml_url = to_proto::url_from_abs_path(&cargo_spec.cargo_toml);
956 let res = vec![LocationLink {
957 origin_selection_range: None,
958 target_uri: cargo_toml_url,
959 target_range: Range::default(),
960 target_selection_range: Range::default(),
961 }]
962 .into();
963 return Ok(Some(res));
964 }
965 }
966
967 let position = try_default!(from_proto::file_position(&snap, params)?);
969 let navs = snap.analysis.parent_module(position)?;
970 let res = to_proto::goto_definition_response(&snap, None, navs)?;
971 Ok(Some(res))
972}
973
974pub(crate) fn handle_child_modules(
975 snap: GlobalStateSnapshot,
976 params: lsp_types::TextDocumentPositionParams,
977) -> anyhow::Result<Option<lsp_types::GotoDefinitionResponse>> {
978 let _p = tracing::info_span!("handle_child_modules").entered();
979 let position = try_default!(from_proto::file_position(&snap, params)?);
981 let navs = snap.analysis.child_modules(position)?;
982 let res = to_proto::goto_definition_response(&snap, None, navs)?;
983 Ok(Some(res))
984}
985
986pub(crate) fn handle_runnables(
987 snap: GlobalStateSnapshot,
988 params: lsp_ext::RunnablesParams,
989) -> anyhow::Result<Vec<lsp_ext::Runnable>> {
990 let _p = tracing::info_span!("handle_runnables").entered();
991 let file_id = try_default!(from_proto::file_id(&snap, ¶ms.text_document.uri)?);
992 let source_root = snap.analysis.source_root_id(file_id).ok();
993 let line_index = snap.file_line_index(file_id)?;
994 let offset = params.position.and_then(|it| from_proto::offset(&line_index, it).ok());
995 let target_spec = TargetSpec::for_file(&snap, file_id)?;
996
997 let mut res = Vec::new();
998 for runnable in snap.analysis.runnables(file_id)? {
999 if should_skip_for_offset(&runnable, offset)
1000 || should_skip_target(&runnable, target_spec.as_ref())
1001 {
1002 continue;
1003 }
1004
1005 let update_test = runnable.update_test;
1006 if let Some(mut runnable) = to_proto::runnable(&snap, runnable)? {
1007 if let Some(runnable) = to_proto::make_update_runnable(&runnable, update_test) {
1008 res.push(runnable);
1009 }
1010
1011 if let lsp_ext::RunnableArgs::Cargo(r) = &mut runnable.args
1012 && let Some(TargetSpec::Cargo(CargoTargetSpec {
1013 sysroot_root: Some(sysroot_root),
1014 ..
1015 })) = &target_spec
1016 {
1017 r.environment.insert("RUSTC_TOOLCHAIN".to_owned(), sysroot_root.to_string());
1018 };
1019
1020 res.push(runnable);
1021 }
1022 }
1023
1024 let config = snap.config.runnables(source_root);
1026 match target_spec {
1027 Some(TargetSpec::Cargo(spec)) => {
1028 let is_crate_no_std = snap.analysis.is_crate_no_std(spec.crate_id)?;
1029 for cmd in ["check", "run", "test"] {
1030 if cmd == "run" && spec.target_kind != TargetKind::Bin {
1031 continue;
1032 }
1033 let cwd = if cmd != "test" || spec.target_kind == TargetKind::Bin {
1034 spec.workspace_root.clone()
1035 } else {
1036 spec.cargo_toml.parent().to_path_buf()
1037 };
1038 let mut cargo_args =
1039 vec![cmd.to_owned(), "--package".to_owned(), spec.package.clone()];
1040 let all_targets = cmd != "run" && !is_crate_no_std;
1041 if all_targets {
1042 cargo_args.push("--all-targets".to_owned());
1043 }
1044 cargo_args.extend(config.cargo_extra_args.iter().cloned());
1045 res.push(lsp_ext::Runnable {
1046 label: format!(
1047 "cargo {cmd} -p {}{all_targets}",
1048 spec.package,
1049 all_targets = if all_targets { " --all-targets" } else { "" }
1050 ),
1051 location: None,
1052 args: lsp_ext::RunnableArgs::Cargo(lsp_ext::CargoRunnableArgs {
1053 workspace_root: Some(spec.workspace_root.clone().into()),
1054 cwd: cwd.into(),
1055 override_cargo: config.override_cargo.clone(),
1056 cargo_args,
1057 executable_args: Vec::new(),
1058 environment: spec
1059 .sysroot_root
1060 .as_ref()
1061 .map(|root| ("RUSTC_TOOLCHAIN".to_owned(), root.to_string()))
1062 .into_iter()
1063 .collect(),
1064 }),
1065 })
1066 }
1067 }
1068 Some(TargetSpec::ProjectJson(_)) => {}
1069 None => {
1070 if !snap.config.linked_or_discovered_projects().is_empty()
1071 && let Some(path) = snap.file_id_to_file_path(file_id).parent()
1072 {
1073 let mut cargo_args = vec!["check".to_owned(), "--workspace".to_owned()];
1074 cargo_args.extend(config.cargo_extra_args.iter().cloned());
1075 res.push(lsp_ext::Runnable {
1076 label: "cargo check --workspace".to_owned(),
1077 location: None,
1078 args: lsp_ext::RunnableArgs::Cargo(lsp_ext::CargoRunnableArgs {
1079 workspace_root: None,
1080 cwd: path.as_path().unwrap().to_path_buf().into(),
1081 override_cargo: config.override_cargo,
1082 cargo_args,
1083 executable_args: Vec::new(),
1084 environment: Default::default(),
1085 }),
1086 });
1087 };
1088 }
1089 }
1090 Ok(res)
1091}
1092
1093fn should_skip_for_offset(runnable: &Runnable, offset: Option<TextSize>) -> bool {
1094 match offset {
1095 None => false,
1096 _ if matches!(&runnable.kind, RunnableKind::TestMod { .. }) => false,
1097 Some(offset) => !runnable.nav.full_range.contains_inclusive(offset),
1098 }
1099}
1100
1101pub(crate) fn handle_related_tests(
1102 snap: GlobalStateSnapshot,
1103 params: lsp_types::TextDocumentPositionParams,
1104) -> anyhow::Result<Vec<lsp_ext::TestInfo>> {
1105 let _p = tracing::info_span!("handle_related_tests").entered();
1106 let position = try_default!(from_proto::file_position(&snap, params)?);
1107
1108 let tests = snap.analysis.related_tests(position, None)?;
1109 let mut res = Vec::new();
1110 for it in tests {
1111 if let Ok(Some(runnable)) = to_proto::runnable(&snap, it) {
1112 res.push(lsp_ext::TestInfo { runnable })
1113 }
1114 }
1115
1116 Ok(res)
1117}
1118
1119pub(crate) fn handle_completion(
1120 snap: GlobalStateSnapshot,
1121 lsp_types::CompletionParams {
1122 text_document_position,
1123 context,
1124 ..
1125 }: lsp_types::CompletionParams,
1126) -> anyhow::Result<Option<lsp_types::CompletionResponse>> {
1127 let _p = tracing::info_span!("handle_completion").entered();
1128 let mut position =
1129 try_default!(from_proto::file_position(&snap, text_document_position.clone())?);
1130 let line_index = snap.file_line_index(position.file_id)?;
1131 let completion_trigger_character =
1132 context.and_then(|ctx| ctx.trigger_character).and_then(|s| s.chars().next());
1133
1134 let source_root = snap.analysis.source_root_id(position.file_id)?;
1135 let completion_config = &snap.config.completion(Some(source_root), snap.minicore());
1136 position.offset = position.offset.min(line_index.index.len());
1138 let items = match snap.analysis.completions(
1139 completion_config,
1140 position,
1141 completion_trigger_character,
1142 )? {
1143 None => return Ok(None),
1144 Some(items) => items,
1145 };
1146
1147 let items = to_proto::completion_items(
1148 &snap.config,
1149 &completion_config.fields_to_resolve,
1150 &line_index,
1151 snap.file_version(position.file_id),
1152 text_document_position,
1153 completion_trigger_character,
1154 items,
1155 );
1156
1157 let completion_list = lsp_types::CompletionList { is_incomplete: true, items };
1158 Ok(Some(completion_list.into()))
1159}
1160
1161pub(crate) fn handle_completion_resolve(
1162 snap: GlobalStateSnapshot,
1163 mut original_completion: CompletionItem,
1164) -> anyhow::Result<CompletionItem> {
1165 let _p = tracing::info_span!("handle_completion_resolve").entered();
1166
1167 if !all_edits_are_disjoint(&original_completion, &[]) {
1168 return Err(invalid_params_error(
1169 "Received a completion with overlapping edits, this is not LSP-compliant".to_owned(),
1170 )
1171 .into());
1172 }
1173
1174 let Some(data) = original_completion.data.take() else {
1175 return Ok(original_completion);
1176 };
1177
1178 let resolve_data: lsp_ext::CompletionResolveData = serde_json::from_value(data)?;
1179
1180 let file_id = from_proto::file_id(&snap, &resolve_data.position.text_document.uri)?
1181 .expect("we never provide completions for excluded files");
1182 let line_index = snap.file_line_index(file_id)?;
1183 let Ok(offset) = from_proto::offset(&line_index, resolve_data.position.position) else {
1185 return Ok(original_completion);
1186 };
1187 let source_root = snap.analysis.source_root_id(file_id)?;
1188
1189 let mut forced_resolve_completions_config =
1190 snap.config.completion(Some(source_root), snap.minicore());
1191 forced_resolve_completions_config.fields_to_resolve = CompletionFieldsToResolve::empty();
1192
1193 let position = FilePosition { file_id, offset };
1194 let Some(completions) = snap.analysis.completions(
1195 &forced_resolve_completions_config,
1196 position,
1197 resolve_data.trigger_character,
1198 )?
1199 else {
1200 return Ok(original_completion);
1201 };
1202 let Ok(resolve_data_hash) = BASE64_STANDARD.decode(resolve_data.hash) else {
1203 return Ok(original_completion);
1204 };
1205
1206 let Some(corresponding_completion) = completions.into_iter().find(|completion_item| {
1207 original_completion.label.starts_with(completion_item.label.primary.as_str())
1210 && resolve_data_hash == completion_item_hash(completion_item, resolve_data.for_ref)
1211 }) else {
1212 return Ok(original_completion);
1213 };
1214
1215 let mut resolved_completions = to_proto::completion_items(
1216 &snap.config,
1217 &forced_resolve_completions_config.fields_to_resolve,
1218 &line_index,
1219 snap.file_version(position.file_id),
1220 resolve_data.position,
1221 resolve_data.trigger_character,
1222 vec![corresponding_completion],
1223 );
1224 let Some(mut resolved_completion) = resolved_completions.pop() else {
1225 return Ok(original_completion);
1226 };
1227
1228 if !resolve_data.imports.is_empty() {
1229 let additional_edits = snap
1230 .analysis
1231 .resolve_completion_edits(
1232 &forced_resolve_completions_config,
1233 position,
1234 resolve_data.imports.into_iter().map(|import| CompletionItemImport {
1235 path: import.full_import_path,
1236 as_underscore: import.as_underscore,
1237 }),
1238 )?
1239 .into_iter()
1240 .flat_map(|edit| edit.into_iter().map(|indel| to_proto::text_edit(&line_index, indel)))
1241 .collect::<Vec<_>>();
1242
1243 if !all_edits_are_disjoint(&resolved_completion, &additional_edits) {
1244 return Err(LspError::new(
1245 ErrorCode::InternalError as i32,
1246 "Import edit overlaps with the original completion edits, this is not LSP-compliant"
1247 .into(),
1248 )
1249 .into());
1250 }
1251
1252 if let Some(original_additional_edits) = resolved_completion.additional_text_edits.as_mut()
1253 {
1254 original_additional_edits.extend(additional_edits)
1255 } else {
1256 resolved_completion.additional_text_edits = Some(additional_edits);
1257 }
1258 }
1259
1260 Ok(resolved_completion)
1261}
1262
1263pub(crate) fn handle_folding_range(
1264 snap: GlobalStateSnapshot,
1265 params: FoldingRangeParams,
1266) -> anyhow::Result<Option<Vec<FoldingRange>>> {
1267 let _p = tracing::info_span!("handle_folding_range").entered();
1268
1269 let file_id = try_default!(from_proto::file_id(&snap, ¶ms.text_document.uri)?);
1270 let collapsed_text = snap.config.folding_range_collapsed_text();
1271 let folds = snap.analysis.folding_ranges(file_id, collapsed_text)?;
1272
1273 let text = snap.analysis.file_text(file_id)?;
1274 let line_index = snap.file_line_index(file_id)?;
1275 let line_folding_only = snap.config.line_folding_only();
1276
1277 let res = folds
1278 .into_iter()
1279 .map(|it| to_proto::folding_range(&text, &line_index, line_folding_only, it))
1280 .collect();
1281 Ok(Some(res))
1282}
1283
1284pub(crate) fn handle_signature_help(
1285 snap: GlobalStateSnapshot,
1286 params: lsp_types::SignatureHelpParams,
1287) -> anyhow::Result<Option<lsp_types::SignatureHelp>> {
1288 let _p = tracing::info_span!("handle_signature_help").entered();
1289 let position =
1290 try_default!(from_proto::file_position(&snap, params.text_document_position_params)?);
1291 let help = match snap.analysis.signature_help(position)? {
1292 Some(it) => it,
1293 None => return Ok(None),
1294 };
1295 let config = snap.config.call_info();
1296 let res = to_proto::signature_help(help, config, snap.config.signature_help_label_offsets());
1297 Ok(Some(res))
1298}
1299
1300pub(crate) fn handle_hover(
1301 snap: GlobalStateSnapshot,
1302 params: lsp_ext::HoverParams,
1303) -> anyhow::Result<Option<lsp_ext::Hover>> {
1304 let _p = tracing::info_span!("handle_hover").entered();
1305 let range = match params.position {
1306 PositionOrRange::Position(position) => Range::new(position, position),
1307 PositionOrRange::Range(range) => range,
1308 };
1309 let file_range = try_default!(from_proto::file_range(&snap, ¶ms.text_document, range)?);
1310
1311 let hover = snap.config.hover(snap.minicore());
1312 let info = match snap.analysis.hover(&hover, file_range)? {
1313 None => return Ok(None),
1314 Some(info) => info,
1315 };
1316
1317 let line_index = snap.file_line_index(file_range.file_id)?;
1318 let range = to_proto::range(&line_index, info.range);
1319 let markup_kind = hover.format;
1320 let hover = lsp_ext::Hover {
1321 hover: lsp_types::Hover {
1322 contents: HoverContents::Markup(to_proto::markup_content(
1323 info.info.markup,
1324 markup_kind,
1325 )),
1326 range: Some(range),
1327 },
1328 actions: if snap.config.hover_actions().none() {
1329 Vec::new()
1330 } else {
1331 prepare_hover_actions(&snap, &info.info.actions)
1332 },
1333 };
1334
1335 Ok(Some(hover))
1336}
1337
1338pub(crate) fn handle_prepare_rename(
1339 snap: GlobalStateSnapshot,
1340 params: lsp_types::TextDocumentPositionParams,
1341) -> anyhow::Result<Option<PrepareRenameResponse>> {
1342 let _p = tracing::info_span!("handle_prepare_rename").entered();
1343 let position = try_default!(from_proto::file_position(&snap, params)?);
1344
1345 let change = snap.analysis.prepare_rename(position)?.map_err(to_proto::rename_error)?;
1346
1347 let line_index = snap.file_line_index(position.file_id)?;
1348 let range = to_proto::range(&line_index, change.range);
1349 Ok(Some(PrepareRenameResponse::Range(range)))
1350}
1351
1352pub(crate) fn handle_rename(
1353 snap: GlobalStateSnapshot,
1354 params: RenameParams,
1355) -> anyhow::Result<Option<WorkspaceEdit>> {
1356 let _p = tracing::info_span!("handle_rename").entered();
1357 let position = try_default!(from_proto::file_position(&snap, params.text_document_position)?);
1358
1359 let source_root = snap.analysis.source_root_id(position.file_id).ok();
1360 let config = snap.config.rename(source_root);
1361 let mut change = snap
1362 .analysis
1363 .rename(position, ¶ms.new_name, &config)?
1364 .map_err(to_proto::rename_error)?;
1365
1366 if !change.file_system_edits.is_empty() && snap.config.will_rename() {
1373 change.source_file_edits.clear();
1374 }
1375
1376 let workspace_edit = to_proto::workspace_edit(&snap, change)?;
1377
1378 if let Some(lsp_types::DocumentChanges::Operations(ops)) =
1379 workspace_edit.document_changes.as_ref()
1380 {
1381 for op in ops {
1382 if let lsp_types::DocumentChangeOperation::Op(doc_change_op) = op {
1383 resource_ops_supported(&snap.config, resolve_resource_op(doc_change_op))?
1384 }
1385 }
1386 }
1387
1388 Ok(Some(workspace_edit))
1389}
1390
1391pub(crate) fn handle_references(
1392 snap: GlobalStateSnapshot,
1393 params: lsp_types::ReferenceParams,
1394) -> anyhow::Result<Option<Vec<Location>>> {
1395 let _p = tracing::info_span!("handle_references").entered();
1396 let position = try_default!(from_proto::file_position(&snap, params.text_document_position)?);
1397
1398 let exclude_imports = snap.config.find_all_refs_exclude_imports();
1399 let exclude_tests = snap.config.find_all_refs_exclude_tests();
1400 let Some(refs) = snap.analysis.find_all_refs(
1401 position,
1402 &FindAllRefsConfig {
1403 search_scope: None,
1404 ra_fixture: snap.config.ra_fixture(snap.minicore()),
1405 exclude_imports,
1406 exclude_tests,
1407 },
1408 )?
1409 else {
1410 return Ok(None);
1411 };
1412
1413 let include_declaration = params.context.include_declaration;
1414 let locations = refs
1415 .into_iter()
1416 .flat_map(|refs| {
1417 let decl = if include_declaration {
1418 refs.declaration.map(|decl| FileRange {
1419 file_id: decl.nav.file_id,
1420 range: decl.nav.focus_or_full_range(),
1421 })
1422 } else {
1423 None
1424 };
1425 refs.references
1426 .into_iter()
1427 .flat_map(|(file_id, refs)| {
1428 refs.into_iter().map(move |(range, _)| FileRange { file_id, range })
1429 })
1430 .chain(decl)
1431 })
1432 .unique()
1433 .filter_map(|frange| to_proto::location(&snap, frange).ok())
1434 .collect();
1435
1436 Ok(Some(locations))
1437}
1438
1439pub(crate) fn handle_formatting(
1440 snap: GlobalStateSnapshot,
1441 params: lsp_types::DocumentFormattingParams,
1442) -> anyhow::Result<Option<Vec<lsp_types::TextEdit>>> {
1443 let _p = tracing::info_span!("handle_formatting").entered();
1444
1445 run_rustfmt(&snap, params.text_document, None)
1446}
1447
1448pub(crate) fn handle_range_formatting(
1449 snap: GlobalStateSnapshot,
1450 params: lsp_types::DocumentRangeFormattingParams,
1451) -> anyhow::Result<Option<Vec<lsp_types::TextEdit>>> {
1452 let _p = tracing::info_span!("handle_range_formatting").entered();
1453
1454 run_rustfmt(&snap, params.text_document, Some(params.range))
1455}
1456
1457pub(crate) fn handle_code_action(
1458 snap: GlobalStateSnapshot,
1459 params: lsp_types::CodeActionParams,
1460) -> anyhow::Result<Option<Vec<lsp_ext::CodeAction>>> {
1461 let _p = tracing::info_span!("handle_code_action").entered();
1462
1463 if !snap.config.code_action_literals() {
1464 return Ok(None);
1468 }
1469
1470 let file_id = try_default!(from_proto::file_id(&snap, ¶ms.text_document.uri)?);
1471 let line_index = snap.file_line_index(file_id)?;
1472 let frange = try_default!(from_proto::file_range(&snap, ¶ms.text_document, params.range)?);
1473 let source_root = snap.analysis.source_root_id(file_id)?;
1474
1475 let mut assists_config = snap.config.assist(Some(source_root));
1476 assists_config.allowed = params
1477 .context
1478 .only
1479 .clone()
1480 .map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect());
1481
1482 let mut res: Vec<lsp_ext::CodeAction> = Vec::new();
1483
1484 let code_action_resolve_cap = snap.config.code_action_resolve();
1485 let resolve = if code_action_resolve_cap {
1486 AssistResolveStrategy::None
1487 } else {
1488 AssistResolveStrategy::All
1489 };
1490 let assists = snap.analysis.assists_with_fixes(
1491 &assists_config,
1492 &snap.config.diagnostic_fixes(Some(source_root)),
1493 resolve,
1494 frange,
1495 )?;
1496 let client_commands = snap.config.client_commands();
1497 for (index, assist) in assists.into_iter().enumerate() {
1498 let resolve_data = if code_action_resolve_cap {
1499 Some((index, params.clone(), snap.file_version(file_id)))
1500 } else {
1501 None
1502 };
1503 let code_action = to_proto::code_action(&snap, &client_commands, assist, resolve_data)?;
1504
1505 let changes = code_action.edit.as_ref().and_then(|it| it.document_changes.as_ref());
1507 if let Some(changes) = changes {
1508 for change in changes {
1509 if let lsp_ext::SnippetDocumentChangeOperation::Op(res_op) = change {
1510 resource_ops_supported(&snap.config, resolve_resource_op(res_op))?
1511 }
1512 }
1513 }
1514
1515 res.push(code_action)
1516 }
1517
1518 for fix in snap
1520 .check_fixes
1521 .iter()
1522 .flat_map(|it| it.values())
1523 .filter_map(|it| it.get(&frange.file_id))
1524 .flatten()
1525 {
1526 let intersect_fix_range = fix
1529 .ranges
1530 .iter()
1531 .copied()
1532 .filter_map(|range| from_proto::text_range(&line_index, range).ok())
1533 .any(|fix_range| fix_range.intersect(frange.range).is_some());
1534 if intersect_fix_range {
1535 res.push(fix.action.clone());
1536 }
1537 }
1538
1539 Ok(Some(res))
1540}
1541
1542pub(crate) fn handle_code_action_resolve(
1543 snap: GlobalStateSnapshot,
1544 mut code_action: lsp_ext::CodeAction,
1545) -> anyhow::Result<lsp_ext::CodeAction> {
1546 let _p = tracing::info_span!("handle_code_action_resolve").entered();
1547 let Some(params) = code_action.data.take() else {
1548 return Ok(code_action);
1549 };
1550
1551 let file_id = from_proto::file_id(&snap, ¶ms.code_action_params.text_document.uri)?
1552 .expect("we never provide code actions for excluded files");
1553 if snap.file_version(file_id) != params.version {
1554 return Err(invalid_params_error("stale code action".to_owned()).into());
1555 }
1556 let line_index = snap.file_line_index(file_id)?;
1557 let range = from_proto::text_range(&line_index, params.code_action_params.range)?;
1558 let frange = FileRange { file_id, range };
1559 let source_root = snap.analysis.source_root_id(file_id)?;
1560
1561 let mut assists_config = snap.config.assist(Some(source_root));
1562 assists_config.allowed = params
1563 .code_action_params
1564 .context
1565 .only
1566 .map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect());
1567
1568 let (assist_index, assist_resolve) = match parse_action_id(¶ms.id) {
1569 Ok(parsed_data) => parsed_data,
1570 Err(e) => {
1571 return Err(invalid_params_error(format!(
1572 "Failed to parse action id string '{}': {e}",
1573 params.id
1574 ))
1575 .into());
1576 }
1577 };
1578
1579 let expected_assist_id = assist_resolve.assist_id.clone();
1580 let expected_kind = assist_resolve.assist_kind;
1581
1582 let assists = snap.analysis.assists_with_fixes(
1583 &assists_config,
1584 &snap.config.diagnostic_fixes(Some(source_root)),
1585 AssistResolveStrategy::Single(assist_resolve),
1586 frange,
1587 )?;
1588
1589 let assist = match assists.get(assist_index) {
1590 Some(assist) => assist,
1591 None => return Err(invalid_params_error(format!(
1592 "Failed to find the assist for index {} provided by the resolve request. Resolve request assist id: {}",
1593 assist_index, params.id,
1594 ))
1595 .into())
1596 };
1597 if assist.id.0 != expected_assist_id || assist.id.1 != expected_kind {
1598 return Err(invalid_params_error(format!(
1599 "Mismatching assist at index {} for the resolve parameters given. Resolve request assist id: {}, actual id: {:?}.",
1600 assist_index, params.id, assist.id
1601 ))
1602 .into());
1603 }
1604 let ca = to_proto::code_action(&snap, &snap.config.client_commands(), assist.clone(), None)?;
1605 code_action.edit = ca.edit;
1606 code_action.command = ca.command;
1607
1608 if let Some(edit) = code_action.edit.as_ref()
1609 && let Some(changes) = edit.document_changes.as_ref()
1610 {
1611 for change in changes {
1612 if let lsp_ext::SnippetDocumentChangeOperation::Op(res_op) = change {
1613 resource_ops_supported(&snap.config, resolve_resource_op(res_op))?
1614 }
1615 }
1616 }
1617
1618 Ok(code_action)
1619}
1620
1621fn parse_action_id(action_id: &str) -> anyhow::Result<(usize, SingleResolve), String> {
1622 let id_parts = action_id.split(':').collect::<Vec<_>>();
1623 match id_parts.as_slice() {
1624 [assist_id_string, assist_kind_string, index_string, subtype_str] => {
1625 let assist_kind: AssistKind = assist_kind_string.parse()?;
1626 let index: usize = match index_string.parse() {
1627 Ok(index) => index,
1628 Err(e) => return Err(format!("Incorrect index string: {e}")),
1629 };
1630 let assist_subtype = subtype_str.parse::<usize>().ok();
1631 Ok((
1632 index,
1633 SingleResolve {
1634 assist_id: assist_id_string.to_string(),
1635 assist_kind,
1636 assist_subtype,
1637 },
1638 ))
1639 }
1640 _ => Err("Action id contains incorrect number of segments".to_owned()),
1641 }
1642}
1643
1644pub(crate) fn handle_code_lens(
1645 snap: GlobalStateSnapshot,
1646 params: lsp_types::CodeLensParams,
1647) -> anyhow::Result<Option<Vec<CodeLens>>> {
1648 let _p = tracing::info_span!("handle_code_lens").entered();
1649
1650 let lens_config = snap.config.lens();
1651 if lens_config.none() {
1652 return Ok(Some(Vec::default()));
1654 }
1655
1656 let file_id = try_default!(from_proto::file_id(&snap, ¶ms.text_document.uri)?);
1657 let target_spec = TargetSpec::for_file(&snap, file_id)?;
1658
1659 let annotations = snap.analysis.annotations(
1660 &lens_config.into_annotation_config(
1661 target_spec
1662 .map(|spec| {
1663 matches!(
1664 spec.target_kind(),
1665 TargetKind::Bin
1666 | TargetKind::Example
1667 | TargetKind::Test
1668 | TargetKind::Bench
1669 )
1670 })
1671 .unwrap_or(false),
1672 snap.minicore(),
1673 ),
1674 file_id,
1675 )?;
1676
1677 let mut res = Vec::new();
1678 for a in annotations {
1679 to_proto::code_lens(&mut res, &snap, a)?;
1680 }
1681
1682 Ok(Some(res))
1683}
1684
1685pub(crate) fn handle_code_lens_resolve(
1686 snap: GlobalStateSnapshot,
1687 mut code_lens: CodeLens,
1688) -> anyhow::Result<CodeLens> {
1689 let Some(data) = code_lens.data.take() else {
1690 return Ok(code_lens);
1691 };
1692 let resolve = serde_json::from_value::<lsp_ext::CodeLensResolveData>(data)?;
1693 let Some(annotation) = from_proto::annotation(&snap, code_lens.range, resolve)? else {
1694 return Ok(code_lens);
1695 };
1696 let config = snap.config.lens().into_annotation_config(false, snap.minicore());
1697 let annotation = snap.analysis.resolve_annotation(&config, annotation)?;
1698
1699 let mut acc = Vec::new();
1700 to_proto::code_lens(&mut acc, &snap, annotation)?;
1701
1702 let mut res = match acc.pop() {
1703 Some(it) if acc.is_empty() => it,
1704 _ => {
1705 never!();
1706 code_lens
1707 }
1708 };
1709 res.data = None;
1710
1711 Ok(res)
1712}
1713
1714pub(crate) fn handle_document_highlight(
1715 snap: GlobalStateSnapshot,
1716 params: lsp_types::DocumentHighlightParams,
1717) -> anyhow::Result<Option<Vec<lsp_types::DocumentHighlight>>> {
1718 let _p = tracing::info_span!("handle_document_highlight").entered();
1719 let position =
1720 try_default!(from_proto::file_position(&snap, params.text_document_position_params)?);
1721 let line_index = snap.file_line_index(position.file_id)?;
1722 let source_root = snap.analysis.source_root_id(position.file_id)?;
1723
1724 let refs = match snap
1725 .analysis
1726 .highlight_related(snap.config.highlight_related(Some(source_root)), position)?
1727 {
1728 None => return Ok(None),
1729 Some(refs) => refs,
1730 };
1731 let res = refs
1732 .into_iter()
1733 .map(|ide::HighlightedRange { range, category }| lsp_types::DocumentHighlight {
1734 range: to_proto::range(&line_index, range),
1735 kind: to_proto::document_highlight_kind(category),
1736 })
1737 .collect();
1738 Ok(Some(res))
1739}
1740
1741pub(crate) fn handle_ssr(
1742 snap: GlobalStateSnapshot,
1743 params: lsp_ext::SsrParams,
1744) -> anyhow::Result<lsp_types::WorkspaceEdit> {
1745 let _p = tracing::info_span!("handle_ssr").entered();
1746 let selections = try_default!(
1747 params
1748 .selections
1749 .iter()
1750 .map(|range| from_proto::file_range(&snap, ¶ms.position.text_document, *range))
1751 .collect::<Result<Option<Vec<_>>, _>>()?
1752 );
1753 let position = try_default!(from_proto::file_position(&snap, params.position)?);
1754 let source_change = snap.analysis.structural_search_replace(
1755 ¶ms.query,
1756 params.parse_only,
1757 position,
1758 selections,
1759 )??;
1760 to_proto::workspace_edit(&snap, source_change).map_err(Into::into)
1761}
1762
1763pub(crate) fn handle_inlay_hints(
1764 snap: GlobalStateSnapshot,
1765 params: InlayHintParams,
1766) -> anyhow::Result<Option<Vec<InlayHint>>> {
1767 let _p = tracing::info_span!("handle_inlay_hints").entered();
1768 let document_uri = ¶ms.text_document.uri;
1769 let FileRange { file_id, range } = try_default!(from_proto::file_range(
1770 &snap,
1771 &TextDocumentIdentifier::new(document_uri.to_owned()),
1772 params.range,
1773 )?);
1774 let line_index = snap.file_line_index(file_id)?;
1775 let range = TextRange::new(
1776 range.start().min(line_index.index.len()),
1777 range.end().min(line_index.index.len()),
1778 );
1779
1780 let inlay_hints_config = snap.config.inlay_hints(snap.minicore());
1781 Ok(Some(
1782 snap.analysis
1783 .inlay_hints(&inlay_hints_config, file_id, Some(range))?
1784 .into_iter()
1785 .map(|it| {
1786 to_proto::inlay_hint(
1787 &snap,
1788 &inlay_hints_config.fields_to_resolve,
1789 &line_index,
1790 file_id,
1791 it,
1792 )
1793 })
1794 .collect::<Cancellable<Vec<_>>>()?,
1795 ))
1796}
1797
1798pub(crate) fn handle_inlay_hints_resolve(
1799 snap: GlobalStateSnapshot,
1800 mut original_hint: InlayHint,
1801) -> anyhow::Result<InlayHint> {
1802 let _p = tracing::info_span!("handle_inlay_hints_resolve").entered();
1803
1804 let Some(data) = original_hint.data.take() else {
1805 return Ok(original_hint);
1806 };
1807 let resolve_data: lsp_ext::InlayHintResolveData = serde_json::from_value(data)?;
1808 let file_id = FileId::from_raw(resolve_data.file_id);
1809 if resolve_data.version != snap.file_version(file_id) {
1810 tracing::warn!("Inlay hint resolve data is outdated");
1811 return Ok(original_hint);
1812 }
1813 let Some(hash) = resolve_data.hash.parse().ok() else {
1814 return Ok(original_hint);
1815 };
1816 anyhow::ensure!(snap.file_exists(file_id), "Invalid LSP resolve data");
1817
1818 let line_index = snap.file_line_index(file_id)?;
1819 let range = from_proto::text_range(&line_index, resolve_data.resolve_range)?;
1820
1821 let mut forced_resolve_inlay_hints_config = snap.config.inlay_hints(snap.minicore());
1822 forced_resolve_inlay_hints_config.fields_to_resolve = InlayFieldsToResolve::empty();
1823 let resolve_hints = snap.analysis.inlay_hints_resolve(
1824 &forced_resolve_inlay_hints_config,
1825 file_id,
1826 range,
1827 hash,
1828 |hint| {
1829 std::hash::BuildHasher::hash_one(
1830 &std::hash::BuildHasherDefault::<ide_db::FxHasher>::default(),
1831 hint,
1832 )
1833 },
1834 )?;
1835
1836 Ok(resolve_hints
1837 .and_then(|it| {
1838 to_proto::inlay_hint(
1839 &snap,
1840 &forced_resolve_inlay_hints_config.fields_to_resolve,
1841 &line_index,
1842 file_id,
1843 it,
1844 )
1845 .ok()
1846 })
1847 .filter(|hint| hint.position == original_hint.position)
1848 .filter(|hint| hint.kind == original_hint.kind)
1849 .unwrap_or(original_hint))
1850}
1851
1852pub(crate) fn handle_call_hierarchy_prepare(
1853 snap: GlobalStateSnapshot,
1854 params: CallHierarchyPrepareParams,
1855) -> anyhow::Result<Option<Vec<CallHierarchyItem>>> {
1856 let _p = tracing::info_span!("handle_call_hierarchy_prepare").entered();
1857 let position =
1858 try_default!(from_proto::file_position(&snap, params.text_document_position_params)?);
1859
1860 let config = snap.config.call_hierarchy(snap.minicore());
1861 let nav_info = match snap.analysis.call_hierarchy(position, &config)? {
1862 None => return Ok(None),
1863 Some(it) => it,
1864 };
1865
1866 let RangeInfo { range: _, info: navs } = nav_info;
1867 let res = navs
1868 .into_iter()
1869 .filter(|it| matches!(it.kind, Some(SymbolKind::Function | SymbolKind::Method)))
1870 .map(|it| to_proto::call_hierarchy_item(&snap, it))
1871 .collect::<Cancellable<Vec<_>>>()?;
1872
1873 Ok(Some(res))
1874}
1875
1876pub(crate) fn handle_call_hierarchy_incoming(
1877 snap: GlobalStateSnapshot,
1878 params: CallHierarchyIncomingCallsParams,
1879) -> anyhow::Result<Option<Vec<CallHierarchyIncomingCall>>> {
1880 let _p = tracing::info_span!("handle_call_hierarchy_incoming").entered();
1881 let item = params.item;
1882
1883 let doc = TextDocumentIdentifier::new(item.uri);
1884 let frange = try_default!(from_proto::file_range(&snap, &doc, item.selection_range)?);
1885 let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
1886
1887 let config = snap.config.call_hierarchy(snap.minicore());
1888 let call_items = match snap.analysis.incoming_calls(&config, fpos)? {
1889 None => return Ok(None),
1890 Some(it) => it,
1891 };
1892
1893 let mut res = vec![];
1894
1895 for call_item in call_items.into_iter() {
1896 let file_id = call_item.target.file_id;
1897 let line_index = snap.file_line_index(file_id)?;
1898 let item = to_proto::call_hierarchy_item(&snap, call_item.target)?;
1899 res.push(CallHierarchyIncomingCall {
1900 from: item,
1901 from_ranges: call_item
1902 .ranges
1903 .into_iter()
1904 .filter(|it| it.file_id == file_id)
1906 .map(|it| to_proto::range(&line_index, it.range))
1907 .collect(),
1908 });
1909 }
1910
1911 Ok(Some(res))
1912}
1913
1914pub(crate) fn handle_call_hierarchy_outgoing(
1915 snap: GlobalStateSnapshot,
1916 params: CallHierarchyOutgoingCallsParams,
1917) -> anyhow::Result<Option<Vec<CallHierarchyOutgoingCall>>> {
1918 let _p = tracing::info_span!("handle_call_hierarchy_outgoing").entered();
1919 let item = params.item;
1920
1921 let doc = TextDocumentIdentifier::new(item.uri);
1922 let frange = try_default!(from_proto::file_range(&snap, &doc, item.selection_range)?);
1923 let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
1924 let line_index = snap.file_line_index(fpos.file_id)?;
1925
1926 let config = snap.config.call_hierarchy(snap.minicore());
1927 let call_items = match snap.analysis.outgoing_calls(&config, fpos)? {
1928 None => return Ok(None),
1929 Some(it) => it,
1930 };
1931
1932 let mut res = vec![];
1933
1934 for call_item in call_items.into_iter() {
1935 let item = to_proto::call_hierarchy_item(&snap, call_item.target)?;
1936 res.push(CallHierarchyOutgoingCall {
1937 to: item,
1938 from_ranges: call_item
1939 .ranges
1940 .into_iter()
1941 .filter(|it| it.file_id == fpos.file_id)
1943 .map(|it| to_proto::range(&line_index, it.range))
1944 .collect(),
1945 });
1946 }
1947
1948 Ok(Some(res))
1949}
1950
1951pub(crate) fn handle_semantic_tokens_full(
1952 snap: GlobalStateSnapshot,
1953 params: SemanticTokensParams,
1954) -> anyhow::Result<Option<SemanticTokensResult>> {
1955 let _p = tracing::info_span!("handle_semantic_tokens_full").entered();
1956
1957 let file_id = try_default!(from_proto::file_id(&snap, ¶ms.text_document.uri)?);
1958 let text = snap.analysis.file_text(file_id)?;
1959 let line_index = snap.file_line_index(file_id)?;
1960
1961 let mut highlight_config = snap.config.highlighting_config(snap.minicore());
1962 highlight_config.syntactic_name_ref_highlighting =
1964 snap.workspaces.is_empty() || !snap.proc_macros_loaded;
1965
1966 let highlights = snap.analysis.highlight(highlight_config, file_id)?;
1967 let semantic_tokens = to_proto::semantic_tokens(
1968 &text,
1969 &line_index,
1970 highlights,
1971 snap.config.semantics_tokens_augments_syntax_tokens(),
1972 snap.config.highlighting_non_standard_tokens(),
1973 );
1974
1975 snap.semantic_tokens_cache.lock().insert(params.text_document.uri, semantic_tokens.clone());
1977
1978 Ok(Some(semantic_tokens.into()))
1979}
1980
1981pub(crate) fn handle_semantic_tokens_full_delta(
1982 snap: GlobalStateSnapshot,
1983 params: SemanticTokensDeltaParams,
1984) -> anyhow::Result<Option<SemanticTokensFullDeltaResult>> {
1985 let _p = tracing::info_span!("handle_semantic_tokens_full_delta").entered();
1986
1987 let file_id = try_default!(from_proto::file_id(&snap, ¶ms.text_document.uri)?);
1988 let text = snap.analysis.file_text(file_id)?;
1989 let line_index = snap.file_line_index(file_id)?;
1990
1991 let mut highlight_config = snap.config.highlighting_config(snap.minicore());
1992 highlight_config.syntactic_name_ref_highlighting =
1994 snap.workspaces.is_empty() || !snap.proc_macros_loaded;
1995
1996 let highlights = snap.analysis.highlight(highlight_config, file_id)?;
1997 let semantic_tokens = to_proto::semantic_tokens(
1998 &text,
1999 &line_index,
2000 highlights,
2001 snap.config.semantics_tokens_augments_syntax_tokens(),
2002 snap.config.highlighting_non_standard_tokens(),
2003 );
2004
2005 let cached_tokens = snap.semantic_tokens_cache.lock().remove(¶ms.text_document.uri);
2006
2007 if let Some(cached_tokens @ lsp_types::SemanticTokens { result_id: Some(prev_id), .. }) =
2008 &cached_tokens
2009 && *prev_id == params.previous_result_id
2010 {
2011 let delta = to_proto::semantic_token_delta(cached_tokens, &semantic_tokens);
2012 snap.semantic_tokens_cache.lock().insert(params.text_document.uri, semantic_tokens);
2013 return Ok(Some(delta.into()));
2014 }
2015
2016 let semantic_tokens_clone = semantic_tokens.clone();
2018 snap.semantic_tokens_cache.lock().insert(params.text_document.uri, semantic_tokens_clone);
2019
2020 Ok(Some(semantic_tokens.into()))
2021}
2022
2023pub(crate) fn handle_semantic_tokens_range(
2024 snap: GlobalStateSnapshot,
2025 params: SemanticTokensRangeParams,
2026) -> anyhow::Result<Option<SemanticTokensRangeResult>> {
2027 let _p = tracing::info_span!("handle_semantic_tokens_range").entered();
2028
2029 let frange = try_default!(from_proto::file_range(&snap, ¶ms.text_document, params.range)?);
2030 let text = snap.analysis.file_text(frange.file_id)?;
2031 let line_index = snap.file_line_index(frange.file_id)?;
2032
2033 let mut highlight_config = snap.config.highlighting_config(snap.minicore());
2034 highlight_config.syntactic_name_ref_highlighting =
2036 snap.workspaces.is_empty() || !snap.proc_macros_loaded;
2037
2038 let highlights = snap.analysis.highlight_range(highlight_config, frange)?;
2039 let semantic_tokens = to_proto::semantic_tokens(
2040 &text,
2041 &line_index,
2042 highlights,
2043 snap.config.semantics_tokens_augments_syntax_tokens(),
2044 snap.config.highlighting_non_standard_tokens(),
2045 );
2046 Ok(Some(semantic_tokens.into()))
2047}
2048
2049pub(crate) fn handle_open_docs(
2050 snap: GlobalStateSnapshot,
2051 params: lsp_types::TextDocumentPositionParams,
2052) -> anyhow::Result<ExternalDocsResponse> {
2053 let _p = tracing::info_span!("handle_open_docs").entered();
2054 let position = try_default!(from_proto::file_position(&snap, params)?);
2055
2056 let ws_and_sysroot = snap.workspaces.iter().find_map(|ws| match &ws.kind {
2057 ProjectWorkspaceKind::Cargo { cargo, .. }
2058 | ProjectWorkspaceKind::DetachedFile { cargo: Some((cargo, _, _)), .. } => {
2059 Some((cargo, &ws.sysroot))
2060 }
2061 ProjectWorkspaceKind::Json { .. } => None,
2062 ProjectWorkspaceKind::DetachedFile { .. } => None,
2063 });
2064
2065 let (cargo, sysroot) = match ws_and_sysroot {
2066 Some((ws, sysroot)) => (Some(ws), Some(sysroot)),
2067 _ => (None, None),
2068 };
2069
2070 let sysroot = sysroot.and_then(|p| p.root()).map(|it| it.as_str());
2071 let target_dir = cargo.map(|cargo| cargo.target_directory()).map(|p| p.as_str());
2072
2073 let Ok(remote_urls) = snap.analysis.external_docs(position, target_dir, sysroot) else {
2074 return if snap.config.local_docs() {
2075 Ok(ExternalDocsResponse::WithLocal(Default::default()))
2076 } else {
2077 Ok(ExternalDocsResponse::Simple(None))
2078 };
2079 };
2080
2081 let web = remote_urls.web_url.and_then(|it| Url::parse(&it).ok());
2082 let local = remote_urls.local_url.and_then(|it| Url::parse(&it).ok());
2083
2084 if snap.config.local_docs() {
2085 Ok(ExternalDocsResponse::WithLocal(ExternalDocsPair { web, local }))
2086 } else {
2087 Ok(ExternalDocsResponse::Simple(web))
2088 }
2089}
2090
2091pub(crate) fn handle_open_cargo_toml(
2092 snap: GlobalStateSnapshot,
2093 params: lsp_ext::OpenCargoTomlParams,
2094) -> anyhow::Result<Option<lsp_types::GotoDefinitionResponse>> {
2095 let _p = tracing::info_span!("handle_open_cargo_toml").entered();
2096 let file_id = try_default!(from_proto::file_id(&snap, ¶ms.text_document.uri)?);
2097
2098 let cargo_spec = match TargetSpec::for_file(&snap, file_id)? {
2099 Some(TargetSpec::Cargo(it)) => it,
2100 Some(TargetSpec::ProjectJson(_)) | None => return Ok(None),
2101 };
2102
2103 let cargo_toml_url = to_proto::url_from_abs_path(&cargo_spec.cargo_toml);
2104 let res: lsp_types::GotoDefinitionResponse =
2105 Location::new(cargo_toml_url, Range::default()).into();
2106 Ok(Some(res))
2107}
2108
2109pub(crate) fn handle_move_item(
2110 snap: GlobalStateSnapshot,
2111 params: lsp_ext::MoveItemParams,
2112) -> anyhow::Result<Vec<lsp_ext::SnippetTextEdit>> {
2113 let _p = tracing::info_span!("handle_move_item").entered();
2114 let file_id = try_default!(from_proto::file_id(&snap, ¶ms.text_document.uri)?);
2115 let range = try_default!(from_proto::file_range(&snap, ¶ms.text_document, params.range)?);
2116
2117 let direction = match params.direction {
2118 lsp_ext::MoveItemDirection::Up => ide::Direction::Up,
2119 lsp_ext::MoveItemDirection::Down => ide::Direction::Down,
2120 };
2121
2122 match snap.analysis.move_item(range, direction)? {
2123 Some(text_edit) => {
2124 let line_index = snap.file_line_index(file_id)?;
2125 Ok(to_proto::snippet_text_edit_vec(
2126 &line_index,
2127 true,
2128 text_edit,
2129 snap.config.change_annotation_support(),
2130 ))
2131 }
2132 None => Ok(vec![]),
2133 }
2134}
2135
2136pub(crate) fn handle_view_recursive_memory_layout(
2137 snap: GlobalStateSnapshot,
2138 params: lsp_types::TextDocumentPositionParams,
2139) -> anyhow::Result<Option<lsp_ext::RecursiveMemoryLayout>> {
2140 let _p = tracing::info_span!("handle_view_recursive_memory_layout").entered();
2141 let file_id = try_default!(from_proto::file_id(&snap, ¶ms.text_document.uri)?);
2142 let line_index = snap.file_line_index(file_id)?;
2143 let offset = from_proto::offset(&line_index, params.position)?;
2144
2145 let res = snap.analysis.get_recursive_memory_layout(FilePosition { file_id, offset })?;
2146 Ok(res.map(|it| lsp_ext::RecursiveMemoryLayout {
2147 nodes: it
2148 .nodes
2149 .iter()
2150 .map(|n| lsp_ext::MemoryLayoutNode {
2151 item_name: n.item_name.clone(),
2152 typename: n.typename.clone(),
2153 size: n.size,
2154 offset: n.offset,
2155 alignment: n.alignment,
2156 parent_idx: n.parent_idx,
2157 children_start: n.children_start,
2158 children_len: n.children_len,
2159 })
2160 .collect(),
2161 }))
2162}
2163
2164fn to_command_link(command: lsp_types::Command, tooltip: String) -> lsp_ext::CommandLink {
2165 lsp_ext::CommandLink { tooltip: Some(tooltip), command }
2166}
2167
2168fn show_impl_command_link(
2169 snap: &GlobalStateSnapshot,
2170 position: &FilePosition,
2171 implementations: bool,
2172 show_references: bool,
2173) -> Option<lsp_ext::CommandLinkGroup> {
2174 if implementations
2175 && show_references
2176 && let Some(nav_data) = snap
2177 .analysis
2178 .goto_implementation(&snap.config.goto_implementation(), *position)
2179 .unwrap_or(None)
2180 {
2181 let uri = to_proto::url(snap, position.file_id);
2182 let line_index = snap.file_line_index(position.file_id).ok()?;
2183 let position = to_proto::position(&line_index, position.offset);
2184 let locations: Vec<_> = nav_data
2185 .info
2186 .into_iter()
2187 .filter_map(|nav| to_proto::location_from_nav(snap, nav).ok())
2188 .collect();
2189 let title = to_proto::implementation_title(locations.len());
2190 let command = to_proto::command::show_references(title, &uri, position, locations);
2191
2192 return Some(lsp_ext::CommandLinkGroup {
2193 commands: vec![to_command_link(command, "Go to implementations".into())],
2194 ..Default::default()
2195 });
2196 }
2197 None
2198}
2199
2200fn show_ref_command_link(
2201 snap: &GlobalStateSnapshot,
2202 position: &FilePosition,
2203 references: bool,
2204 show_reference: bool,
2205) -> Option<lsp_ext::CommandLinkGroup> {
2206 if references
2207 && show_reference
2208 && let Some(ref_search_res) = snap
2209 .analysis
2210 .find_all_refs(
2211 *position,
2212 &FindAllRefsConfig {
2213 search_scope: None,
2214
2215 ra_fixture: snap.config.ra_fixture(snap.minicore()),
2216 exclude_imports: snap.config.find_all_refs_exclude_imports(),
2217 exclude_tests: snap.config.find_all_refs_exclude_tests(),
2218 },
2219 )
2220 .unwrap_or(None)
2221 {
2222 let uri = to_proto::url(snap, position.file_id);
2223 let line_index = snap.file_line_index(position.file_id).ok()?;
2224 let position = to_proto::position(&line_index, position.offset);
2225 let locations: Vec<_> = ref_search_res
2226 .into_iter()
2227 .flat_map(|res| res.references)
2228 .flat_map(|(file_id, ranges)| {
2229 ranges.into_iter().map(move |(range, _)| FileRange { file_id, range })
2230 })
2231 .unique()
2232 .filter_map(|range| to_proto::location(snap, range).ok())
2233 .collect();
2234 let title = to_proto::reference_title(locations.len());
2235 let command = to_proto::command::show_references(title, &uri, position, locations);
2236
2237 return Some(lsp_ext::CommandLinkGroup {
2238 commands: vec![to_command_link(command, "Go to references".into())],
2239 ..Default::default()
2240 });
2241 }
2242 None
2243}
2244
2245fn runnable_action_links(
2246 snap: &GlobalStateSnapshot,
2247 runnable: Runnable,
2248 hover_actions_config: &HoverActionsConfig,
2249 client_commands_config: &ClientCommandsConfig,
2250) -> Option<lsp_ext::CommandLinkGroup> {
2251 if !hover_actions_config.runnable() {
2252 return None;
2253 }
2254
2255 let target_spec = TargetSpec::for_file(snap, runnable.nav.file_id).ok()?;
2256 if should_skip_target(&runnable, target_spec.as_ref()) {
2257 return None;
2258 }
2259
2260 if !(client_commands_config.run_single || client_commands_config.debug_single) {
2261 return None;
2262 }
2263
2264 let title = runnable.title();
2265 let update_test = runnable.update_test;
2266 let r = to_proto::runnable(snap, runnable).ok()??;
2267
2268 let mut group = lsp_ext::CommandLinkGroup::default();
2269
2270 if hover_actions_config.run && client_commands_config.run_single {
2271 let run_command = to_proto::command::run_single(&r, &title);
2272 group.commands.push(to_command_link(run_command, r.label.clone()));
2273 }
2274
2275 if hover_actions_config.debug && client_commands_config.debug_single {
2276 let dbg_command = to_proto::command::debug_single(&r);
2277 group.commands.push(to_command_link(dbg_command, r.label.clone()));
2278 }
2279
2280 if hover_actions_config.update_test && client_commands_config.run_single {
2281 let label = update_test.label();
2282 if let Some(r) = to_proto::make_update_runnable(&r, update_test) {
2283 let update_command = to_proto::command::run_single(&r, label.unwrap().as_str());
2284 group.commands.push(to_command_link(update_command, r.label));
2285 }
2286 }
2287
2288 Some(group)
2289}
2290
2291fn goto_type_action_links(
2292 snap: &GlobalStateSnapshot,
2293 nav_targets: &[HoverGotoTypeData],
2294 hover_actions: &HoverActionsConfig,
2295 client_commands: &ClientCommandsConfig,
2296) -> Option<lsp_ext::CommandLinkGroup> {
2297 if !hover_actions.goto_type_def || nav_targets.is_empty() || !client_commands.goto_location {
2298 return None;
2299 }
2300
2301 Some(lsp_ext::CommandLinkGroup {
2302 title: Some("Go to ".into()),
2303 commands: nav_targets
2304 .iter()
2305 .filter_map(|it| {
2306 to_proto::command::goto_location(snap, &it.nav)
2307 .map(|cmd| to_command_link(cmd, it.mod_path.clone()))
2308 })
2309 .collect(),
2310 })
2311}
2312
2313fn prepare_hover_actions(
2314 snap: &GlobalStateSnapshot,
2315 actions: &[HoverAction],
2316) -> Vec<lsp_ext::CommandLinkGroup> {
2317 let hover_actions = snap.config.hover_actions();
2318 let client_commands = snap.config.client_commands();
2319 actions
2320 .iter()
2321 .filter_map(|it| match it {
2322 HoverAction::Implementation(position) => show_impl_command_link(
2323 snap,
2324 position,
2325 hover_actions.implementations,
2326 client_commands.show_reference,
2327 ),
2328 HoverAction::Reference(position) => show_ref_command_link(
2329 snap,
2330 position,
2331 hover_actions.references,
2332 client_commands.show_reference,
2333 ),
2334 HoverAction::Runnable(r) => {
2335 runnable_action_links(snap, r.clone(), &hover_actions, &client_commands)
2336 }
2337 HoverAction::GoToType(targets) => {
2338 goto_type_action_links(snap, targets, &hover_actions, &client_commands)
2339 }
2340 })
2341 .collect()
2342}
2343
2344fn should_skip_target(runnable: &Runnable, cargo_spec: Option<&TargetSpec>) -> bool {
2345 match runnable.kind {
2346 RunnableKind::Bin => {
2347 match &cargo_spec {
2349 Some(spec) => !matches!(
2350 spec.target_kind(),
2351 TargetKind::Bin | TargetKind::Example | TargetKind::Test | TargetKind::Bench
2352 ),
2353 None => true,
2354 }
2355 }
2356 _ => false,
2357 }
2358}
2359
2360fn run_rustfmt(
2361 snap: &GlobalStateSnapshot,
2362 text_document: TextDocumentIdentifier,
2363 range: Option<lsp_types::Range>,
2364) -> anyhow::Result<Option<Vec<lsp_types::TextEdit>>> {
2365 let file_id = try_default!(from_proto::file_id(snap, &text_document.uri)?);
2366 let file = snap.analysis.file_text(file_id)?;
2367
2368 let line_index = snap.file_line_index(file_id)?;
2369 let source_root_id = snap.analysis.source_root_id(file_id).ok();
2370 let crates = snap.analysis.relevant_crates_for(file_id)?;
2371
2372 let current_dir = match text_document.uri.to_file_path() {
2376 Ok(mut path) => {
2377 if path.pop() && path.is_dir() { path } else { std::env::current_dir()? }
2379 }
2380 Err(_) => {
2381 tracing::error!(
2382 text_document = ?text_document.uri,
2383 "Unable to get path, rustfmt.toml might be ignored"
2384 );
2385 std::env::current_dir()?
2386 }
2387 };
2388
2389 let mut command = match snap.config.rustfmt(source_root_id) {
2390 RustfmtConfig::Rustfmt { extra_args, enable_range_formatting } => {
2391 let Ok(editions) = crates
2394 .iter()
2395 .map(|&crate_id| snap.analysis.crate_edition(crate_id))
2396 .collect::<Result<Vec<_>, _>>()
2397 else {
2398 return Ok(None);
2399 };
2400 let edition = editions.iter().copied().max();
2401
2402 let mut cmd = toolchain::command(
2404 toolchain::Tool::Rustfmt.path(),
2405 current_dir,
2406 snap.config.extra_env(source_root_id),
2407 );
2408 cmd.args(extra_args);
2409
2410 if let Some(edition) = edition {
2411 cmd.arg("--edition");
2412 cmd.arg(edition.to_string());
2413 }
2414
2415 if let Some(range) = range {
2416 if !enable_range_formatting {
2417 return Err(LspError::new(
2418 ErrorCode::InvalidRequest as i32,
2419 String::from(
2420 "rustfmt range formatting is unstable. \
2421 Opt-in by using a nightly build of rustfmt and setting \
2422 `rustfmt.rangeFormatting.enable` to true in your LSP configuration",
2423 ),
2424 )
2425 .into());
2426 }
2427
2428 let frange = try_default!(from_proto::file_range(snap, &text_document, range)?);
2429 let start_line = line_index.index.line_col(frange.range.start()).line;
2430 let end_line = line_index.index.line_col(frange.range.end()).line;
2431
2432 cmd.arg("--unstable-features");
2433 cmd.arg("--file-lines");
2434 cmd.arg(
2435 json!([{
2436 "file": "stdin",
2437 "range": [start_line + 1, end_line + 1]
2439 }])
2440 .to_string(),
2441 );
2442 }
2443
2444 cmd
2445 }
2446 RustfmtConfig::CustomCommand { command, args } => {
2447 let cmd = Utf8PathBuf::from(&command);
2448 let target_spec = TargetSpec::for_file(snap, file_id).ok().flatten();
2449 let extra_env = snap.config.extra_env(source_root_id);
2450 let mut cmd = match target_spec {
2451 Some(TargetSpec::Cargo(_)) => {
2452 let cmd_path = if command.contains(std::path::MAIN_SEPARATOR)
2456 || (cfg!(windows) && command.contains('/'))
2457 {
2458 let project_root = Utf8PathBuf::from_path_buf(current_dir.clone())
2459 .ok()
2460 .and_then(|p| AbsPathBuf::try_from(p).ok());
2461 let project_root = project_root
2462 .as_ref()
2463 .map(|dir| snap.config.workspace_root_for(dir))
2464 .unwrap_or(snap.config.default_root_path());
2465 project_root.join(cmd).into()
2466 } else {
2467 cmd
2468 };
2469 toolchain::command(cmd_path, current_dir, extra_env)
2470 }
2471 _ => toolchain::command(cmd, current_dir, extra_env),
2472 };
2473
2474 cmd.args(args);
2475 cmd
2476 }
2477 };
2478
2479 let output = {
2480 let _p = tracing::info_span!("rustfmt", ?command).entered();
2481
2482 let mut rustfmt = command
2483 .stdin(Stdio::piped())
2484 .stdout(Stdio::piped())
2485 .stderr(Stdio::piped())
2486 .spawn()
2487 .context(format!("Failed to spawn {command:?}"))?;
2488
2489 rustfmt.stdin.as_mut().unwrap().write_all(file.as_bytes())?;
2490
2491 rustfmt.wait_with_output()?
2492 };
2493
2494 let captured_stdout = String::from_utf8(output.stdout)?;
2495 let captured_stderr = String::from_utf8(output.stderr).unwrap_or_default();
2496
2497 if !output.status.success() {
2498 let rustfmt_not_installed =
2499 captured_stderr.contains("not installed") || captured_stderr.contains("not available");
2500
2501 return match output.status.code() {
2502 Some(1) if !rustfmt_not_installed => {
2503 tracing::warn!(
2509 ?command,
2510 %captured_stderr,
2511 "rustfmt exited with status 1"
2512 );
2513 Ok(None)
2514 }
2515 Some(101)
2517 if !rustfmt_not_installed
2518 && (captured_stderr.starts_with("error[")
2519 || captured_stderr.starts_with("error:")) =>
2520 {
2521 Ok(None)
2522 }
2523 _ => {
2524 tracing::error!(
2526 ?command,
2527 %output.status,
2528 %captured_stdout,
2529 %captured_stderr,
2530 "rustfmt failed"
2531 );
2532 Ok(None)
2533 }
2534 };
2535 }
2536
2537 let (new_text, new_line_endings) = LineEndings::normalize(captured_stdout);
2538
2539 if line_index.endings != new_line_endings {
2540 Ok(Some(to_proto::text_edit_vec(
2544 &line_index,
2545 TextEdit::replace(TextRange::up_to(TextSize::of(&*file)), new_text),
2546 )))
2547 } else if *file == new_text {
2548 Ok(None)
2550 } else {
2551 Ok(Some(to_proto::text_edit_vec(&line_index, diff(&file, &new_text))))
2552 }
2553}
2554
2555pub(crate) fn fetch_dependency_list(
2556 state: GlobalStateSnapshot,
2557 _params: FetchDependencyListParams,
2558) -> anyhow::Result<FetchDependencyListResult> {
2559 let crates = state.analysis.fetch_crates()?;
2560 let crate_infos = crates
2561 .into_iter()
2562 .filter_map(|it| {
2563 let root_file_path = state.file_id_to_file_path(it.root_file_id);
2564 crate_path(&root_file_path).and_then(to_url).map(|path| CrateInfoResult {
2565 name: it.name,
2566 version: it.version,
2567 path,
2568 })
2569 })
2570 .collect();
2571 Ok(FetchDependencyListResult { crates: crate_infos })
2572}
2573
2574pub(crate) fn internal_testing_fetch_config(
2575 state: GlobalStateSnapshot,
2576 params: InternalTestingFetchConfigParams,
2577) -> anyhow::Result<Option<InternalTestingFetchConfigResponse>> {
2578 let source_root = match params.text_document {
2579 Some(it) => Some(
2580 state
2581 .analysis
2582 .source_root_id(try_default!(from_proto::file_id(&state, &it.uri)?))
2583 .map_err(anyhow::Error::from)?,
2584 ),
2585 None => None,
2586 };
2587 Ok(Some(match params.config {
2588 InternalTestingFetchConfigOption::AssistEmitMustUse => {
2589 InternalTestingFetchConfigResponse::AssistEmitMustUse(
2590 state.config.assist(source_root).assist_emit_must_use,
2591 )
2592 }
2593 InternalTestingFetchConfigOption::CheckWorkspace => {
2594 InternalTestingFetchConfigResponse::CheckWorkspace(
2595 state.config.flycheck_workspace(source_root),
2596 )
2597 }
2598 }))
2599}
2600
2601pub(crate) fn get_failed_obligations(
2602 snap: GlobalStateSnapshot,
2603 params: GetFailedObligationsParams,
2604) -> anyhow::Result<String> {
2605 let _p = tracing::info_span!("get_failed_obligations").entered();
2606 let file_id = try_default!(from_proto::file_id(&snap, ¶ms.text_document.uri)?);
2607 let line_index = snap.file_line_index(file_id)?;
2608 let offset = from_proto::offset(&line_index, params.position)?;
2609
2610 Ok(snap.analysis.get_failed_obligations(offset, file_id)?)
2611}
2612
2613fn crate_path(root_file_path: &VfsPath) -> Option<VfsPath> {
2625 let mut current_dir = root_file_path.parent();
2626 while let Some(path) = current_dir {
2627 let cargo_toml_path = path.join("../Cargo.toml")?;
2628 if fs::metadata(cargo_toml_path.as_path()?).is_ok() {
2629 let crate_path = cargo_toml_path.parent()?;
2630 return Some(crate_path);
2631 }
2632 current_dir = path.parent();
2633 }
2634 None
2635}
2636
2637fn to_url(path: VfsPath) -> Option<Url> {
2638 let path = path.as_path()?;
2639 let str_path = path.as_os_str().to_str()?;
2640 Url::from_file_path(str_path).ok()
2641}
2642
2643fn resource_ops_supported(config: &Config, kind: ResourceOperationKind) -> anyhow::Result<()> {
2644 if !matches!(config.workspace_edit_resource_operations(), Some(resops) if resops.contains(&kind))
2645 {
2646 return Err(LspError::new(
2647 ErrorCode::RequestFailed as i32,
2648 format!(
2649 "Client does not support {} capability.",
2650 match kind {
2651 ResourceOperationKind::Create => "create",
2652 ResourceOperationKind::Rename => "rename",
2653 ResourceOperationKind::Delete => "delete",
2654 }
2655 ),
2656 )
2657 .into());
2658 }
2659
2660 Ok(())
2661}
2662
2663fn resolve_resource_op(op: &ResourceOp) -> ResourceOperationKind {
2664 match op {
2665 ResourceOp::Create(_) => ResourceOperationKind::Create,
2666 ResourceOp::Rename(_) => ResourceOperationKind::Rename,
2667 ResourceOp::Delete(_) => ResourceOperationKind::Delete,
2668 }
2669}
2670
2671pub(crate) fn diff(left: &str, right: &str) -> TextEdit {
2672 use dissimilar::Chunk;
2673
2674 let chunks = dissimilar::diff(left, right);
2675
2676 let mut builder = TextEdit::builder();
2677 let mut pos = TextSize::default();
2678
2679 let mut chunks = chunks.into_iter().peekable();
2680 while let Some(chunk) = chunks.next() {
2681 if let (Chunk::Delete(deleted), Some(&Chunk::Insert(inserted))) = (chunk, chunks.peek()) {
2682 chunks.next().unwrap();
2683 let deleted_len = TextSize::of(deleted);
2684 builder.replace(TextRange::at(pos, deleted_len), inserted.into());
2685 pos += deleted_len;
2686 continue;
2687 }
2688
2689 match chunk {
2690 Chunk::Equal(text) => {
2691 pos += TextSize::of(text);
2692 }
2693 Chunk::Delete(deleted) => {
2694 let deleted_len = TextSize::of(deleted);
2695 builder.delete(TextRange::at(pos, deleted_len));
2696 pos += deleted_len;
2697 }
2698 Chunk::Insert(inserted) => {
2699 builder.insert(pos, inserted.into());
2700 }
2701 }
2702 }
2703 builder.finish()
2704}
2705
2706#[test]
2707fn diff_smoke_test() {
2708 let mut original = String::from("fn foo(a:u32){\n}");
2709 let result = "fn foo(a: u32) {}";
2710 let edit = diff(&original, result);
2711 edit.apply(&mut original);
2712 assert_eq!(original, result);
2713}