Skip to content

Commit 6c0f20d

Browse files
bors[bot]matklad
andauthored
Merge #8838
8838: internal: use more mutable APIs r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents a32589f + 883dd15 commit 6c0f20d

File tree

3 files changed

+33
-53
lines changed

3 files changed

+33
-53
lines changed

crates/ide_assists/src/utils.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use syntax::{
1717
ast::AttrsOwner,
1818
ast::NameOwner,
1919
ast::{self, edit, make, ArgListOwner, GenericParamsOwner},
20-
AstNode, Direction, SmolStr,
20+
ted, AstNode, Direction, SmolStr,
2121
SyntaxKind::*,
2222
SyntaxNode, TextSize, T,
2323
};
@@ -139,34 +139,32 @@ pub fn add_trait_assoc_items_to_impl(
139139
.into_iter()
140140
.map(|it| it.clone_for_update())
141141
.inspect(|it| ast_transform::apply(&*ast_transform, it))
142-
.map(|it| match it {
143-
ast::AssocItem::Fn(def) => ast::AssocItem::Fn(add_body(def)),
144-
ast::AssocItem::TypeAlias(def) => ast::AssocItem::TypeAlias(def.remove_bounds()),
145-
_ => it,
146-
})
147142
.map(|it| edit::remove_attrs_and_docs(&it).clone_subtree().clone_for_update());
148143

149144
let res = impl_.clone_for_update();
145+
150146
let assoc_item_list = res.get_or_create_assoc_item_list();
151147
let mut first_item = None;
152148
for item in items {
153-
if first_item.is_none() {
154-
first_item = Some(item.clone())
155-
}
156-
assoc_item_list.add_item(item)
157-
}
158-
return (res, first_item.unwrap());
159-
160-
fn add_body(fn_def: ast::Fn) -> ast::Fn {
161-
match fn_def.body() {
162-
Some(_) => fn_def,
163-
None => {
149+
first_item.get_or_insert_with(|| item.clone());
150+
match &item {
151+
ast::AssocItem::Fn(fn_) if fn_.body().is_none() => {
164152
let body = make::block_expr(None, Some(make::ext::expr_todo()))
165153
.indent(edit::IndentLevel(1));
166-
fn_def.with_body(body)
154+
ted::replace(fn_.get_or_create_body().syntax(), body.clone_for_update().syntax())
167155
}
156+
ast::AssocItem::TypeAlias(type_alias) => {
157+
if let Some(type_bound_list) = type_alias.type_bound_list() {
158+
type_bound_list.remove()
159+
}
160+
}
161+
_ => {}
168162
}
163+
164+
assoc_item_list.add_item(item)
169165
}
166+
167+
(res, first_item.unwrap())
170168
}
171169

172170
#[derive(Clone, Copy, Debug)]

crates/syntax/src/ast/edit.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
ast::{
1414
self,
1515
make::{self, tokens},
16-
AstNode, TypeBoundsOwner,
16+
AstNode,
1717
},
1818
ted, AstToken, Direction, InsertPosition, NodeOrToken, SmolStr, SyntaxElement, SyntaxKind,
1919
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
@@ -29,25 +29,6 @@ impl ast::BinExpr {
2929
}
3030
}
3131

32-
impl ast::Fn {
33-
#[must_use]
34-
pub fn with_body(&self, body: ast::BlockExpr) -> ast::Fn {
35-
let mut to_insert: ArrayVec<SyntaxElement, 2> = ArrayVec::new();
36-
let old_body_or_semi: SyntaxElement = if let Some(old_body) = self.body() {
37-
old_body.syntax().clone().into()
38-
} else if let Some(semi) = self.semicolon_token() {
39-
to_insert.push(make::tokens::single_space().into());
40-
semi.into()
41-
} else {
42-
to_insert.push(make::tokens::single_space().into());
43-
to_insert.push(body.syntax().clone().into());
44-
return self.insert_children(InsertPosition::Last, to_insert);
45-
};
46-
to_insert.push(body.syntax().clone().into());
47-
self.replace_children(single_node(old_body_or_semi), to_insert)
48-
}
49-
}
50-
5132
fn make_multiline<N>(node: N) -> N
5233
where
5334
N: AstNode + Clone,
@@ -156,21 +137,6 @@ impl ast::RecordExprFieldList {
156137
}
157138
}
158139

159-
impl ast::TypeAlias {
160-
#[must_use]
161-
pub fn remove_bounds(&self) -> ast::TypeAlias {
162-
let colon = match self.colon_token() {
163-
Some(it) => it,
164-
None => return self.clone(),
165-
};
166-
let end = match self.type_bound_list() {
167-
Some(it) => it.syntax().clone().into(),
168-
None => colon.clone().into(),
169-
};
170-
self.replace_children(colon.into()..=end, iter::empty())
171-
}
172-
}
173-
174140
impl ast::Path {
175141
#[must_use]
176142
pub fn with_segment(&self, segment: ast::PathSegment) -> ast::Path {

crates/syntax/src/ast/edit_in_place.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,22 @@ impl ast::AssocItemList {
330330
}
331331
}
332332

333+
impl ast::Fn {
334+
pub fn get_or_create_body(&self) -> ast::BlockExpr {
335+
if self.body().is_none() {
336+
let body = make::ext::empty_block_expr().clone_for_update();
337+
match self.semicolon_token() {
338+
Some(semi) => {
339+
ted::replace(semi, body.syntax());
340+
ted::insert(Position::before(body.syntax), make::tokens::single_space());
341+
}
342+
None => ted::append_child(self.syntax(), body.syntax()),
343+
}
344+
}
345+
self.body().unwrap()
346+
}
347+
}
348+
333349
#[cfg(test)]
334350
mod tests {
335351
use std::fmt;

0 commit comments

Comments
 (0)