Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/syntax/src/syntax_editor/edits.rs')
-rw-r--r--crates/syntax/src/syntax_editor/edits.rs50
1 files changed, 28 insertions, 22 deletions
diff --git a/crates/syntax/src/syntax_editor/edits.rs b/crates/syntax/src/syntax_editor/edits.rs
index 1718f1a51c..62fd7db8d3 100644
--- a/crates/syntax/src/syntax_editor/edits.rs
+++ b/crates/syntax/src/syntax_editor/edits.rs
@@ -12,20 +12,21 @@ pub trait GetOrCreateWhereClause: ast::HasGenericParams {
fn get_or_create_where_clause(
&self,
- editor: &mut SyntaxEditor,
+ editor: &SyntaxEditor,
new_preds: impl Iterator<Item = ast::WherePred>,
) {
+ let make = editor.make();
let existing = self.where_clause();
let all_preds: Vec<_> =
existing.iter().flat_map(|wc| wc.predicates()).chain(new_preds).collect();
- let new_where = editor.make().where_clause(all_preds);
+ let new_where = make.where_clause(all_preds);
if let Some(existing) = &existing {
editor.replace(existing.syntax(), new_where.syntax());
} else if let Some(pos) = self.where_clause_position() {
editor.insert_all(
pos,
- vec![editor.make().whitespace(" ").into(), new_where.syntax().clone().into()],
+ vec![make.whitespace(" ").into(), new_where.syntax().clone().into()],
);
}
}
@@ -109,7 +110,7 @@ impl GetOrCreateWhereClause for ast::Enum {
impl SyntaxEditor {
/// Adds a new generic param to the function using `SyntaxEditor`
- pub fn add_generic_param(&mut self, function: &Fn, new_param: GenericParam) {
+ pub fn add_generic_param(&self, function: &Fn, new_param: GenericParam) {
match function.generic_param_list() {
Some(generic_param_list) => match generic_param_list.generic_params().last() {
Some(last_param) => {
@@ -173,7 +174,8 @@ impl SyntaxEditor {
}
}
-fn get_or_insert_comma_after(editor: &mut SyntaxEditor, syntax: &SyntaxNode) -> SyntaxToken {
+fn get_or_insert_comma_after(editor: &SyntaxEditor, syntax: &SyntaxNode) -> SyntaxToken {
+ let make = editor.make();
match syntax
.siblings_with_tokens(Direction::Next)
.filter_map(|it| it.into_token())
@@ -181,7 +183,7 @@ fn get_or_insert_comma_after(editor: &mut SyntaxEditor, syntax: &SyntaxNode) ->
{
Some(it) => it,
None => {
- let comma = editor.make().token(T![,]);
+ let comma = make.token(T![,]);
editor.insert(Position::after(syntax), &comma);
comma
}
@@ -193,7 +195,7 @@ impl ast::AssocItemList {
///
/// Attention! This function does align the first line of `item` with respect to `self`,
/// but it does _not_ change indentation of other lines (if any).
- pub fn add_items(&self, editor: &mut SyntaxEditor, items: Vec<ast::AssocItem>) {
+ pub fn add_items(&self, editor: &SyntaxEditor, items: Vec<ast::AssocItem>) {
let (indent, position, whitespace) = match self.assoc_items().last() {
Some(last_item) => (
IndentLevel::from_node(last_item.syntax()),
@@ -227,15 +229,16 @@ impl ast::AssocItemList {
impl ast::Impl {
pub fn get_or_create_assoc_item_list_with_editor(
&self,
- editor: &mut SyntaxEditor,
+ editor: &SyntaxEditor,
) -> ast::AssocItemList {
+ let make = editor.make();
if let Some(list) = self.assoc_item_list() {
list
} else {
- let list = editor.make().assoc_item_list_empty();
+ let list = make.assoc_item_list_empty();
editor.insert_all(
Position::last_child_of(self.syntax()),
- vec![editor.make().whitespace(" ").into(), list.syntax().clone().into()],
+ vec![make.whitespace(" ").into(), list.syntax().clone().into()],
);
list
}
@@ -243,7 +246,8 @@ impl ast::Impl {
}
impl ast::VariantList {
- pub fn add_variant(&self, editor: &mut SyntaxEditor, variant: &ast::Variant) {
+ pub fn add_variant(&self, editor: &SyntaxEditor, variant: &ast::Variant) {
+ let make = editor.make();
let (indent, position) = match self.variants().last() {
Some(last_item) => (
IndentLevel::from_node(last_item.syntax()),
@@ -258,16 +262,16 @@ impl ast::VariantList {
},
};
let elements: Vec<SyntaxElement> = vec![
- editor.make().whitespace(&format!("{}{indent}", "\n")).into(),
+ make.whitespace(&format!("{}{indent}", "\n")).into(),
variant.syntax().clone().into(),
- editor.make().token(T![,]).into(),
+ make.token(T![,]).into(),
];
editor.insert_all(position, elements);
}
}
impl ast::Fn {
- pub fn replace_or_insert_body(&self, editor: &mut SyntaxEditor, body: ast::BlockExpr) {
+ pub fn replace_or_insert_body(&self, editor: &SyntaxEditor, body: ast::BlockExpr) {
if let Some(old_body) = self.body() {
editor.replace(old_body.syntax(), body.syntax());
} else {
@@ -283,7 +287,8 @@ impl ast::Fn {
}
}
-fn normalize_ws_between_braces(editor: &mut SyntaxEditor, node: &SyntaxNode) -> Option<()> {
+fn normalize_ws_between_braces(editor: &SyntaxEditor, node: &SyntaxNode) -> Option<()> {
+ let make = editor.make();
let l = node
.children_with_tokens()
.filter_map(|it| it.into_token())
@@ -298,11 +303,11 @@ fn normalize_ws_between_braces(editor: &mut SyntaxEditor, node: &SyntaxNode) ->
match l.next_sibling_or_token() {
Some(ws) if ws.kind() == SyntaxKind::WHITESPACE => {
if ws.next_sibling_or_token()?.into_token()? == r {
- editor.replace(ws, editor.make().whitespace(&format!("\n{indent}")));
+ editor.replace(ws, make.whitespace(&format!("\n{indent}")));
}
}
Some(ws) if ws.kind() == T!['}'] => {
- editor.insert(Position::after(l), editor.make().whitespace(&format!("\n{indent}")));
+ editor.insert(Position::after(l), make.whitespace(&format!("\n{indent}")));
}
_ => (),
}
@@ -310,11 +315,11 @@ fn normalize_ws_between_braces(editor: &mut SyntaxEditor, node: &SyntaxNode) ->
}
pub trait Removable: AstNode {
- fn remove(&self, editor: &mut SyntaxEditor);
+ fn remove(&self, editor: &SyntaxEditor);
}
impl Removable for ast::TypeBoundList {
- fn remove(&self, editor: &mut SyntaxEditor) {
+ fn remove(&self, editor: &SyntaxEditor) {
match self.syntax().siblings_with_tokens(Direction::Prev).find(|it| it.kind() == T![:]) {
Some(colon) => editor.delete_all(colon..=self.syntax().clone().into()),
None => editor.delete(self.syntax()),
@@ -323,7 +328,8 @@ impl Removable for ast::TypeBoundList {
}
impl Removable for ast::Use {
- fn remove(&self, editor: &mut SyntaxEditor) {
+ fn remove(&self, editor: &SyntaxEditor) {
+ let make = editor.make();
let next_ws = self
.syntax()
.next_sibling_or_token()
@@ -335,7 +341,7 @@ impl Removable for ast::Use {
if rest.is_empty() {
editor.delete(next_ws.syntax());
} else {
- editor.replace(next_ws.syntax(), editor.make().whitespace(rest));
+ editor.replace(next_ws.syntax(), make.whitespace(rest));
}
}
}
@@ -345,7 +351,7 @@ impl Removable for ast::Use {
}
impl Removable for ast::UseTree {
- fn remove(&self, editor: &mut SyntaxEditor) {
+ fn remove(&self, editor: &SyntaxEditor) {
for dir in [Direction::Next, Direction::Prev] {
if let Some(next_use_tree) = neighbor(self, dir) {
let separators = self