Add inlay hint for exclusive ranges

Adds an inlay hint containing a '<' character to exclusive range expressions and patterns that specify an upper bound.
This commit is contained in:
riverbl 2024-01-06 22:03:45 +00:00
parent f595e60b6d
commit 3c378b9c70
14 changed files with 209 additions and 17 deletions

View file

@ -14,6 +14,8 @@ use crate::{
ted, NodeOrToken, SmolStr, SyntaxElement, SyntaxToken, TokenText, T,
};
use super::{RangeItem, RangeOp};
impl ast::Lifetime {
pub fn text(&self) -> TokenText<'_> {
text_of_first_token(self.syntax())
@ -875,8 +877,10 @@ impl ast::Module {
}
}
impl ast::RangePat {
pub fn start(&self) -> Option<ast::Pat> {
impl RangeItem for ast::RangePat {
type Bound = ast::Pat;
fn start(&self) -> Option<ast::Pat> {
self.syntax()
.children_with_tokens()
.take_while(|it| !(it.kind() == T![..] || it.kind() == T![..=]))
@ -884,13 +888,37 @@ impl ast::RangePat {
.find_map(ast::Pat::cast)
}
pub fn end(&self) -> Option<ast::Pat> {
fn end(&self) -> Option<ast::Pat> {
self.syntax()
.children_with_tokens()
.skip_while(|it| !(it.kind() == T![..] || it.kind() == T![..=]))
.filter_map(|it| it.into_node())
.find_map(ast::Pat::cast)
}
fn op_token(&self) -> Option<SyntaxToken> {
self.syntax().children_with_tokens().find_map(|it| {
let token = it.into_token()?;
match token.kind() {
T![..] => Some(token),
T![..=] => Some(token),
_ => None,
}
})
}
fn op_kind(&self) -> Option<RangeOp> {
self.syntax().children_with_tokens().find_map(|it| {
let token = it.into_token()?;
match token.kind() {
T![..] => Some(RangeOp::Exclusive),
T![..=] => Some(RangeOp::Inclusive),
_ => None,
}
})
}
}
impl ast::TokenTree {