-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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. ![2024-01-07-095056_257x415_scrot](https://github.com/rust-lang/rust-analyzer/assets/94326797/d6bbc0de-52a5-4af4-b53c-a034749b6cab) Inspired by [this comment](rust-lang/rust#37854 (comment)) noting that IntelliJ Rust has this feature.
- Loading branch information
Showing
14 changed files
with
209 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 {} | ||
}"#, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters