Skip to content

Commit 0f02014

Browse files
committed
minor: Add token_tree constructor to SyntaxFactory
1 parent e77581e commit 0f02014

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

crates/syntax/src/ast/syntax_factory/constructors.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use itertools::Itertools;
44
use crate::{
55
ast::{self, make, HasName},
66
syntax_editor::SyntaxMappingBuilder,
7-
AstNode, SyntaxKind, SyntaxToken,
7+
AstNode, NodeOrToken, SyntaxKind, SyntaxNode, SyntaxToken,
88
};
99

1010
use super::SyntaxFactory;
@@ -125,6 +125,32 @@ impl SyntaxFactory {
125125
ast
126126
}
127127

128+
pub fn token_tree(
129+
&self,
130+
delimiter: SyntaxKind,
131+
tt: Vec<NodeOrToken<ast::TokenTree, SyntaxToken>>,
132+
) -> ast::TokenTree {
133+
let tt: Vec<_> = tt.into_iter().collect();
134+
let input: Vec<_> = tt.iter().cloned().filter_map(only_nodes).collect();
135+
136+
let ast = make::token_tree(delimiter, tt).clone_for_update();
137+
138+
if let Some(mut mapping) = self.mappings() {
139+
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
140+
builder.map_children(
141+
input.into_iter(),
142+
ast.token_trees_and_tokens().filter_map(only_nodes),
143+
);
144+
builder.finish(&mut mapping);
145+
}
146+
147+
return ast;
148+
149+
fn only_nodes(element: NodeOrToken<ast::TokenTree, SyntaxToken>) -> Option<SyntaxNode> {
150+
element.as_node().map(|it| it.syntax().clone())
151+
}
152+
}
153+
128154
pub fn token(&self, kind: SyntaxKind) -> SyntaxToken {
129155
make::token(kind)
130156
}

0 commit comments

Comments
 (0)