about summary refs log tree commit diff
diff options
context:
space:
mode:
authorKirill Bulatov <mail4score@gmail.com>2023-08-29 11:14:21 +0300
committerKirill Bulatov <mail4score@gmail.com>2023-09-02 18:28:36 +0300
commit10464c7c42950f1138290670c7177873f8fc3cbe (patch)
tree7d138f10161379e770ca2cfef2b9e098184c69f1
parente07fbabcfef2069e29ae053d92fbeaa0329492fc (diff)
downloadrust-10464c7c42950f1138290670c7177873f8fc3cbe.tar.gz
rust-10464c7c42950f1138290670c7177873f8fc3cbe.zip
Simplify InlayFieldsToResolve handling
-rw-r--r--crates/ide/src/inlay_hints.rs58
-rw-r--r--crates/rust-analyzer/src/config.rs10
-rw-r--r--crates/rust-analyzer/src/lsp/to_proto.rs14
3 files changed, 35 insertions, 47 deletions
diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs
index f61252d84da..8061ee5ee20 100644
--- a/crates/ide/src/inlay_hints.rs
+++ b/crates/ide/src/inlay_hints.rs
@@ -55,38 +55,32 @@ pub struct InlayHintsConfig {
     pub fields_to_resolve: InlayFieldsToResolve,
 }
 
-#[derive(Clone, Debug, PartialEq, Eq)]
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
 pub struct InlayFieldsToResolve {
-    pub client_capability_fields: Vec<String>,
+    pub resolve_text_edits: bool,
+    pub resolve_hint_tooltip: bool,
+    pub resolve_label_tooltip: bool,
+    pub resolve_label_location: bool,
+    pub resolve_label_command: bool,
 }
 
 impl InlayFieldsToResolve {
     pub const fn empty() -> Self {
-        Self { client_capability_fields: Vec::new() }
-    }
-
-    pub fn is_empty(&self) -> bool {
-        self.client_capability_fields.is_empty()
-    }
-
-    pub fn resolve_text_edits(&self) -> bool {
-        self.client_capability_fields.iter().find(|s| s.as_str() == "textEdits").is_some()
-    }
-
-    pub fn resolve_hint_tooltip(&self) -> bool {
-        self.client_capability_fields.iter().find(|s| s.as_str() == "tooltip").is_some()
-    }
-
-    pub fn resolve_label_tooltip(&self) -> bool {
-        self.client_capability_fields.iter().find(|s| s.as_str() == "label.tooltip").is_some()
-    }
-
-    pub fn resolve_label_location(&self) -> bool {
-        self.client_capability_fields.iter().find(|s| s.as_str() == "label.location").is_some()
+        Self {
+            resolve_text_edits: false,
+            resolve_hint_tooltip: false,
+            resolve_label_tooltip: false,
+            resolve_label_location: false,
+            resolve_label_command: false,
+        }
     }
 
-    pub fn resolve_label_command(&self) -> bool {
-        self.client_capability_fields.iter().find(|s| s.as_str() == "label.command").is_some()
+    pub fn can_resolve(&self) -> bool {
+        self.resolve_text_edits
+            || self.resolve_hint_tooltip
+            || self.resolve_label_tooltip
+            || self.resolve_label_location
+            || self.resolve_label_command
     }
 }
 
@@ -605,19 +599,7 @@ mod tests {
         closure_return_type_hints: ClosureReturnTypeHints::WithBlock,
         binding_mode_hints: true,
         lifetime_elision_hints: LifetimeElisionHints::Always,
-        discriminant_hints: DiscriminantHints::Never,
-        render_colons: false,
-        closure_capture_hints: false,
-        adjustment_hints: AdjustmentHints::Never,
-        adjustment_hints_mode: AdjustmentHintsMode::Prefix,
-        adjustment_hints_hide_outside_unsafe: false,
-        hide_named_constructor_hints: false,
-        hide_closure_initialization_hints: false,
-        closure_style: ClosureStyle::ImplFn,
-        param_names_for_lifetime_elision_hints: false,
-        max_length: None,
-        closing_brace_hints_min_lines: None,
-        fields_to_resolve: InlayFieldsToResolve::empty(),
+        ..DISABLED_CONFIG
     };
 
     #[track_caller]
diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs
index f58de03ec46..ea3a21241cb 100644
--- a/crates/rust-analyzer/src/config.rs
+++ b/crates/rust-analyzer/src/config.rs
@@ -1346,7 +1346,7 @@ impl Config {
             .into_iter()
             .flatten()
             .cloned()
-            .collect::<Vec<_>>();
+            .collect::<FxHashSet<_>>();
 
         InlayHintsConfig {
             render_colons: self.data.inlayHints_renderColons,
@@ -1408,7 +1408,13 @@ impl Config {
             } else {
                 None
             },
-            fields_to_resolve: InlayFieldsToResolve { client_capability_fields },
+            fields_to_resolve: InlayFieldsToResolve {
+                resolve_text_edits: client_capability_fields.contains("textEdits"),
+                resolve_hint_tooltip: client_capability_fields.contains("tooltip"),
+                resolve_label_tooltip: client_capability_fields.contains("label.tooltip"),
+                resolve_label_location: client_capability_fields.contains("label.location"),
+                resolve_label_command: client_capability_fields.contains("label.command"),
+            },
         }
     }
 
diff --git a/crates/rust-analyzer/src/lsp/to_proto.rs b/crates/rust-analyzer/src/lsp/to_proto.rs
index 758dc66b43c..4f9a026aa19 100644
--- a/crates/rust-analyzer/src/lsp/to_proto.rs
+++ b/crates/rust-analyzer/src/lsp/to_proto.rs
@@ -443,12 +443,12 @@ pub(crate) fn inlay_hint(
     inlay_hint: InlayHint,
 ) -> Cancellable<lsp_types::InlayHint> {
     let (label, tooltip) = inlay_hint_label(snap, fields_to_resolve, inlay_hint.label)?;
-    let data = if fields_to_resolve.is_empty() {
-        None
-    } else {
+    let data = if fields_to_resolve.can_resolve() {
         Some(to_value(lsp_ext::InlayHintResolveData { file_id: file_id.0 }).unwrap())
+    } else {
+        None
     };
-    let text_edits = if fields_to_resolve.resolve_text_edits() {
+    let text_edits = if fields_to_resolve.resolve_text_edits {
         None
     } else {
         inlay_hint.text_edit.map(|it| text_edit_vec(line_index, it))
@@ -481,7 +481,7 @@ fn inlay_hint_label(
     let res = match &*label.parts {
         [InlayHintLabelPart { linked_location: None, .. }] => {
             let InlayHintLabelPart { text, tooltip, .. } = label.parts.pop().unwrap();
-            let hint_tooltip = if fields_to_resolve.resolve_hint_tooltip() {
+            let hint_tooltip = if fields_to_resolve.resolve_hint_tooltip {
                 None
             } else {
                 match tooltip {
@@ -504,7 +504,7 @@ fn inlay_hint_label(
                 .parts
                 .into_iter()
                 .map(|part| {
-                    let tooltip = if fields_to_resolve.resolve_label_tooltip() {
+                    let tooltip = if fields_to_resolve.resolve_label_tooltip {
                         None
                     } else {
                         match part.tooltip {
@@ -522,7 +522,7 @@ fn inlay_hint_label(
                             None => None,
                         }
                     };
-                    let location = if fields_to_resolve.resolve_label_location() {
+                    let location = if fields_to_resolve.resolve_label_location {
                         None
                     } else {
                         part.linked_location.map(|range| location(snap, range)).transpose()?