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::{ManifestPath, ProjectWorkspace, TargetKind};
31 use stdx::{format_to, never};
32 use syntax::{algo, ast, AstNode, TextRange, TextSize, T};
36 cargo_target_spec::CargoTargetSpec,
37 config::RustfmtConfig,
40 global_state::{GlobalState, GlobalStateSnapshot},
41 line_index::LineEndings,
43 self, InlayHint, InlayHintsParams, PositionOrRange, ViewCrateGraphParams,
44 WorkspaceSymbolParams,
46 lsp_utils::{all_edits_are_disjoint, invalid_params_error},
47 to_proto, LspError, Result,
50 pub(crate) fn handle_analyzer_status(
51 snap: GlobalStateSnapshot,
52 params: lsp_ext::AnalyzerStatusParams,
54 let _p = profile::span("handle_analyzer_status");
56 let mut buf = String::new();
58 let mut file_id = None;
59 if let Some(tdi) = params.text_document {
60 match from_proto::file_id(&snap, &tdi.uri) {
61 Ok(it) => file_id = Some(it),
62 Err(_) => format_to!(buf, "file {} not found in vfs", tdi.uri),
66 if snap.workspaces.is_empty() {
67 buf.push_str("No workspaces\n")
69 buf.push_str("Workspaces:\n");
72 "Loaded {:?} packages across {} workspace{}.\n",
73 snap.workspaces.iter().map(|w| w.n_packages()).sum::<usize>(),
74 snap.workspaces.len(),
75 if snap.workspaces.len() == 1 { "" } else { "s" }
78 buf.push_str("\nAnalysis:\n");
83 .unwrap_or_else(|_| "Analysis retrieval was cancelled".to_owned()),
88 pub(crate) fn handle_memory_usage(state: &mut GlobalState, _: ()) -> Result<String> {
89 let _p = profile::span("handle_memory_usage");
90 let mut mem = state.analysis_host.per_query_memory_usage();
91 mem.push(("Remaining".into(), profile::memory_usage().allocated));
93 let mut out = String::new();
94 for (name, bytes) in mem {
95 format_to!(out, "{:>8} {}\n", bytes, name);
100 pub(crate) fn handle_syntax_tree(
101 snap: GlobalStateSnapshot,
102 params: lsp_ext::SyntaxTreeParams,
103 ) -> Result<String> {
104 let _p = profile::span("handle_syntax_tree");
105 let id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
106 let line_index = snap.file_line_index(id)?;
107 let text_range = params.range.map(|r| from_proto::text_range(&line_index, r));
108 let res = snap.analysis.syntax_tree(id, text_range)?;
112 pub(crate) fn handle_view_hir(
113 snap: GlobalStateSnapshot,
114 params: lsp_types::TextDocumentPositionParams,
115 ) -> Result<String> {
116 let _p = profile::span("handle_view_hir");
117 let position = from_proto::file_position(&snap, params)?;
118 let res = snap.analysis.view_hir(position)?;
122 pub(crate) fn handle_view_item_tree(
123 snap: GlobalStateSnapshot,
124 params: lsp_ext::ViewItemTreeParams,
125 ) -> Result<String> {
126 let _p = profile::span("handle_view_item_tree");
127 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
128 let res = snap.analysis.view_item_tree(file_id)?;
132 pub(crate) fn handle_view_crate_graph(
133 snap: GlobalStateSnapshot,
134 params: ViewCrateGraphParams,
135 ) -> Result<String> {
136 let _p = profile::span("handle_view_crate_graph");
137 let dot = snap.analysis.view_crate_graph(params.full)??;
141 pub(crate) fn handle_expand_macro(
142 snap: GlobalStateSnapshot,
143 params: lsp_ext::ExpandMacroParams,
144 ) -> Result<Option<lsp_ext::ExpandedMacro>> {
145 let _p = profile::span("handle_expand_macro");
146 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
147 let line_index = snap.file_line_index(file_id)?;
148 let offset = from_proto::offset(&line_index, params.position);
150 let res = snap.analysis.expand_macro(FilePosition { file_id, offset })?;
151 Ok(res.map(|it| lsp_ext::ExpandedMacro { name: it.name, expansion: it.expansion }))
154 pub(crate) fn handle_selection_range(
155 snap: GlobalStateSnapshot,
156 params: lsp_types::SelectionRangeParams,
157 ) -> Result<Option<Vec<lsp_types::SelectionRange>>> {
158 let _p = profile::span("handle_selection_range");
159 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
160 let line_index = snap.file_line_index(file_id)?;
161 let res: Result<Vec<lsp_types::SelectionRange>> = params
165 let offset = from_proto::offset(&line_index, position);
166 let mut ranges = Vec::new();
168 let mut range = TextRange::new(offset, offset);
171 let frange = FileRange { file_id, range };
172 let next = snap.analysis.extend_selection(frange)?;
180 let mut range = lsp_types::SelectionRange {
181 range: to_proto::range(&line_index, *ranges.last().unwrap()),
184 for &r in ranges.iter().rev().skip(1) {
185 range = lsp_types::SelectionRange {
186 range: to_proto::range(&line_index, r),
187 parent: Some(Box::new(range)),
197 pub(crate) fn handle_matching_brace(
198 snap: GlobalStateSnapshot,
199 params: lsp_ext::MatchingBraceParams,
200 ) -> Result<Vec<Position>> {
201 let _p = profile::span("handle_matching_brace");
202 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
203 let line_index = snap.file_line_index(file_id)?;
208 let offset = from_proto::offset(&line_index, position);
209 let offset = match snap.analysis.matching_brace(FilePosition { file_id, offset }) {
210 Ok(Some(matching_brace_offset)) => matching_brace_offset,
211 Err(_) | Ok(None) => offset,
213 to_proto::position(&line_index, offset)
219 pub(crate) fn handle_join_lines(
220 snap: GlobalStateSnapshot,
221 params: lsp_ext::JoinLinesParams,
222 ) -> Result<Vec<lsp_types::TextEdit>> {
223 let _p = profile::span("handle_join_lines");
225 let config = snap.config.join_lines();
226 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
227 let line_index = snap.file_line_index(file_id)?;
229 let mut res = TextEdit::default();
230 for range in params.ranges {
231 let range = from_proto::text_range(&line_index, range);
232 let edit = snap.analysis.join_lines(&config, FileRange { file_id, range })?;
233 match res.union(edit) {
236 // just ignore overlapping edits
241 Ok(to_proto::text_edit_vec(&line_index, res))
244 pub(crate) fn handle_on_enter(
245 snap: GlobalStateSnapshot,
246 params: lsp_types::TextDocumentPositionParams,
247 ) -> Result<Option<Vec<lsp_ext::SnippetTextEdit>>> {
248 let _p = profile::span("handle_on_enter");
249 let position = from_proto::file_position(&snap, params)?;
250 let edit = match snap.analysis.on_enter(position)? {
251 None => return Ok(None),
254 let line_index = snap.file_line_index(position.file_id)?;
255 let edit = to_proto::snippet_text_edit_vec(&line_index, true, edit);
259 pub(crate) fn handle_on_type_formatting(
260 snap: GlobalStateSnapshot,
261 params: lsp_types::DocumentOnTypeFormattingParams,
262 ) -> Result<Option<Vec<lsp_types::TextEdit>>> {
263 let _p = profile::span("handle_on_type_formatting");
264 let mut position = from_proto::file_position(&snap, params.text_document_position)?;
265 let line_index = snap.file_line_index(position.file_id)?;
267 // in `ide`, the `on_type` invariant is that
268 // `text.char_at(position) == typed_char`.
269 position.offset -= TextSize::of('.');
270 let char_typed = params.ch.chars().next().unwrap_or('\0');
272 let text = snap.analysis.file_text(position.file_id)?;
273 if !text[usize::from(position.offset)..].starts_with(char_typed) {
274 // Add `always!` here once VS Code bug is fixed:
275 // https://github.com/rust-analyzer/rust-analyzer/issues/10002
279 // We have an assist that inserts ` ` after typing `->` in `fn foo() ->{`,
280 // but it requires precise cursor positioning to work, and one can't
281 // position the cursor with on_type formatting. So, let's just toggle this
282 // feature off here, hoping that we'll enable it one day, 😿.
283 if char_typed == '>' {
287 let edit = snap.analysis.on_char_typed(position, char_typed)?;
288 let edit = match edit {
290 None => return Ok(None),
293 // This should be a single-file edit
294 let (_, edit) = edit.source_file_edits.into_iter().next().unwrap();
296 let change = to_proto::text_edit_vec(&line_index, edit);
300 pub(crate) fn handle_document_symbol(
301 snap: GlobalStateSnapshot,
302 params: lsp_types::DocumentSymbolParams,
303 ) -> Result<Option<lsp_types::DocumentSymbolResponse>> {
304 let _p = profile::span("handle_document_symbol");
305 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
306 let line_index = snap.file_line_index(file_id)?;
308 let mut parents: Vec<(lsp_types::DocumentSymbol, Option<usize>)> = Vec::new();
310 for symbol in snap.analysis.file_structure(file_id)? {
311 let mut tags = Vec::new();
312 if symbol.deprecated {
313 tags.push(SymbolTag::DEPRECATED)
317 let doc_symbol = lsp_types::DocumentSymbol {
319 detail: symbol.detail,
320 kind: to_proto::structure_node_kind(symbol.kind),
322 deprecated: Some(symbol.deprecated),
323 range: to_proto::range(&line_index, symbol.node_range),
324 selection_range: to_proto::range(&line_index, symbol.navigation_range),
327 parents.push((doc_symbol, symbol.parent));
330 // Builds hierarchy from a flat list, in reverse order (so that indices
332 let document_symbols = {
333 let mut acc = Vec::new();
334 while let Some((mut node, parent_idx)) = parents.pop() {
335 if let Some(children) = &mut node.children {
338 let parent = match parent_idx {
340 Some(i) => parents[i].0.children.get_or_insert_with(Vec::new),
348 let res = if snap.config.hierarchical_symbols() {
349 document_symbols.into()
351 let url = to_proto::url(&snap, file_id);
352 let mut symbol_information = Vec::<SymbolInformation>::new();
353 for symbol in document_symbols {
354 flatten_document_symbol(&symbol, None, &url, &mut symbol_information);
356 symbol_information.into()
358 return Ok(Some(res));
360 fn flatten_document_symbol(
361 symbol: &lsp_types::DocumentSymbol,
362 container_name: Option<String>,
364 res: &mut Vec<SymbolInformation>,
366 let mut tags = Vec::new();
369 if let Some(true) = symbol.deprecated {
370 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 = match params.search_kind {
432 Some(ref search_kind) => search_kind,
433 None => &config.search_kind,
435 all_symbols = match search_kind {
436 lsp_ext::WorkspaceSymbolSearchKind::OnlyTypes => false,
437 lsp_ext::WorkspaceSymbolSearchKind::AllSymbols => true,
442 let search_scope = match params.search_scope {
443 Some(ref search_scope) => search_scope,
444 None => &config.search_scope,
446 libs = match search_scope {
447 lsp_ext::WorkspaceSymbolSearchScope::Workspace => false,
448 lsp_ext::WorkspaceSymbolSearchScope::WorkspaceAndDependencies => true,
455 fn exec_query(snap: &GlobalStateSnapshot, query: Query) -> Result<Vec<SymbolInformation>> {
456 let mut res = Vec::new();
457 for nav in snap.analysis.symbol_search(query)? {
458 let container_name = nav.container_name.as_ref().map(|v| v.to_string());
461 let info = SymbolInformation {
462 name: nav.name.to_string(),
465 .map(to_proto::symbol_kind)
466 .unwrap_or(lsp_types::SymbolKind::VARIABLE),
468 location: to_proto::location_from_nav(snap, nav)?,
478 pub(crate) fn handle_will_rename_files(
479 snap: GlobalStateSnapshot,
480 params: lsp_types::RenameFilesParams,
481 ) -> Result<Option<lsp_types::WorkspaceEdit>> {
482 let _p = profile::span("handle_will_rename_files");
484 let source_changes: Vec<SourceChange> = params
487 .filter_map(|file_rename| {
488 let from = Url::parse(&file_rename.old_uri).ok()?;
489 let to = Url::parse(&file_rename.new_uri).ok()?;
491 let from_path = from.to_file_path().ok()?;
492 let to_path = to.to_file_path().ok()?;
494 // Limit to single-level moves for now.
495 match (from_path.parent(), to_path.parent()) {
496 (Some(p1), Some(p2)) if p1 == p2 => {
497 if from_path.is_dir() {
498 // add '/' to end of url -- from `file://path/to/folder` to `file://path/to/folder/`
499 let mut old_folder_name = from_path.file_stem()?.to_str()?.to_string();
500 old_folder_name.push('/');
501 let from_with_trailing_slash = from.join(&old_folder_name).ok()?;
503 let imitate_from_url = from_with_trailing_slash.join("mod.rs").ok()?;
504 let new_file_name = to_path.file_name()?.to_str()?;
506 snap.url_to_file_id(&imitate_from_url).ok()?,
507 new_file_name.to_string(),
510 let old_name = from_path.file_stem()?.to_str()?;
511 let new_name = to_path.file_stem()?.to_str()?;
512 match (old_name, new_name) {
515 _ => Some((snap.url_to_file_id(&from).ok()?, new_name.to_string())),
522 .filter_map(|(file_id, new_name)| {
523 snap.analysis.will_rename_file(file_id, &new_name).ok()?
527 // Drop file system edits since we're just renaming things on the same level
528 let mut source_changes = source_changes.into_iter();
529 let mut source_change = source_changes.next().unwrap_or_default();
530 source_change.file_system_edits.clear();
531 // no collect here because we want to merge text edits on same file ids
532 source_change.extend(source_changes.map(|it| it.source_file_edits).flatten());
533 if source_change.source_file_edits.is_empty() {
536 to_proto::workspace_edit(&snap, source_change).map(Some)
540 pub(crate) fn handle_goto_definition(
541 snap: GlobalStateSnapshot,
542 params: lsp_types::GotoDefinitionParams,
543 ) -> Result<Option<lsp_types::GotoDefinitionResponse>> {
544 let _p = profile::span("handle_goto_definition");
545 let position = from_proto::file_position(&snap, params.text_document_position_params)?;
546 let nav_info = match snap.analysis.goto_definition(position)? {
547 None => return Ok(None),
550 let src = FileRange { file_id: position.file_id, range: nav_info.range };
551 let res = to_proto::goto_definition_response(&snap, Some(src), nav_info.info)?;
555 pub(crate) fn handle_goto_declaration(
556 snap: GlobalStateSnapshot,
557 params: lsp_types::request::GotoDeclarationParams,
558 ) -> Result<Option<lsp_types::request::GotoDeclarationResponse>> {
559 let _p = profile::span("handle_goto_declaration");
560 let position = from_proto::file_position(&snap, params.text_document_position_params.clone())?;
561 let nav_info = match snap.analysis.goto_declaration(position)? {
562 None => return handle_goto_definition(snap, params),
565 let src = FileRange { file_id: position.file_id, range: nav_info.range };
566 let res = to_proto::goto_definition_response(&snap, Some(src), nav_info.info)?;
570 pub(crate) fn handle_goto_implementation(
571 snap: GlobalStateSnapshot,
572 params: lsp_types::request::GotoImplementationParams,
573 ) -> Result<Option<lsp_types::request::GotoImplementationResponse>> {
574 let _p = profile::span("handle_goto_implementation");
575 let position = from_proto::file_position(&snap, params.text_document_position_params)?;
576 let nav_info = match snap.analysis.goto_implementation(position)? {
577 None => return Ok(None),
580 let src = FileRange { file_id: position.file_id, range: nav_info.range };
581 let res = to_proto::goto_definition_response(&snap, Some(src), nav_info.info)?;
585 pub(crate) fn handle_goto_type_definition(
586 snap: GlobalStateSnapshot,
587 params: lsp_types::request::GotoTypeDefinitionParams,
588 ) -> Result<Option<lsp_types::request::GotoTypeDefinitionResponse>> {
589 let _p = profile::span("handle_goto_type_definition");
590 let position = from_proto::file_position(&snap, params.text_document_position_params)?;
591 let nav_info = match snap.analysis.goto_type_definition(position)? {
592 None => return Ok(None),
595 let src = FileRange { file_id: position.file_id, range: nav_info.range };
596 let res = to_proto::goto_definition_response(&snap, Some(src), nav_info.info)?;
600 pub(crate) fn handle_parent_module(
601 snap: GlobalStateSnapshot,
602 params: lsp_types::TextDocumentPositionParams,
603 ) -> Result<Option<lsp_types::GotoDefinitionResponse>> {
604 let _p = profile::span("handle_parent_module");
605 if let Ok(file_path) = ¶ms.text_document.uri.to_file_path() {
606 if file_path.file_name().unwrap_or_default() == "Cargo.toml" {
607 // search workspaces for parent packages or fallback to workspace root
608 let abs_path_buf = match AbsPathBuf::try_from(file_path.to_path_buf()).ok() {
609 Some(abs_path_buf) => abs_path_buf,
610 None => return Ok(None),
613 let manifest_path = match ManifestPath::try_from(abs_path_buf).ok() {
614 Some(manifest_path) => manifest_path,
615 None => return Ok(None),
618 let links: Vec<LocationLink> = snap
621 .filter_map(|ws| match ws {
622 ProjectWorkspace::Cargo { cargo, .. } => cargo.parent_manifests(&manifest_path),
626 .map(|parent_manifest_path| LocationLink {
627 origin_selection_range: None,
628 target_uri: to_proto::url_from_abs_path(&parent_manifest_path),
629 target_range: Range::default(),
630 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"] {
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 },
848 .map(|import| (import.full_import_path, import.imported_name)),
851 .flat_map(|edit| edit.into_iter().map(|indel| to_proto::text_edit(&line_index, indel)))
854 if !all_edits_are_disjoint(&original_completion, &additional_edits) {
855 return Err(LspError::new(
856 ErrorCode::InternalError as i32,
857 "Import edit overlaps with the original completion edits, this is not LSP-compliant"
863 if let Some(original_additional_edits) = original_completion.additional_text_edits.as_mut() {
864 original_additional_edits.extend(additional_edits.into_iter())
866 original_completion.additional_text_edits = Some(additional_edits);
869 Ok(original_completion)
872 pub(crate) fn handle_folding_range(
873 snap: GlobalStateSnapshot,
874 params: FoldingRangeParams,
875 ) -> Result<Option<Vec<FoldingRange>>> {
876 let _p = profile::span("handle_folding_range");
877 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
878 let folds = snap.analysis.folding_ranges(file_id)?;
879 let text = snap.analysis.file_text(file_id)?;
880 let line_index = snap.file_line_index(file_id)?;
881 let line_folding_only = snap.config.line_folding_only();
884 .map(|it| to_proto::folding_range(&*text, &line_index, line_folding_only, it))
889 pub(crate) fn handle_signature_help(
890 snap: GlobalStateSnapshot,
891 params: lsp_types::SignatureHelpParams,
892 ) -> Result<Option<lsp_types::SignatureHelp>> {
893 let _p = profile::span("handle_signature_help");
894 let position = from_proto::file_position(&snap, params.text_document_position_params)?;
895 let call_info = match snap.analysis.call_info(position)? {
897 None => return Ok(None),
899 let concise = !snap.config.call_info_full();
901 to_proto::signature_help(call_info, concise, snap.config.signature_help_label_offsets());
905 pub(crate) fn handle_hover(
906 snap: GlobalStateSnapshot,
907 params: lsp_ext::HoverParams,
908 ) -> Result<Option<lsp_ext::Hover>> {
909 let _p = profile::span("handle_hover");
910 let range = match params.position {
911 PositionOrRange::Position(position) => Range::new(position, position),
912 PositionOrRange::Range(range) => range,
915 let file_range = from_proto::file_range(&snap, params.text_document, range)?;
916 let info = match snap.analysis.hover(&snap.config.hover(), file_range)? {
917 None => return Ok(None),
921 let line_index = snap.file_line_index(file_range.file_id)?;
922 let range = to_proto::range(&line_index, info.range);
924 snap.config.hover().documentation.map_or(ide::HoverDocFormat::Markdown, |kind| kind);
925 let hover = lsp_ext::Hover {
926 hover: lsp_types::Hover {
927 contents: HoverContents::Markup(to_proto::markup_content(
933 actions: if snap.config.hover_actions().none() {
936 prepare_hover_actions(&snap, &info.info.actions)
943 pub(crate) fn handle_prepare_rename(
944 snap: GlobalStateSnapshot,
945 params: lsp_types::TextDocumentPositionParams,
946 ) -> Result<Option<PrepareRenameResponse>> {
947 let _p = profile::span("handle_prepare_rename");
948 let position = from_proto::file_position(&snap, params)?;
950 let change = snap.analysis.prepare_rename(position)?.map_err(to_proto::rename_error)?;
952 let line_index = snap.file_line_index(position.file_id)?;
953 let range = to_proto::range(&line_index, change.range);
954 Ok(Some(PrepareRenameResponse::Range(range)))
957 pub(crate) fn handle_rename(
958 snap: GlobalStateSnapshot,
959 params: RenameParams,
960 ) -> Result<Option<WorkspaceEdit>> {
961 let _p = profile::span("handle_rename");
962 let position = from_proto::file_position(&snap, params.text_document_position)?;
965 snap.analysis.rename(position, &*params.new_name)?.map_err(to_proto::rename_error)?;
967 // this is kind of a hack to prevent double edits from happening when moving files
968 // When a module gets renamed by renaming the mod declaration this causes the file to move
969 // which in turn will trigger a WillRenameFiles request to the server for which we reply with a
970 // a second identical set of renames, the client will then apply both edits causing incorrect edits
971 // with this we only emit source_file_edits in the WillRenameFiles response which will do the rename instead
972 // See https://github.com/microsoft/vscode-languageserver-node/issues/752 for more info
973 if !change.file_system_edits.is_empty() && snap.config.will_rename() {
974 change.source_file_edits.clear();
976 let workspace_edit = to_proto::workspace_edit(&snap, change)?;
977 Ok(Some(workspace_edit))
980 pub(crate) fn handle_references(
981 snap: GlobalStateSnapshot,
982 params: lsp_types::ReferenceParams,
983 ) -> Result<Option<Vec<Location>>> {
984 let _p = profile::span("handle_references");
985 let position = from_proto::file_position(&snap, params.text_document_position)?;
987 let refs = match snap.analysis.find_all_refs(position, None)? {
988 None => return Ok(None),
992 let include_declaration = params.context.include_declaration;
996 let decl = if include_declaration {
997 refs.declaration.map(|decl| FileRange {
998 file_id: decl.nav.file_id,
999 range: decl.nav.focus_or_full_range(),
1006 .flat_map(|(file_id, refs)| {
1007 refs.into_iter().map(move |(range, _)| FileRange { file_id, range })
1011 .filter_map(|frange| to_proto::location(&snap, frange).ok())
1017 pub(crate) fn handle_formatting(
1018 snap: GlobalStateSnapshot,
1019 params: DocumentFormattingParams,
1020 ) -> Result<Option<Vec<lsp_types::TextEdit>>> {
1021 let _p = profile::span("handle_formatting");
1023 run_rustfmt(&snap, params.text_document, None)
1026 pub(crate) fn handle_range_formatting(
1027 snap: GlobalStateSnapshot,
1028 params: lsp_types::DocumentRangeFormattingParams,
1029 ) -> Result<Option<Vec<lsp_types::TextEdit>>> {
1030 let _p = profile::span("handle_range_formatting");
1032 run_rustfmt(&snap, params.text_document, Some(params.range))
1035 pub(crate) fn handle_code_action(
1036 snap: GlobalStateSnapshot,
1037 params: lsp_types::CodeActionParams,
1038 ) -> Result<Option<Vec<lsp_ext::CodeAction>>> {
1039 let _p = profile::span("handle_code_action");
1041 if !snap.config.code_action_literals() {
1042 // We intentionally don't support command-based actions, as those either
1043 // require either custom client-code or server-initiated edits. Server
1044 // initiated edits break causality, so we avoid those.
1049 snap.file_line_index(from_proto::file_id(&snap, ¶ms.text_document.uri)?)?;
1050 let frange = from_proto::file_range(&snap, params.text_document.clone(), params.range)?;
1052 let mut assists_config = snap.config.assist();
1053 assists_config.allowed = params
1057 .map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect());
1059 let mut res: Vec<lsp_ext::CodeAction> = Vec::new();
1061 let code_action_resolve_cap = snap.config.code_action_resolve();
1062 let resolve = if code_action_resolve_cap {
1063 AssistResolveStrategy::None
1065 AssistResolveStrategy::All
1067 let assists = snap.analysis.assists_with_fixes(
1069 &snap.config.diagnostics(),
1073 for (index, assist) in assists.into_iter().enumerate() {
1075 if code_action_resolve_cap { Some((index, params.clone())) } else { None };
1076 let code_action = to_proto::code_action(&snap, assist, resolve_data)?;
1077 res.push(code_action)
1080 // Fixes from `cargo check`.
1081 for fix in snap.check_fixes.get(&frange.file_id).into_iter().flatten() {
1082 // FIXME: this mapping is awkward and shouldn't exist. Refactor
1083 // `snap.check_fixes` to not convert to LSP prematurely.
1084 let intersect_fix_range = fix
1088 .map(|range| from_proto::text_range(&line_index, range))
1089 .any(|fix_range| fix_range.intersect(frange.range).is_some());
1090 if intersect_fix_range {
1091 res.push(fix.action.clone());
1098 pub(crate) fn handle_code_action_resolve(
1099 snap: GlobalStateSnapshot,
1100 mut code_action: lsp_ext::CodeAction,
1101 ) -> Result<lsp_ext::CodeAction> {
1102 let _p = profile::span("handle_code_action_resolve");
1103 let params = match code_action.data.take() {
1105 None => return Err(invalid_params_error("code action without data".to_string()).into()),
1108 let file_id = from_proto::file_id(&snap, ¶ms.code_action_params.text_document.uri)?;
1109 let line_index = snap.file_line_index(file_id)?;
1110 let range = from_proto::text_range(&line_index, params.code_action_params.range);
1111 let frange = FileRange { file_id, range };
1113 let mut assists_config = snap.config.assist();
1114 assists_config.allowed = params
1118 .map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect());
1120 let (assist_index, assist_resolve) = match parse_action_id(¶ms.id) {
1121 Ok(parsed_data) => parsed_data,
1123 return Err(invalid_params_error(format!(
1124 "Failed to parse action id string '{}': {}",
1131 let expected_assist_id = assist_resolve.assist_id.clone();
1132 let expected_kind = assist_resolve.assist_kind;
1134 let assists = snap.analysis.assists_with_fixes(
1136 &snap.config.diagnostics(),
1137 AssistResolveStrategy::Single(assist_resolve),
1141 let assist = match assists.get(assist_index) {
1142 Some(assist) => assist,
1143 None => return Err(invalid_params_error(format!(
1144 "Failed to find the assist for index {} provided by the resolve request. Resolve request assist id: {}",
1145 assist_index, params.id,
1149 if assist.id.0 != expected_assist_id || assist.id.1 != expected_kind {
1150 return Err(invalid_params_error(format!(
1151 "Mismatching assist at index {} for the resolve parameters given. Resolve request assist id: {}, actual id: {:?}.",
1152 assist_index, params.id, assist.id
1156 let edit = to_proto::code_action(&snap, assist.clone(), None)?.edit;
1157 code_action.edit = edit;
1161 fn parse_action_id(action_id: &str) -> Result<(usize, SingleResolve), String> {
1162 let id_parts = action_id.split(':').collect_vec();
1163 match id_parts.as_slice() {
1164 [assist_id_string, assist_kind_string, index_string] => {
1165 let assist_kind: AssistKind = assist_kind_string.parse()?;
1166 let index: usize = match index_string.parse() {
1168 Err(e) => return Err(format!("Incorrect index string: {}", e)),
1170 Ok((index, SingleResolve { assist_id: assist_id_string.to_string(), assist_kind }))
1172 _ => Err("Action id contains incorrect number of segments".to_string()),
1176 pub(crate) fn handle_code_lens(
1177 snap: GlobalStateSnapshot,
1178 params: lsp_types::CodeLensParams,
1179 ) -> Result<Option<Vec<CodeLens>>> {
1180 let _p = profile::span("handle_code_lens");
1182 let lens_config = snap.config.lens();
1183 if lens_config.none() {
1184 // early return before any db query!
1185 return Ok(Some(Vec::default()));
1188 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
1189 let cargo_target_spec = CargoTargetSpec::for_file(&snap, file_id)?;
1191 let annotations = snap.analysis.annotations(
1193 binary_target: cargo_target_spec
1197 TargetKind::Bin | TargetKind::Example | TargetKind::Test
1201 annotate_runnables: lens_config.runnable(),
1202 annotate_impls: lens_config.implementations,
1203 annotate_references: lens_config.refs,
1204 annotate_method_references: lens_config.method_refs,
1205 annotate_enum_variant_references: lens_config.enum_variant_refs,
1210 let mut res = Vec::new();
1211 for a in annotations {
1212 to_proto::code_lens(&mut res, &snap, a)?;
1218 pub(crate) fn handle_code_lens_resolve(
1219 snap: GlobalStateSnapshot,
1220 code_lens: CodeLens,
1221 ) -> Result<CodeLens> {
1222 let annotation = from_proto::annotation(&snap, code_lens.clone())?;
1223 let annotation = snap.analysis.resolve_annotation(annotation)?;
1225 let mut acc = Vec::new();
1226 to_proto::code_lens(&mut acc, &snap, annotation)?;
1228 let res = match acc.pop() {
1229 Some(it) if acc.is_empty() => it,
1239 pub(crate) fn handle_document_highlight(
1240 snap: GlobalStateSnapshot,
1241 params: lsp_types::DocumentHighlightParams,
1242 ) -> Result<Option<Vec<lsp_types::DocumentHighlight>>> {
1243 let _p = profile::span("handle_document_highlight");
1244 let position = from_proto::file_position(&snap, params.text_document_position_params)?;
1245 let line_index = snap.file_line_index(position.file_id)?;
1247 let refs = match snap.analysis.highlight_related(snap.config.highlight_related(), position)? {
1248 None => return Ok(None),
1253 .map(|ide::HighlightedRange { range, category }| lsp_types::DocumentHighlight {
1254 range: to_proto::range(&line_index, range),
1255 kind: category.map(to_proto::document_highlight_kind),
1261 pub(crate) fn handle_ssr(
1262 snap: GlobalStateSnapshot,
1263 params: lsp_ext::SsrParams,
1264 ) -> Result<lsp_types::WorkspaceEdit> {
1265 let _p = profile::span("handle_ssr");
1266 let selections = params
1269 .map(|range| from_proto::file_range(&snap, params.position.text_document.clone(), *range))
1270 .collect::<Result<Vec<_>, _>>()?;
1271 let position = from_proto::file_position(&snap, params.position)?;
1272 let source_change = snap.analysis.structural_search_replace(
1278 to_proto::workspace_edit(&snap, source_change)
1281 pub(crate) fn publish_diagnostics(
1282 snap: &GlobalStateSnapshot,
1284 ) -> Result<Vec<Diagnostic>> {
1285 let _p = profile::span("publish_diagnostics");
1286 let line_index = snap.file_line_index(file_id)?;
1288 let diagnostics: Vec<Diagnostic> = snap
1290 .diagnostics(&snap.config.diagnostics(), AssistResolveStrategy::None, file_id)?
1292 .map(|d| Diagnostic {
1293 range: to_proto::range(&line_index, d.range),
1294 severity: Some(to_proto::diagnostic_severity(d.severity)),
1295 code: Some(NumberOrString::String(d.code.as_str().to_string())),
1296 code_description: Some(lsp_types::CodeDescription {
1297 href: lsp_types::Url::parse(&format!(
1298 "https://rust-analyzer.github.io/manual.html#{}",
1303 source: Some("rust-analyzer".to_string()),
1305 related_information: None,
1306 tags: if d.unused { Some(vec![DiagnosticTag::UNNECESSARY]) } else { None },
1313 pub(crate) fn handle_inlay_hints(
1314 snap: GlobalStateSnapshot,
1315 params: InlayHintsParams,
1316 ) -> Result<Vec<InlayHint>> {
1317 let _p = profile::span("handle_inlay_hints");
1318 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
1319 let line_index = snap.file_line_index(file_id)?;
1322 .inlay_hints(&snap.config.inlay_hints(), file_id)?
1324 .map(|it| to_proto::inlay_hint(&line_index, it))
1328 pub(crate) fn handle_call_hierarchy_prepare(
1329 snap: GlobalStateSnapshot,
1330 params: CallHierarchyPrepareParams,
1331 ) -> Result<Option<Vec<CallHierarchyItem>>> {
1332 let _p = profile::span("handle_call_hierarchy_prepare");
1333 let position = from_proto::file_position(&snap, params.text_document_position_params)?;
1335 let nav_info = match snap.analysis.call_hierarchy(position)? {
1336 None => return Ok(None),
1340 let RangeInfo { range: _, info: navs } = nav_info;
1343 .filter(|it| it.kind == Some(SymbolKind::Function))
1344 .map(|it| to_proto::call_hierarchy_item(&snap, it))
1345 .collect::<Result<Vec<_>>>()?;
1350 pub(crate) fn handle_call_hierarchy_incoming(
1351 snap: GlobalStateSnapshot,
1352 params: CallHierarchyIncomingCallsParams,
1353 ) -> Result<Option<Vec<CallHierarchyIncomingCall>>> {
1354 let _p = profile::span("handle_call_hierarchy_incoming");
1355 let item = params.item;
1357 let doc = TextDocumentIdentifier::new(item.uri);
1358 let frange = from_proto::file_range(&snap, doc, item.selection_range)?;
1359 let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
1361 let call_items = match snap.analysis.incoming_calls(fpos)? {
1362 None => return Ok(None),
1366 let mut res = vec![];
1368 for call_item in call_items.into_iter() {
1369 let file_id = call_item.target.file_id;
1370 let line_index = snap.file_line_index(file_id)?;
1371 let item = to_proto::call_hierarchy_item(&snap, call_item.target)?;
1372 res.push(CallHierarchyIncomingCall {
1374 from_ranges: call_item
1377 .map(|it| to_proto::range(&line_index, it))
1385 pub(crate) fn handle_call_hierarchy_outgoing(
1386 snap: GlobalStateSnapshot,
1387 params: CallHierarchyOutgoingCallsParams,
1388 ) -> Result<Option<Vec<CallHierarchyOutgoingCall>>> {
1389 let _p = profile::span("handle_call_hierarchy_outgoing");
1390 let item = params.item;
1392 let doc = TextDocumentIdentifier::new(item.uri);
1393 let frange = from_proto::file_range(&snap, doc, item.selection_range)?;
1394 let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
1396 let call_items = match snap.analysis.outgoing_calls(fpos)? {
1397 None => return Ok(None),
1401 let mut res = vec![];
1403 for call_item in call_items.into_iter() {
1404 let file_id = call_item.target.file_id;
1405 let line_index = snap.file_line_index(file_id)?;
1406 let item = to_proto::call_hierarchy_item(&snap, call_item.target)?;
1407 res.push(CallHierarchyOutgoingCall {
1409 from_ranges: call_item
1412 .map(|it| to_proto::range(&line_index, it))
1420 pub(crate) fn handle_semantic_tokens_full(
1421 snap: GlobalStateSnapshot,
1422 params: SemanticTokensParams,
1423 ) -> Result<Option<SemanticTokensResult>> {
1424 let _p = profile::span("handle_semantic_tokens_full");
1426 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
1427 let text = snap.analysis.file_text(file_id)?;
1428 let line_index = snap.file_line_index(file_id)?;
1430 let highlights = snap.analysis.highlight(file_id)?;
1431 let highlight_strings = snap.config.highlighting_strings();
1432 let semantic_tokens =
1433 to_proto::semantic_tokens(&text, &line_index, highlights, highlight_strings);
1435 // Unconditionally cache the tokens
1436 snap.semantic_tokens_cache.lock().insert(params.text_document.uri, semantic_tokens.clone());
1438 Ok(Some(semantic_tokens.into()))
1441 pub(crate) fn handle_semantic_tokens_full_delta(
1442 snap: GlobalStateSnapshot,
1443 params: SemanticTokensDeltaParams,
1444 ) -> Result<Option<SemanticTokensFullDeltaResult>> {
1445 let _p = profile::span("handle_semantic_tokens_full_delta");
1447 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
1448 let text = snap.analysis.file_text(file_id)?;
1449 let line_index = snap.file_line_index(file_id)?;
1451 let highlights = snap.analysis.highlight(file_id)?;
1452 let highlight_strings = snap.config.highlighting_strings();
1453 let semantic_tokens =
1454 to_proto::semantic_tokens(&text, &line_index, highlights, highlight_strings);
1456 let mut cache = snap.semantic_tokens_cache.lock();
1457 let cached_tokens = cache.entry(params.text_document.uri).or_default();
1459 if let Some(prev_id) = &cached_tokens.result_id {
1460 if *prev_id == params.previous_result_id {
1461 let delta = to_proto::semantic_token_delta(cached_tokens, &semantic_tokens);
1462 *cached_tokens = semantic_tokens;
1463 return Ok(Some(delta.into()));
1467 *cached_tokens = semantic_tokens.clone();
1469 Ok(Some(semantic_tokens.into()))
1472 pub(crate) fn handle_semantic_tokens_range(
1473 snap: GlobalStateSnapshot,
1474 params: SemanticTokensRangeParams,
1475 ) -> Result<Option<SemanticTokensRangeResult>> {
1476 let _p = profile::span("handle_semantic_tokens_range");
1478 let frange = from_proto::file_range(&snap, params.text_document, params.range)?;
1479 let text = snap.analysis.file_text(frange.file_id)?;
1480 let line_index = snap.file_line_index(frange.file_id)?;
1482 let highlights = snap.analysis.highlight_range(frange)?;
1483 let highlight_strings = snap.config.highlighting_strings();
1484 let semantic_tokens =
1485 to_proto::semantic_tokens(&text, &line_index, highlights, highlight_strings);
1486 Ok(Some(semantic_tokens.into()))
1489 pub(crate) fn handle_open_docs(
1490 snap: GlobalStateSnapshot,
1491 params: lsp_types::TextDocumentPositionParams,
1492 ) -> Result<Option<lsp_types::Url>> {
1493 let _p = profile::span("handle_open_docs");
1494 let position = from_proto::file_position(&snap, params)?;
1496 let remote = snap.analysis.external_docs(position)?;
1498 Ok(remote.and_then(|remote| Url::parse(&remote).ok()))
1501 pub(crate) fn handle_open_cargo_toml(
1502 snap: GlobalStateSnapshot,
1503 params: lsp_ext::OpenCargoTomlParams,
1504 ) -> Result<Option<lsp_types::GotoDefinitionResponse>> {
1505 let _p = profile::span("handle_open_cargo_toml");
1506 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
1508 let cargo_spec = match CargoTargetSpec::for_file(&snap, file_id)? {
1510 None => return Ok(None),
1513 let cargo_toml_url = to_proto::url_from_abs_path(&cargo_spec.cargo_toml);
1514 let res: lsp_types::GotoDefinitionResponse =
1515 Location::new(cargo_toml_url, Range::default()).into();
1519 pub(crate) fn handle_move_item(
1520 snap: GlobalStateSnapshot,
1521 params: lsp_ext::MoveItemParams,
1522 ) -> Result<Vec<lsp_ext::SnippetTextEdit>> {
1523 let _p = profile::span("handle_move_item");
1524 let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?;
1525 let range = from_proto::file_range(&snap, params.text_document, params.range)?;
1527 let direction = match params.direction {
1528 lsp_ext::MoveItemDirection::Up => ide::Direction::Up,
1529 lsp_ext::MoveItemDirection::Down => ide::Direction::Down,
1532 match snap.analysis.move_item(range, direction)? {
1533 Some(text_edit) => {
1534 let line_index = snap.file_line_index(file_id)?;
1535 Ok(to_proto::snippet_text_edit_vec(&line_index, true, text_edit))
1541 fn to_command_link(command: lsp_types::Command, tooltip: String) -> lsp_ext::CommandLink {
1542 lsp_ext::CommandLink { tooltip: Some(tooltip), command }
1545 fn show_impl_command_link(
1546 snap: &GlobalStateSnapshot,
1547 position: &FilePosition,
1548 ) -> Option<lsp_ext::CommandLinkGroup> {
1549 if snap.config.hover_actions().implementations && snap.config.client_commands().show_reference {
1550 if let Some(nav_data) = snap.analysis.goto_implementation(*position).unwrap_or(None) {
1551 let uri = to_proto::url(snap, position.file_id);
1552 let line_index = snap.file_line_index(position.file_id).ok()?;
1553 let position = to_proto::position(&line_index, position.offset);
1554 let locations: Vec<_> = nav_data
1557 .filter_map(|nav| to_proto::location_from_nav(snap, nav).ok())
1559 let title = to_proto::implementation_title(locations.len());
1560 let command = to_proto::command::show_references(title, &uri, position, locations);
1562 return Some(lsp_ext::CommandLinkGroup {
1563 commands: vec![to_command_link(command, "Go to implementations".into())],
1564 ..Default::default()
1571 fn show_ref_command_link(
1572 snap: &GlobalStateSnapshot,
1573 position: &FilePosition,
1574 ) -> Option<lsp_ext::CommandLinkGroup> {
1575 if snap.config.hover_actions().references && snap.config.client_commands().show_reference {
1576 if let Some(ref_search_res) = snap.analysis.find_all_refs(*position, None).unwrap_or(None) {
1577 let uri = to_proto::url(snap, position.file_id);
1578 let line_index = snap.file_line_index(position.file_id).ok()?;
1579 let position = to_proto::position(&line_index, position.offset);
1580 let locations: Vec<_> = ref_search_res
1582 .flat_map(|res| res.references)
1583 .flat_map(|(file_id, ranges)| {
1584 ranges.into_iter().filter_map(move |(range, _)| {
1585 to_proto::location(snap, FileRange { file_id, range }).ok()
1589 let title = to_proto::reference_title(locations.len());
1590 let command = to_proto::command::show_references(title, &uri, position, locations);
1592 return Some(lsp_ext::CommandLinkGroup {
1593 commands: vec![to_command_link(command, "Go to references".into())],
1594 ..Default::default()
1601 fn runnable_action_links(
1602 snap: &GlobalStateSnapshot,
1604 ) -> Option<lsp_ext::CommandLinkGroup> {
1605 let hover_actions_config = snap.config.hover_actions();
1606 if !hover_actions_config.runnable() {
1610 let cargo_spec = CargoTargetSpec::for_file(snap, runnable.nav.file_id).ok()?;
1611 if should_skip_target(&runnable, cargo_spec.as_ref()) {
1615 let client_commands_config = snap.config.client_commands();
1616 if !(client_commands_config.run_single || client_commands_config.debug_single) {
1620 let title = runnable.title();
1621 let r = to_proto::runnable(snap, runnable).ok()?;
1623 let mut group = lsp_ext::CommandLinkGroup::default();
1625 if hover_actions_config.run && client_commands_config.run_single {
1626 let run_command = to_proto::command::run_single(&r, &title);
1627 group.commands.push(to_command_link(run_command, r.label.clone()));
1630 if hover_actions_config.debug && client_commands_config.debug_single {
1631 let dbg_command = to_proto::command::debug_single(&r);
1632 group.commands.push(to_command_link(dbg_command, r.label));
1638 fn goto_type_action_links(
1639 snap: &GlobalStateSnapshot,
1640 nav_targets: &[HoverGotoTypeData],
1641 ) -> Option<lsp_ext::CommandLinkGroup> {
1642 if !snap.config.hover_actions().goto_type_def
1643 || nav_targets.is_empty()
1644 || !snap.config.client_commands().goto_location
1649 Some(lsp_ext::CommandLinkGroup {
1650 title: Some("Go to ".into()),
1651 commands: nav_targets
1654 to_proto::command::goto_location(snap, &it.nav)
1655 .map(|cmd| to_command_link(cmd, it.mod_path.clone()))
1661 fn prepare_hover_actions(
1662 snap: &GlobalStateSnapshot,
1663 actions: &[HoverAction],
1664 ) -> Vec<lsp_ext::CommandLinkGroup> {
1667 .filter_map(|it| match it {
1668 HoverAction::Implementation(position) => show_impl_command_link(snap, position),
1669 HoverAction::Reference(position) => show_ref_command_link(snap, position),
1670 HoverAction::Runnable(r) => runnable_action_links(snap, r.clone()),
1671 HoverAction::GoToType(targets) => goto_type_action_links(snap, targets),
1676 fn should_skip_target(runnable: &Runnable, cargo_spec: Option<&CargoTargetSpec>) -> bool {
1677 match runnable.kind {
1678 RunnableKind::Bin => {
1679 // Do not suggest binary run on other target than binary
1681 Some(spec) => !matches!(
1683 TargetKind::Bin | TargetKind::Example | TargetKind::Test
1693 snap: &GlobalStateSnapshot,
1694 text_document: TextDocumentIdentifier,
1695 range: Option<lsp_types::Range>,
1696 ) -> Result<Option<Vec<lsp_types::TextEdit>>> {
1697 let file_id = from_proto::file_id(snap, &text_document.uri)?;
1698 let file = snap.analysis.file_text(file_id)?;
1699 let crate_ids = snap.analysis.crate_for(file_id)?;
1701 let line_index = snap.file_line_index(file_id)?;
1703 let mut rustfmt = match snap.config.rustfmt() {
1704 RustfmtConfig::Rustfmt { extra_args, enable_range_formatting } => {
1705 let mut cmd = process::Command::new(toolchain::rustfmt());
1706 cmd.args(extra_args);
1707 // try to chdir to the file so we can respect `rustfmt.toml`
1708 // FIXME: use `rustfmt --config-path` once
1709 // https://github.com/rust-lang/rustfmt/issues/4660 gets fixed
1710 match text_document.uri.to_file_path() {
1712 // pop off file name
1713 if path.pop() && path.is_dir() {
1714 cmd.current_dir(path);
1719 "Unable to get file path for {}, rustfmt.toml might be ignored",
1724 if let Some(&crate_id) = crate_ids.first() {
1725 // Assume all crates are in the same edition
1726 let edition = snap.analysis.crate_edition(crate_id)?;
1727 cmd.arg("--edition");
1728 cmd.arg(edition.to_string());
1731 if let Some(range) = range {
1732 if !enable_range_formatting {
1733 return Err(LspError::new(
1734 ErrorCode::InvalidRequest as i32,
1736 "rustfmt range formatting is unstable. \
1737 Opt-in by using a nightly build of rustfmt and setting \
1738 `rustfmt.enableRangeFormatting` to true in your LSP configuration",
1744 let frange = from_proto::file_range(snap, text_document, range)?;
1745 let start_line = line_index.index.line_col(frange.range.start()).line;
1746 let end_line = line_index.index.line_col(frange.range.end()).line;
1748 cmd.arg("--unstable-features");
1749 cmd.arg("--file-lines");
1753 "range": [start_line, end_line]
1761 RustfmtConfig::CustomCommand { command, args } => {
1762 let mut cmd = process::Command::new(command);
1768 let mut rustfmt = rustfmt
1769 .stdin(Stdio::piped())
1770 .stdout(Stdio::piped())
1771 .stderr(Stdio::piped())
1773 .context(format!("Failed to spawn {:?}", rustfmt))?;
1775 rustfmt.stdin.as_mut().unwrap().write_all(file.as_bytes())?;
1777 let output = rustfmt.wait_with_output()?;
1778 let captured_stdout = String::from_utf8(output.stdout)?;
1779 let captured_stderr = String::from_utf8(output.stderr).unwrap_or_default();
1781 if !output.status.success() {
1782 let rustfmt_not_installed =
1783 captured_stderr.contains("not installed") || captured_stderr.contains("not available");
1785 return match output.status.code() {
1786 Some(1) if !rustfmt_not_installed => {
1787 // While `rustfmt` doesn't have a specific exit code for parse errors this is the
1788 // likely cause exiting with 1. Most Language Servers swallow parse errors on
1789 // formatting because otherwise an error is surfaced to the user on top of the
1790 // syntax error diagnostics they're already receiving. This is especially jarring
1791 // if they have format on save enabled.
1792 tracing::info!("rustfmt exited with status 1, assuming parse error and ignoring");
1796 // Something else happened - e.g. `rustfmt` is missing or caught a signal
1800 r#"rustfmt exited with:
1804 output.status, captured_stdout, captured_stderr,
1812 let (new_text, new_line_endings) = LineEndings::normalize(captured_stdout);
1814 if line_index.endings != new_line_endings {
1815 // If line endings are different, send the entire file.
1816 // Diffing would not work here, as the line endings might be the only
1818 Ok(Some(to_proto::text_edit_vec(
1820 TextEdit::replace(TextRange::up_to(TextSize::of(&*file)), new_text),
1822 } else if *file == new_text {
1823 // The document is already formatted correctly -- no edits needed.
1826 Ok(Some(to_proto::text_edit_vec(&line_index, diff(&file, &new_text))))