fix: Allow configuration of colons in inlay-hints

This commit is contained in:
Lukas Wirth 2022-03-11 21:06:26 +01:00
parent 224a255c5a
commit 62265ee9cb
8 changed files with 40 additions and 10 deletions

View file

@ -12,6 +12,7 @@ use crate::FileId;
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct InlayHintsConfig { pub struct InlayHintsConfig {
pub render_colons: bool,
pub type_hints: bool, pub type_hints: bool,
pub parameter_hints: bool, pub parameter_hints: bool,
pub chaining_hints: bool, pub chaining_hints: bool,
@ -574,6 +575,7 @@ mod tests {
use crate::{fixture, inlay_hints::InlayHintsConfig}; use crate::{fixture, inlay_hints::InlayHintsConfig};
const TEST_CONFIG: InlayHintsConfig = InlayHintsConfig { const TEST_CONFIG: InlayHintsConfig = InlayHintsConfig {
render_colons: true,
type_hints: true, type_hints: true,
parameter_hints: true, parameter_hints: true,
chaining_hints: true, chaining_hints: true,
@ -590,6 +592,7 @@ mod tests {
fn check_params(ra_fixture: &str) { fn check_params(ra_fixture: &str) {
check_with_config( check_with_config(
InlayHintsConfig { InlayHintsConfig {
render_colons: true,
parameter_hints: true, parameter_hints: true,
type_hints: false, type_hints: false,
chaining_hints: false, chaining_hints: false,
@ -604,6 +607,7 @@ mod tests {
fn check_types(ra_fixture: &str) { fn check_types(ra_fixture: &str) {
check_with_config( check_with_config(
InlayHintsConfig { InlayHintsConfig {
render_colons: true,
parameter_hints: false, parameter_hints: false,
type_hints: true, type_hints: true,
chaining_hints: false, chaining_hints: false,
@ -618,6 +622,7 @@ mod tests {
fn check_chains(ra_fixture: &str) { fn check_chains(ra_fixture: &str) {
check_with_config( check_with_config(
InlayHintsConfig { InlayHintsConfig {
render_colons: true,
parameter_hints: false, parameter_hints: false,
type_hints: false, type_hints: false,
chaining_hints: true, chaining_hints: true,
@ -649,6 +654,7 @@ mod tests {
fn hints_disabled() { fn hints_disabled() {
check_with_config( check_with_config(
InlayHintsConfig { InlayHintsConfig {
render_colons: true,
type_hints: false, type_hints: false,
parameter_hints: false, parameter_hints: false,
chaining_hints: false, chaining_hints: false,
@ -1104,6 +1110,7 @@ fn main() {
let inlay_hints = analysis let inlay_hints = analysis
.inlay_hints( .inlay_hints(
&InlayHintsConfig { &InlayHintsConfig {
render_colons: true,
parameter_hints: false, parameter_hints: false,
type_hints: true, type_hints: true,
chaining_hints: false, chaining_hints: false,
@ -1413,6 +1420,7 @@ fn main() {
fn skip_constructor_and_enum_type_hints() { fn skip_constructor_and_enum_type_hints() {
check_with_config( check_with_config(
InlayHintsConfig { InlayHintsConfig {
render_colons: true,
type_hints: true, type_hints: true,
parameter_hints: true, parameter_hints: true,
chaining_hints: true, chaining_hints: true,
@ -1590,6 +1598,7 @@ fn main() {
fn chaining_hints_ignore_comments() { fn chaining_hints_ignore_comments() {
check_expect( check_expect(
InlayHintsConfig { InlayHintsConfig {
render_colons: true,
parameter_hints: false, parameter_hints: false,
type_hints: false, type_hints: false,
chaining_hints: true, chaining_hints: true,
@ -1647,6 +1656,7 @@ fn main() {
fn struct_access_chaining_hints() { fn struct_access_chaining_hints() {
check_expect( check_expect(
InlayHintsConfig { InlayHintsConfig {
render_colons: true,
parameter_hints: false, parameter_hints: false,
type_hints: false, type_hints: false,
chaining_hints: true, chaining_hints: true,
@ -1692,6 +1702,7 @@ fn main() {
fn generic_chaining_hints() { fn generic_chaining_hints() {
check_expect( check_expect(
InlayHintsConfig { InlayHintsConfig {
render_colons: true,
parameter_hints: false, parameter_hints: false,
type_hints: false, type_hints: false,
chaining_hints: true, chaining_hints: true,
@ -1738,6 +1749,7 @@ fn main() {
fn shorten_iterator_chaining_hints() { fn shorten_iterator_chaining_hints() {
check_expect( check_expect(
InlayHintsConfig { InlayHintsConfig {
render_colons: true,
parameter_hints: false, parameter_hints: false,
type_hints: false, type_hints: false,
chaining_hints: true, chaining_hints: true,

View file

@ -105,6 +105,7 @@ impl StaticIndex<'_> {
.analysis .analysis
.inlay_hints( .inlay_hints(
&InlayHintsConfig { &InlayHintsConfig {
render_colons: true,
type_hints: true, type_hints: true,
parameter_hints: true, parameter_hints: true,
chaining_hints: true, chaining_hints: true,

View file

@ -242,6 +242,8 @@ config_data! {
/// `#rust-analyzer.hoverActions.enable#` is set. /// `#rust-analyzer.hoverActions.enable#` is set.
hoverActions_run: bool = "true", 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. /// Whether to show inlay type hints for method chains.
inlayHints_chainingHints: bool = "true", inlayHints_chainingHints: bool = "true",
/// Maximum length for inlay hints. Set to null to have an unlimited length. /// 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 { pub fn inlay_hints(&self) -> InlayHintsConfig {
InlayHintsConfig { InlayHintsConfig {
render_colons: self.data.inlayHints_renderColons,
type_hints: self.data.inlayHints_typeHints, type_hints: self.data.inlayHints_typeHints,
parameter_hints: self.data.inlayHints_parameterHints, parameter_hints: self.data.inlayHints_parameterHints,
chaining_hints: self.data.inlayHints_chainingHints, chaining_hints: self.data.inlayHints_chainingHints,

View file

@ -1331,11 +1331,12 @@ pub(crate) fn handle_inlay_hints(
) )
}) })
.transpose()?; .transpose()?;
let inlay_hints_config = snap.config.inlay_hints();
Ok(snap Ok(snap
.analysis .analysis
.inlay_hints(&snap.config.inlay_hints(), file_id, range)? .inlay_hints(&inlay_hints_config, file_id, range)?
.into_iter() .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()) .collect())
} }

View file

@ -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 { lsp_ext::InlayHint {
label: match inlay_hint.kind { label: match inlay_hint.kind {
InlayKind::ParameterHint => format!("{}:", inlay_hint.label), InlayKind::ParameterHint if render_colons => format!("{}:", inlay_hint.label),
InlayKind::TypeHint => format!(": {}", inlay_hint.label), InlayKind::TypeHint if render_colons => format!(": {}", inlay_hint.label),
InlayKind::ChainingHint => inlay_hint.label.to_string(), _ => inlay_hint.label.to_string(),
}, },
position: match inlay_hint.kind { position: match inlay_hint.kind {
InlayKind::ParameterHint => position(line_index, inlay_hint.range.start()), 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, tooltip: None,
padding_left: Some(match inlay_hint.kind { padding_left: Some(match inlay_hint.kind {
InlayKind::TypeHint => false, InlayKind::TypeHint | InlayKind::ParameterHint => false,
InlayKind::ParameterHint => false,
InlayKind::ChainingHint => true, InlayKind::ChainingHint => true,
}), }),
padding_right: Some(match inlay_hint.kind { padding_right: Some(match inlay_hint.kind {
InlayKind::TypeHint => false, InlayKind::TypeHint | InlayKind::ChainingHint => false,
InlayKind::ParameterHint => true, InlayKind::ParameterHint => true,
InlayKind::ChainingHint => false,
}), }),
} }
} }

View file

@ -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 /// Checks that the `file` has the specified `contents`. If that is not the
/// case, updates the file and then fails the test. /// case, updates the file and then fails the test.
#[track_caller]
pub fn ensure_file_contents(file: &Path, contents: &str) { pub fn ensure_file_contents(file: &Path, contents: &str) {
if let Err(()) = try_ensure_file_contents(file, contents) { if let Err(()) = try_ensure_file_contents(file, contents) {
panic!("Some files were not up-to-date"); panic!("Some files were not up-to-date");

View file

@ -347,6 +347,11 @@ Whether to show `References` action. Only applies when
Whether to show `Run` action. Only applies when Whether to show `Run` action. Only applies when
`#rust-analyzer.hoverActions.enable#` is set. `#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`):: [[rust-analyzer.inlayHints.chainingHints]]rust-analyzer.inlayHints.chainingHints (default: `true`)::
+ +
-- --

View file

@ -766,6 +766,11 @@
"default": true, "default": true,
"type": "boolean" "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": { "rust-analyzer.inlayHints.chainingHints": {
"markdownDescription": "Whether to show inlay type hints for method chains.", "markdownDescription": "Whether to show inlay type hints for method chains.",
"default": true, "default": true,