Skip to content

Commit a1c2653

Browse files
committed
Auto merge of rust-lang#13091 - ice1k:hey, r=Veykril
Remove type alias definition on inline Fix rust-lang#13079
2 parents 67920f7 + 364d9c4 commit a1c2653

File tree

10 files changed

+102
-58
lines changed

10 files changed

+102
-58
lines changed

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

+1
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_call.rs

+20-17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use ide_db::{
77
imports::insert_use::remove_path_if_in_use_stmt,
88
path_transform::PathTransform,
99
search::{FileReference, SearchScope},
10+
source_change::SourceChangeBuilder,
1011
syntax_helpers::{insert_whitespace_into_node::insert_ws_into, node_ext::expr_as_name_ref},
1112
RootDatabase,
1213
};
@@ -100,18 +101,7 @@ pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext<'_>) ->
100101
builder.edit_file(file_id);
101102
let count = refs.len();
102103
// The collects are required as we are otherwise iterating while mutating 🙅‍♀️🙅‍♂️
103-
let (name_refs, name_refs_use): (Vec<_>, Vec<_>) = refs
104-
.into_iter()
105-
.filter_map(|file_ref| match file_ref.name {
106-
ast::NameLike::NameRef(name_ref) => Some(name_ref),
107-
_ => None,
108-
})
109-
.partition_map(|name_ref| {
110-
match name_ref.syntax().ancestors().find_map(ast::UseTree::cast) {
111-
Some(use_tree) => Either::Right(builder.make_mut(use_tree)),
112-
None => Either::Left(name_ref),
113-
}
114-
});
104+
let (name_refs, name_refs_use) = split_refs_and_uses(builder, refs, Some);
115105
let call_infos: Vec<_> = name_refs
116106
.into_iter()
117107
.filter_map(CallInfo::from_name_ref)
@@ -130,11 +120,7 @@ pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext<'_>) ->
130120
.count();
131121
if replaced + name_refs_use.len() == count {
132122
// we replaced all usages in this file, so we can remove the imports
133-
name_refs_use.into_iter().for_each(|use_tree| {
134-
if let Some(path) = use_tree.path() {
135-
remove_path_if_in_use_stmt(&path);
136-
}
137-
})
123+
name_refs_use.iter().for_each(remove_path_if_in_use_stmt);
138124
} else {
139125
remove_def = false;
140126
}
@@ -153,6 +139,23 @@ pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext<'_>) ->
153139
)
154140
}
155141

142+
pub(super) fn split_refs_and_uses<T: ast::AstNode>(
143+
builder: &mut SourceChangeBuilder,
144+
iter: impl IntoIterator<Item = FileReference>,
145+
mut map_ref: impl FnMut(ast::NameRef) -> Option<T>,
146+
) -> (Vec<T>, Vec<ast::Path>) {
147+
iter.into_iter()
148+
.filter_map(|file_ref| match file_ref.name {
149+
ast::NameLike::NameRef(name_ref) => Some(name_ref),
150+
_ => None,
151+
})
152+
.filter_map(|name_ref| match name_ref.syntax().ancestors().find_map(ast::UseTree::cast) {
153+
Some(use_tree) => builder.make_mut(use_tree).path().map(Either::Right),
154+
None => map_ref(name_ref).map(Either::Left),
155+
})
156+
.partition_map(|either| either)
157+
}
158+
156159
// Assist: inline_call
157160
//
158161
// Inlines a function or method body creating a `let` statement per parameter unless the parameter

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

+34-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
// - Remove unused aliases if there are no longer any users, see inline_call.rs.
44

55
use hir::{HasSource, PathResolution};
6-
use ide_db::{defs::Definition, search::FileReference};
6+
use ide_db::{
7+
defs::Definition, imports::insert_use::ast_to_remove_for_path_in_use_stmt,
8+
search::FileReference,
9+
};
710
use itertools::Itertools;
811
use std::collections::HashMap;
912
use syntax::{
@@ -16,6 +19,8 @@ use crate::{
1619
AssistId, AssistKind,
1720
};
1821

22+
use super::inline_call::split_refs_and_uses;
23+
1924
// Assist: inline_type_alias_uses
2025
//
2126
// Inline a type alias into all of its uses where possible.
@@ -31,7 +36,7 @@ use crate::{
3136
// ```
3237
// ->
3338
// ```
34-
// type A = i32;
39+
//
3540
// fn id(x: i32) -> i32 {
3641
// x
3742
// };
@@ -58,32 +63,41 @@ pub(crate) fn inline_type_alias_uses(acc: &mut Assists, ctx: &AssistContext<'_>)
5863
name.syntax().text_range(),
5964
|builder| {
6065
let usages = usages.all();
66+
let mut definition_deleted = false;
6167

6268
let mut inline_refs_for_file = |file_id, refs: Vec<FileReference>| {
6369
builder.edit_file(file_id);
6470

65-
let path_types: Vec<ast::PathType> = refs
66-
.into_iter()
67-
.filter_map(|file_ref| match file_ref.name {
68-
ast::NameLike::NameRef(path_type) => {
69-
path_type.syntax().ancestors().nth(3).and_then(ast::PathType::cast)
70-
}
71-
_ => None,
72-
})
73-
.collect();
71+
let (path_types, path_type_uses) =
72+
split_refs_and_uses(builder, refs, |path_type| {
73+
path_type.syntax().ancestors().nth(3).and_then(ast::PathType::cast)
74+
});
7475

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()));
7580
for (target, replacement) in path_types.into_iter().filter_map(|path_type| {
7681
let replacement = inline(&ast_alias, &path_type)?.to_text(&concrete_type);
7782
let target = path_type.syntax().text_range();
7883
Some((target, replacement))
7984
}) {
8085
builder.replace(target, replacement);
8186
}
87+
88+
if file_id == ctx.file_id() {
89+
builder.delete(ast_alias.syntax().text_range());
90+
definition_deleted = true;
91+
}
8292
};
8393

8494
for (file_id, refs) in usages.into_iter() {
8595
inline_refs_for_file(file_id, refs);
8696
}
97+
if !definition_deleted {
98+
builder.edit_file(ctx.file_id());
99+
builder.delete(ast_alias.syntax().text_range());
100+
}
87101
},
88102
)
89103
}
@@ -929,7 +943,7 @@ fn foo() {
929943
}
930944
"#,
931945
r#"
932-
type A = u32;
946+
933947
934948
fn foo() {
935949
let _: u32 = 3;
@@ -960,13 +974,13 @@ fn foo() {
960974
r#"
961975
//- /lib.rs
962976
mod foo;
963-
type T<E> = Vec<E>;
977+
964978
fn f() -> Vec<&str> {
965979
vec!["hello"]
966980
}
967981
968982
//- /foo.rs
969-
use super::T;
983+
970984
fn foo() {
971985
let _: Vec<i8> = Vec::new();
972986
}
@@ -990,7 +1004,12 @@ fn foo() {
9901004
}
9911005
"#,
9921006
r#"
993-
use super::I;
1007+
//- /lib.rs
1008+
mod foo;
1009+
1010+
1011+
//- /foo.rs
1012+
9941013
fn foo() {
9951014
let _: i32 = 0;
9961015
}

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
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::{
4+
algo::neighbor,
5+
ast::{self, edit_in_place::Removable},
6+
match_ast, ted, AstNode, SyntaxElement, SyntaxNode,
7+
};
48

59
use crate::{
610
assist_context::{AssistContext, Assists},
@@ -76,7 +80,7 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
7680
.collect();
7781
for edit in edits_mut {
7882
match edit {
79-
Remove(it) => it.as_ref().either(ast::Use::remove, ast::UseTree::remove),
83+
Remove(it) => it.as_ref().either(Removable::remove, Removable::remove),
8084
Replace(old, new) => ted::replace(old, new),
8185
}
8286
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
use syntax::{
2-
ast::{self, edit_in_place::GenericParamsOwnerEdit, make, AstNode, HasName, HasTypeBounds},
2+
ast::{
3+
self,
4+
edit_in_place::{GenericParamsOwnerEdit, Removable},
5+
make, AstNode, HasName, HasTypeBounds,
6+
},
37
match_ast,
48
};
59

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

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

crates/ide-assists/src/tests/generated.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,7 @@ fn foo() {
13901390
}
13911391
"#####,
13921392
r#####"
1393-
type A = i32;
1393+
13941394
fn id(x: i32) -> i32 {
13951395
x
13961396
};

crates/ide-assists/src/utils.rs

+1-1
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

+19-12
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

+14-8
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)