From 62265ee9cb702209f95fc529c3de02b0ac973c8f Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 11 Mar 2022 21:06:26 +0100 Subject: [PATCH 1/2] fix: Allow configuration of colons in inlay-hints --- crates/ide/src/inlay_hints.rs | 12 ++++++++++++ crates/ide/src/static_index.rs | 1 + crates/rust-analyzer/src/config.rs | 3 +++ crates/rust-analyzer/src/handlers.rs | 5 +++-- crates/rust-analyzer/src/to_proto.rs | 18 ++++++++++-------- crates/test_utils/src/lib.rs | 1 + docs/user/generated_config.adoc | 5 +++++ editors/code/package.json | 5 +++++ 8 files changed, 40 insertions(+), 10 deletions(-) diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 8ecc5567b43..322a9b820a6 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -12,6 +12,7 @@ #[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 @@ fn check(ra_fixture: &str) { 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 @@ fn check_params(ra_fixture: &str) { 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 @@ fn check_types(ra_fixture: &str) { 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 @@ fn check_expect(config: InlayHintsConfig, ra_fixture: &str, expect: Expect) { 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 generic(t: T) { 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 31d85c60e7a..ef3487545ca 100644 --- a/crates/ide/src/static_index.rs +++ b/crates/ide/src/static_index.rs @@ -105,6 +105,7 @@ fn add_file(&mut self, file_id: FileId) { .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 1194fd9fafb..e50bcbb690a 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -242,6 +242,8 @@ struct ConfigData { /// `#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 @@ pub fn runnables(&self) -> RunnablesConfig { } 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 249e861f9b7..b5e9776000c 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 7809d24dd6c..6db47cdb1fd 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 871ff20a9ba..d811331ae38 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -440,6 +440,7 @@ fn drop(&mut self) { /// 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 7e6c8225b18..dd819abdd29 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 c94a09f084d..6ccd6af80c0 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, From 119ba82e4b099bf33a9eb266e518f010a6bae7be Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 11 Mar 2022 22:36:58 +0100 Subject: [PATCH 2/2] minor: add missing definitions of lsp_ext::InlayHintLabel --- crates/rust-analyzer/src/lsp_ext.rs | 20 +++++++++++++++++++- crates/rust-analyzer/src/to_proto.rs | 4 ++-- docs/dev/lsp-extensions.md | 2 +- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index 846f1513044..2e131eeac95 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs @@ -255,7 +255,7 @@ impl InlayHintKind { #[derive(Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct InlayHint { - pub label: String, + pub label: InlayHintLabel, pub position: Position, pub kind: Option, pub tooltip: Option, @@ -263,6 +263,24 @@ pub struct InlayHint { pub padding_right: Option, } +#[derive(Debug, Deserialize, Serialize)] +#[serde(untagged)] +pub enum InlayHintLabel { + String(String), + Parts(Vec), +} + +#[derive(Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct InlayHintLabelPart { + pub value: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub tooltip: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub location: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub command: Option, +} pub enum Ssr {} impl Request for Ssr { diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 6db47cdb1fd..fd6602624f3 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -419,11 +419,11 @@ pub(crate) fn inlay_hint( inlay_hint: InlayHint, ) -> lsp_ext::InlayHint { lsp_ext::InlayHint { - label: match inlay_hint.kind { + label: lsp_ext::InlayHintLabel::String(match inlay_hint.kind { 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()), InlayKind::TypeHint | InlayKind::ChainingHint => { diff --git a/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md index 9f1c7fe0a3a..3091bdcbf07 100644 --- a/docs/dev/lsp-extensions.md +++ b/docs/dev/lsp-extensions.md @@ -1,5 +1,5 @@