mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-21 17:50:18 +00:00
implement AstNode
for Either
This commit is contained in:
parent
32be158630
commit
cfc01150bf
2 changed files with 31 additions and 8 deletions
|
@ -13,6 +13,8 @@ pub mod prec;
|
|||
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use itertools::Either;
|
||||
|
||||
use crate::{
|
||||
syntax_node::{SyntaxNode, SyntaxNodeChildren, SyntaxToken},
|
||||
SyntaxKind,
|
||||
|
@ -98,6 +100,34 @@ impl<N: AstNode> Iterator for AstChildren<N> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<L, R> AstNode for Either<L, R>
|
||||
where
|
||||
L: AstNode,
|
||||
R: AstNode,
|
||||
{
|
||||
fn can_cast(kind: SyntaxKind) -> bool
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
L::can_cast(kind) || R::can_cast(kind)
|
||||
}
|
||||
|
||||
fn cast(syntax: SyntaxNode) -> Option<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if L::can_cast(syntax.kind()) {
|
||||
L::cast(syntax).map(Either::Left)
|
||||
} else {
|
||||
R::cast(syntax).map(Either::Right)
|
||||
}
|
||||
}
|
||||
|
||||
fn syntax(&self) -> &SyntaxNode {
|
||||
self.as_ref().either(L::syntax, R::syntax)
|
||||
}
|
||||
}
|
||||
|
||||
mod support {
|
||||
use super::{AstChildren, AstNode, SyntaxKind, SyntaxNode, SyntaxToken};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue