convenience api

This commit is contained in:
Aleksey Kladov 2019-07-19 19:05:34 +03:00
parent a6df224f7d
commit 191a6ba330
8 changed files with 63 additions and 50 deletions

View file

@ -299,7 +299,7 @@ mod tokens {
.tree() .tree()
.syntax() .syntax()
.descendants_with_tokens() .descendants_with_tokens()
.filter_map(|it| it.as_token().cloned()) .filter_map(|it| it.into_token())
.find(|it| it.kind() == T![,]) .find(|it| it.kind() == T![,])
.unwrap() .unwrap()
} }
@ -309,7 +309,7 @@ mod tokens {
.tree() .tree()
.syntax() .syntax()
.descendants_with_tokens() .descendants_with_tokens()
.filter_map(|it| it.as_token().cloned()) .filter_map(|it| it.into_token())
.find(|it| it.kind() == WHITESPACE && it.text().as_str() == " ") .find(|it| it.kind() == WHITESPACE && it.text().as_str() == " ")
.unwrap() .unwrap()
} }
@ -320,7 +320,7 @@ mod tokens {
.tree() .tree()
.syntax() .syntax()
.descendants_with_tokens() .descendants_with_tokens()
.filter_map(|it| it.as_token().cloned()) .filter_map(|it| it.into_token())
.find(|it| it.kind() == WHITESPACE && it.text().as_str() == "\n") .find(|it| it.kind() == WHITESPACE && it.text().as_str() == "\n")
.unwrap() .unwrap()
} }
@ -332,7 +332,7 @@ mod tokens {
WsBuilder(SourceFile::parse(text).ok().unwrap()) WsBuilder(SourceFile::parse(text).ok().unwrap())
} }
pub(crate) fn ws(&self) -> SyntaxToken { pub(crate) fn ws(&self) -> SyntaxToken {
self.0.syntax().first_child_or_token().unwrap().as_token().cloned().unwrap() self.0.syntax().first_child_or_token().unwrap().into_token().unwrap()
} }
} }

View file

@ -156,7 +156,7 @@ fn extend_list_item(node: &SyntaxNode) -> Option<TextRange> {
SyntaxElement::Token(it) => is_single_line_ws(it), SyntaxElement::Token(it) => is_single_line_ws(it),
}) })
.next() .next()
.and_then(|it| it.as_token().cloned()) .and_then(|it| it.into_token())
.filter(|node| node.kind() == T![,]) .filter(|node| node.kind() == T![,])
} }
@ -167,7 +167,7 @@ fn extend_list_item(node: &SyntaxNode) -> Option<TextRange> {
// Include any following whitespace when comma if after list item. // Include any following whitespace when comma if after list item.
let final_node = comma_node let final_node = comma_node
.next_sibling_or_token() .next_sibling_or_token()
.and_then(|it| it.as_token().cloned()) .and_then(|it| it.into_token())
.filter(|node| is_single_line_ws(node)) .filter(|node| is_single_line_ws(node))
.unwrap_or(comma_node); .unwrap_or(comma_node);

View file

@ -27,7 +27,7 @@ pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit {
SyntaxElement::Token(token) => token.parent(), SyntaxElement::Token(token) => token.parent(),
}; };
let mut edit = TextEditBuilder::default(); let mut edit = TextEditBuilder::default();
for token in node.descendants_with_tokens().filter_map(|it| it.as_token().cloned()) { for token in node.descendants_with_tokens().filter_map(|it| it.into_token()) {
let range = match range.intersection(&token.range()) { let range = match range.intersection(&token.range()) {
Some(range) => range, Some(range) => range,
None => continue, None => continue,

View file

@ -237,8 +237,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
let mut buf = String::new(); let mut buf = String::new();
buf.push_str(&STYLE); buf.push_str(&STYLE);
buf.push_str("<pre><code>"); buf.push_str("<pre><code>");
let tokens = let tokens = parse.tree().syntax().descendants_with_tokens().filter_map(|it| it.into_token());
parse.tree().syntax().descendants_with_tokens().filter_map(|it| it.as_token().cloned());
for token in tokens { for token in tokens {
could_intersect.retain(|it| token.range().start() <= it.range.end()); could_intersect.retain(|it| token.range().start() <= it.range.end());
while let Some(r) = ranges.get(frontier) { while let Some(r) = ranges.get(frontier) {

View file

@ -60,7 +60,7 @@ impl ast::PrefixExpr {
} }
pub fn op_token(&self) -> Option<SyntaxToken> { pub fn op_token(&self) -> Option<SyntaxToken> {
self.syntax().first_child_or_token()?.as_token().cloned() self.syntax().first_child_or_token()?.into_token()
} }
} }
@ -132,41 +132,41 @@ pub enum BinOp {
impl ast::BinExpr { impl ast::BinExpr {
fn op_details(&self) -> Option<(SyntaxToken, BinOp)> { fn op_details(&self) -> Option<(SyntaxToken, BinOp)> {
self.syntax().children_with_tokens().filter_map(|it| it.as_token().cloned()).find_map(|c| { self.syntax().children_with_tokens().filter_map(|it| it.into_token()).find_map(|c| match c
match c.kind() { .kind()
T![||] => Some((c, BinOp::BooleanOr)), {
T![&&] => Some((c, BinOp::BooleanAnd)), T![||] => Some((c, BinOp::BooleanOr)),
T![==] => Some((c, BinOp::EqualityTest)), T![&&] => Some((c, BinOp::BooleanAnd)),
T![!=] => Some((c, BinOp::NegatedEqualityTest)), T![==] => Some((c, BinOp::EqualityTest)),
T![<=] => Some((c, BinOp::LesserEqualTest)), T![!=] => Some((c, BinOp::NegatedEqualityTest)),
T![>=] => Some((c, BinOp::GreaterEqualTest)), T![<=] => Some((c, BinOp::LesserEqualTest)),
T![<] => Some((c, BinOp::LesserTest)), T![>=] => Some((c, BinOp::GreaterEqualTest)),
T![>] => Some((c, BinOp::GreaterTest)), T![<] => Some((c, BinOp::LesserTest)),
T![+] => Some((c, BinOp::Addition)), T![>] => Some((c, BinOp::GreaterTest)),
T![*] => Some((c, BinOp::Multiplication)), T![+] => Some((c, BinOp::Addition)),
T![-] => Some((c, BinOp::Subtraction)), T![*] => Some((c, BinOp::Multiplication)),
T![/] => Some((c, BinOp::Division)), T![-] => Some((c, BinOp::Subtraction)),
T![%] => Some((c, BinOp::Remainder)), T![/] => Some((c, BinOp::Division)),
T![<<] => Some((c, BinOp::LeftShift)), T![%] => Some((c, BinOp::Remainder)),
T![>>] => Some((c, BinOp::RightShift)), T![<<] => Some((c, BinOp::LeftShift)),
T![^] => Some((c, BinOp::BitwiseXor)), T![>>] => Some((c, BinOp::RightShift)),
T![|] => Some((c, BinOp::BitwiseOr)), T![^] => Some((c, BinOp::BitwiseXor)),
T![&] => Some((c, BinOp::BitwiseAnd)), T![|] => Some((c, BinOp::BitwiseOr)),
T![..] => Some((c, BinOp::RangeRightOpen)), T![&] => Some((c, BinOp::BitwiseAnd)),
T![..=] => Some((c, BinOp::RangeRightClosed)), T![..] => Some((c, BinOp::RangeRightOpen)),
T![=] => Some((c, BinOp::Assignment)), T![..=] => Some((c, BinOp::RangeRightClosed)),
T![+=] => Some((c, BinOp::AddAssign)), T![=] => Some((c, BinOp::Assignment)),
T![/=] => Some((c, BinOp::DivAssign)), T![+=] => Some((c, BinOp::AddAssign)),
T![*=] => Some((c, BinOp::MulAssign)), T![/=] => Some((c, BinOp::DivAssign)),
T![%=] => Some((c, BinOp::RemAssign)), T![*=] => Some((c, BinOp::MulAssign)),
T![>>=] => Some((c, BinOp::ShrAssign)), T![%=] => Some((c, BinOp::RemAssign)),
T![<<=] => Some((c, BinOp::ShlAssign)), T![>>=] => Some((c, BinOp::ShrAssign)),
T![-=] => Some((c, BinOp::SubAssign)), T![<<=] => Some((c, BinOp::ShlAssign)),
T![|=] => Some((c, BinOp::BitOrAssign)), T![-=] => Some((c, BinOp::SubAssign)),
T![&=] => Some((c, BinOp::BitAndAssign)), T![|=] => Some((c, BinOp::BitOrAssign)),
T![^=] => Some((c, BinOp::BitXorAssign)), T![&=] => Some((c, BinOp::BitAndAssign)),
_ => None, T![^=] => Some((c, BinOp::BitXorAssign)),
} _ => None,
}) })
} }

View file

@ -239,7 +239,7 @@ impl ast::FnDef {
pub fn semicolon_token(&self) -> Option<SyntaxToken> { pub fn semicolon_token(&self) -> Option<SyntaxToken> {
self.syntax() self.syntax()
.last_child_or_token() .last_child_or_token()
.and_then(|it| it.as_token().cloned()) .and_then(|it| it.into_token())
.filter(|it| it.kind() == T![;]) .filter(|it| it.kind() == T![;])
} }
} }
@ -332,7 +332,7 @@ impl ast::SelfParam {
pub fn self_kw_token(&self) -> SyntaxToken { pub fn self_kw_token(&self) -> SyntaxToken {
self.syntax() self.syntax()
.children_with_tokens() .children_with_tokens()
.filter_map(|it| it.as_token().cloned()) .filter_map(|it| it.into_token())
.find(|it| it.kind() == T![self]) .find(|it| it.kind() == T![self])
.expect("invalid tree: self param must have self") .expect("invalid tree: self param must have self")
} }
@ -361,7 +361,7 @@ impl ast::LifetimeParam {
pub fn lifetime_token(&self) -> Option<SyntaxToken> { pub fn lifetime_token(&self) -> Option<SyntaxToken> {
self.syntax() self.syntax()
.children_with_tokens() .children_with_tokens()
.filter_map(|it| it.as_token().cloned()) .filter_map(|it| it.into_token())
.find(|it| it.kind() == LIFETIME) .find(|it| it.kind() == LIFETIME)
} }
} }
@ -370,7 +370,7 @@ impl ast::WherePred {
pub fn lifetime_token(&self) -> Option<SyntaxToken> { pub fn lifetime_token(&self) -> Option<SyntaxToken> {
self.syntax() self.syntax()
.children_with_tokens() .children_with_tokens()
.filter_map(|it| it.as_token().cloned()) .filter_map(|it| it.into_token())
.find(|it| it.kind() == LIFETIME) .find(|it| it.kind() == LIFETIME)
} }
} }

View file

@ -155,7 +155,7 @@ pub struct CommentIter {
impl Iterator for CommentIter { impl Iterator for CommentIter {
type Item = ast::Comment; type Item = ast::Comment;
fn next(&mut self) -> Option<ast::Comment> { fn next(&mut self) -> Option<ast::Comment> {
self.iter.by_ref().find_map(|el| el.as_token().cloned().and_then(ast::Comment::cast)) self.iter.by_ref().find_map(|el| el.into_token().and_then(ast::Comment::cast))
} }
} }

View file

@ -423,6 +423,13 @@ impl SyntaxElement {
} }
} }
pub fn into_node(self) -> Option<SyntaxNode> {
match self {
SyntaxElement::Node(node) => Some(node),
SyntaxElement::Token(_) => None,
}
}
pub fn as_token(&self) -> Option<&SyntaxToken> { pub fn as_token(&self) -> Option<&SyntaxToken> {
match self { match self {
SyntaxElement::Node(_) => None, SyntaxElement::Node(_) => None,
@ -430,6 +437,13 @@ impl SyntaxElement {
} }
} }
pub fn into_token(self) -> Option<SyntaxToken> {
match self {
SyntaxElement::Node(_) => None,
SyntaxElement::Token(token) => Some(token),
}
}
pub fn next_sibling_or_token(&self) -> Option<SyntaxElement> { pub fn next_sibling_or_token(&self) -> Option<SyntaxElement> {
match self { match self {
SyntaxElement::Node(it) => it.next_sibling_or_token(), SyntaxElement::Node(it) => it.next_sibling_or_token(),