Skip to content

Commit 1e0d203

Browse files
bors[bot]matklad
andauthored
Merge #9900
9900: internal: remove one more usage of old editing API. r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 01de902 + e300f58 commit 1e0d203

File tree

4 files changed

+25
-55
lines changed

4 files changed

+25
-55
lines changed

crates/ide_assists/src/utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ pub fn add_trait_assoc_items_to_impl(
130130
let items = items.into_iter().map(|assoc_item| {
131131
let assoc_item = assoc_item.clone_for_update();
132132
transform.apply(assoc_item.syntax());
133-
edit::remove_attrs_and_docs(&assoc_item).clone_subtree().clone_for_update()
133+
edit::remove_attrs_and_docs(&assoc_item);
134+
assoc_item
134135
});
135136

136137
let res = impl_.clone_for_update();

crates/ide_completion/src/completions/trait_impl.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ fn get_transformed_assoc_item(
194194
);
195195

196196
transform.apply(assoc_item.syntax());
197-
Some(match assoc_item {
198-
ast::AssocItem::Fn(func) => ast::AssocItem::Fn(edit::remove_attrs_and_docs(&func)),
199-
_ => assoc_item,
200-
})
197+
if let ast::AssocItem::Fn(func) = &assoc_item {
198+
edit::remove_attrs_and_docs(func)
199+
}
200+
Some(assoc_item)
201201
}
202202

203203
fn add_type_alias_impl(
@@ -253,7 +253,7 @@ fn add_const_impl(
253253
}
254254

255255
fn make_const_compl_syntax(const_: &ast::Const) -> String {
256-
let const_ = edit::remove_attrs_and_docs(const_);
256+
edit::remove_attrs_and_docs(const_);
257257

258258
let const_start = const_.syntax().text_range().start();
259259
let const_end = const_.syntax().text_range().end();

crates/syntax/src/algo.rs

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Collection of assorted algorithms for syntax trees.
22
3-
use std::{hash::BuildHasherDefault, ops::RangeInclusive};
3+
use std::hash::BuildHasherDefault;
44

55
use indexmap::IndexMap;
66
use itertools::Itertools;
@@ -295,41 +295,6 @@ fn _insert_children(
295295
with_children(parent, new_children)
296296
}
297297

298-
/// Replaces all nodes in `to_delete` with nodes from `to_insert`
299-
///
300-
/// This is a type-unsafe low-level editing API, if you need to use it,
301-
/// prefer to create a type-safe abstraction on top of it instead.
302-
pub fn replace_children(
303-
parent: &SyntaxNode,
304-
to_delete: RangeInclusive<SyntaxElement>,
305-
to_insert: impl IntoIterator<Item = SyntaxElement>,
306-
) -> SyntaxNode {
307-
let mut to_insert = to_insert.into_iter();
308-
_replace_children(parent, to_delete, &mut to_insert)
309-
}
310-
311-
fn _replace_children(
312-
parent: &SyntaxNode,
313-
to_delete: RangeInclusive<SyntaxElement>,
314-
to_insert: &mut dyn Iterator<Item = SyntaxElement>,
315-
) -> SyntaxNode {
316-
let start = position_of_child(parent, to_delete.start().clone());
317-
let end = position_of_child(parent, to_delete.end().clone());
318-
let parent_green = parent.green();
319-
let mut old_children = parent_green.children().map(|it| match it {
320-
NodeOrToken::Token(it) => NodeOrToken::Token(it.to_owned()),
321-
NodeOrToken::Node(it) => NodeOrToken::Node(it.to_owned()),
322-
});
323-
324-
let before = old_children.by_ref().take(start).collect::<Vec<_>>();
325-
let new_children = before
326-
.into_iter()
327-
.chain(to_insert.map(to_green_element))
328-
.chain(old_children.skip(end + 1 - start))
329-
.collect::<Vec<_>>();
330-
with_children(parent, new_children)
331-
}
332-
333298
fn with_children(
334299
parent: &SyntaxNode,
335300
new_children: Vec<NodeOrToken<rowan::GreenNode, rowan::GreenToken>>,

crates/syntax/src/ast/edit.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,26 @@ impl ast::UseTree {
4848
}
4949
}
5050

51-
#[must_use]
52-
pub fn remove_attrs_and_docs<N: ast::AttrsOwner>(node: &N) -> N {
53-
N::cast(remove_attrs_and_docs_inner(node.syntax().clone())).unwrap()
51+
pub fn remove_attrs_and_docs<N: ast::AttrsOwner>(node: &N) {
52+
remove_attrs_and_docs_inner(node.syntax())
5453
}
5554

56-
fn remove_attrs_and_docs_inner(mut node: SyntaxNode) -> SyntaxNode {
57-
while let Some(start) =
58-
node.children_with_tokens().find(|it| it.kind() == ATTR || it.kind() == COMMENT)
59-
{
60-
let end = match &start.next_sibling_or_token() {
61-
Some(el) if el.kind() == WHITESPACE => el.clone(),
62-
Some(_) | None => start.clone(),
63-
};
64-
node = algo::replace_children(&node, start..=end, &mut iter::empty());
55+
fn remove_attrs_and_docs_inner(node: &SyntaxNode) {
56+
let mut remove_next_ws = false;
57+
for child in node.children_with_tokens() {
58+
match child.kind() {
59+
ATTR | COMMENT => {
60+
remove_next_ws = true;
61+
child.detach();
62+
continue;
63+
}
64+
WHITESPACE if remove_next_ws => {
65+
child.detach();
66+
}
67+
_ => (),
68+
}
69+
remove_next_ws = false;
6570
}
66-
node
6771
}
6872

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

0 commit comments

Comments
 (0)