diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 8ecc5567b4..322a9b820a 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -12,6 +12,7 @@ use crate::FileId; #[derive(Clone, Debug, PartialEq, Eq)] pub struct InlayHintsConfig { + pub render_colons: bool, pub type_hints: bool, pub parameter_hints: bool, pub chaining_hints: bool, @@ -574,6 +575,7 @@ mod tests { use crate::{fixture, inlay_hints::InlayHintsConfig}; const TEST_CONFIG: InlayHintsConfig = InlayHintsConfig { + render_colons: true, type_hints: true, parameter_hints: true, chaining_hints: true, @@ -590,6 +592,7 @@ mod tests { fn check_params(ra_fixture: &str) { check_with_config( InlayHintsConfig { + render_colons: true, parameter_hints: true, type_hints: false, chaining_hints: false, @@ -604,6 +607,7 @@ mod tests { fn check_types(ra_fixture: &str) { check_with_config( InlayHintsConfig { + render_colons: true, parameter_hints: false, type_hints: true, chaining_hints: false, @@ -618,6 +622,7 @@ mod tests { fn check_chains(ra_fixture: &str) { check_with_config( InlayHintsConfig { + render_colons: true, parameter_hints: false, type_hints: false, chaining_hints: true, @@ -649,6 +654,7 @@ mod tests { fn hints_disabled() { check_with_config( InlayHintsConfig { + render_colons: true, type_hints: false, parameter_hints: false, chaining_hints: false, @@ -1104,6 +1110,7 @@ fn main() { let inlay_hints = analysis .inlay_hints( &InlayHintsConfig { + render_colons: true, parameter_hints: false, type_hints: true, chaining_hints: false, @@ -1413,6 +1420,7 @@ fn main() { fn skip_constructor_and_enum_type_hints() { check_with_config( InlayHintsConfig { + render_colons: true, type_hints: true, parameter_hints: true, chaining_hints: true, @@ -1590,6 +1598,7 @@ fn main() { fn chaining_hints_ignore_comments() { check_expect( InlayHintsConfig { + render_colons: true, parameter_hints: false, type_hints: false, chaining_hints: true, @@ -1647,6 +1656,7 @@ fn main() { fn struct_access_chaining_hints() { check_expect( InlayHintsConfig { + render_colons: true, parameter_hints: false, type_hints: false, chaining_hints: true, @@ -1692,6 +1702,7 @@ fn main() { fn generic_chaining_hints() { check_expect( InlayHintsConfig { + render_colons: true, parameter_hints: false, type_hints: false, chaining_hints: true, @@ -1738,6 +1749,7 @@ fn main() { fn shorten_iterator_chaining_hints() { check_expect( InlayHintsConfig { + render_colons: true, parameter_hints: false, type_hints: false, chaining_hints: true, diff --git a/crates/ide/src/static_index.rs b/crates/ide/src/static_index.rs index 31d85c60e7..ef3487545c 100644 --- a/crates/ide/src/static_index.rs +++ b/crates/ide/src/static_index.rs @@ -105,6 +105,7 @@ impl StaticIndex<'_> { .analysis .inlay_hints( &InlayHintsConfig { + render_colons: true, type_hints: true, parameter_hints: true, chaining_hints: true, diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 1194fd9faf..e50bcbb690 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -242,6 +242,8 @@ config_data! { /// `#rust-analyzer.hoverActions.enable#` is set. hoverActions_run: bool = "true", + /// Whether to render trailing colons for parameter hints, and trailing colons for parameter hints. + inlayHints_renderColons: bool = "true", /// Whether to show inlay type hints for method chains. inlayHints_chainingHints: bool = "true", /// Maximum length for inlay hints. Set to null to have an unlimited length. @@ -846,6 +848,7 @@ impl Config { } pub fn inlay_hints(&self) -> InlayHintsConfig { InlayHintsConfig { + render_colons: self.data.inlayHints_renderColons, type_hints: self.data.inlayHints_typeHints, parameter_hints: self.data.inlayHints_parameterHints, chaining_hints: self.data.inlayHints_chainingHints, diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 249e861f9b..b5e9776000 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -1331,11 +1331,12 @@ pub(crate) fn handle_inlay_hints( ) }) .transpose()?; + let inlay_hints_config = snap.config.inlay_hints(); Ok(snap .analysis - .inlay_hints(&snap.config.inlay_hints(), file_id, range)? + .inlay_hints(&inlay_hints_config, file_id, range)? .into_iter() - .map(|it| to_proto::inlay_hint(&line_index, it)) + .map(|it| to_proto::inlay_hint(inlay_hints_config.render_colons, &line_index, it)) .collect()) } diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 7809d24dd6..6db47cdb1f 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -413,12 +413,16 @@ pub(crate) fn signature_help( } } -pub(crate) fn inlay_hint(line_index: &LineIndex, inlay_hint: InlayHint) -> lsp_ext::InlayHint { +pub(crate) fn inlay_hint( + render_colons: bool, + line_index: &LineIndex, + inlay_hint: InlayHint, +) -> lsp_ext::InlayHint { lsp_ext::InlayHint { label: match inlay_hint.kind { - InlayKind::ParameterHint => format!("{}:", inlay_hint.label), - InlayKind::TypeHint => format!(": {}", inlay_hint.label), - InlayKind::ChainingHint => inlay_hint.label.to_string(), + InlayKind::ParameterHint if render_colons => format!("{}:", inlay_hint.label), + InlayKind::TypeHint if render_colons => format!(": {}", inlay_hint.label), + _ => inlay_hint.label.to_string(), }, position: match inlay_hint.kind { InlayKind::ParameterHint => position(line_index, inlay_hint.range.start()), @@ -433,14 +437,12 @@ pub(crate) fn inlay_hint(line_index: &LineIndex, inlay_hint: InlayHint) -> lsp_e }, tooltip: None, padding_left: Some(match inlay_hint.kind { - InlayKind::TypeHint => false, - InlayKind::ParameterHint => false, + InlayKind::TypeHint | InlayKind::ParameterHint => false, InlayKind::ChainingHint => true, }), padding_right: Some(match inlay_hint.kind { - InlayKind::TypeHint => false, + InlayKind::TypeHint | InlayKind::ChainingHint => false, InlayKind::ParameterHint => true, - InlayKind::ChainingHint => false, }), } } diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 871ff20a9b..d811331ae3 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -440,6 +440,7 @@ pub fn bench(label: &'static str) -> impl Drop { /// Checks that the `file` has the specified `contents`. If that is not the /// case, updates the file and then fails the test. +#[track_caller] pub fn ensure_file_contents(file: &Path, contents: &str) { if let Err(()) = try_ensure_file_contents(file, contents) { panic!("Some files were not up-to-date"); diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index 7e6c8225b1..dd819abdd2 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -347,6 +347,11 @@ Whether to show `References` action. Only applies when Whether to show `Run` action. Only applies when `#rust-analyzer.hoverActions.enable#` is set. -- +[[rust-analyzer.inlayHints.renderColons]]rust-analyzer.inlayHints.renderColons (default: `true`):: ++ +-- +Whether to render trailing colons for parameter hints, and trailing colons for parameter hints. +-- [[rust-analyzer.inlayHints.chainingHints]]rust-analyzer.inlayHints.chainingHints (default: `true`):: + -- diff --git a/editors/code/package.json b/editors/code/package.json index c94a09f084..6ccd6af80c 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -766,6 +766,11 @@ "default": true, "type": "boolean" }, + "rust-analyzer.inlayHints.renderColons": { + "markdownDescription": "Whether to render trailing colons for parameter hints, and trailing colons for parameter hints.", + "default": true, + "type": "boolean" + }, "rust-analyzer.inlayHints.chainingHints": { "markdownDescription": "Whether to show inlay type hints for method chains.", "default": true,