Skip to content

Commit 3c49a9f

Browse files
committed
minor: move functionality to a better place
1 parent 78c7940 commit 3c49a9f

File tree

4 files changed

+35
-30
lines changed

4 files changed

+35
-30
lines changed

crates/ide_assists/src/utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use syntax::{
1313
ast::{
1414
self,
1515
edit::{self, AstNodeEdit},
16+
edit_in_place::AttrsOwnerEdit,
1617
make, ArgListOwner, AttrsOwner, GenericParamsOwner, NameOwner, TypeBoundsOwner,
1718
},
1819
ted, AstNode, Direction, SmolStr,
@@ -130,7 +131,7 @@ pub fn add_trait_assoc_items_to_impl(
130131
let items = items.into_iter().map(|assoc_item| {
131132
let assoc_item = assoc_item.clone_for_update();
132133
transform.apply(assoc_item.syntax());
133-
edit::remove_attrs_and_docs(&assoc_item);
134+
assoc_item.remove_attrs_and_docs();
134135
assoc_item
135136
});
136137

crates/ide_completion/src/completions/trait_impl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
use hir::{self, HasAttrs, HasSource};
3535
use ide_db::{path_transform::PathTransform, traits::get_missing_assoc_items, SymbolKind};
3636
use syntax::{
37-
ast::{self, edit},
37+
ast::{self, edit_in_place::AttrsOwnerEdit},
3838
display::function_declaration,
3939
AstNode, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, T,
4040
};
@@ -195,7 +195,7 @@ fn get_transformed_assoc_item(
195195

196196
transform.apply(assoc_item.syntax());
197197
if let ast::AssocItem::Fn(func) = &assoc_item {
198-
edit::remove_attrs_and_docs(func)
198+
func.remove_attrs_and_docs()
199199
}
200200
Some(assoc_item)
201201
}
@@ -253,7 +253,7 @@ fn add_const_impl(
253253
}
254254

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

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

crates/syntax/src/ast/edit.rs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use std::{fmt, iter, ops};
66
use crate::{
77
algo,
88
ast::{self, make, AstNode},
9-
ted, AstToken, NodeOrToken, SyntaxElement,
10-
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
11-
SyntaxNode, SyntaxToken,
9+
ted, AstToken, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxToken,
1210
};
1311

1412
impl ast::UseTree {
@@ -48,28 +46,6 @@ impl ast::UseTree {
4846
}
4947
}
5048

51-
pub fn remove_attrs_and_docs<N: ast::AttrsOwner>(node: &N) {
52-
remove_attrs_and_docs_inner(node.syntax())
53-
}
54-
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;
70-
}
71-
}
72-
7349
#[derive(Debug, Clone, Copy)]
7450
pub struct IndentLevel(pub u8);
7551

crates/syntax/src/ast/edit_in_place.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use crate::{
1313
make, GenericParamsOwner,
1414
},
1515
ted::{self, Position},
16-
AstNode, AstToken, Direction, SyntaxNode,
16+
AstNode, AstToken, Direction,
17+
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
18+
SyntaxNode,
1719
};
1820

1921
use super::NameOwner;
@@ -196,6 +198,32 @@ fn create_generic_param_list(position: Position) -> ast::GenericParamList {
196198
gpl
197199
}
198200

201+
pub trait AttrsOwnerEdit: ast::AttrsOwner + AstNodeEdit {
202+
fn remove_attrs_and_docs(&self) {
203+
remove_attrs_and_docs(self.syntax());
204+
205+
fn remove_attrs_and_docs(node: &SyntaxNode) {
206+
let mut remove_next_ws = false;
207+
for child in node.children_with_tokens() {
208+
match child.kind() {
209+
ATTR | COMMENT => {
210+
remove_next_ws = true;
211+
child.detach();
212+
continue;
213+
}
214+
WHITESPACE if remove_next_ws => {
215+
child.detach();
216+
}
217+
_ => (),
218+
}
219+
remove_next_ws = false;
220+
}
221+
}
222+
}
223+
}
224+
225+
impl<T: ast::AttrsOwner + AstNodeEdit> AttrsOwnerEdit for T {}
226+
199227
impl ast::GenericParamList {
200228
pub fn add_generic_param(&self, generic_param: ast::GenericParam) {
201229
match self.generic_params().last() {

0 commit comments

Comments
 (0)