1 //! This module is responsible for implementing handlers for Language Server
2 //! Protocol. The majority of requests are fulfilled by calling into the
7 process::{self, Stdio},
12 AnnotationConfig, AssistKind, AssistResolveStrategy, FileId, FilePosition, FileRange,
13 HoverAction, HoverGotoTypeData, Query, RangeInfo, Runnable, RunnableKind, SingleResolve,
14 SourceChange, TextEdit,
16 use ide_db::SymbolKind;
17 use itertools::Itertools;
18 use lsp_server::ErrorCode;
20 CallHierarchyIncomingCall, CallHierarchyIncomingCallsParams, CallHierarchyItem,
21 CallHierarchyOutgoingCall, CallHierarchyOutgoingCallsParams, CallHierarchyPrepareParams,
22 CodeLens, CompletionItem, Diagnostic, DiagnosticTag, DocumentFormattingParams, FoldingRange,
23 FoldingRangeParams, HoverContents, Location, LocationLink, NumberOrString, Position,
24 PrepareRenameResponse, Range, RenameParams, SemanticTokensDeltaParams,
25 SemanticTokensFullDeltaResult, SemanticTokensParams, SemanticTokensRangeParams,
26 SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation, SymbolTag,
27 TextDocumentIdentifier, Url, WorkspaceEdit,
29 use project_model::{ProjectWorkspace, TargetKind};
31 use stdx::{format_to, never};
32 use syntax::{algo, ast, AstNode, TextRange, TextSize, T};
35 cargo_target_spec::CargoTargetSpec,
36 config::RustfmtConfig,
39 global_state::{GlobalState, GlobalStateSnapshot},
40 line_index::LineEndings,
42 self, InlayHint, InlayHintsParams, PositionOrRange, ViewCrateGraphParams,
43 WorkspaceSymbolParams,
45 lsp_utils::{all_edits_are_disjoint, invalid_params_error},
46 to_proto, LspError, Result,
49 pub(crate) fn handle_analyzer_status(
50 snap: GlobalStateSnapshot,
51 params: lsp_ext::AnalyzerStatusParams,
53 let _p = profile::span("handle_analyzer_status");
55 let mut buf = String::new();
57 let mut file_id = None;
58 if let Some(tdi) = params.text_document {
59 match from_proto::file_id(&snap, &tdi.uri) {
60 Ok(it) => file_id = Some(it),
61 Err(_) => format_to!(buf, "file {} not found in vfs", tdi.uri),
65 if snap.workspaces.is_empty() {
66 buf.push_str("No workspaces\n")
68 buf.push_str("Workspaces:\n");
71 "Loaded {:?} packages across {} workspace{}.\n",
72 snap.workspaces.iter().map(|w| w.n_packages()).sum::<usize>(),
73 snap.workspaces.len(),
74 if snap.workspaces.len() == 1 { "" } else { "s" }
77 buf.push_str("\nAnalysis:\n");
82 .unwrap_or_else(|_| "Analysis retrieval was cancelled".to_owned()),
87 pub(crate) fn handle_memory_usage(state: &mut GlobalState, _: ()) -> Result<String> {
88 let _p = profile::span("handle_memory_usage");
89 let mut mem = state.analysis_host.per_query_memory_usage();
90 mem.push(("Remaining".into(), profile::memory_usage().allocated));
92 let mut out = String::new();
93 for (name, bytes) in mem {
94 format_to!(out, "{:>8} {}\n", bytes, name);
99 pub(crate) fn handle_syntax_tree(
100 snap: GlobalStateSnapshot,
101 params: lsp_ext::SyntaxTreeParams,
102 ) -> Result<String> {
103 let _p = profile::span("handle_syntax_tree");
104 let id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
105 let line_index = snap.file_line_index(id)?;
106 let text_range = params.range.map(|r| from_proto::text_range(&line_index, r));
107 let res = snap.analysis.syntax_tree(id, text_range)?;
111 pub(crate) fn handle_view_hir(
112 snap: GlobalStateSnapshot,
113 params: lsp_types::TextDocumentPositionParams,
114 ) -> Result<String> {
115 let _p = profile::span("handle_view_hir");
116 let position = from_proto::file_position(&snap, params)?;
117 let res = snap.analysis.view_hir(position)?;
121 pub(crate) fn handle_view_item_tree(
122 snap: GlobalStateSnapshot,
123 params: lsp_ext::ViewItemTreeParams,
124 ) -> Result<String> {
125 let _p = profile::span("handle_view_item_tree");
126 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
127 let res = snap.analysis.view_item_tree(file_id)?;
131 pub(crate) fn handle_view_crate_graph(
132 snap: GlobalStateSnapshot,
133 params: ViewCrateGraphParams,
134 ) -> Result<String> {
135 let _p = profile::span("handle_view_crate_graph");
136 let dot = snap.analysis.view_crate_graph(params.full)??;
140 pub(crate) fn handle_expand_macro(
141 snap: GlobalStateSnapshot,
142 params: lsp_ext::ExpandMacroParams,
143 ) -> Result<Option<lsp_ext::ExpandedMacro>> {
144 let _p = profile::span("handle_expand_macro");
145 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
146 let line_index = snap.file_line_index(file_id)?;
147 let offset = from_proto::offset(&line_index, params.position);
149 let res = snap.analysis.expand_macro(FilePosition { file_id, offset })?;
150 Ok(res.map(|it| lsp_ext::ExpandedMacro { name: it.name, expansion: it.expansion }))
153 pub(crate) fn handle_selection_range(
154 snap: GlobalStateSnapshot,
155 params: lsp_types::SelectionRangeParams,
156 ) -> Result<Option<Vec<lsp_types::SelectionRange>>> {
157 let _p = profile::span("handle_selection_range");
158 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
159 let line_index = snap.file_line_index(file_id)?;
160 let res: Result<Vec<lsp_types::SelectionRange>> = params
164 let offset = from_proto::offset(&line_index, position);
165 let mut ranges = Vec::new();
167 let mut range = TextRange::new(offset, offset);
170 let frange = FileRange { file_id, range };
171 let next = snap.analysis.extend_selection(frange)?;
179 let mut range = lsp_types::SelectionRange {
180 range: to_proto::range(&line_index, *ranges.last().unwrap()),
183 for &r in ranges.iter().rev().skip(1) {
184 range = lsp_types::SelectionRange {
185 range: to_proto::range(&line_index, r),
186 parent: Some(Box::new(range)),
196 pub(crate) fn handle_matching_brace(
197 snap: GlobalStateSnapshot,
198 params: lsp_ext::MatchingBraceParams,
199 ) -> Result<Vec<Position>> {
200 let _p = profile::span("handle_matching_brace");
201 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
202 let line_index = snap.file_line_index(file_id)?;
207 let offset = from_proto::offset(&line_index, position);
208 let offset = match snap.analysis.matching_brace(FilePosition { file_id, offset }) {
209 Ok(Some(matching_brace_offset)) => matching_brace_offset,
210 Err(_) | Ok(None) => offset,
212 to_proto::position(&line_index, offset)
218 pub(crate) fn handle_join_lines(
219 snap: GlobalStateSnapshot,
220 params: lsp_ext::JoinLinesParams,
221 ) -> Result<Vec<lsp_types::TextEdit>> {
222 let _p = profile::span("handle_join_lines");
224 let config = snap.config.join_lines();
225 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
226 let line_index = snap.file_line_index(file_id)?;
228 let mut res = TextEdit::default();
229 for range in params.ranges {
230 let range = from_proto::text_range(&line_index, range);
231 let edit = snap.analysis.join_lines(&config, FileRange { file_id, range })?;
232 match res.union(edit) {
235 // just ignore overlapping edits
240 Ok(to_proto::text_edit_vec(&line_index, res))
243 pub(crate) fn handle_on_enter(
244 snap: GlobalStateSnapshot,
245 params: lsp_types::TextDocumentPositionParams,
246 ) -> Result<Option<Vec<lsp_ext::SnippetTextEdit>>> {
247 let _p = profile::span("handle_on_enter");
248 let position = from_proto::file_position(&snap, params)?;
249 let edit = match snap.analysis.on_enter(position)? {
250 None => return Ok(None),
253 let line_index = snap.file_line_index(position.file_id)?;
254 let edit = to_proto::snippet_text_edit_vec(&line_index, true, edit);
258 pub(crate) fn handle_on_type_formatting(
259 snap: GlobalStateSnapshot,
260 params: lsp_types::DocumentOnTypeFormattingParams,
261 ) -> Result<Option<Vec<lsp_types::TextEdit>>> {
262 let _p = profile::span("handle_on_type_formatting");
263 let mut position = from_proto::file_position(&snap, params.text_document_position)?;
264 let line_index = snap.file_line_index(position.file_id)?;
266 // in `ide`, the `on_type` invariant is that
267 // `text.char_at(position) == typed_char`.
268 position.offset -= TextSize::of('.');
269 let char_typed = params.ch.chars().next().unwrap_or('\0');
271 let text = snap.analysis.file_text(position.file_id)?;
272 if !text[usize::from(position.offset)..].starts_with(char_typed) {
273 // Add `always!` here once VS Code bug is fixed:
274 // https://github.com/rust-analyzer/rust-analyzer/issues/10002
278 // We have an assist that inserts ` ` after typing `->` in `fn foo() ->{`,
279 // but it requires precise cursor positioning to work, and one can't
280 // position the cursor with on_type formatting. So, let's just toggle this
281 // feature off here, hoping that we'll enable it one day, 😿.
282 if char_typed == '>' {
286 let edit = snap.analysis.on_char_typed(position, char_typed)?;
287 let edit = match edit {
289 None => return Ok(None),
292 // This should be a single-file edit
293 let (_, edit) = edit.source_file_edits.into_iter().next().unwrap();
295 let change = to_proto::text_edit_vec(&line_index, edit);
299 pub(crate) fn handle_document_symbol(
300 snap: GlobalStateSnapshot,
301 params: lsp_types::DocumentSymbolParams,
302 ) -> Result<Option<lsp_types::DocumentSymbolResponse>> {
303 let _p = profile::span("handle_document_symbol");
304 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
305 let line_index = snap.file_line_index(file_id)?;
307 let mut parents: Vec<(lsp_types::DocumentSymbol, Option<usize>)> = Vec::new();
309 for symbol in snap.analysis.file_structure(file_id)? {
310 let mut tags = Vec::new();
311 if symbol.deprecated {
312 tags.push(SymbolTag::Deprecated)
316 let doc_symbol = lsp_types::DocumentSymbol {
318 detail: symbol.detail,
319 kind: to_proto::structure_node_kind(symbol.kind),
321 deprecated: Some(symbol.deprecated),
322 range: to_proto::range(&line_index, symbol.node_range),
323 selection_range: to_proto::range(&line_index, symbol.navigation_range),
326 parents.push((doc_symbol, symbol.parent));
329 // Builds hierarchy from a flat list, in reverse order (so that indices
331 let document_symbols = {
332 let mut acc = Vec::new();
333 while let Some((mut node, parent_idx)) = parents.pop() {
334 if let Some(children) = &mut node.children {
337 let parent = match parent_idx {
339 Some(i) => parents[i].0.children.get_or_insert_with(Vec::new),
347 let res = if snap.config.hierarchical_symbols() {
348 document_symbols.into()
350 let url = to_proto::url(&snap, file_id);
351 let mut symbol_information = Vec::<SymbolInformation>::new();
352 for symbol in document_symbols {
353 flatten_document_symbol(&symbol, None, &url, &mut symbol_information);
355 symbol_information.into()
357 return Ok(Some(res));
359 fn flatten_document_symbol(
360 symbol: &lsp_types::DocumentSymbol,
361 container_name: Option<String>,
363 res: &mut Vec<SymbolInformation>,
365 let mut tags = Vec::new();
368 match symbol.deprecated {
369 Some(true) => tags.push(SymbolTag::Deprecated),
374 res.push(SymbolInformation {
375 name: symbol.name.clone(),
378 deprecated: symbol.deprecated,
379 location: Location::new(url.clone(), symbol.range),
383 for child in symbol.children.iter().flatten() {
384 flatten_document_symbol(child, Some(symbol.name.clone()), url, res);
389 pub(crate) fn handle_workspace_symbol(
390 snap: GlobalStateSnapshot,
391 params: WorkspaceSymbolParams,
392 ) -> Result<Option<Vec<SymbolInformation>>> {
393 let _p = profile::span("handle_workspace_symbol");
395 let (all_symbols, libs) = decide_search_scope_and_kind(¶ms, &snap);
398 let query: String = params.query.chars().filter(|&c| c != '#' && c != '*').collect();
399 let mut q = Query::new(query);
409 let mut res = exec_query(&snap, query)?;
410 if res.is_empty() && !all_symbols {
411 let mut query = Query::new(params.query);
413 res = exec_query(&snap, query)?;
416 return Ok(Some(res));
418 fn decide_search_scope_and_kind(
419 params: &WorkspaceSymbolParams,
420 snap: &GlobalStateSnapshot,
422 // Support old-style parsing of markers in the query.
423 let mut all_symbols = params.query.contains('#');
424 let mut libs = params.query.contains('*');
426 let config = snap.config.workspace_symbol();
428 // If no explicit marker was set, check request params. If that's also empty
429 // use global config.
431 let search_kind = if let Some(ref search_kind) = params.search_kind {
436 all_symbols = match search_kind {
437 lsp_ext::WorkspaceSymbolSearchKind::OnlyTypes => false,
438 lsp_ext::WorkspaceSymbolSearchKind::AllSymbols => true,
443 let search_scope = if let Some(ref search_scope) = params.search_scope {
448 libs = match search_scope {
449 lsp_ext::WorkspaceSymbolSearchScope::Workspace => false,
450 lsp_ext::WorkspaceSymbolSearchScope::WorkspaceAndDependencies => true,
457 fn exec_query(snap: &GlobalStateSnapshot, query: Query) -> Result<Vec<SymbolInformation>> {
458 let mut res = Vec::new();
459 for nav in snap.analysis.symbol_search(query)? {
460 let container_name = nav.container_name.as_ref().map(|v| v.to_string());
463 let info = SymbolInformation {
464 name: nav.name.to_string(),
467 .map(to_proto::symbol_kind)
468 .unwrap_or(lsp_types::SymbolKind::Variable),
470 location: to_proto::location_from_nav(snap, nav)?,
480 pub(crate) fn handle_will_rename_files(
481 snap: GlobalStateSnapshot,
482 params: lsp_types::RenameFilesParams,
483 ) -> Result<Option<lsp_types::WorkspaceEdit>> {
484 let _p = profile::span("handle_will_rename_files");
486 let source_changes: Vec<SourceChange> = params
489 .filter_map(|file_rename| {
490 let from = Url::parse(&file_rename.old_uri).ok()?;
491 let to = Url::parse(&file_rename.new_uri).ok()?;
493 let from_path = from.to_file_path().ok()?;
494 let to_path = to.to_file_path().ok()?;
496 // Limit to single-level moves for now.
497 match (from_path.parent(), to_path.parent()) {
498 (Some(p1), Some(p2)) if p1 == p2 => {
499 if from_path.is_dir() {
500 // add '/' to end of url -- from `file://path/to/folder` to `file://path/to/folder/`
501 let mut old_folder_name = from_path.file_stem()?.to_str()?.to_string();
502 old_folder_name.push('/');
503 let from_with_trailing_slash = from.join(&old_folder_name).ok()?;
505 let imitate_from_url = from_with_trailing_slash.join("mod.rs").ok()?;
506 let new_file_name = to_path.file_name()?.to_str()?;
508 snap.url_to_file_id(&imitate_from_url).ok()?,
509 new_file_name.to_string(),
512 let old_name = from_path.file_stem()?.to_str()?;
513 let new_name = to_path.file_stem()?.to_str()?;
514 match (old_name, new_name) {
517 _ => Some((snap.url_to_file_id(&from).ok()?, new_name.to_string())),
524 .filter_map(|(file_id, new_name)| {
525 snap.analysis.will_rename_file(file_id, &new_name).ok()?
529 // Drop file system edits since we're just renaming things on the same level
530 let mut source_changes = source_changes.into_iter();
531 let mut source_change = source_changes.next().unwrap_or_default();
532 source_change.file_system_edits.clear();
533 // no collect here because we want to merge text edits on same file ids
534 source_change.extend(source_changes.map(|it| it.source_file_edits).flatten());
535 if source_change.source_file_edits.is_empty() {
538 to_proto::workspace_edit(&snap, source_change).map(Some)
542 pub(crate) fn handle_goto_definition(
543 snap: GlobalStateSnapshot,
544 params: lsp_types::GotoDefinitionParams,
545 ) -> Result<Option<lsp_types::GotoDefinitionResponse>> {
546 let _p = profile::span("handle_goto_definition");
547 let position = from_proto::file_position(&snap, params.text_document_position_params)?;
548 let nav_info = match snap.analysis.goto_definition(position)? {
549 None => return Ok(None),
552 let src = FileRange { file_id: position.file_id, range: nav_info.range };
553 let res = to_proto::goto_definition_response(&snap, Some(src), nav_info.info)?;
557 pub(crate) fn handle_goto_declaration(
558 snap: GlobalStateSnapshot,
559 params: lsp_types::request::GotoDeclarationParams,
560 ) -> Result<Option<lsp_types::request::GotoDeclarationResponse>> {
561 let _p = profile::span("handle_goto_declaration");
562 let position = from_proto::file_position(&snap, params.text_document_position_params.clone())?;
563 let nav_info = match snap.analysis.goto_declaration(position)? {
564 None => return handle_goto_definition(snap, params),
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)?;
572 pub(crate) fn handle_goto_implementation(
573 snap: GlobalStateSnapshot,
574 params: lsp_types::request::GotoImplementationParams,
575 ) -> Result<Option<lsp_types::request::GotoImplementationResponse>> {
576 let _p = profile::span("handle_goto_implementation");
577 let position = from_proto::file_position(&snap, params.text_document_position_params)?;
578 let nav_info = match snap.analysis.goto_implementation(position)? {
579 None => return Ok(None),
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)?;
587 pub(crate) fn handle_goto_type_definition(
588 snap: GlobalStateSnapshot,
589 params: lsp_types::request::GotoTypeDefinitionParams,
590 ) -> Result<Option<lsp_types::request::GotoTypeDefinitionResponse>> {
591 let _p = profile::span("handle_goto_type_definition");
592 let position = from_proto::file_position(&snap, params.text_document_position_params)?;
593 let nav_info = match snap.analysis.goto_type_definition(position)? {
594 None => return Ok(None),
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)?;
602 pub(crate) fn handle_parent_module(
603 snap: GlobalStateSnapshot,
604 params: lsp_types::TextDocumentPositionParams,
605 ) -> Result<Option<lsp_types::GotoDefinitionResponse>> {
606 let _p = profile::span("handle_parent_module");
607 if let Ok(file_path) = ¶ms.text_document.uri.to_file_path() {
608 if file_path.file_name().unwrap_or_default() == "Cargo.toml" {
609 // search parent workspace and collect a list of `LocationLink`path,
610 // since cargo.toml doesn't have file_id
611 let links: Vec<LocationLink> = snap
614 .filter_map(|ws| match ws {
615 ProjectWorkspace::Cargo { cargo, .. } => cargo
617 .find(|&pkg| cargo[pkg].manifest.as_ref() == file_path)
618 .and_then(|_| Some(cargo)),
622 let target_cargo_toml_path = ws.workspace_root().join("Cargo.toml");
623 let target_cargo_toml_url =
624 to_proto::url_from_abs_path(&target_cargo_toml_path);
626 origin_selection_range: None,
627 target_uri: target_cargo_toml_url,
628 target_range: Range::default(),
629 target_selection_range: Range::default(),
633 return Ok(Some(links.into()));
636 // check if invoked at the crate root
637 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
638 let crate_id = match snap.analysis.crate_for(file_id)?.first() {
639 Some(&crate_id) => crate_id,
640 None => return Ok(None),
642 let cargo_spec = match CargoTargetSpec::for_file(&snap, file_id)? {
644 None => return Ok(None),
647 if snap.analysis.crate_root(crate_id)? == file_id {
648 let cargo_toml_url = to_proto::url_from_abs_path(&cargo_spec.cargo_toml);
649 let res = vec![LocationLink {
650 origin_selection_range: None,
651 target_uri: cargo_toml_url,
652 target_range: Range::default(),
653 target_selection_range: Range::default(),
656 return Ok(Some(res));
660 // locate parent module by semantics
661 let position = from_proto::file_position(&snap, params)?;
662 let navs = snap.analysis.parent_module(position)?;
663 let res = to_proto::goto_definition_response(&snap, None, navs)?;
667 pub(crate) fn handle_runnables(
668 snap: GlobalStateSnapshot,
669 params: lsp_ext::RunnablesParams,
670 ) -> Result<Vec<lsp_ext::Runnable>> {
671 let _p = profile::span("handle_runnables");
672 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
673 let line_index = snap.file_line_index(file_id)?;
674 let offset = params.position.map(|it| from_proto::offset(&line_index, it));
675 let cargo_spec = CargoTargetSpec::for_file(&snap, file_id)?;
677 let expect_test = match offset {
679 let source_file = snap.analysis.parse(file_id)?;
680 algo::find_node_at_offset::<ast::MacroCall>(source_file.syntax(), offset)
681 .and_then(|it| it.path()?.segment()?.name_ref())
682 .map_or(false, |it| it.text() == "expect" || it.text() == "expect_file")
687 let mut res = Vec::new();
688 for runnable in snap.analysis.runnables(file_id)? {
689 if let Some(offset) = offset {
690 if !runnable.nav.full_range.contains_inclusive(offset) {
694 if should_skip_target(&runnable, cargo_spec.as_ref()) {
697 let mut runnable = to_proto::runnable(&snap, runnable)?;
699 runnable.label = format!("{} + expect", runnable.label);
700 runnable.args.expect_test = Some(true);
705 // Add `cargo check` and `cargo test` for all targets of the whole package
706 let config = snap.config.runnables();
709 for &cmd in ["check", "test"].iter() {
710 res.push(lsp_ext::Runnable {
711 label: format!("cargo {} -p {} --all-targets", cmd, spec.package),
713 kind: lsp_ext::RunnableKind::Cargo,
714 args: lsp_ext::CargoRunnable {
715 workspace_root: Some(spec.workspace_root.clone().into()),
716 override_cargo: config.override_cargo.clone(),
719 "--package".to_string(),
720 spec.package.clone(),
721 "--all-targets".to_string(),
723 cargo_extra_args: config.cargo_extra_args.clone(),
724 executable_args: Vec::new(),
731 if !snap.config.linked_projects().is_empty()
736 .map(|projects| projects.is_empty())
739 res.push(lsp_ext::Runnable {
740 label: "cargo check --workspace".to_string(),
742 kind: lsp_ext::RunnableKind::Cargo,
743 args: lsp_ext::CargoRunnable {
744 workspace_root: None,
745 override_cargo: config.override_cargo,
746 cargo_args: vec!["check".to_string(), "--workspace".to_string()],
747 cargo_extra_args: config.cargo_extra_args,
748 executable_args: Vec::new(),
758 pub(crate) fn handle_related_tests(
759 snap: GlobalStateSnapshot,
760 params: lsp_types::TextDocumentPositionParams,
761 ) -> Result<Vec<lsp_ext::TestInfo>> {
762 let _p = profile::span("handle_related_tests");
763 let position = from_proto::file_position(&snap, params)?;
765 let tests = snap.analysis.related_tests(position, None)?;
766 let mut res = Vec::new();
768 if let Ok(runnable) = to_proto::runnable(&snap, it) {
769 res.push(lsp_ext::TestInfo { runnable })
776 pub(crate) fn handle_completion(
777 snap: GlobalStateSnapshot,
778 params: lsp_types::CompletionParams,
779 ) -> Result<Option<lsp_types::CompletionResponse>> {
780 let _p = profile::span("handle_completion");
781 let text_document_position = params.text_document_position.clone();
782 let position = from_proto::file_position(&snap, params.text_document_position)?;
783 let completion_triggered_after_single_colon = {
785 if let Some(ctx) = params.context {
786 if ctx.trigger_character.as_deref() == Some(":") {
787 let source_file = snap.analysis.parse(position.file_id)?;
789 source_file.syntax().token_at_offset(position.offset).left_biased();
791 Some(left_token) => res = left_token.kind() == T![:],
798 if completion_triggered_after_single_colon {
802 let completion_config = &snap.config.completion();
803 let items = match snap.analysis.completions(completion_config, position)? {
804 None => return Ok(None),
805 Some(items) => items,
807 let line_index = snap.file_line_index(position.file_id)?;
810 to_proto::completion_items(&snap.config, &line_index, text_document_position, items);
812 let completion_list = lsp_types::CompletionList { is_incomplete: true, items };
813 Ok(Some(completion_list.into()))
816 pub(crate) fn handle_completion_resolve(
817 snap: GlobalStateSnapshot,
818 mut original_completion: CompletionItem,
819 ) -> Result<CompletionItem> {
820 let _p = profile::span("handle_completion_resolve");
822 if !all_edits_are_disjoint(&original_completion, &[]) {
823 return Err(invalid_params_error(
824 "Received a completion with overlapping edits, this is not LSP-compliant".to_string(),
829 let data = match original_completion.data.take() {
831 None => return Ok(original_completion),
834 let resolve_data: lsp_ext::CompletionResolveData = serde_json::from_value(data)?;
836 let file_id = from_proto::file_id(&snap, &resolve_data.position.text_document.uri)?;
837 let line_index = snap.file_line_index(file_id)?;
838 let offset = from_proto::offset(&line_index, resolve_data.position.position);
840 let additional_edits = snap
842 .resolve_completion_edits(
843 &snap.config.completion(),
844 FilePosition { file_id, offset },
845 &resolve_data.full_import_path,
846 resolve_data.imported_name,
849 .flat_map(|edit| edit.into_iter().map(|indel| to_proto::text_edit(&line_index, indel)))
852 if !all_edits_are_disjoint(&original_completion, &additional_edits) {
853 return Err(LspError::new(
854 ErrorCode::InternalError as i32,
855 "Import edit overlaps with the original completion edits, this is not LSP-compliant"
861 if let Some(original_additional_edits) = original_completion.additional_text_edits.as_mut() {
862 original_additional_edits.extend(additional_edits.into_iter())
864 original_completion.additional_text_edits = Some(additional_edits);
867 Ok(original_completion)
870 pub(crate) fn handle_folding_range(
871 snap: GlobalStateSnapshot,
872 params: FoldingRangeParams,
873 ) -> Result<Option<Vec<FoldingRange>>> {
874 let _p = profile::span("handle_folding_range");
875 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
876 let folds = snap.analysis.folding_ranges(file_id)?;
877 let text = snap.analysis.file_text(file_id)?;
878 let line_index = snap.file_line_index(file_id)?;
879 let line_folding_only = snap.config.line_folding_only();
882 .map(|it| to_proto::folding_range(&*text, &line_index, line_folding_only, it))
887 pub(crate) fn handle_signature_help(
888 snap: GlobalStateSnapshot,
889 params: lsp_types::SignatureHelpParams,
890 ) -> Result<Option<lsp_types::SignatureHelp>> {
891 let _p = profile::span("handle_signature_help");
892 let position = from_proto::file_position(&snap, params.text_document_position_params)?;
893 let call_info = match snap.analysis.call_info(position)? {
895 None => return Ok(None),
897 let concise = !snap.config.call_info_full();
899 to_proto::signature_help(call_info, concise, snap.config.signature_help_label_offsets());
903 pub(crate) fn handle_hover(
904 snap: GlobalStateSnapshot,
905 params: lsp_ext::HoverParams,
906 ) -> Result<Option<lsp_ext::Hover>> {
907 let _p = profile::span("handle_hover");
908 let range = match params.position {
909 PositionOrRange::Position(position) => Range::new(position, position),
910 PositionOrRange::Range(range) => range,
913 let file_range = from_proto::file_range(&snap, params.text_document, range)?;
914 let info = match snap.analysis.hover(&snap.config.hover(), file_range)? {
915 None => return Ok(None),
919 let line_index = snap.file_line_index(file_range.file_id)?;
920 let range = to_proto::range(&line_index, info.range);
921 let hover = lsp_ext::Hover {
922 hover: lsp_types::Hover {
923 contents: HoverContents::Markup(to_proto::markup_content(info.info.markup)),
926 actions: if snap.config.hover_actions().none() {
929 prepare_hover_actions(&snap, &info.info.actions)
936 pub(crate) fn handle_prepare_rename(
937 snap: GlobalStateSnapshot,
938 params: lsp_types::TextDocumentPositionParams,
939 ) -> Result<Option<PrepareRenameResponse>> {
940 let _p = profile::span("handle_prepare_rename");
941 let position = from_proto::file_position(&snap, params)?;
943 let change = snap.analysis.prepare_rename(position)?.map_err(to_proto::rename_error)?;
945 let line_index = snap.file_line_index(position.file_id)?;
946 let range = to_proto::range(&line_index, change.range);
947 Ok(Some(PrepareRenameResponse::Range(range)))
950 pub(crate) fn handle_rename(
951 snap: GlobalStateSnapshot,
952 params: RenameParams,
953 ) -> Result<Option<WorkspaceEdit>> {
954 let _p = profile::span("handle_rename");
955 let position = from_proto::file_position(&snap, params.text_document_position)?;
958 snap.analysis.rename(position, &*params.new_name)?.map_err(to_proto::rename_error)?;
960 // this is kind of a hack to prevent double edits from happening when moving files
961 // When a module gets renamed by renaming the mod declaration this causes the file to move
962 // which in turn will trigger a WillRenameFiles request to the server for which we reply with a
963 // a second identical set of renames, the client will then apply both edits causing incorrect edits
964 // with this we only emit source_file_edits in the WillRenameFiles response which will do the rename instead
965 // See https://github.com/microsoft/vscode-languageserver-node/issues/752 for more info
966 if !change.file_system_edits.is_empty() && snap.config.will_rename() {
967 change.source_file_edits.clear();
969 let workspace_edit = to_proto::workspace_edit(&snap, change)?;
970 Ok(Some(workspace_edit))
973 pub(crate) fn handle_references(
974 snap: GlobalStateSnapshot,
975 params: lsp_types::ReferenceParams,
976 ) -> Result<Option<Vec<Location>>> {
977 let _p = profile::span("handle_references");
978 let position = from_proto::file_position(&snap, params.text_document_position)?;
980 let refs = match snap.analysis.find_all_refs(position, None)? {
981 None => return Ok(None),
985 let include_declaration = params.context.include_declaration;
989 let decl = if include_declaration {
990 refs.declaration.map(|decl| FileRange {
991 file_id: decl.nav.file_id,
992 range: decl.nav.focus_or_full_range(),
999 .flat_map(|(file_id, refs)| {
1000 refs.into_iter().map(move |(range, _)| FileRange { file_id, range })
1004 .filter_map(|frange| to_proto::location(&snap, frange).ok())
1010 pub(crate) fn handle_formatting(
1011 snap: GlobalStateSnapshot,
1012 params: DocumentFormattingParams,
1013 ) -> Result<Option<Vec<lsp_types::TextEdit>>> {
1014 let _p = profile::span("handle_formatting");
1016 run_rustfmt(&snap, params.text_document, None)
1019 pub(crate) fn handle_range_formatting(
1020 snap: GlobalStateSnapshot,
1021 params: lsp_types::DocumentRangeFormattingParams,
1022 ) -> Result<Option<Vec<lsp_types::TextEdit>>> {
1023 let _p = profile::span("handle_range_formatting");
1025 run_rustfmt(&snap, params.text_document, Some(params.range))
1028 pub(crate) fn handle_code_action(
1029 snap: GlobalStateSnapshot,
1030 params: lsp_types::CodeActionParams,
1031 ) -> Result<Option<Vec<lsp_ext::CodeAction>>> {
1032 let _p = profile::span("handle_code_action");
1034 if !snap.config.code_action_literals() {
1035 // We intentionally don't support command-based actions, as those either
1036 // require either custom client-code or server-initiated edits. Server
1037 // initiated edits break causality, so we avoid those.
1042 snap.file_line_index(from_proto::file_id(&snap, ¶ms.text_document.uri)?)?;
1043 let frange = from_proto::file_range(&snap, params.text_document.clone(), params.range)?;
1045 let mut assists_config = snap.config.assist();
1046 assists_config.allowed = params
1050 .map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect());
1052 let mut res: Vec<lsp_ext::CodeAction> = Vec::new();
1054 let code_action_resolve_cap = snap.config.code_action_resolve();
1055 let resolve = if code_action_resolve_cap {
1056 AssistResolveStrategy::None
1058 AssistResolveStrategy::All
1060 let assists = snap.analysis.assists_with_fixes(
1062 &snap.config.diagnostics(),
1066 for (index, assist) in assists.into_iter().enumerate() {
1068 if code_action_resolve_cap { Some((index, params.clone())) } else { None };
1069 let code_action = to_proto::code_action(&snap, assist, resolve_data)?;
1070 res.push(code_action)
1073 // Fixes from `cargo check`.
1074 for fix in snap.check_fixes.get(&frange.file_id).into_iter().flatten() {
1075 // FIXME: this mapping is awkward and shouldn't exist. Refactor
1076 // `snap.check_fixes` to not convert to LSP prematurely.
1077 let fix_range = from_proto::text_range(&line_index, fix.range);
1078 if fix_range.intersect(frange.range).is_some() {
1079 res.push(fix.action.clone());
1086 pub(crate) fn handle_code_action_resolve(
1087 snap: GlobalStateSnapshot,
1088 mut code_action: lsp_ext::CodeAction,
1089 ) -> Result<lsp_ext::CodeAction> {
1090 let _p = profile::span("handle_code_action_resolve");
1091 let params = match code_action.data.take() {
1093 None => return Err(invalid_params_error(format!("code action without data")).into()),
1096 let file_id = from_proto::file_id(&snap, ¶ms.code_action_params.text_document.uri)?;
1097 let line_index = snap.file_line_index(file_id)?;
1098 let range = from_proto::text_range(&line_index, params.code_action_params.range);
1099 let frange = FileRange { file_id, range };
1101 let mut assists_config = snap.config.assist();
1102 assists_config.allowed = params
1106 .map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect());
1108 let (assist_index, assist_resolve) = match parse_action_id(¶ms.id) {
1109 Ok(parsed_data) => parsed_data,
1111 return Err(invalid_params_error(format!(
1112 "Failed to parse action id string '{}': {}",
1119 let expected_assist_id = assist_resolve.assist_id.clone();
1120 let expected_kind = assist_resolve.assist_kind;
1122 let assists = snap.analysis.assists_with_fixes(
1124 &snap.config.diagnostics(),
1125 AssistResolveStrategy::Single(assist_resolve),
1129 let assist = match assists.get(assist_index) {
1130 Some(assist) => assist,
1131 None => return Err(invalid_params_error(format!(
1132 "Failed to find the assist for index {} provided by the resolve request. Resolve request assist id: {}",
1133 assist_index, params.id,
1137 if assist.id.0 != expected_assist_id || assist.id.1 != expected_kind {
1138 return Err(invalid_params_error(format!(
1139 "Mismatching assist at index {} for the resolve parameters given. Resolve request assist id: {}, actual id: {:?}.",
1140 assist_index, params.id, assist.id
1144 let edit = to_proto::code_action(&snap, assist.clone(), None)?.edit;
1145 code_action.edit = edit;
1149 fn parse_action_id(action_id: &str) -> Result<(usize, SingleResolve), String> {
1150 let id_parts = action_id.split(':').collect_vec();
1151 match id_parts.as_slice() {
1152 &[assist_id_string, assist_kind_string, index_string] => {
1153 let assist_kind: AssistKind = assist_kind_string.parse()?;
1154 let index: usize = match index_string.parse() {
1156 Err(e) => return Err(format!("Incorrect index string: {}", e)),
1158 Ok((index, SingleResolve { assist_id: assist_id_string.to_string(), assist_kind }))
1160 _ => Err("Action id contains incorrect number of segments".to_string()),
1164 pub(crate) fn handle_code_lens(
1165 snap: GlobalStateSnapshot,
1166 params: lsp_types::CodeLensParams,
1167 ) -> Result<Option<Vec<CodeLens>>> {
1168 let _p = profile::span("handle_code_lens");
1170 let lens_config = snap.config.lens();
1171 if lens_config.none() {
1172 // early return before any db query!
1173 return Ok(Some(Vec::default()));
1176 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
1177 let cargo_target_spec = CargoTargetSpec::for_file(&snap, file_id)?;
1179 let annotations = snap.analysis.annotations(
1181 binary_target: cargo_target_spec
1185 TargetKind::Bin | TargetKind::Example | TargetKind::Test
1189 annotate_runnables: lens_config.runnable(),
1190 annotate_impls: lens_config.implementations,
1191 annotate_references: lens_config.refs,
1192 annotate_method_references: lens_config.method_refs,
1197 let mut res = Vec::new();
1198 for a in annotations {
1199 to_proto::code_lens(&mut res, &snap, a)?;
1205 pub(crate) fn handle_code_lens_resolve(
1206 snap: GlobalStateSnapshot,
1207 code_lens: CodeLens,
1208 ) -> Result<CodeLens> {
1209 let annotation = from_proto::annotation(&snap, code_lens.clone())?;
1210 let annotation = snap.analysis.resolve_annotation(annotation)?;
1212 let mut acc = Vec::new();
1213 to_proto::code_lens(&mut acc, &snap, annotation)?;
1215 let res = match acc.pop() {
1216 Some(it) if acc.is_empty() => it,
1226 pub(crate) fn handle_document_highlight(
1227 snap: GlobalStateSnapshot,
1228 params: lsp_types::DocumentHighlightParams,
1229 ) -> Result<Option<Vec<lsp_types::DocumentHighlight>>> {
1230 let _p = profile::span("handle_document_highlight");
1231 let position = from_proto::file_position(&snap, params.text_document_position_params)?;
1232 let line_index = snap.file_line_index(position.file_id)?;
1234 let refs = match snap.analysis.highlight_related(snap.config.highlight_related(), position)? {
1235 None => return Ok(None),
1240 .map(|ide::HighlightedRange { range, category }| lsp_types::DocumentHighlight {
1241 range: to_proto::range(&line_index, range),
1242 kind: category.map(to_proto::document_highlight_kind),
1248 pub(crate) fn handle_ssr(
1249 snap: GlobalStateSnapshot,
1250 params: lsp_ext::SsrParams,
1251 ) -> Result<lsp_types::WorkspaceEdit> {
1252 let _p = profile::span("handle_ssr");
1253 let selections = params
1256 .map(|range| from_proto::file_range(&snap, params.position.text_document.clone(), *range))
1257 .collect::<Result<Vec<_>, _>>()?;
1258 let position = from_proto::file_position(&snap, params.position)?;
1259 let source_change = snap.analysis.structural_search_replace(
1265 to_proto::workspace_edit(&snap, source_change)
1268 pub(crate) fn publish_diagnostics(
1269 snap: &GlobalStateSnapshot,
1271 ) -> Result<Vec<Diagnostic>> {
1272 let _p = profile::span("publish_diagnostics");
1273 let line_index = snap.file_line_index(file_id)?;
1275 let diagnostics: Vec<Diagnostic> = snap
1277 .diagnostics(&snap.config.diagnostics(), AssistResolveStrategy::None, file_id)?
1279 .map(|d| Diagnostic {
1280 range: to_proto::range(&line_index, d.range),
1281 severity: Some(to_proto::diagnostic_severity(d.severity)),
1282 code: Some(NumberOrString::String(d.code.as_str().to_string())),
1283 code_description: Some(lsp_types::CodeDescription {
1284 href: lsp_types::Url::parse(&format!(
1285 "https://rust-analyzer.github.io/manual.html#{}",
1290 source: Some("rust-analyzer".to_string()),
1292 related_information: None,
1293 tags: if d.unused { Some(vec![DiagnosticTag::Unnecessary]) } else { None },
1300 pub(crate) fn handle_inlay_hints(
1301 snap: GlobalStateSnapshot,
1302 params: InlayHintsParams,
1303 ) -> Result<Vec<InlayHint>> {
1304 let _p = profile::span("handle_inlay_hints");
1305 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
1306 let line_index = snap.file_line_index(file_id)?;
1309 .inlay_hints(&snap.config.inlay_hints(), file_id)?
1311 .map(|it| to_proto::inlay_hint(&line_index, it))
1315 pub(crate) fn handle_call_hierarchy_prepare(
1316 snap: GlobalStateSnapshot,
1317 params: CallHierarchyPrepareParams,
1318 ) -> Result<Option<Vec<CallHierarchyItem>>> {
1319 let _p = profile::span("handle_call_hierarchy_prepare");
1320 let position = from_proto::file_position(&snap, params.text_document_position_params)?;
1322 let nav_info = match snap.analysis.call_hierarchy(position)? {
1323 None => return Ok(None),
1327 let RangeInfo { range: _, info: navs } = nav_info;
1330 .filter(|it| it.kind == Some(SymbolKind::Function))
1331 .map(|it| to_proto::call_hierarchy_item(&snap, it))
1332 .collect::<Result<Vec<_>>>()?;
1337 pub(crate) fn handle_call_hierarchy_incoming(
1338 snap: GlobalStateSnapshot,
1339 params: CallHierarchyIncomingCallsParams,
1340 ) -> Result<Option<Vec<CallHierarchyIncomingCall>>> {
1341 let _p = profile::span("handle_call_hierarchy_incoming");
1342 let item = params.item;
1344 let doc = TextDocumentIdentifier::new(item.uri);
1345 let frange = from_proto::file_range(&snap, doc, item.selection_range)?;
1346 let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
1348 let call_items = match snap.analysis.incoming_calls(fpos)? {
1349 None => return Ok(None),
1353 let mut res = vec![];
1355 for call_item in call_items.into_iter() {
1356 let file_id = call_item.target.file_id;
1357 let line_index = snap.file_line_index(file_id)?;
1358 let item = to_proto::call_hierarchy_item(&snap, call_item.target)?;
1359 res.push(CallHierarchyIncomingCall {
1361 from_ranges: call_item
1364 .map(|it| to_proto::range(&line_index, it))
1372 pub(crate) fn handle_call_hierarchy_outgoing(
1373 snap: GlobalStateSnapshot,
1374 params: CallHierarchyOutgoingCallsParams,
1375 ) -> Result<Option<Vec<CallHierarchyOutgoingCall>>> {
1376 let _p = profile::span("handle_call_hierarchy_outgoing");
1377 let item = params.item;
1379 let doc = TextDocumentIdentifier::new(item.uri);
1380 let frange = from_proto::file_range(&snap, doc, item.selection_range)?;
1381 let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
1383 let call_items = match snap.analysis.outgoing_calls(fpos)? {
1384 None => return Ok(None),
1388 let mut res = vec![];
1390 for call_item in call_items.into_iter() {
1391 let file_id = call_item.target.file_id;
1392 let line_index = snap.file_line_index(file_id)?;
1393 let item = to_proto::call_hierarchy_item(&snap, call_item.target)?;
1394 res.push(CallHierarchyOutgoingCall {
1396 from_ranges: call_item
1399 .map(|it| to_proto::range(&line_index, it))
1407 pub(crate) fn handle_semantic_tokens_full(
1408 snap: GlobalStateSnapshot,
1409 params: SemanticTokensParams,
1410 ) -> Result<Option<SemanticTokensResult>> {
1411 let _p = profile::span("handle_semantic_tokens_full");
1413 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
1414 let text = snap.analysis.file_text(file_id)?;
1415 let line_index = snap.file_line_index(file_id)?;
1417 let highlights = snap.analysis.highlight(file_id)?;
1418 let highlight_strings = snap.config.highlighting_strings();
1419 let semantic_tokens =
1420 to_proto::semantic_tokens(&text, &line_index, highlights, highlight_strings);
1422 // Unconditionally cache the tokens
1423 snap.semantic_tokens_cache.lock().insert(params.text_document.uri, semantic_tokens.clone());
1425 Ok(Some(semantic_tokens.into()))
1428 pub(crate) fn handle_semantic_tokens_full_delta(
1429 snap: GlobalStateSnapshot,
1430 params: SemanticTokensDeltaParams,
1431 ) -> Result<Option<SemanticTokensFullDeltaResult>> {
1432 let _p = profile::span("handle_semantic_tokens_full_delta");
1434 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
1435 let text = snap.analysis.file_text(file_id)?;
1436 let line_index = snap.file_line_index(file_id)?;
1438 let highlights = snap.analysis.highlight(file_id)?;
1439 let highlight_strings = snap.config.highlighting_strings();
1440 let semantic_tokens =
1441 to_proto::semantic_tokens(&text, &line_index, highlights, highlight_strings);
1443 let mut cache = snap.semantic_tokens_cache.lock();
1444 let cached_tokens = cache.entry(params.text_document.uri).or_default();
1446 if let Some(prev_id) = &cached_tokens.result_id {
1447 if *prev_id == params.previous_result_id {
1448 let delta = to_proto::semantic_token_delta(cached_tokens, &semantic_tokens);
1449 *cached_tokens = semantic_tokens;
1450 return Ok(Some(delta.into()));
1454 *cached_tokens = semantic_tokens.clone();
1456 Ok(Some(semantic_tokens.into()))
1459 pub(crate) fn handle_semantic_tokens_range(
1460 snap: GlobalStateSnapshot,
1461 params: SemanticTokensRangeParams,
1462 ) -> Result<Option<SemanticTokensRangeResult>> {
1463 let _p = profile::span("handle_semantic_tokens_range");
1465 let frange = from_proto::file_range(&snap, params.text_document, params.range)?;
1466 let text = snap.analysis.file_text(frange.file_id)?;
1467 let line_index = snap.file_line_index(frange.file_id)?;
1469 let highlights = snap.analysis.highlight_range(frange)?;
1470 let highlight_strings = snap.config.highlighting_strings();
1471 let semantic_tokens =
1472 to_proto::semantic_tokens(&text, &line_index, highlights, highlight_strings);
1473 Ok(Some(semantic_tokens.into()))
1476 pub(crate) fn handle_open_docs(
1477 snap: GlobalStateSnapshot,
1478 params: lsp_types::TextDocumentPositionParams,
1479 ) -> Result<Option<lsp_types::Url>> {
1480 let _p = profile::span("handle_open_docs");
1481 let position = from_proto::file_position(&snap, params)?;
1483 let remote = snap.analysis.external_docs(position)?;
1485 Ok(remote.and_then(|remote| Url::parse(&remote).ok()))
1488 pub(crate) fn handle_open_cargo_toml(
1489 snap: GlobalStateSnapshot,
1490 params: lsp_ext::OpenCargoTomlParams,
1491 ) -> Result<Option<lsp_types::GotoDefinitionResponse>> {
1492 let _p = profile::span("handle_open_cargo_toml");
1493 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
1495 let cargo_spec = match CargoTargetSpec::for_file(&snap, file_id)? {
1497 None => return Ok(None),
1500 let cargo_toml_url = to_proto::url_from_abs_path(&cargo_spec.cargo_toml);
1501 let res: lsp_types::GotoDefinitionResponse =
1502 Location::new(cargo_toml_url, Range::default()).into();
1506 pub(crate) fn handle_move_item(
1507 snap: GlobalStateSnapshot,
1508 params: lsp_ext::MoveItemParams,
1509 ) -> Result<Vec<lsp_ext::SnippetTextEdit>> {
1510 let _p = profile::span("handle_move_item");
1511 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
1512 let range = from_proto::file_range(&snap, params.text_document, params.range)?;
1514 let direction = match params.direction {
1515 lsp_ext::MoveItemDirection::Up => ide::Direction::Up,
1516 lsp_ext::MoveItemDirection::Down => ide::Direction::Down,
1519 match snap.analysis.move_item(range, direction)? {
1520 Some(text_edit) => {
1521 let line_index = snap.file_line_index(file_id)?;
1522 Ok(to_proto::snippet_text_edit_vec(&line_index, true, text_edit))
1528 fn to_command_link(command: lsp_types::Command, tooltip: String) -> lsp_ext::CommandLink {
1529 lsp_ext::CommandLink { tooltip: Some(tooltip), command }
1532 fn show_impl_command_link(
1533 snap: &GlobalStateSnapshot,
1534 position: &FilePosition,
1535 ) -> Option<lsp_ext::CommandLinkGroup> {
1536 if snap.config.hover_actions().implementations && snap.config.client_commands().show_reference {
1537 if let Some(nav_data) = snap.analysis.goto_implementation(*position).unwrap_or(None) {
1538 let uri = to_proto::url(snap, position.file_id);
1539 let line_index = snap.file_line_index(position.file_id).ok()?;
1540 let position = to_proto::position(&line_index, position.offset);
1541 let locations: Vec<_> = nav_data
1544 .filter_map(|nav| to_proto::location_from_nav(snap, nav).ok())
1546 let title = to_proto::implementation_title(locations.len());
1547 let command = to_proto::command::show_references(title, &uri, position, locations);
1549 return Some(lsp_ext::CommandLinkGroup {
1550 commands: vec![to_command_link(command, "Go to implementations".into())],
1551 ..Default::default()
1558 fn show_ref_command_link(
1559 snap: &GlobalStateSnapshot,
1560 position: &FilePosition,
1561 ) -> Option<lsp_ext::CommandLinkGroup> {
1562 if snap.config.hover_actions().references && snap.config.client_commands().show_reference {
1563 if let Some(ref_search_res) = snap.analysis.find_all_refs(*position, None).unwrap_or(None) {
1564 let uri = to_proto::url(snap, position.file_id);
1565 let line_index = snap.file_line_index(position.file_id).ok()?;
1566 let position = to_proto::position(&line_index, position.offset);
1567 let locations: Vec<_> = ref_search_res
1569 .flat_map(|res| res.references)
1570 .flat_map(|(file_id, ranges)| {
1571 ranges.into_iter().filter_map(move |(range, _)| {
1572 to_proto::location(snap, FileRange { file_id, range }).ok()
1576 let title = to_proto::reference_title(locations.len());
1577 let command = to_proto::command::show_references(title, &uri, position, locations);
1579 return Some(lsp_ext::CommandLinkGroup {
1580 commands: vec![to_command_link(command, "Go to references".into())],
1581 ..Default::default()
1588 fn runnable_action_links(
1589 snap: &GlobalStateSnapshot,
1591 ) -> Option<lsp_ext::CommandLinkGroup> {
1592 let hover_actions_config = snap.config.hover_actions();
1593 if !hover_actions_config.runnable() {
1597 let cargo_spec = CargoTargetSpec::for_file(snap, runnable.nav.file_id).ok()?;
1598 if should_skip_target(&runnable, cargo_spec.as_ref()) {
1602 let client_commands_config = snap.config.client_commands();
1603 if !(client_commands_config.run_single || client_commands_config.debug_single) {
1607 let title = runnable.title();
1608 let r = to_proto::runnable(snap, runnable).ok()?;
1610 let mut group = lsp_ext::CommandLinkGroup::default();
1612 if hover_actions_config.run && client_commands_config.run_single {
1613 let run_command = to_proto::command::run_single(&r, &title);
1614 group.commands.push(to_command_link(run_command, r.label.clone()));
1617 if hover_actions_config.debug && client_commands_config.debug_single {
1618 let dbg_command = to_proto::command::debug_single(&r);
1619 group.commands.push(to_command_link(dbg_command, r.label));
1625 fn goto_type_action_links(
1626 snap: &GlobalStateSnapshot,
1627 nav_targets: &[HoverGotoTypeData],
1628 ) -> Option<lsp_ext::CommandLinkGroup> {
1629 if !snap.config.hover_actions().goto_type_def
1630 || nav_targets.is_empty()
1631 || !snap.config.client_commands().goto_location
1636 Some(lsp_ext::CommandLinkGroup {
1637 title: Some("Go to ".into()),
1638 commands: nav_targets
1641 to_proto::command::goto_location(snap, &it.nav)
1642 .map(|cmd| to_command_link(cmd, it.mod_path.clone()))
1648 fn prepare_hover_actions(
1649 snap: &GlobalStateSnapshot,
1650 actions: &[HoverAction],
1651 ) -> Vec<lsp_ext::CommandLinkGroup> {
1654 .filter_map(|it| match it {
1655 HoverAction::Implementation(position) => show_impl_command_link(snap, position),
1656 HoverAction::Reference(position) => show_ref_command_link(snap, position),
1657 HoverAction::Runnable(r) => runnable_action_links(snap, r.clone()),
1658 HoverAction::GoToType(targets) => goto_type_action_links(snap, targets),
1663 fn should_skip_target(runnable: &Runnable, cargo_spec: Option<&CargoTargetSpec>) -> bool {
1664 match runnable.kind {
1665 RunnableKind::Bin => {
1666 // Do not suggest binary run on other target than binary
1668 Some(spec) => !matches!(
1670 TargetKind::Bin | TargetKind::Example | TargetKind::Test
1680 snap: &GlobalStateSnapshot,
1681 text_document: TextDocumentIdentifier,
1682 range: Option<lsp_types::Range>,
1683 ) -> Result<Option<Vec<lsp_types::TextEdit>>> {
1684 let file_id = from_proto::file_id(snap, &text_document.uri)?;
1685 let file = snap.analysis.file_text(file_id)?;
1686 let crate_ids = snap.analysis.crate_for(file_id)?;
1688 let line_index = snap.file_line_index(file_id)?;
1690 let mut rustfmt = match snap.config.rustfmt() {
1691 RustfmtConfig::Rustfmt { extra_args, enable_range_formatting } => {
1692 let mut cmd = process::Command::new(toolchain::rustfmt());
1693 cmd.args(extra_args);
1694 // try to chdir to the file so we can respect `rustfmt.toml`
1695 // FIXME: use `rustfmt --config-path` once
1696 // https://github.com/rust-lang/rustfmt/issues/4660 gets fixed
1697 match text_document.uri.to_file_path() {
1699 // pop off file name
1700 if path.pop() && path.is_dir() {
1701 cmd.current_dir(path);
1706 "Unable to get file path for {}, rustfmt.toml might be ignored",
1711 if let Some(&crate_id) = crate_ids.first() {
1712 // Assume all crates are in the same edition
1713 let edition = snap.analysis.crate_edition(crate_id)?;
1714 cmd.arg("--edition");
1715 cmd.arg(edition.to_string());
1718 if let Some(range) = range {
1719 if !enable_range_formatting {
1720 return Err(LspError::new(
1721 ErrorCode::InvalidRequest as i32,
1723 "rustfmt range formatting is unstable. \
1724 Opt-in by using a nightly build of rustfmt and setting \
1725 `rustfmt.enableRangeFormatting` to true in your LSP configuration",
1731 let frange = from_proto::file_range(snap, text_document, range)?;
1732 let start_line = line_index.index.line_col(frange.range.start()).line;
1733 let end_line = line_index.index.line_col(frange.range.end()).line;
1735 cmd.arg("--unstable-features");
1736 cmd.arg("--file-lines");
1740 "range": [start_line, end_line]
1748 RustfmtConfig::CustomCommand { command, args } => {
1749 let mut cmd = process::Command::new(command);
1755 let mut rustfmt = rustfmt
1756 .stdin(Stdio::piped())
1757 .stdout(Stdio::piped())
1758 .stderr(Stdio::piped())
1760 .context(format!("Failed to spawn {:?}", rustfmt))?;
1762 rustfmt.stdin.as_mut().unwrap().write_all(file.as_bytes())?;
1764 let output = rustfmt.wait_with_output()?;
1765 let captured_stdout = String::from_utf8(output.stdout)?;
1766 let captured_stderr = String::from_utf8(output.stderr).unwrap_or_default();
1768 if !output.status.success() {
1769 let rustfmt_not_installed =
1770 captured_stderr.contains("not installed") || captured_stderr.contains("not available");
1772 return match output.status.code() {
1773 Some(1) if !rustfmt_not_installed => {
1774 // While `rustfmt` doesn't have a specific exit code for parse errors this is the
1775 // likely cause exiting with 1. Most Language Servers swallow parse errors on
1776 // formatting because otherwise an error is surfaced to the user on top of the
1777 // syntax error diagnostics they're already receiving. This is especially jarring
1778 // if they have format on save enabled.
1779 tracing::info!("rustfmt exited with status 1, assuming parse error and ignoring");
1783 // Something else happened - e.g. `rustfmt` is missing or caught a signal
1787 r#"rustfmt exited with:
1791 output.status, captured_stdout, captured_stderr,
1799 let (new_text, new_line_endings) = LineEndings::normalize(captured_stdout);
1801 if line_index.endings != new_line_endings {
1802 // If line endings are different, send the entire file.
1803 // Diffing would not work here, as the line endings might be the only
1805 Ok(Some(to_proto::text_edit_vec(
1807 TextEdit::replace(TextRange::up_to(TextSize::of(&*file)), new_text),
1809 } else if *file == new_text {
1810 // The document is already formatted correctly -- no edits needed.
1813 Ok(Some(to_proto::text_edit_vec(&line_index, diff(&file, &new_text))))