mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-26 11:59:49 +00:00
Auto merge of #16298 - riverbl:exclusive-range-hint, r=Veykril
feat: Add inlay hint for exclusive ranges
Adds an inlay hint containing a '<' character to exclusive range expressions and patterns that specify an upper bound.

Inspired by [this comment](https://github.com/rust-lang/rust/issues/37854#issuecomment-1865124907) noting that IntelliJ Rust has this feature.
This commit is contained in:
commit
6ce3f44597
14 changed files with 209 additions and 17 deletions
|
@ -32,6 +32,7 @@ mod fn_lifetime_fn;
|
|||
mod implicit_static;
|
||||
mod param_name;
|
||||
mod implicit_drop;
|
||||
mod range_exclusive;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct InlayHintsConfig {
|
||||
|
@ -51,6 +52,7 @@ pub struct InlayHintsConfig {
|
|||
pub param_names_for_lifetime_elision_hints: bool,
|
||||
pub hide_named_constructor_hints: bool,
|
||||
pub hide_closure_initialization_hints: bool,
|
||||
pub range_exclusive_hints: bool,
|
||||
pub closure_style: ClosureStyle,
|
||||
pub max_length: Option<usize>,
|
||||
pub closing_brace_hints_min_lines: Option<usize>,
|
||||
|
@ -127,6 +129,7 @@ pub enum InlayKind {
|
|||
Parameter,
|
||||
Type,
|
||||
Drop,
|
||||
RangeExclusive,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -517,13 +520,20 @@ fn hints(
|
|||
closure_captures::hints(hints, famous_defs, config, file_id, it.clone());
|
||||
closure_ret::hints(hints, famous_defs, config, file_id, it)
|
||||
},
|
||||
ast::Expr::RangeExpr(it) => range_exclusive::hints(hints, config, it),
|
||||
_ => None,
|
||||
}
|
||||
},
|
||||
ast::Pat(it) => {
|
||||
binding_mode::hints(hints, sema, config, &it);
|
||||
if let ast::Pat::IdentPat(it) = it {
|
||||
bind_pat::hints(hints, famous_defs, config, file_id, &it);
|
||||
match it {
|
||||
ast::Pat::IdentPat(it) => {
|
||||
bind_pat::hints(hints, famous_defs, config, file_id, &it);
|
||||
}
|
||||
ast::Pat::RangePat(it) => {
|
||||
range_exclusive::hints(hints, config, it);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Some(())
|
||||
},
|
||||
|
@ -621,6 +631,7 @@ mod tests {
|
|||
closing_brace_hints_min_lines: None,
|
||||
fields_to_resolve: InlayFieldsToResolve::empty(),
|
||||
implicit_drop_hints: false,
|
||||
range_exclusive_hints: false,
|
||||
};
|
||||
pub(super) const TEST_CONFIG: InlayHintsConfig = InlayHintsConfig {
|
||||
type_hints: true,
|
||||
|
|
121
crates/ide/src/inlay_hints/range_exclusive.rs
Normal file
121
crates/ide/src/inlay_hints/range_exclusive.rs
Normal file
|
@ -0,0 +1,121 @@
|
|||
//! Implementation of "range exclusive" inlay hints:
|
||||
//! ```no_run
|
||||
//! for i in 0../* < */10 {}
|
||||
//! if let ../* < */100 = 50 {}
|
||||
//! ```
|
||||
use syntax::{ast, SyntaxToken, T};
|
||||
|
||||
use crate::{InlayHint, InlayHintsConfig};
|
||||
|
||||
pub(super) fn hints(
|
||||
acc: &mut Vec<InlayHint>,
|
||||
config: &InlayHintsConfig,
|
||||
range: impl ast::RangeItem,
|
||||
) -> Option<()> {
|
||||
(config.range_exclusive_hints && range.end().is_some())
|
||||
.then(|| {
|
||||
range.op_token().filter(|token| token.kind() == T![..]).map(|token| {
|
||||
acc.push(inlay_hint(token));
|
||||
})
|
||||
})
|
||||
.flatten()
|
||||
}
|
||||
|
||||
fn inlay_hint(token: SyntaxToken) -> InlayHint {
|
||||
InlayHint {
|
||||
range: token.text_range(),
|
||||
position: crate::InlayHintPosition::After,
|
||||
pad_left: false,
|
||||
pad_right: false,
|
||||
kind: crate::InlayKind::RangeExclusive,
|
||||
label: crate::InlayHintLabel::from("<"),
|
||||
text_edit: None,
|
||||
needs_resolve: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{
|
||||
inlay_hints::tests::{check_with_config, DISABLED_CONFIG},
|
||||
InlayHintsConfig,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn range_exclusive_expression_bounded_above_hints() {
|
||||
check_with_config(
|
||||
InlayHintsConfig { range_exclusive_hints: true, ..DISABLED_CONFIG },
|
||||
r#"
|
||||
fn main() {
|
||||
let a = 0..10;
|
||||
//^^<
|
||||
let b = ..100;
|
||||
//^^<
|
||||
let c = (2 - 1)..(7 * 8)
|
||||
//^^<
|
||||
}"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn range_exclusive_expression_unbounded_above_no_hints() {
|
||||
check_with_config(
|
||||
InlayHintsConfig { range_exclusive_hints: true, ..DISABLED_CONFIG },
|
||||
r#"
|
||||
fn main() {
|
||||
let a = 0..;
|
||||
let b = ..;
|
||||
}"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn range_inclusive_expression_no_hints() {
|
||||
check_with_config(
|
||||
InlayHintsConfig { range_exclusive_hints: true, ..DISABLED_CONFIG },
|
||||
r#"
|
||||
fn main() {
|
||||
let a = 0..=10;
|
||||
let b = ..=100;
|
||||
}"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn range_exclusive_pattern_bounded_above_hints() {
|
||||
check_with_config(
|
||||
InlayHintsConfig { range_exclusive_hints: true, ..DISABLED_CONFIG },
|
||||
r#"
|
||||
fn main() {
|
||||
if let 0..10 = 0 {}
|
||||
//^^<
|
||||
if let ..100 = 0 {}
|
||||
//^^<
|
||||
}"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn range_exclusive_pattern_unbounded_above_no_hints() {
|
||||
check_with_config(
|
||||
InlayHintsConfig { range_exclusive_hints: true, ..DISABLED_CONFIG },
|
||||
r#"
|
||||
fn main() {
|
||||
if let 0.. = 0 {}
|
||||
if let .. = 0 {}
|
||||
}"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn range_inclusive_pattern_no_hints() {
|
||||
check_with_config(
|
||||
InlayHintsConfig { range_exclusive_hints: true, ..DISABLED_CONFIG },
|
||||
r#"
|
||||
fn main() {
|
||||
if let 0..=10 = 0 {}
|
||||
if let ..=100 = 0 {}
|
||||
}"#,
|
||||
);
|
||||
}
|
||||
}
|
|
@ -133,6 +133,7 @@ impl StaticIndex<'_> {
|
|||
closure_capture_hints: false,
|
||||
closing_brace_hints_min_lines: Some(25),
|
||||
fields_to_resolve: InlayFieldsToResolve::empty(),
|
||||
range_exclusive_hints: false,
|
||||
},
|
||||
file_id,
|
||||
None,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue