Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/syntax/src/syntax_editor.rs')
| -rw-r--r-- | crates/syntax/src/syntax_editor.rs | 88 |
1 files changed, 41 insertions, 47 deletions
diff --git a/crates/syntax/src/syntax_editor.rs b/crates/syntax/src/syntax_editor.rs index 8e4dc75d22..4c20dc7a15 100644 --- a/crates/syntax/src/syntax_editor.rs +++ b/crates/syntax/src/syntax_editor.rs @@ -30,8 +30,8 @@ pub use mapping::{SyntaxMapping, SyntaxMappingBuilder}; pub struct SyntaxEditor { root: SyntaxNode, changes: Vec<Change>, - mappings: SyntaxMapping, annotations: Vec<(SyntaxElement, SyntaxAnnotation)>, + make: SyntaxFactory, } impl SyntaxEditor { @@ -51,8 +51,8 @@ impl SyntaxEditor { let editor = Self { root: root.clone(), changes: Vec::new(), - mappings: SyntaxMapping::default(), annotations: Vec::new(), + make: SyntaxFactory::with_mappings(), }; (editor, root) @@ -68,6 +68,10 @@ impl SyntaxEditor { (editor, T::cast(root).unwrap()) } + pub fn make(&self) -> &SyntaxFactory { + &self.make + } + pub fn add_annotation(&mut self, element: impl Element, annotation: SyntaxAnnotation) { self.annotations.push((element.syntax_element(), annotation)) } @@ -90,7 +94,9 @@ impl SyntaxEditor { ); self.changes.append(&mut other.changes); - self.mappings.merge(other.mappings); + if let Some(mut m) = self.make.mappings() { + m.merge(other.make.take()); + } self.annotations.append(&mut other.annotations); } @@ -104,28 +110,22 @@ impl SyntaxEditor { self.changes.push(Change::InsertAll(position, elements)) } - pub fn insert_with_whitespace( - &mut self, - position: Position, - element: impl Element, - factory: &SyntaxFactory, - ) { - self.insert_all_with_whitespace(position, vec![element.syntax_element()], factory) + pub fn insert_with_whitespace(&mut self, position: Position, element: impl Element) { + self.insert_all_with_whitespace(position, vec![element.syntax_element()]) } pub fn insert_all_with_whitespace( &mut self, position: Position, mut elements: Vec<SyntaxElement>, - factory: &SyntaxFactory, ) { if let Some(first) = elements.first() - && let Some(ws) = ws_before(&position, first, factory) + && let Some(ws) = ws_before(&position, first, &self.make) { elements.insert(0, ws.into()); } if let Some(last) = elements.last() - && let Some(ws) = ws_after(&position, last, factory) + && let Some(ws) = ws_after(&position, last, &self.make) { elements.push(ws.into()); } @@ -181,10 +181,6 @@ impl SyntaxEditor { pub fn finish(self) -> SyntaxEdit { edit_algo::apply_edits(self) } - - pub fn add_mappings(&mut self, other: SyntaxMapping) { - self.mappings.merge(other); - } } /// Represents a completed [`SyntaxEditor`] operation. @@ -538,7 +534,7 @@ mod tests { use crate::{ AstNode, - ast::{self, make, syntax_factory::SyntaxFactory}, + ast::{self, make}, }; use super::*; @@ -564,8 +560,6 @@ mod tests { let to_wrap = root.syntax().descendants().find_map(ast::TupleExpr::cast).unwrap(); let to_replace = root.syntax().descendants().find_map(ast::BinExpr::cast).unwrap(); - let make = SyntaxFactory::with_mappings(); - let name = make::name("var_name"); let name_ref = make::name_ref("var_name").clone_for_update(); @@ -573,10 +567,11 @@ mod tests { editor.add_annotation(name.syntax(), placeholder_snippet); editor.add_annotation(name_ref.syntax(), placeholder_snippet); - let new_block = make.block_expr( - [make + let new_block = editor.make().block_expr( + [editor + .make() .let_stmt( - make.ident_pat(false, false, name.clone()).into(), + editor.make().ident_pat(false, false, name.clone()).into(), None, Some(to_replace.clone().into()), ) @@ -586,7 +581,6 @@ mod tests { editor.replace(to_replace.syntax(), name_ref.syntax()); editor.replace(to_wrap.syntax(), new_block.syntax()); - editor.add_mappings(make.finish_with_mappings()); let edit = editor.finish(); @@ -620,26 +614,29 @@ mod tests { let (mut editor, root) = SyntaxEditor::with_ast_node(&root); let second_let = root.syntax().descendants().find_map(ast::LetStmt::cast).unwrap(); - let make = SyntaxFactory::without_mappings(); editor.insert( Position::first_child_of(root.stmt_list().unwrap().syntax()), - make.let_stmt( - make::ext::simple_ident_pat(make::name("first")).into(), - None, - Some(make::expr_literal("1").into()), - ) - .syntax(), + editor + .make() + .let_stmt( + make::ext::simple_ident_pat(make::name("first")).into(), + None, + Some(make::expr_literal("1").into()), + ) + .syntax(), ); editor.insert( Position::after(second_let.syntax()), - make.let_stmt( - make::ext::simple_ident_pat(make::name("third")).into(), - None, - Some(make::expr_literal("3").into()), - ) - .syntax(), + editor + .make() + .let_stmt( + make::ext::simple_ident_pat(make::name("third")).into(), + None, + Some(make::expr_literal("3").into()), + ) + .syntax(), ); let edit = editor.finish(); @@ -675,17 +672,16 @@ mod tests { root.syntax().descendants().flat_map(ast::BlockExpr::cast).nth(1).unwrap(); let second_let = root.syntax().descendants().find_map(ast::LetStmt::cast).unwrap(); - let make = SyntaxFactory::with_mappings(); - - let new_block_expr = make.block_expr([], Some(ast::Expr::BlockExpr(inner_block.clone()))); + let new_block_expr = + editor.make().block_expr([], Some(ast::Expr::BlockExpr(inner_block.clone()))); - let first_let = make.let_stmt( + let first_let = editor.make().let_stmt( make::ext::simple_ident_pat(make::name("first")).into(), None, Some(make::expr_literal("1").into()), ); - let third_let = make.let_stmt( + let third_let = editor.make().let_stmt( make::ext::simple_ident_pat(make::name("third")).into(), None, Some(make::expr_literal("3").into()), @@ -697,7 +693,6 @@ mod tests { ); editor.insert(Position::after(second_let.syntax()), third_let.syntax()); editor.replace(inner_block.syntax(), new_block_expr.syntax()); - editor.add_mappings(make.finish_with_mappings()); let edit = editor.finish(); @@ -727,11 +722,11 @@ mod tests { let (mut editor, root) = SyntaxEditor::with_ast_node(&root); let inner_block = root; - let make = SyntaxFactory::with_mappings(); - let new_block_expr = make.block_expr([], Some(ast::Expr::BlockExpr(inner_block.clone()))); + let new_block_expr = + editor.make().block_expr([], Some(ast::Expr::BlockExpr(inner_block.clone()))); - let first_let = make.let_stmt( + let first_let = editor.make().let_stmt( make::ext::simple_ident_pat(make::name("first")).into(), None, Some(make::expr_literal("1").into()), @@ -742,7 +737,6 @@ mod tests { first_let.syntax(), ); editor.replace(inner_block.syntax(), new_block_expr.syntax()); - editor.add_mappings(make.finish_with_mappings()); let edit = editor.finish(); |