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

@ -13,6 +13,8 @@ use crate::{
SyntaxNode, SyntaxToken, T,
};
use super::RangeItem;
impl ast::HasAttrs for ast::Expr {}
impl ast::Expr {
@ -227,16 +229,12 @@ impl ast::RangeExpr {
Some((ix, token, bin_op))
})
}
}
pub fn op_kind(&self) -> Option<RangeOp> {
self.op_details().map(|t| t.2)
}
impl RangeItem for ast::RangeExpr {
type Bound = ast::Expr;
pub fn op_token(&self) -> Option<SyntaxToken> {
self.op_details().map(|t| t.1)
}
pub fn start(&self) -> Option<ast::Expr> {
fn start(&self) -> Option<ast::Expr> {
let op_ix = self.op_details()?.0;
self.syntax()
.children_with_tokens()
@ -244,13 +242,21 @@ impl ast::RangeExpr {
.find_map(|it| ast::Expr::cast(it.into_node()?))
}
pub fn end(&self) -> Option<ast::Expr> {
fn end(&self) -> Option<ast::Expr> {
let op_ix = self.op_details()?.0;
self.syntax()
.children_with_tokens()
.skip(op_ix + 1)
.find_map(|it| ast::Expr::cast(it.into_node()?))
}
fn op_token(&self) -> Option<SyntaxToken> {
self.op_details().map(|t| t.1)
}
fn op_kind(&self) -> Option<RangeOp> {
self.op_details().map(|t| t.2)
}
}
impl ast::IndexExpr {

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 {

View file

@ -1,7 +1,7 @@
//! Precedence representation.
use crate::{
ast::{self, BinaryOp, Expr, HasArgList},
ast::{self, BinaryOp, Expr, HasArgList, RangeItem},
match_ast, AstNode, SyntaxNode,
};