Skip to content

Commit a5c3f50

Browse files
committed
add item_list and mod_ constructors to SyntaxFactory
1 parent f206624 commit a5c3f50

1 file changed

Lines changed: 41 additions & 2 deletions

File tree

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use either::Either;
44
use crate::{
55
AstNode, NodeOrToken, SyntaxKind, SyntaxNode, SyntaxToken,
66
ast::{
7-
self, HasArgList, HasAttrs, HasGenericArgs, HasGenericParams, HasLoopBody, HasName,
8-
HasTypeBounds, HasVisibility, Lifetime, Param, RangeItem, make,
7+
self, HasArgList, HasAttrs, HasGenericArgs, HasGenericParams, HasLoopBody, HasModuleItem,
8+
HasName, HasTypeBounds, HasVisibility, Lifetime, Param, RangeItem, make,
99
},
1010
syntax_editor::SyntaxMappingBuilder,
1111
};
@@ -1785,6 +1785,45 @@ impl SyntaxFactory {
17851785
make::assoc_item_list(None).clone_for_update()
17861786
}
17871787

1788+
pub fn item_list(
1789+
&self,
1790+
items: impl IntoIterator<Item = ast::Item>,
1791+
) -> ast::ItemList {
1792+
let (items, input) = iterator_input(items);
1793+
let items_vec: Vec<_> = items.into_iter().collect();
1794+
let ast = make::item_list(Some(items_vec)).clone_for_update();
1795+
1796+
if let Some(mut mapping) = self.mappings() {
1797+
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
1798+
builder.map_children(input, ast.items().map(|item: ast::Item| item.syntax().clone()));
1799+
builder.finish(&mut mapping);
1800+
}
1801+
1802+
ast
1803+
}
1804+
1805+
pub fn mod_(
1806+
&self,
1807+
name: ast::Name,
1808+
body: Option<ast::ItemList>,
1809+
) -> ast::Module {
1810+
let ast = make::mod_(name.clone(), body.clone()).clone_for_update();
1811+
1812+
if let Some(mut mapping) = self.mappings() {
1813+
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
1814+
builder.map_node(name.syntax().clone(), ast.name().unwrap().syntax().clone());
1815+
if let Some(body) = body {
1816+
builder.map_node(
1817+
body.syntax().clone(),
1818+
ast.item_list().unwrap().syntax().clone(),
1819+
);
1820+
}
1821+
builder.finish(&mut mapping);
1822+
}
1823+
1824+
ast
1825+
}
1826+
17881827
pub fn attr_outer(&self, meta: ast::Meta) -> ast::Attr {
17891828
let ast = make::attr_outer(meta.clone()).clone_for_update();
17901829

0 commit comments

Comments
 (0)