mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 13:48:50 +00:00
minor: Add token_tree
constructor to SyntaxFactory
This commit is contained in:
parent
d55879d1cb
commit
8fd7790eb5
1 changed files with 27 additions and 1 deletions
|
@ -4,7 +4,7 @@ use itertools::Itertools;
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{self, make, HasName, HasTypeBounds},
|
ast::{self, make, HasName, HasTypeBounds},
|
||||||
syntax_editor::SyntaxMappingBuilder,
|
syntax_editor::SyntaxMappingBuilder,
|
||||||
AstNode, SyntaxKind, SyntaxToken,
|
AstNode, NodeOrToken, SyntaxKind, SyntaxNode, SyntaxToken,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::SyntaxFactory;
|
use super::SyntaxFactory;
|
||||||
|
@ -169,6 +169,32 @@ impl SyntaxFactory {
|
||||||
ast
|
ast
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn token_tree(
|
||||||
|
&self,
|
||||||
|
delimiter: SyntaxKind,
|
||||||
|
tt: Vec<NodeOrToken<ast::TokenTree, SyntaxToken>>,
|
||||||
|
) -> ast::TokenTree {
|
||||||
|
let tt: Vec<_> = tt.into_iter().collect();
|
||||||
|
let input: Vec<_> = tt.iter().cloned().filter_map(only_nodes).collect();
|
||||||
|
|
||||||
|
let ast = make::token_tree(delimiter, tt).clone_for_update();
|
||||||
|
|
||||||
|
if let Some(mut mapping) = self.mappings() {
|
||||||
|
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
|
||||||
|
builder.map_children(
|
||||||
|
input.into_iter(),
|
||||||
|
ast.token_trees_and_tokens().filter_map(only_nodes),
|
||||||
|
);
|
||||||
|
builder.finish(&mut mapping);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ast;
|
||||||
|
|
||||||
|
fn only_nodes(element: NodeOrToken<ast::TokenTree, SyntaxToken>) -> Option<SyntaxNode> {
|
||||||
|
element.as_node().map(|it| it.syntax().clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn token(&self, kind: SyntaxKind) -> SyntaxToken {
|
pub fn token(&self, kind: SyntaxKind) -> SyntaxToken {
|
||||||
make::token(kind)
|
make::token(kind)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue