1 //! This module is responsible for implementing handlers for Language Server
2 //! Protocol. The majority of requests are fulfilled by calling into the
6 io::{Read, Write as _},
7 process::{self, Command, Stdio},
11 AnnotationConfig, AssistKind, AssistResolveStrategy, FileId, FilePosition, FileRange,
12 HoverAction, HoverGotoTypeData, Query, RangeInfo, Runnable, RunnableKind, SingleResolve,
13 SourceChange, TextEdit,
15 use ide_db::SymbolKind;
16 use itertools::Itertools;
17 use lsp_server::ErrorCode;
19 CallHierarchyIncomingCall, CallHierarchyIncomingCallsParams, CallHierarchyItem,
20 CallHierarchyOutgoingCall, CallHierarchyOutgoingCallsParams, CallHierarchyPrepareParams,
21 CodeLens, CompletionItem, Diagnostic, DiagnosticTag, DocumentFormattingParams, FoldingRange,
22 FoldingRangeParams, HoverContents, Location, NumberOrString, Position, PrepareRenameResponse,
23 Range, RenameParams, SemanticTokensDeltaParams, SemanticTokensFullDeltaResult,
24 SemanticTokensParams, SemanticTokensRangeParams, SemanticTokensRangeResult,
25 SemanticTokensResult, SymbolInformation, SymbolTag, TextDocumentIdentifier,
26 TextDocumentPositionParams, Url, WorkspaceEdit,
28 use project_model::TargetKind;
29 use serde::{Deserialize, Serialize};
30 use serde_json::{json, to_value};
32 use syntax::{algo, ast, AstNode, TextRange, TextSize};
35 cargo_target_spec::CargoTargetSpec,
36 config::RustfmtConfig,
39 global_state::{GlobalState, GlobalStateSnapshot},
40 line_index::LineEndings,
41 lsp_ext::{self, InlayHint, InlayHintsParams, WorkspaceSymbolParams},
42 lsp_utils::all_edits_are_disjoint,
43 to_proto, LspError, Result,
46 pub(crate) fn handle_analyzer_status(
47 snap: GlobalStateSnapshot,
48 params: lsp_ext::AnalyzerStatusParams,
50 let _p = profile::span("handle_analyzer_status");
52 let mut buf = String::new();
54 let mut file_id = None;
55 if let Some(tdi) = params.text_document {
56 match from_proto::file_id(&snap, &tdi.uri) {
57 Ok(it) => file_id = Some(it),
58 Err(_) => format_to!(buf, "file {} not found in vfs", tdi.uri),
62 if snap.workspaces.is_empty() {
63 buf.push_str("no workspaces\n")
65 buf.push_str("workspaces:\n");
66 for w in snap.workspaces.iter() {
67 format_to!(buf, "{} packages loaded\n", w.n_packages());
70 buf.push_str("\nanalysis:\n");
75 .unwrap_or_else(|_| "Analysis retrieval was cancelled".to_owned()),
77 format_to!(buf, "\n\nrequests:\n");
78 let requests = snap.latest_requests.read();
79 for (is_last, r) in requests.iter() {
80 let mark = if is_last { "*" } else { " " };
81 format_to!(buf, "{}{:4} {:<36}{}ms\n", mark, r.id, r.method, r.duration.as_millis());
86 pub(crate) fn handle_memory_usage(state: &mut GlobalState, _: ()) -> Result<String> {
87 let _p = profile::span("handle_memory_usage");
88 let mut mem = state.analysis_host.per_query_memory_usage();
89 mem.push(("Remaining".into(), profile::memory_usage().allocated));
91 let mut out = String::new();
92 for (name, bytes) in mem {
93 format_to!(out, "{:>8} {}\n", bytes, name);
98 pub(crate) fn handle_syntax_tree(
99 snap: GlobalStateSnapshot,
100 params: lsp_ext::SyntaxTreeParams,
101 ) -> Result<String> {
102 let _p = profile::span("handle_syntax_tree");
103 let id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
104 let line_index = snap.file_line_index(id)?;
105 let text_range = params.range.map(|r| from_proto::text_range(&line_index, r));
106 let res = snap.analysis.syntax_tree(id, text_range)?;
110 pub(crate) fn handle_view_hir(
111 snap: GlobalStateSnapshot,
112 params: lsp_types::TextDocumentPositionParams,
113 ) -> Result<String> {
114 let _p = profile::span("handle_view_hir");
115 let position = from_proto::file_position(&snap, params)?;
116 let res = snap.analysis.view_hir(position)?;
120 pub(crate) fn handle_view_item_tree(
121 snap: GlobalStateSnapshot,
122 params: lsp_ext::ViewItemTreeParams,
123 ) -> Result<String> {
124 let _p = profile::span("handle_view_item_tree");
125 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
126 let res = snap.analysis.view_item_tree(file_id)?;
130 pub(crate) fn handle_view_crate_graph(snap: GlobalStateSnapshot, (): ()) -> Result<String> {
131 let _p = profile::span("handle_view_crate_graph");
132 let dot = snap.analysis.view_crate_graph()??;
134 // We shell out to `dot` to render to SVG, as there does not seem to be a pure-Rust renderer.
135 let child = Command::new("dot")
137 .stdin(Stdio::piped())
138 .stdout(Stdio::piped())
140 .map_err(|err| format!("failed to spawn `dot`: {}", err))?;
141 child.stdin.unwrap().write_all(dot.as_bytes())?;
143 let mut svg = String::new();
144 child.stdout.unwrap().read_to_string(&mut svg)?;
148 pub(crate) fn handle_expand_macro(
149 snap: GlobalStateSnapshot,
150 params: lsp_ext::ExpandMacroParams,
151 ) -> Result<Option<lsp_ext::ExpandedMacro>> {
152 let _p = profile::span("handle_expand_macro");
153 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
154 let line_index = snap.file_line_index(file_id)?;
155 let offset = from_proto::offset(&line_index, params.position);
157 let res = snap.analysis.expand_macro(FilePosition { file_id, offset })?;
158 Ok(res.map(|it| lsp_ext::ExpandedMacro { name: it.name, expansion: it.expansion }))
161 pub(crate) fn handle_selection_range(
162 snap: GlobalStateSnapshot,
163 params: lsp_types::SelectionRangeParams,
164 ) -> Result<Option<Vec<lsp_types::SelectionRange>>> {
165 let _p = profile::span("handle_selection_range");
166 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
167 let line_index = snap.file_line_index(file_id)?;
168 let res: Result<Vec<lsp_types::SelectionRange>> = params
172 let offset = from_proto::offset(&line_index, position);
173 let mut ranges = Vec::new();
175 let mut range = TextRange::new(offset, offset);
178 let frange = FileRange { file_id, range };
179 let next = snap.analysis.extend_selection(frange)?;
187 let mut range = lsp_types::SelectionRange {
188 range: to_proto::range(&line_index, *ranges.last().unwrap()),
191 for &r in ranges.iter().rev().skip(1) {
192 range = lsp_types::SelectionRange {
193 range: to_proto::range(&line_index, r),
194 parent: Some(Box::new(range)),
204 pub(crate) fn handle_matching_brace(
205 snap: GlobalStateSnapshot,
206 params: lsp_ext::MatchingBraceParams,
207 ) -> Result<Vec<Position>> {
208 let _p = profile::span("handle_matching_brace");
209 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
210 let line_index = snap.file_line_index(file_id)?;
215 let offset = from_proto::offset(&line_index, position);
216 let offset = match snap.analysis.matching_brace(FilePosition { file_id, offset }) {
217 Ok(Some(matching_brace_offset)) => matching_brace_offset,
218 Err(_) | Ok(None) => offset,
220 to_proto::position(&line_index, offset)
226 pub(crate) fn handle_join_lines(
227 snap: GlobalStateSnapshot,
228 params: lsp_ext::JoinLinesParams,
229 ) -> Result<Vec<lsp_types::TextEdit>> {
230 let _p = profile::span("handle_join_lines");
231 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
232 let line_index = snap.file_line_index(file_id)?;
233 let mut res = TextEdit::default();
234 for range in params.ranges {
235 let range = from_proto::text_range(&line_index, range);
236 let edit = snap.analysis.join_lines(FileRange { file_id, range })?;
237 match res.union(edit) {
240 // just ignore overlapping edits
244 let res = to_proto::text_edit_vec(&line_index, res);
248 pub(crate) fn handle_on_enter(
249 snap: GlobalStateSnapshot,
250 params: lsp_types::TextDocumentPositionParams,
251 ) -> Result<Option<Vec<lsp_ext::SnippetTextEdit>>> {
252 let _p = profile::span("handle_on_enter");
253 let position = from_proto::file_position(&snap, params)?;
254 let edit = match snap.analysis.on_enter(position)? {
255 None => return Ok(None),
258 let line_index = snap.file_line_index(position.file_id)?;
259 let edit = to_proto::snippet_text_edit_vec(&line_index, true, edit);
263 pub(crate) fn handle_on_type_formatting(
264 snap: GlobalStateSnapshot,
265 params: lsp_types::DocumentOnTypeFormattingParams,
266 ) -> Result<Option<Vec<lsp_types::TextEdit>>> {
267 let _p = profile::span("handle_on_type_formatting");
268 let mut position = from_proto::file_position(&snap, params.text_document_position)?;
269 let line_index = snap.file_line_index(position.file_id)?;
271 // in `ide`, the `on_type` invariant is that
272 // `text.char_at(position) == typed_char`.
273 position.offset -= TextSize::of('.');
274 let char_typed = params.ch.chars().next().unwrap_or('\0');
276 let text = snap.analysis.file_text(position.file_id)?;
277 text[usize::from(position.offset)..].starts_with(char_typed)
280 // We have an assist that inserts ` ` after typing `->` in `fn foo() ->{`,
281 // but it requires precise cursor positioning to work, and one can't
282 // position the cursor with on_type formatting. So, let's just toggle this
283 // feature off here, hoping that we'll enable it one day, 😿.
284 if char_typed == '>' {
288 let edit = snap.analysis.on_char_typed(position, char_typed)?;
289 let edit = match edit {
291 None => return Ok(None),
294 // This should be a single-file edit
295 let (_, edit) = edit.source_file_edits.into_iter().next().unwrap();
297 let change = to_proto::text_edit_vec(&line_index, edit);
301 pub(crate) fn handle_document_symbol(
302 snap: GlobalStateSnapshot,
303 params: lsp_types::DocumentSymbolParams,
304 ) -> Result<Option<lsp_types::DocumentSymbolResponse>> {
305 let _p = profile::span("handle_document_symbol");
306 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
307 let line_index = snap.file_line_index(file_id)?;
309 let mut parents: Vec<(lsp_types::DocumentSymbol, Option<usize>)> = Vec::new();
311 for symbol in snap.analysis.file_structure(file_id)? {
312 let mut tags = Vec::new();
313 if symbol.deprecated {
314 tags.push(SymbolTag::Deprecated)
318 let doc_symbol = lsp_types::DocumentSymbol {
320 detail: symbol.detail,
321 kind: to_proto::structure_node_kind(symbol.kind),
323 deprecated: Some(symbol.deprecated),
324 range: to_proto::range(&line_index, symbol.node_range),
325 selection_range: to_proto::range(&line_index, symbol.navigation_range),
328 parents.push((doc_symbol, symbol.parent));
331 // Builds hierarchy from a flat list, in reverse order (so that indices
333 let document_symbols = {
334 let mut acc = Vec::new();
335 while let Some((mut node, parent_idx)) = parents.pop() {
336 if let Some(children) = &mut node.children {
339 let parent = match parent_idx {
341 Some(i) => parents[i].0.children.get_or_insert_with(Vec::new),
349 let res = if snap.config.hierarchical_symbols() {
350 document_symbols.into()
352 let url = to_proto::url(&snap, file_id);
353 let mut symbol_information = Vec::<SymbolInformation>::new();
354 for symbol in document_symbols {
355 flatten_document_symbol(&symbol, None, &url, &mut symbol_information);
357 symbol_information.into()
359 return Ok(Some(res));
361 fn flatten_document_symbol(
362 symbol: &lsp_types::DocumentSymbol,
363 container_name: Option<String>,
365 res: &mut Vec<SymbolInformation>,
367 let mut tags = Vec::new();
370 match symbol.deprecated {
371 Some(true) => tags.push(SymbolTag::Deprecated),
376 res.push(SymbolInformation {
377 name: symbol.name.clone(),
380 deprecated: symbol.deprecated,
381 location: Location::new(url.clone(), symbol.range),
385 for child in symbol.children.iter().flatten() {
386 flatten_document_symbol(child, Some(symbol.name.clone()), url, res);
391 pub(crate) fn handle_workspace_symbol(
392 snap: GlobalStateSnapshot,
393 params: WorkspaceSymbolParams,
394 ) -> Result<Option<Vec<SymbolInformation>>> {
395 let _p = profile::span("handle_workspace_symbol");
397 let (all_symbols, libs) = decide_search_scope_and_kind(¶ms, &snap);
400 let query: String = params.query.chars().filter(|&c| c != '#' && c != '*').collect();
401 let mut q = Query::new(query);
411 let mut res = exec_query(&snap, query)?;
412 if res.is_empty() && !all_symbols {
413 let mut query = Query::new(params.query);
415 res = exec_query(&snap, query)?;
418 return Ok(Some(res));
420 fn decide_search_scope_and_kind(
421 params: &WorkspaceSymbolParams,
422 snap: &GlobalStateSnapshot,
424 // Support old-style parsing of markers in the query.
425 let mut all_symbols = params.query.contains('#');
426 let mut libs = params.query.contains('*');
428 let config = snap.config.workspace_symbol();
430 // If no explicit marker was set, check request params. If that's also empty
431 // use global config.
433 let search_kind = if let Some(ref search_kind) = params.search_kind {
438 all_symbols = match search_kind {
439 lsp_ext::WorkspaceSymbolSearchKind::OnlyTypes => false,
440 lsp_ext::WorkspaceSymbolSearchKind::AllSymbols => true,
445 let search_scope = if let Some(ref search_scope) = params.search_scope {
450 libs = match search_scope {
451 lsp_ext::WorkspaceSymbolSearchScope::Workspace => false,
452 lsp_ext::WorkspaceSymbolSearchScope::WorkspaceAndDependencies => true,
459 fn exec_query(snap: &GlobalStateSnapshot, query: Query) -> Result<Vec<SymbolInformation>> {
460 let mut res = Vec::new();
461 for nav in snap.analysis.symbol_search(query)? {
462 let container_name = nav.container_name.as_ref().map(|v| v.to_string());
465 let info = SymbolInformation {
466 name: nav.name.to_string(),
469 .map(to_proto::symbol_kind)
470 .unwrap_or(lsp_types::SymbolKind::Variable),
472 location: to_proto::location_from_nav(snap, nav)?,
482 pub(crate) fn handle_will_rename_files(
483 snap: GlobalStateSnapshot,
484 params: lsp_types::RenameFilesParams,
485 ) -> Result<Option<lsp_types::WorkspaceEdit>> {
486 let _p = profile::span("handle_will_rename_files");
488 let source_changes: Vec<SourceChange> = params
491 .filter_map(|file_rename| {
492 let from = Url::parse(&file_rename.old_uri).ok()?;
493 let to = Url::parse(&file_rename.new_uri).ok()?;
495 let from_path = from.to_file_path().ok()?;
496 let to_path = to.to_file_path().ok()?;
498 // Limit to single-level moves for now.
499 match (from_path.parent(), to_path.parent()) {
500 (Some(p1), Some(p2)) if p1 == p2 => {
501 if from_path.is_dir() {
502 // add '/' to end of url -- from `file://path/to/folder` to `file://path/to/folder/`
503 let mut old_folder_name = from_path.file_stem()?.to_str()?.to_string();
504 old_folder_name.push('/');
505 let from_with_trailing_slash = from.join(&old_folder_name).ok()?;
507 let imitate_from_url = from_with_trailing_slash.join("mod.rs").ok()?;
508 let new_file_name = to_path.file_name()?.to_str()?;
510 snap.url_to_file_id(&imitate_from_url).ok()?,
511 new_file_name.to_string(),
514 let old_name = from_path.file_stem()?.to_str()?;
515 let new_name = to_path.file_stem()?.to_str()?;
516 match (old_name, new_name) {
519 _ => Some((snap.url_to_file_id(&from).ok()?, new_name.to_string())),
526 .filter_map(|(file_id, new_name)| {
527 snap.analysis.will_rename_file(file_id, &new_name).ok()?
531 // Drop file system edits since we're just renaming things on the same level
532 let mut source_changes = source_changes.into_iter();
533 let mut source_change = source_changes.next().unwrap_or_default();
534 source_change.file_system_edits.clear();
535 // no collect here because we want to merge text edits on same file ids
536 source_change.extend(source_changes.map(|it| it.source_file_edits).flatten());
537 if source_change.source_file_edits.is_empty() {
540 to_proto::workspace_edit(&snap, source_change).map(Some)
544 pub(crate) fn handle_goto_definition(
545 snap: GlobalStateSnapshot,
546 params: lsp_types::GotoDefinitionParams,
547 ) -> Result<Option<lsp_types::GotoDefinitionResponse>> {
548 let _p = profile::span("handle_goto_definition");
549 let position = from_proto::file_position(&snap, params.text_document_position_params)?;
550 let nav_info = match snap.analysis.goto_definition(position)? {
551 None => return Ok(None),
554 let src = FileRange { file_id: position.file_id, range: nav_info.range };
555 let res = to_proto::goto_definition_response(&snap, Some(src), nav_info.info)?;
559 pub(crate) fn handle_goto_implementation(
560 snap: GlobalStateSnapshot,
561 params: lsp_types::request::GotoImplementationParams,
562 ) -> Result<Option<lsp_types::request::GotoImplementationResponse>> {
563 let _p = profile::span("handle_goto_implementation");
564 let position = from_proto::file_position(&snap, params.text_document_position_params)?;
565 let nav_info = match snap.analysis.goto_implementation(position)? {
566 None => return Ok(None),
569 let src = FileRange { file_id: position.file_id, range: nav_info.range };
570 let res = to_proto::goto_definition_response(&snap, Some(src), nav_info.info)?;
574 pub(crate) fn handle_goto_type_definition(
575 snap: GlobalStateSnapshot,
576 params: lsp_types::request::GotoTypeDefinitionParams,
577 ) -> Result<Option<lsp_types::request::GotoTypeDefinitionResponse>> {
578 let _p = profile::span("handle_goto_type_definition");
579 let position = from_proto::file_position(&snap, params.text_document_position_params)?;
580 let nav_info = match snap.analysis.goto_type_definition(position)? {
581 None => return Ok(None),
584 let src = FileRange { file_id: position.file_id, range: nav_info.range };
585 let res = to_proto::goto_definition_response(&snap, Some(src), nav_info.info)?;
589 pub(crate) fn handle_parent_module(
590 snap: GlobalStateSnapshot,
591 params: lsp_types::TextDocumentPositionParams,
592 ) -> Result<Option<lsp_types::GotoDefinitionResponse>> {
593 let _p = profile::span("handle_parent_module");
594 let position = from_proto::file_position(&snap, params)?;
595 let navs = snap.analysis.parent_module(position)?;
596 let res = to_proto::goto_definition_response(&snap, None, navs)?;
600 pub(crate) fn handle_runnables(
601 snap: GlobalStateSnapshot,
602 params: lsp_ext::RunnablesParams,
603 ) -> Result<Vec<lsp_ext::Runnable>> {
604 let _p = profile::span("handle_runnables");
605 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
606 let line_index = snap.file_line_index(file_id)?;
607 let offset = params.position.map(|it| from_proto::offset(&line_index, it));
608 let cargo_spec = CargoTargetSpec::for_file(&snap, file_id)?;
610 let expect_test = match offset {
612 let source_file = snap.analysis.parse(file_id)?;
613 algo::find_node_at_offset::<ast::MacroCall>(source_file.syntax(), offset)
614 .and_then(|it| it.path()?.segment()?.name_ref())
615 .map_or(false, |it| it.text() == "expect" || it.text() == "expect_file")
620 let mut res = Vec::new();
621 for runnable in snap.analysis.runnables(file_id)? {
622 if let Some(offset) = offset {
623 if !runnable.nav.full_range.contains_inclusive(offset) {
627 if should_skip_target(&runnable, cargo_spec.as_ref()) {
630 let mut runnable = to_proto::runnable(&snap, runnable)?;
632 runnable.label = format!("{} + expect", runnable.label);
633 runnable.args.expect_test = Some(true);
638 // Add `cargo check` and `cargo test` for all targets of the whole package
639 let config = snap.config.runnables();
642 for &cmd in ["check", "test"].iter() {
643 res.push(lsp_ext::Runnable {
644 label: format!("cargo {} -p {} --all-targets", cmd, spec.package),
646 kind: lsp_ext::RunnableKind::Cargo,
647 args: lsp_ext::CargoRunnable {
648 workspace_root: Some(spec.workspace_root.clone().into()),
649 override_cargo: config.override_cargo.clone(),
652 "--package".to_string(),
653 spec.package.clone(),
654 "--all-targets".to_string(),
656 cargo_extra_args: config.cargo_extra_args.clone(),
657 executable_args: Vec::new(),
664 if !snap.config.linked_projects().is_empty()
669 .map(|projects| projects.is_empty())
672 res.push(lsp_ext::Runnable {
673 label: "cargo check --workspace".to_string(),
675 kind: lsp_ext::RunnableKind::Cargo,
676 args: lsp_ext::CargoRunnable {
677 workspace_root: None,
678 override_cargo: config.override_cargo,
679 cargo_args: vec!["check".to_string(), "--workspace".to_string()],
680 cargo_extra_args: config.cargo_extra_args,
681 executable_args: Vec::new(),
691 pub(crate) fn handle_related_tests(
692 snap: GlobalStateSnapshot,
693 params: lsp_types::TextDocumentPositionParams,
694 ) -> Result<Vec<lsp_ext::TestInfo>> {
695 let _p = profile::span("handle_related_tests");
696 let position = from_proto::file_position(&snap, params)?;
698 let tests = snap.analysis.related_tests(position, None)?;
699 let mut res = Vec::new();
701 if let Ok(runnable) = to_proto::runnable(&snap, it) {
702 res.push(lsp_ext::TestInfo { runnable })
709 pub(crate) fn handle_completion(
710 snap: GlobalStateSnapshot,
711 params: lsp_types::CompletionParams,
712 ) -> Result<Option<lsp_types::CompletionResponse>> {
713 let _p = profile::span("handle_completion");
714 let text_document_position = params.text_document_position.clone();
715 let position = from_proto::file_position(&snap, params.text_document_position)?;
716 let completion_triggered_after_single_colon = {
718 if let Some(ctx) = params.context {
719 if ctx.trigger_character.unwrap_or_default() == ":" {
720 let source_file = snap.analysis.parse(position.file_id)?;
721 let syntax = source_file.syntax();
722 let text = syntax.text();
723 if let Some(next_char) = text.char_at(position.offset) {
724 let diff = TextSize::of(next_char) + TextSize::of(':');
725 let prev_char = position.offset - diff;
726 if text.char_at(prev_char) != Some(':') {
734 if completion_triggered_after_single_colon {
738 let completion_config = &snap.config.completion();
739 let items = match snap.analysis.completions(completion_config, position)? {
740 None => return Ok(None),
741 Some(items) => items,
743 let line_index = snap.file_line_index(position.file_id)?;
745 let insert_replace_support =
746 snap.config.insert_replace_support().then(|| text_document_position.position);
747 let items: Vec<CompletionItem> = items
750 let mut new_completion_items =
751 to_proto::completion_item(insert_replace_support, &line_index, item.clone());
753 if completion_config.enable_imports_on_the_fly {
754 for new_item in &mut new_completion_items {
755 fill_resolve_data(&mut new_item.data, &item, &text_document_position);
763 let completion_list = lsp_types::CompletionList { is_incomplete: true, items };
764 Ok(Some(completion_list.into()))
767 pub(crate) fn handle_completion_resolve(
768 snap: GlobalStateSnapshot,
769 mut original_completion: CompletionItem,
770 ) -> Result<CompletionItem> {
771 let _p = profile::span("handle_completion_resolve");
773 if !all_edits_are_disjoint(&original_completion, &[]) {
774 return Err(LspError::new(
775 ErrorCode::InvalidParams as i32,
776 "Received a completion with overlapping edits, this is not LSP-compliant".into(),
781 let resolve_data = match original_completion
784 .map(serde_json::from_value::<CompletionResolveData>)
788 None => return Ok(original_completion),
791 let file_id = from_proto::file_id(&snap, &resolve_data.position.text_document.uri)?;
792 let line_index = snap.file_line_index(file_id)?;
793 let offset = from_proto::offset(&line_index, resolve_data.position.position);
795 let additional_edits = snap
797 .resolve_completion_edits(
798 &snap.config.completion(),
799 FilePosition { file_id, offset },
800 &resolve_data.full_import_path,
801 resolve_data.imported_name,
804 .flat_map(|edit| edit.into_iter().map(|indel| to_proto::text_edit(&line_index, indel)))
807 if !all_edits_are_disjoint(&original_completion, &additional_edits) {
808 return Err(LspError::new(
809 ErrorCode::InternalError as i32,
810 "Import edit overlaps with the original completion edits, this is not LSP-compliant"
816 if let Some(original_additional_edits) = original_completion.additional_text_edits.as_mut() {
817 original_additional_edits.extend(additional_edits.into_iter())
819 original_completion.additional_text_edits = Some(additional_edits);
822 Ok(original_completion)
825 pub(crate) fn handle_folding_range(
826 snap: GlobalStateSnapshot,
827 params: FoldingRangeParams,
828 ) -> Result<Option<Vec<FoldingRange>>> {
829 let _p = profile::span("handle_folding_range");
830 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
831 let folds = snap.analysis.folding_ranges(file_id)?;
832 let text = snap.analysis.file_text(file_id)?;
833 let line_index = snap.file_line_index(file_id)?;
834 let line_folding_only = snap.config.line_folding_only();
837 .map(|it| to_proto::folding_range(&*text, &line_index, line_folding_only, it))
842 pub(crate) fn handle_signature_help(
843 snap: GlobalStateSnapshot,
844 params: lsp_types::SignatureHelpParams,
845 ) -> Result<Option<lsp_types::SignatureHelp>> {
846 let _p = profile::span("handle_signature_help");
847 let position = from_proto::file_position(&snap, params.text_document_position_params)?;
848 let call_info = match snap.analysis.call_info(position)? {
850 None => return Ok(None),
852 let concise = !snap.config.call_info_full();
854 to_proto::signature_help(call_info, concise, snap.config.signature_help_label_offsets());
858 pub(crate) fn handle_hover(
859 snap: GlobalStateSnapshot,
860 params: lsp_types::HoverParams,
861 ) -> Result<Option<lsp_ext::Hover>> {
862 let _p = profile::span("handle_hover");
863 let position = from_proto::file_position(&snap, params.text_document_position_params)?;
864 let info = match snap.analysis.hover(position, &snap.config.hover())? {
865 None => return Ok(None),
868 let line_index = snap.file_line_index(position.file_id)?;
869 let range = to_proto::range(&line_index, info.range);
870 let hover = lsp_ext::Hover {
871 hover: lsp_types::Hover {
872 contents: HoverContents::Markup(to_proto::markup_content(info.info.markup)),
875 actions: prepare_hover_actions(&snap, &info.info.actions),
881 pub(crate) fn handle_prepare_rename(
882 snap: GlobalStateSnapshot,
883 params: lsp_types::TextDocumentPositionParams,
884 ) -> Result<Option<PrepareRenameResponse>> {
885 let _p = profile::span("handle_prepare_rename");
886 let position = from_proto::file_position(&snap, params)?;
888 let change = snap.analysis.prepare_rename(position)?.map_err(to_proto::rename_error)?;
890 let line_index = snap.file_line_index(position.file_id)?;
891 let range = to_proto::range(&line_index, change.range);
892 Ok(Some(PrepareRenameResponse::Range(range)))
895 pub(crate) fn handle_rename(
896 snap: GlobalStateSnapshot,
897 params: RenameParams,
898 ) -> Result<Option<WorkspaceEdit>> {
899 let _p = profile::span("handle_rename");
900 let position = from_proto::file_position(&snap, params.text_document_position)?;
903 snap.analysis.rename(position, &*params.new_name)?.map_err(to_proto::rename_error)?;
905 // this is kind of a hack to prevent double edits from happening when moving files
906 // When a module gets renamed by renaming the mod declaration this causes the file to move
907 // which in turn will trigger a WillRenameFiles request to the server for which we reply with a
908 // a second identical set of renames, the client will then apply both edits causing incorrect edits
909 // with this we only emit source_file_edits in the WillRenameFiles response which will do the rename instead
910 // See https://github.com/microsoft/vscode-languageserver-node/issues/752 for more info
911 if !change.file_system_edits.is_empty() && snap.config.will_rename() {
912 change.source_file_edits.clear();
914 let workspace_edit = to_proto::workspace_edit(&snap, change)?;
915 Ok(Some(workspace_edit))
918 pub(crate) fn handle_references(
919 snap: GlobalStateSnapshot,
920 params: lsp_types::ReferenceParams,
921 ) -> Result<Option<Vec<Location>>> {
922 let _p = profile::span("handle_references");
923 let position = from_proto::file_position(&snap, params.text_document_position)?;
925 let refs = match snap.analysis.find_all_refs(position, None)? {
926 None => return Ok(None),
930 let decl = if params.context.include_declaration {
931 refs.declaration.map(|decl| FileRange {
932 file_id: decl.nav.file_id,
933 range: decl.nav.focus_or_full_range(),
941 .flat_map(|(file_id, refs)| {
942 refs.into_iter().map(move |(range, _)| FileRange { file_id, range })
945 .filter_map(|frange| to_proto::location(&snap, frange).ok())
951 pub(crate) fn handle_formatting(
952 snap: GlobalStateSnapshot,
953 params: DocumentFormattingParams,
954 ) -> Result<Option<Vec<lsp_types::TextEdit>>> {
955 let _p = profile::span("handle_formatting");
957 run_rustfmt(&snap, params.text_document, None)
960 pub(crate) fn handle_range_formatting(
961 snap: GlobalStateSnapshot,
962 params: lsp_types::DocumentRangeFormattingParams,
963 ) -> Result<Option<Vec<lsp_types::TextEdit>>> {
964 let _p = profile::span("handle_range_formatting");
966 run_rustfmt(&snap, params.text_document, Some(params.range))
969 pub(crate) fn handle_code_action(
970 snap: GlobalStateSnapshot,
971 params: lsp_types::CodeActionParams,
972 ) -> Result<Option<Vec<lsp_ext::CodeAction>>> {
973 let _p = profile::span("handle_code_action");
975 if !snap.config.code_action_literals() {
976 // We intentionally don't support command-based actions, as those either
977 // require either custom client-code or server-initiated edits. Server
978 // initiated edits break causality, so we avoid those.
983 snap.file_line_index(from_proto::file_id(&snap, ¶ms.text_document.uri)?)?;
984 let frange = from_proto::file_range(&snap, params.text_document.clone(), params.range)?;
986 let mut assists_config = snap.config.assist();
987 assists_config.allowed = params
991 .map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect());
993 let mut res: Vec<lsp_ext::CodeAction> = Vec::new();
995 let code_action_resolve_cap = snap.config.code_action_resolve();
996 let resolve = if code_action_resolve_cap {
997 AssistResolveStrategy::None
999 AssistResolveStrategy::All
1001 let assists = snap.analysis.assists_with_fixes(
1003 &snap.config.diagnostics(),
1007 for (index, assist) in assists.into_iter().enumerate() {
1009 if code_action_resolve_cap { Some((index, params.clone())) } else { None };
1010 let code_action = to_proto::code_action(&snap, assist, resolve_data)?;
1011 res.push(code_action)
1014 // Fixes from `cargo check`.
1015 for fix in snap.check_fixes.get(&frange.file_id).into_iter().flatten() {
1016 // FIXME: this mapping is awkward and shouldn't exist. Refactor
1017 // `snap.check_fixes` to not convert to LSP prematurely.
1018 let fix_range = from_proto::text_range(&line_index, fix.range);
1019 if fix_range.intersect(frange.range).is_some() {
1020 res.push(fix.action.clone());
1027 pub(crate) fn handle_code_action_resolve(
1028 snap: GlobalStateSnapshot,
1029 mut code_action: lsp_ext::CodeAction,
1030 ) -> Result<lsp_ext::CodeAction> {
1031 let _p = profile::span("handle_code_action_resolve");
1032 let params = match code_action.data.take() {
1034 None => Err("can't resolve code action without data")?,
1037 let file_id = from_proto::file_id(&snap, ¶ms.code_action_params.text_document.uri)?;
1038 let line_index = snap.file_line_index(file_id)?;
1039 let range = from_proto::text_range(&line_index, params.code_action_params.range);
1040 let frange = FileRange { file_id, range };
1042 let mut assists_config = snap.config.assist();
1043 assists_config.allowed = params
1047 .map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect());
1049 let (assist_index, assist_resolve) = match parse_action_id(¶ms.id) {
1050 Ok(parsed_data) => parsed_data,
1052 return Err(LspError::new(
1053 ErrorCode::InvalidParams as i32,
1054 format!("Failed to parse action id string '{}': {}", params.id, e),
1060 let expected_assist_id = assist_resolve.assist_id.clone();
1061 let expected_kind = assist_resolve.assist_kind;
1063 let assists = snap.analysis.assists_with_fixes(
1065 &snap.config.diagnostics(),
1066 AssistResolveStrategy::Single(assist_resolve),
1070 let assist = match assists.get(assist_index) {
1071 Some(assist) => assist,
1072 None => return Err(LspError::new(
1073 ErrorCode::InvalidParams as i32,
1075 "Failed to find the assist for index {} provided by the resolve request. Resolve request assist id: {}",
1076 assist_index, params.id,
1081 if assist.id.0 != expected_assist_id || assist.id.1 != expected_kind {
1082 return Err(LspError::new(
1083 ErrorCode::InvalidParams as i32,
1085 "Mismatching assist at index {} for the resolve parameters given. Resolve request assist id: {}, actual id: {:?}.",
1086 assist_index, params.id, assist.id
1091 let edit = to_proto::code_action(&snap, assist.clone(), None)?.edit;
1092 code_action.edit = edit;
1096 fn parse_action_id(action_id: &str) -> Result<(usize, SingleResolve), String> {
1097 let id_parts = action_id.split(':').collect_vec();
1098 match id_parts.as_slice() {
1099 &[assist_id_string, assist_kind_string, index_string] => {
1100 let assist_kind: AssistKind = assist_kind_string.parse()?;
1101 let index: usize = match index_string.parse() {
1103 Err(e) => return Err(format!("Incorrect index string: {}", e)),
1105 Ok((index, SingleResolve { assist_id: assist_id_string.to_string(), assist_kind }))
1107 _ => Err("Action id contains incorrect number of segments".to_string()),
1111 pub(crate) fn handle_code_lens(
1112 snap: GlobalStateSnapshot,
1113 params: lsp_types::CodeLensParams,
1114 ) -> Result<Option<Vec<CodeLens>>> {
1115 let _p = profile::span("handle_code_lens");
1117 let lens_config = snap.config.lens();
1118 if lens_config.none() {
1119 // early return before any db query!
1120 return Ok(Some(Vec::default()));
1123 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
1124 let cargo_target_spec = CargoTargetSpec::for_file(&snap, file_id)?;
1131 binary_target: cargo_target_spec
1135 TargetKind::Bin | TargetKind::Example | TargetKind::Test
1139 annotate_runnables: lens_config.runnable(),
1140 annotate_impls: lens_config.implementations,
1141 annotate_references: lens_config.refs,
1142 annotate_method_references: lens_config.method_refs,
1143 run: lens_config.run,
1144 debug: lens_config.debug,
1148 .map(|annotation| to_proto::code_lens(&snap, annotation).unwrap())
1154 pub(crate) fn handle_code_lens_resolve(
1155 snap: GlobalStateSnapshot,
1156 code_lens: CodeLens,
1157 ) -> Result<CodeLens> {
1158 let annotation = from_proto::annotation(&snap, code_lens)?;
1160 to_proto::code_lens(&snap, snap.analysis.resolve_annotation(annotation)?)
1163 pub(crate) fn handle_document_highlight(
1164 snap: GlobalStateSnapshot,
1165 params: lsp_types::DocumentHighlightParams,
1166 ) -> Result<Option<Vec<lsp_types::DocumentHighlight>>> {
1167 let _p = profile::span("handle_document_highlight");
1168 let position = from_proto::file_position(&snap, params.text_document_position_params)?;
1169 let line_index = snap.file_line_index(position.file_id)?;
1171 let refs = match snap.analysis.highlight_related(position)? {
1172 None => return Ok(None),
1177 .map(|ide::DocumentHighlight { range, access }| lsp_types::DocumentHighlight {
1178 range: to_proto::range(&line_index, range),
1179 kind: access.map(to_proto::document_highlight_kind),
1185 pub(crate) fn handle_ssr(
1186 snap: GlobalStateSnapshot,
1187 params: lsp_ext::SsrParams,
1188 ) -> Result<lsp_types::WorkspaceEdit> {
1189 let _p = profile::span("handle_ssr");
1190 let selections = params
1193 .map(|range| from_proto::file_range(&snap, params.position.text_document.clone(), *range))
1194 .collect::<Result<Vec<_>, _>>()?;
1195 let position = from_proto::file_position(&snap, params.position)?;
1196 let source_change = snap.analysis.structural_search_replace(
1202 to_proto::workspace_edit(&snap, source_change)
1205 pub(crate) fn publish_diagnostics(
1206 snap: &GlobalStateSnapshot,
1208 ) -> Result<Vec<Diagnostic>> {
1209 let _p = profile::span("publish_diagnostics");
1210 let line_index = snap.file_line_index(file_id)?;
1212 let diagnostics: Vec<Diagnostic> = snap
1214 .diagnostics(&snap.config.diagnostics(), AssistResolveStrategy::None, file_id)?
1216 .map(|d| Diagnostic {
1217 range: to_proto::range(&line_index, d.range),
1218 severity: Some(to_proto::diagnostic_severity(d.severity)),
1219 code: Some(NumberOrString::String(d.code.as_str().to_string())),
1220 code_description: Some(lsp_types::CodeDescription {
1221 href: lsp_types::Url::parse(&format!(
1222 "https://rust-analyzer.github.io/manual.html#{}",
1227 source: Some("rust-analyzer".to_string()),
1229 related_information: None,
1230 tags: if d.unused { Some(vec![DiagnosticTag::Unnecessary]) } else { None },
1237 pub(crate) fn handle_inlay_hints(
1238 snap: GlobalStateSnapshot,
1239 params: InlayHintsParams,
1240 ) -> Result<Vec<InlayHint>> {
1241 let _p = profile::span("handle_inlay_hints");
1242 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
1243 let line_index = snap.file_line_index(file_id)?;
1246 .inlay_hints(file_id, &snap.config.inlay_hints())?
1248 .map(|it| to_proto::inlay_hint(&line_index, it))
1252 pub(crate) fn handle_call_hierarchy_prepare(
1253 snap: GlobalStateSnapshot,
1254 params: CallHierarchyPrepareParams,
1255 ) -> Result<Option<Vec<CallHierarchyItem>>> {
1256 let _p = profile::span("handle_call_hierarchy_prepare");
1257 let position = from_proto::file_position(&snap, params.text_document_position_params)?;
1259 let nav_info = match snap.analysis.call_hierarchy(position)? {
1260 None => return Ok(None),
1264 let RangeInfo { range: _, info: navs } = nav_info;
1267 .filter(|it| it.kind == Some(SymbolKind::Function))
1268 .map(|it| to_proto::call_hierarchy_item(&snap, it))
1269 .collect::<Result<Vec<_>>>()?;
1274 pub(crate) fn handle_call_hierarchy_incoming(
1275 snap: GlobalStateSnapshot,
1276 params: CallHierarchyIncomingCallsParams,
1277 ) -> Result<Option<Vec<CallHierarchyIncomingCall>>> {
1278 let _p = profile::span("handle_call_hierarchy_incoming");
1279 let item = params.item;
1281 let doc = TextDocumentIdentifier::new(item.uri);
1282 let frange = from_proto::file_range(&snap, doc, item.selection_range)?;
1283 let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
1285 let call_items = match snap.analysis.incoming_calls(fpos)? {
1286 None => return Ok(None),
1290 let mut res = vec![];
1292 for call_item in call_items.into_iter() {
1293 let file_id = call_item.target.file_id;
1294 let line_index = snap.file_line_index(file_id)?;
1295 let item = to_proto::call_hierarchy_item(&snap, call_item.target)?;
1296 res.push(CallHierarchyIncomingCall {
1298 from_ranges: call_item
1301 .map(|it| to_proto::range(&line_index, it))
1309 pub(crate) fn handle_call_hierarchy_outgoing(
1310 snap: GlobalStateSnapshot,
1311 params: CallHierarchyOutgoingCallsParams,
1312 ) -> Result<Option<Vec<CallHierarchyOutgoingCall>>> {
1313 let _p = profile::span("handle_call_hierarchy_outgoing");
1314 let item = params.item;
1316 let doc = TextDocumentIdentifier::new(item.uri);
1317 let frange = from_proto::file_range(&snap, doc, item.selection_range)?;
1318 let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
1320 let call_items = match snap.analysis.outgoing_calls(fpos)? {
1321 None => return Ok(None),
1325 let mut res = vec![];
1327 for call_item in call_items.into_iter() {
1328 let file_id = call_item.target.file_id;
1329 let line_index = snap.file_line_index(file_id)?;
1330 let item = to_proto::call_hierarchy_item(&snap, call_item.target)?;
1331 res.push(CallHierarchyOutgoingCall {
1333 from_ranges: call_item
1336 .map(|it| to_proto::range(&line_index, it))
1344 pub(crate) fn handle_semantic_tokens_full(
1345 snap: GlobalStateSnapshot,
1346 params: SemanticTokensParams,
1347 ) -> Result<Option<SemanticTokensResult>> {
1348 let _p = profile::span("handle_semantic_tokens_full");
1350 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
1351 let text = snap.analysis.file_text(file_id)?;
1352 let line_index = snap.file_line_index(file_id)?;
1354 let highlights = snap.analysis.highlight(file_id)?;
1355 let highlight_strings = snap.config.highlighting_strings();
1356 let semantic_tokens =
1357 to_proto::semantic_tokens(&text, &line_index, highlights, highlight_strings);
1359 // Unconditionally cache the tokens
1360 snap.semantic_tokens_cache.lock().insert(params.text_document.uri, semantic_tokens.clone());
1362 Ok(Some(semantic_tokens.into()))
1365 pub(crate) fn handle_semantic_tokens_full_delta(
1366 snap: GlobalStateSnapshot,
1367 params: SemanticTokensDeltaParams,
1368 ) -> Result<Option<SemanticTokensFullDeltaResult>> {
1369 let _p = profile::span("handle_semantic_tokens_full_delta");
1371 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
1372 let text = snap.analysis.file_text(file_id)?;
1373 let line_index = snap.file_line_index(file_id)?;
1375 let highlights = snap.analysis.highlight(file_id)?;
1376 let highlight_strings = snap.config.highlighting_strings();
1377 let semantic_tokens =
1378 to_proto::semantic_tokens(&text, &line_index, highlights, highlight_strings);
1380 let mut cache = snap.semantic_tokens_cache.lock();
1381 let cached_tokens = cache.entry(params.text_document.uri).or_default();
1383 if let Some(prev_id) = &cached_tokens.result_id {
1384 if *prev_id == params.previous_result_id {
1385 let delta = to_proto::semantic_token_delta(cached_tokens, &semantic_tokens);
1386 *cached_tokens = semantic_tokens;
1387 return Ok(Some(delta.into()));
1391 *cached_tokens = semantic_tokens.clone();
1393 Ok(Some(semantic_tokens.into()))
1396 pub(crate) fn handle_semantic_tokens_range(
1397 snap: GlobalStateSnapshot,
1398 params: SemanticTokensRangeParams,
1399 ) -> Result<Option<SemanticTokensRangeResult>> {
1400 let _p = profile::span("handle_semantic_tokens_range");
1402 let frange = from_proto::file_range(&snap, params.text_document, params.range)?;
1403 let text = snap.analysis.file_text(frange.file_id)?;
1404 let line_index = snap.file_line_index(frange.file_id)?;
1406 let highlights = snap.analysis.highlight_range(frange)?;
1407 let highlight_strings = snap.config.highlighting_strings();
1408 let semantic_tokens =
1409 to_proto::semantic_tokens(&text, &line_index, highlights, highlight_strings);
1410 Ok(Some(semantic_tokens.into()))
1413 pub(crate) fn handle_open_docs(
1414 snap: GlobalStateSnapshot,
1415 params: lsp_types::TextDocumentPositionParams,
1416 ) -> Result<Option<lsp_types::Url>> {
1417 let _p = profile::span("handle_open_docs");
1418 let position = from_proto::file_position(&snap, params)?;
1420 let remote = snap.analysis.external_docs(position)?;
1422 Ok(remote.and_then(|remote| Url::parse(&remote).ok()))
1425 pub(crate) fn handle_open_cargo_toml(
1426 snap: GlobalStateSnapshot,
1427 params: lsp_ext::OpenCargoTomlParams,
1428 ) -> Result<Option<lsp_types::GotoDefinitionResponse>> {
1429 let _p = profile::span("handle_open_cargo_toml");
1430 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
1432 let cargo_spec = match CargoTargetSpec::for_file(&snap, file_id)? {
1434 None => return Ok(None),
1437 let cargo_toml_url = to_proto::url_from_abs_path(&cargo_spec.cargo_toml);
1438 let res: lsp_types::GotoDefinitionResponse =
1439 Location::new(cargo_toml_url, Range::default()).into();
1443 pub(crate) fn handle_move_item(
1444 snap: GlobalStateSnapshot,
1445 params: lsp_ext::MoveItemParams,
1446 ) -> Result<Vec<lsp_ext::SnippetTextEdit>> {
1447 let _p = profile::span("handle_move_item");
1448 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
1449 let range = from_proto::file_range(&snap, params.text_document, params.range)?;
1451 let direction = match params.direction {
1452 lsp_ext::MoveItemDirection::Up => ide::Direction::Up,
1453 lsp_ext::MoveItemDirection::Down => ide::Direction::Down,
1456 match snap.analysis.move_item(range, direction)? {
1457 Some(text_edit) => {
1458 let line_index = snap.file_line_index(file_id)?;
1459 Ok(to_proto::snippet_text_edit_vec(&line_index, true, text_edit))
1465 fn to_command_link(command: lsp_types::Command, tooltip: String) -> lsp_ext::CommandLink {
1466 lsp_ext::CommandLink { tooltip: Some(tooltip), command }
1469 fn show_impl_command_link(
1470 snap: &GlobalStateSnapshot,
1471 position: &FilePosition,
1472 ) -> Option<lsp_ext::CommandLinkGroup> {
1473 if snap.config.hover_actions().implementations {
1474 if let Some(nav_data) = snap.analysis.goto_implementation(*position).unwrap_or(None) {
1475 let uri = to_proto::url(snap, position.file_id);
1476 let line_index = snap.file_line_index(position.file_id).ok()?;
1477 let position = to_proto::position(&line_index, position.offset);
1478 let locations: Vec<_> = nav_data
1481 .filter_map(|nav| to_proto::location_from_nav(snap, nav).ok())
1483 let title = to_proto::implementation_title(locations.len());
1484 let command = to_proto::command::show_references(title, &uri, position, locations);
1486 return Some(lsp_ext::CommandLinkGroup {
1487 commands: vec![to_command_link(command, "Go to implementations".into())],
1488 ..Default::default()
1495 fn show_ref_command_link(
1496 snap: &GlobalStateSnapshot,
1497 position: &FilePosition,
1498 ) -> Option<lsp_ext::CommandLinkGroup> {
1499 if snap.config.hover_actions().references {
1500 if let Some(ref_search_res) = snap.analysis.find_all_refs(*position, None).unwrap_or(None) {
1501 let uri = to_proto::url(snap, position.file_id);
1502 let line_index = snap.file_line_index(position.file_id).ok()?;
1503 let position = to_proto::position(&line_index, position.offset);
1504 let locations: Vec<_> = ref_search_res
1507 .flat_map(|(file_id, ranges)| {
1508 ranges.into_iter().filter_map(move |(range, _)| {
1509 to_proto::location(snap, FileRange { file_id, range }).ok()
1513 let title = to_proto::reference_title(locations.len());
1514 let command = to_proto::command::show_references(title, &uri, position, locations);
1516 return Some(lsp_ext::CommandLinkGroup {
1517 commands: vec![to_command_link(command, "Go to references".into())],
1518 ..Default::default()
1525 fn runnable_action_links(
1526 snap: &GlobalStateSnapshot,
1528 ) -> Option<lsp_ext::CommandLinkGroup> {
1529 let cargo_spec = CargoTargetSpec::for_file(snap, runnable.nav.file_id).ok()?;
1530 let hover_actions_config = snap.config.hover_actions();
1531 if !hover_actions_config.runnable() || should_skip_target(&runnable, cargo_spec.as_ref()) {
1535 let action: &'static _ = runnable.action();
1536 to_proto::runnable(snap, runnable).ok().map(|r| {
1537 let mut group = lsp_ext::CommandLinkGroup::default();
1539 if hover_actions_config.run {
1540 let run_command = to_proto::command::run_single(&r, action.run_title);
1541 group.commands.push(to_command_link(run_command, r.label.clone()));
1544 if hover_actions_config.debug {
1545 let dbg_command = to_proto::command::debug_single(&r);
1546 group.commands.push(to_command_link(dbg_command, r.label));
1553 fn goto_type_action_links(
1554 snap: &GlobalStateSnapshot,
1555 nav_targets: &[HoverGotoTypeData],
1556 ) -> Option<lsp_ext::CommandLinkGroup> {
1557 if !snap.config.hover_actions().goto_type_def || nav_targets.is_empty() {
1561 Some(lsp_ext::CommandLinkGroup {
1562 title: Some("Go to ".into()),
1563 commands: nav_targets
1566 to_proto::command::goto_location(snap, &it.nav)
1567 .map(|cmd| to_command_link(cmd, it.mod_path.clone()))
1573 fn prepare_hover_actions(
1574 snap: &GlobalStateSnapshot,
1575 actions: &[HoverAction],
1576 ) -> Vec<lsp_ext::CommandLinkGroup> {
1577 if snap.config.hover_actions().none() || !snap.config.experimental_hover_actions() {
1583 .filter_map(|it| match it {
1584 HoverAction::Implementation(position) => show_impl_command_link(snap, position),
1585 HoverAction::Reference(position) => show_ref_command_link(snap, position),
1586 HoverAction::Runnable(r) => runnable_action_links(snap, r.clone()),
1587 HoverAction::GoToType(targets) => goto_type_action_links(snap, targets),
1592 fn should_skip_target(runnable: &Runnable, cargo_spec: Option<&CargoTargetSpec>) -> bool {
1593 match runnable.kind {
1594 RunnableKind::Bin => {
1595 // Do not suggest binary run on other target than binary
1597 Some(spec) => !matches!(
1599 TargetKind::Bin | TargetKind::Example | TargetKind::Test
1609 snap: &GlobalStateSnapshot,
1610 text_document: TextDocumentIdentifier,
1611 range: Option<lsp_types::Range>,
1612 ) -> Result<Option<Vec<lsp_types::TextEdit>>> {
1613 let file_id = from_proto::file_id(snap, &text_document.uri)?;
1614 let file = snap.analysis.file_text(file_id)?;
1615 let crate_ids = snap.analysis.crate_for(file_id)?;
1617 let line_index = snap.file_line_index(file_id)?;
1619 let mut rustfmt = match snap.config.rustfmt() {
1620 RustfmtConfig::Rustfmt { extra_args, enable_range_formatting } => {
1621 let mut cmd = process::Command::new(toolchain::rustfmt());
1622 cmd.args(extra_args);
1623 // try to chdir to the file so we can respect `rustfmt.toml`
1624 // FIXME: use `rustfmt --config-path` once
1625 // https://github.com/rust-lang/rustfmt/issues/4660 gets fixed
1626 match text_document.uri.to_file_path() {
1628 // pop off file name
1629 if path.pop() && path.is_dir() {
1630 cmd.current_dir(path);
1635 "Unable to get file path for {}, rustfmt.toml might be ignored",
1640 if let Some(&crate_id) = crate_ids.first() {
1641 // Assume all crates are in the same edition
1642 let edition = snap.analysis.crate_edition(crate_id)?;
1643 cmd.arg("--edition");
1644 cmd.arg(edition.to_string());
1647 if let Some(range) = range {
1648 if !enable_range_formatting {
1649 return Err(LspError::new(
1650 ErrorCode::InvalidRequest as i32,
1652 "rustfmt range formatting is unstable. \
1653 Opt-in by using a nightly build of rustfmt and setting \
1654 `rustfmt.enableRangeFormatting` to true in your LSP configuration",
1660 let frange = from_proto::file_range(snap, text_document, range)?;
1661 let start_line = line_index.index.line_col(frange.range.start()).line;
1662 let end_line = line_index.index.line_col(frange.range.end()).line;
1664 cmd.arg("--unstable-features");
1665 cmd.arg("--file-lines");
1669 "range": [start_line, end_line]
1677 RustfmtConfig::CustomCommand { command, args } => {
1678 let mut cmd = process::Command::new(command);
1685 rustfmt.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn()?;
1687 rustfmt.stdin.as_mut().unwrap().write_all(file.as_bytes())?;
1689 let output = rustfmt.wait_with_output()?;
1690 let captured_stdout = String::from_utf8(output.stdout)?;
1691 let captured_stderr = String::from_utf8(output.stderr).unwrap_or_default();
1693 if !output.status.success() {
1694 let rustfmt_not_installed =
1695 captured_stderr.contains("not installed") || captured_stderr.contains("not available");
1697 return match output.status.code() {
1698 Some(1) if !rustfmt_not_installed => {
1699 // While `rustfmt` doesn't have a specific exit code for parse errors this is the
1700 // likely cause exiting with 1. Most Language Servers swallow parse errors on
1701 // formatting because otherwise an error is surfaced to the user on top of the
1702 // syntax error diagnostics they're already receiving. This is especially jarring
1703 // if they have format on save enabled.
1704 log::info!("rustfmt exited with status 1, assuming parse error and ignoring");
1708 // Something else happened - e.g. `rustfmt` is missing or caught a signal
1712 r#"rustfmt exited with:
1716 output.status, captured_stdout, captured_stderr,
1724 let (new_text, new_line_endings) = LineEndings::normalize(captured_stdout);
1726 if line_index.endings != new_line_endings {
1727 // If line endings are different, send the entire file.
1728 // Diffing would not work here, as the line endings might be the only
1730 Ok(Some(to_proto::text_edit_vec(
1732 TextEdit::replace(TextRange::up_to(TextSize::of(&*file)), new_text),
1734 } else if *file == new_text {
1735 // The document is already formatted correctly -- no edits needed.
1738 Ok(Some(to_proto::text_edit_vec(&line_index, diff(&file, &new_text))))
1742 #[derive(Debug, Serialize, Deserialize)]
1743 struct CompletionResolveData {
1744 position: lsp_types::TextDocumentPositionParams,
1745 full_import_path: String,
1746 imported_name: String,
1749 fn fill_resolve_data(
1750 resolve_data: &mut Option<serde_json::Value>,
1751 item: &ide::CompletionItem,
1752 position: &TextDocumentPositionParams,
1754 let import_edit = item.import_to_add()?;
1755 let import_path = &import_edit.import.import_path;
1757 *resolve_data = Some(
1758 to_value(CompletionResolveData {
1759 position: position.to_owned(),
1760 full_import_path: import_path.to_string(),
1761 imported_name: import_path.segments().last()?.to_string(),