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

Commit 79e5c36

Browse files
committed
Extract shared logic
1 parent fcc6133 commit 79e5c36

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

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

Lines changed: 20 additions & 17 deletions
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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
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::remove_path_if_in_use_stmt, search::FileReference,
8+
};
79
use itertools::Itertools;
810
use std::collections::HashMap;
911
use syntax::{
@@ -16,6 +18,8 @@ use crate::{
1618
AssistId, AssistKind,
1719
};
1820

21+
use super::inline_call::split_refs_and_uses;
22+
1923
// Assist: inline_type_alias_uses
2024
//
2125
// Inline a type alias into all of its uses where possible.
@@ -31,7 +35,7 @@ use crate::{
3135
// ```
3236
// ->
3337
// ```
34-
//
38+
//
3539
// fn id(x: i32) -> i32 {
3640
// x
3741
// };
@@ -62,15 +66,10 @@ pub(crate) fn inline_type_alias_uses(acc: &mut Assists, ctx: &AssistContext<'_>)
6266
let mut inline_refs_for_file = |file_id, refs: Vec<FileReference>| {
6367
builder.edit_file(file_id);
6468

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();
69+
let (path_types, path_type_uses) =
70+
split_refs_and_uses(builder, refs, |path_type| {
71+
path_type.syntax().ancestors().nth(3).and_then(ast::PathType::cast)
72+
});
7473

7574
for (target, replacement) in path_types.into_iter().filter_map(|path_type| {
7675
let replacement = inline(&ast_alias, &path_type)?.to_text(&concrete_type);
@@ -79,6 +78,10 @@ pub(crate) fn inline_type_alias_uses(acc: &mut Assists, ctx: &AssistContext<'_>)
7978
}) {
8079
builder.replace(target, replacement);
8180
}
81+
if !path_type_uses.is_empty() {
82+
builder.edit_file(file_id);
83+
path_type_uses.iter().for_each(remove_path_if_in_use_stmt);
84+
}
8285
};
8386

8487
for (file_id, refs) in usages.into_iter() {
@@ -993,7 +996,12 @@ fn foo() {
993996
}
994997
"#,
995998
r#"
996-
use super::I;
999+
//- /lib.rs
1000+
mod foo;
1001+
1002+
1003+
//- /foo.rs
1004+
9971005
fn foo() {
9981006
let _: i32 = 0;
9991007
}

0 commit comments

Comments
 (0)