]> git.lizzy.rs Git - rust.git/blobdiff - crates/rust-analyzer/src/handlers.rs
Switch to LSP inlay hints
[rust.git] / crates / rust-analyzer / src / handlers.rs
index 4160b3ecd1fbbe3d764f578c77ec160145db79dc..fb64eeea4f657d2aa88049dd26676930fa8399c9 100644 (file)
     CallHierarchyIncomingCall, CallHierarchyIncomingCallsParams, CallHierarchyItem,
     CallHierarchyOutgoingCall, CallHierarchyOutgoingCallsParams, CallHierarchyPrepareParams,
     CodeLens, CompletionItem, Diagnostic, DiagnosticTag, DocumentFormattingParams, FoldingRange,
-    FoldingRangeParams, HoverContents, Location, LocationLink, NumberOrString, Position,
-    PrepareRenameResponse, Range, RenameParams, SemanticTokensDeltaParams,
-    SemanticTokensFullDeltaResult, SemanticTokensParams, SemanticTokensRangeParams,
-    SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation, SymbolTag,
-    TextDocumentIdentifier, Url, WorkspaceEdit,
+    FoldingRangeParams, HoverContents, InlayHint, InlayHintParams, Location, LocationLink,
+    NumberOrString, Position, PrepareRenameResponse, Range, RenameParams,
+    SemanticTokensDeltaParams, SemanticTokensFullDeltaResult, SemanticTokensParams,
+    SemanticTokensRangeParams, SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation,
+    SymbolTag, TextDocumentIdentifier, Url, WorkspaceEdit,
 };
 use project_model::{ManifestPath, ProjectWorkspace, TargetKind};
 use serde_json::json;
 
 use crate::{
     cargo_target_spec::CargoTargetSpec,
-    config::RustfmtConfig,
+    config::{RustfmtConfig, WorkspaceSymbolConfig},
     diff::diff,
     from_proto,
     global_state::{GlobalState, GlobalStateSnapshot},
     line_index::LineEndings,
-    lsp_ext::{
-        self, InlayHint, InlayHintsParams, PositionOrRange, ViewCrateGraphParams,
-        WorkspaceSymbolParams,
-    },
+    lsp_ext::{self, PositionOrRange, ViewCrateGraphParams, WorkspaceSymbolParams},
     lsp_utils::{all_edits_are_disjoint, invalid_params_error},
     to_proto, LspError, Result,
 };
@@ -123,6 +120,14 @@ pub(crate) fn handle_view_hir(
     Ok(res)
 }
 
+pub(crate) fn handle_view_file_text(
+    snap: GlobalStateSnapshot,
+    params: lsp_types::TextDocumentIdentifier,
+) -> Result<String> {
+    let file_id = from_proto::file_id(&snap, &params.uri)?;
+    Ok(snap.analysis.file_text(file_id)?.to_string())
+}
+
 pub(crate) fn handle_view_item_tree(
     snap: GlobalStateSnapshot,
     params: lsp_ext::ViewItemTreeParams,
@@ -275,9 +280,7 @@ pub(crate) fn handle_on_type_formatting(
     let char_typed = params.ch.chars().next().unwrap_or('\0');
 
     let text = snap.analysis.file_text(position.file_id)?;
-    if !text[usize::from(position.offset)..].starts_with(char_typed) {
-        // Add `always!` here once VS Code bug is fixed:
-        //   https://github.com/rust-analyzer/rust-analyzer/issues/10002
+    if stdx::never!(!text[usize::from(position.offset)..].starts_with(char_typed)) {
         return Ok(None);
     }
 
@@ -397,7 +400,9 @@ pub(crate) fn handle_workspace_symbol(
 ) -> Result<Option<Vec<SymbolInformation>>> {
     let _p = profile::span("handle_workspace_symbol");
 
-    let (all_symbols, libs) = decide_search_scope_and_kind(&params, &snap);
+    let config = snap.config.workspace_symbol();
+    let (all_symbols, libs) = decide_search_scope_and_kind(&params, &config);
+    let limit = config.search_limit;
 
     let query = {
         let query: String = params.query.chars().filter(|&c| c != '#' && c != '*').collect();
@@ -408,13 +413,13 @@ pub(crate) fn handle_workspace_symbol(
         if libs {
             q.libs();
         }
-        q.limit(128);
+        q.limit(limit);
         q
     };
     let mut res = exec_query(&snap, query)?;
     if res.is_empty() && !all_symbols {
         let mut query = Query::new(params.query);
-        query.limit(128);
+        query.limit(limit);
         res = exec_query(&snap, query)?;
     }
 
@@ -422,14 +427,12 @@ pub(crate) fn handle_workspace_symbol(
 
     fn decide_search_scope_and_kind(
         params: &WorkspaceSymbolParams,
-        snap: &GlobalStateSnapshot,
+        config: &WorkspaceSymbolConfig,
     ) -> (bool, bool) {
         // Support old-style parsing of markers in the query.
         let mut all_symbols = params.query.contains('#');
         let mut libs = params.query.contains('*');
 
-        let config = snap.config.workspace_symbol();
-
         // If no explicit marker was set, check request params. If that's also empty
         // use global config.
         if !all_symbols {
@@ -534,7 +537,7 @@ pub(crate) fn handle_will_rename_files(
     let mut source_change = source_changes.next().unwrap_or_default();
     source_change.file_system_edits.clear();
     // no collect here because we want to merge text edits on same file ids
-    source_change.extend(source_changes.map(|it| it.source_file_edits).flatten());
+    source_change.extend(source_changes.flat_map(|it| it.source_file_edits));
     if source_change.source_file_edits.is_empty() {
         Ok(None)
     } else {
@@ -897,13 +900,12 @@ pub(crate) fn handle_signature_help(
 ) -> Result<Option<lsp_types::SignatureHelp>> {
     let _p = profile::span("handle_signature_help");
     let position = from_proto::file_position(&snap, params.text_document_position_params)?;
-    let call_info = match snap.analysis.call_info(position)? {
+    let help = match snap.analysis.signature_help(position)? {
         Some(it) => it,
         None => return Ok(None),
     };
     let concise = !snap.config.call_info_full();
-    let res =
-        to_proto::signature_help(call_info, concise, snap.config.signature_help_label_offsets());
+    let res = to_proto::signature_help(help, concise, snap.config.signature_help_label_offsets());
     Ok(Some(res))
 }
 
@@ -1317,17 +1319,25 @@ pub(crate) fn publish_diagnostics(
 
 pub(crate) fn handle_inlay_hints(
     snap: GlobalStateSnapshot,
-    params: InlayHintsParams,
-) -> Result<Vec<InlayHint>> {
+    params: InlayHintParams,
+) -> Result<Option<Vec<InlayHint>>> {
     let _p = profile::span("handle_inlay_hints");
-    let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
+    let document_uri = &params.text_document.uri;
+    let file_id = from_proto::file_id(&snap, document_uri)?;
     let line_index = snap.file_line_index(file_id)?;
-    Ok(snap
-        .analysis
-        .inlay_hints(&snap.config.inlay_hints(), file_id)?
-        .into_iter()
-        .map(|it| to_proto::inlay_hint(&line_index, it))
-        .collect())
+    let range = from_proto::file_range(
+        &snap,
+        TextDocumentIdentifier::new(document_uri.to_owned()),
+        params.range,
+    )?;
+    let inlay_hints_config = snap.config.inlay_hints();
+    Ok(Some(
+        snap.analysis
+            .inlay_hints(&inlay_hints_config, file_id, Some(range))?
+            .into_iter()
+            .map(|it| to_proto::inlay_hint(inlay_hints_config.render_colons, &line_index, it))
+            .collect(),
+    ))
 }
 
 pub(crate) fn handle_call_hierarchy_prepare(