Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a695e90

Browse files
committed
Create trait Removable, replace ted APIs with builder APIs
1 parent 37e20de commit a695e90

File tree

8 files changed

+45
-27
lines changed

8 files changed

+45
-27
lines changed

crates/ide-assists/src/handlers/add_missing_match_arms.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use hir::{Adt, Crate, HasAttrs, HasSource, ModuleDef, Semantics};
55
use ide_db::RootDatabase;
66
use ide_db::{famous_defs::FamousDefs, helpers::mod_path_to_ast};
77
use itertools::Itertools;
8+
use syntax::ast::edit_in_place::Removable;
89
use syntax::ast::{self, make, AstNode, HasName, MatchArmList, MatchExpr, Pat};
910

1011
use crate::{

crates/ide-assists/src/handlers/inline_type_alias.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
use hir::{HasSource, PathResolution};
66
use ide_db::{
7-
defs::Definition, imports::insert_use::remove_path_if_in_use_stmt, search::FileReference,
7+
defs::Definition, imports::insert_use::ast_to_remove_for_path_in_use_stmt,
8+
search::FileReference,
89
};
910
use itertools::Itertools;
1011
use std::collections::HashMap;
@@ -72,7 +73,10 @@ pub(crate) fn inline_type_alias_uses(acc: &mut Assists, ctx: &AssistContext<'_>)
7273
path_type.syntax().ancestors().nth(3).and_then(ast::PathType::cast)
7374
});
7475

75-
path_type_uses.iter().for_each(remove_path_if_in_use_stmt);
76+
path_type_uses
77+
.iter()
78+
.flat_map(ast_to_remove_for_path_in_use_stmt)
79+
.for_each(|x| builder.delete(x.syntax().text_range()));
7680
for (target, replacement) in path_types.into_iter().filter_map(|path_type| {
7781
let replacement = inline(&ast_alias, &path_type)?.to_text(&concrete_type);
7882
let target = path_type.syntax().text_range();

crates/ide-assists/src/handlers/merge_imports.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use either::Either;
22
use ide_db::imports::merge_imports::{try_merge_imports, try_merge_trees, MergeBehavior};
3-
use syntax::{algo::neighbor, ast, match_ast, ted, AstNode, SyntaxElement, SyntaxNode};
3+
use syntax::{algo::neighbor, ast::{self, edit_in_place::Removable}, match_ast, ted, AstNode, SyntaxElement, SyntaxNode};
44

55
use crate::{
66
assist_context::{AssistContext, Assists},
@@ -76,7 +76,7 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
7676
.collect();
7777
for edit in edits_mut {
7878
match edit {
79-
Remove(it) => it.as_ref().either(ast::Use::remove, ast::UseTree::remove),
79+
Remove(it) => it.as_ref().either(Removable::remove, Removable::remove),
8080
Replace(old, new) => ted::replace(old, new),
8181
}
8282
}

crates/ide-assists/src/handlers/move_bounds.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use syntax::{
2-
ast::{self, edit_in_place::GenericParamsOwnerEdit, make, AstNode, HasName, HasTypeBounds},
2+
ast::{self, edit_in_place::{GenericParamsOwnerEdit, Removable}, make, AstNode, HasName, HasTypeBounds},
33
match_ast,
44
};
55

crates/ide-assists/src/handlers/unmerge_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use syntax::{
2-
ast::{self, make, HasVisibility},
2+
ast::{self, make, HasVisibility, edit_in_place::Removable},
33
ted::{self, Position},
44
AstNode, SyntaxKind,
55
};

crates/ide-assists/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use syntax::{
1212
ast::{
1313
self,
1414
edit::{self, AstNodeEdit},
15-
edit_in_place::AttrsOwnerEdit,
15+
edit_in_place::{AttrsOwnerEdit, Removable},
1616
make, HasArgList, HasAttrs, HasGenericParams, HasName, HasTypeBounds, Whitespace,
1717
},
1818
ted, AstNode, AstToken, Direction, SmolStr, SourceFile,

crates/ide-db/src/imports/insert_use.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use std::cmp::Ordering;
77
use hir::Semantics;
88
use syntax::{
99
algo,
10-
ast::{self, make, AstNode, HasAttrs, HasModuleItem, HasVisibility, PathSegmentKind},
10+
ast::{
11+
self, edit_in_place::Removable, make, AstNode, HasAttrs, HasModuleItem, HasVisibility,
12+
PathSegmentKind,
13+
},
1114
ted, Direction, NodeOrToken, SyntaxKind, SyntaxNode,
1215
};
1316

@@ -192,20 +195,24 @@ pub fn insert_use(scope: &ImportScope, path: ast::Path, cfg: &InsertUseConfig) {
192195
insert_use_(scope, &path, cfg.group, use_item);
193196
}
194197

195-
pub fn remove_path_if_in_use_stmt(path: &ast::Path) {
198+
pub fn ast_to_remove_for_path_in_use_stmt(path: &ast::Path) -> Option<Box<dyn Removable>> {
196199
// FIXME: improve this
197200
if path.parent_path().is_some() {
198-
return;
201+
return None;
199202
}
200-
if let Some(use_tree) = path.syntax().parent().and_then(ast::UseTree::cast) {
201-
if use_tree.use_tree_list().is_some() || use_tree.star_token().is_some() {
202-
return;
203-
}
204-
if let Some(use_) = use_tree.syntax().parent().and_then(ast::Use::cast) {
205-
use_.remove();
206-
return;
207-
}
208-
use_tree.remove();
203+
let use_tree = path.syntax().parent().and_then(ast::UseTree::cast)?;
204+
if use_tree.use_tree_list().is_some() || use_tree.star_token().is_some() {
205+
return None;
206+
}
207+
if let Some(use_) = use_tree.syntax().parent().and_then(ast::Use::cast) {
208+
return Some(Box::new(use_));
209+
}
210+
Some(Box::new(use_tree))
211+
}
212+
213+
pub fn remove_path_if_in_use_stmt(path: &ast::Path) {
214+
if let Some(node) = ast_to_remove_for_path_in_use_stmt(path) {
215+
node.remove();
209216
}
210217
}
211218

crates/syntax/src/ast/edit_in_place.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,12 @@ impl ast::WhereClause {
248248
}
249249
}
250250

251-
impl ast::TypeBoundList {
252-
pub fn remove(&self) {
251+
pub trait Removable : AstNode {
252+
fn remove(&self);
253+
}
254+
255+
impl Removable for ast::TypeBoundList {
256+
fn remove(&self) {
253257
match self.syntax().siblings_with_tokens(Direction::Prev).find(|it| it.kind() == T![:]) {
254258
Some(colon) => ted::remove_all(colon..=self.syntax().clone().into()),
255259
None => ted::remove(self.syntax()),
@@ -267,8 +271,8 @@ impl ast::PathSegment {
267271
}
268272
}
269273

270-
impl ast::UseTree {
271-
pub fn remove(&self) {
274+
impl Removable for ast::UseTree {
275+
fn remove(&self) {
272276
for dir in [Direction::Next, Direction::Prev] {
273277
if let Some(next_use_tree) = neighbor(self, dir) {
274278
let separators = self
@@ -282,7 +286,9 @@ impl ast::UseTree {
282286
}
283287
ted::remove(self.syntax());
284288
}
289+
}
285290

291+
impl ast::UseTree {
286292
pub fn get_or_create_use_tree_list(&self) -> ast::UseTreeList {
287293
match self.use_tree_list() {
288294
Some(it) => it,
@@ -373,8 +379,8 @@ impl ast::UseTreeList {
373379
}
374380
}
375381

376-
impl ast::Use {
377-
pub fn remove(&self) {
382+
impl Removable for ast::Use {
383+
fn remove(&self) {
378384
let next_ws = self
379385
.syntax()
380386
.next_sibling_or_token()
@@ -444,8 +450,8 @@ impl ast::Fn {
444450
}
445451
}
446452

447-
impl ast::MatchArm {
448-
pub fn remove(&self) {
453+
impl Removable for ast::MatchArm {
454+
fn remove(&self) {
449455
if let Some(sibling) = self.syntax().prev_sibling_or_token() {
450456
if sibling.kind() == SyntaxKind::WHITESPACE {
451457
ted::remove(sibling);

0 commit comments

Comments
 (0)