Skip to content

Commit 7d2e672

Browse files
committed
internal: add item_list and mod_ constructors to SyntaxFactory
Part of #18285
1 parent 8d1edc4 commit 7d2e672

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

crates/ide-assists/src/handlers/extract_module.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ use syntax::{
1717
ast::{
1818
self, HasVisibility,
1919
edit::{AstNodeEdit, IndentLevel},
20-
make,
20+
syntax_factory::SyntaxFactory,
2121
},
22-
match_ast, ted,
22+
match_ast,
23+
syntax_editor::{Position, SyntaxEditor},
2324
};
2425

2526
use crate::{AssistContext, Assists};

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use either::Either;
44
use crate::{
55
AstNode, Edition, NodeOrToken, SyntaxElement, 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
};
@@ -2003,6 +2003,35 @@ impl SyntaxFactory {
20032003
make::assoc_item_list(None).clone_for_update()
20042004
}
20052005

2006+
pub fn item_list(&self, items: impl IntoIterator<Item = ast::Item>) -> ast::ItemList {
2007+
let (items, input) = iterator_input(items);
2008+
let items_vec: Vec<_> = items.into_iter().collect();
2009+
let ast = make::item_list(Some(items_vec)).clone_for_update();
2010+
2011+
if let Some(mut mapping) = self.mappings() {
2012+
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
2013+
builder.map_children(input, ast.items().map(|item: ast::Item| item.syntax().clone()));
2014+
builder.finish(&mut mapping);
2015+
}
2016+
2017+
ast
2018+
}
2019+
2020+
pub fn mod_(&self, name: ast::Name, body: Option<ast::ItemList>) -> ast::Module {
2021+
let ast = make::mod_(name.clone(), body.clone()).clone_for_update();
2022+
2023+
if let Some(mut mapping) = self.mappings() {
2024+
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
2025+
builder.map_node(name.syntax().clone(), ast.name().unwrap().syntax().clone());
2026+
if let Some(body) = body {
2027+
builder.map_node(body.syntax().clone(), ast.item_list().unwrap().syntax().clone());
2028+
}
2029+
builder.finish(&mut mapping);
2030+
}
2031+
2032+
ast
2033+
}
2034+
20062035
pub fn attr_outer(&self, meta: ast::Meta) -> ast::Attr {
20072036
let ast = make::attr_outer(meta.clone()).clone_for_update();
20082037

0 commit comments

Comments
 (0)