Skip to content

Commit 867fd64

Browse files
committed
Make import sort order edition-wise
1 parent 4e147e7 commit 867fd64

File tree

15 files changed

+408
-118
lines changed

15 files changed

+408
-118
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,12 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
129129
range,
130130
|builder| {
131131
let scope = builder.make_import_scope_mut(scope.clone());
132-
insert_use(&scope, mod_path_to_ast(&import_path, edition), &ctx.config.insert_use);
132+
insert_use(
133+
&scope,
134+
mod_path_to_ast(&import_path, edition),
135+
&ctx.config.insert_use,
136+
edition,
137+
);
133138
},
134139
);
135140

@@ -154,6 +159,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
154159
&scope,
155160
mod_path_to_ast(&import_path, edition),
156161
&ctx.config.insert_use,
162+
edition,
157163
);
158164
},
159165
);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub(crate) fn convert_bool_to_enum(acc: &mut Assists, ctx: &AssistContext<'_>) -
5454
let BoolNodeData { target_node, name, ty_annotation, initializer, definition } =
5555
find_bool_node(ctx)?;
5656
let target_module = ctx.sema.scope(&target_node)?.module().nearest_non_block_module(ctx.db());
57+
let edition = target_module.krate().edition(ctx.db());
5758

5859
let target = name.syntax().text_range();
5960
acc.add(
@@ -75,7 +76,7 @@ pub(crate) fn convert_bool_to_enum(acc: &mut Assists, ctx: &AssistContext<'_>) -
7576
let mut delayed_mutations = Vec::new();
7677
replace_usages(edit, ctx, usages, definition, &target_module, &mut delayed_mutations);
7778
for (scope, path) in delayed_mutations {
78-
insert_use(&scope, path, &ctx.config.insert_use);
79+
insert_use(&scope, path, &ctx.config.insert_use, edition);
7980
}
8081
},
8182
)

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use ide_db::{
1111
syntax_helpers::node_ext::{for_each_tail_expr, walk_expr},
1212
};
1313
use syntax::{
14-
AstNode, SyntaxNode,
14+
AstNode, Edition, SyntaxNode,
1515
ast::{self, HasName, edit::IndentLevel, edit_in_place::Indent, make},
1616
match_ast, ted,
1717
};
@@ -60,6 +60,7 @@ pub(crate) fn convert_tuple_return_type_to_struct(
6060
let fn_def = ctx.sema.to_def(&fn_)?;
6161
let fn_name = fn_.name()?;
6262
let target_module = ctx.sema.scope(fn_.syntax())?.module().nearest_non_block_module(ctx.db());
63+
let edition = target_module.krate().edition(ctx.db());
6364

6465
let target = type_ref.syntax().text_range();
6566
acc.add(
@@ -92,7 +93,7 @@ pub(crate) fn convert_tuple_return_type_to_struct(
9293
replace_body_return_values(ast::Expr::BlockExpr(fn_body), &struct_name);
9394
}
9495

95-
replace_usages(edit, ctx, &usages, &struct_name, &target_module);
96+
replace_usages(edit, ctx, &usages, &struct_name, &target_module, edition);
9697
},
9798
)
9899
}
@@ -104,6 +105,7 @@ fn replace_usages(
104105
usages: &UsageSearchResult,
105106
struct_name: &str,
106107
target_module: &hir::Module,
108+
edition: Edition,
107109
) {
108110
for (file_id, references) in usages.iter() {
109111
edit.edit_file(file_id.file_id(ctx.db()));
@@ -156,7 +158,7 @@ fn replace_usages(
156158
}
157159
// add imports across modules where needed
158160
if let Some((import_scope, path)) = import_data {
159-
insert_use(&import_scope, path, &ctx.config.insert_use);
161+
insert_use(&import_scope, path, &ctx.config.insert_use, edition);
160162
}
161163
})
162164
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
221221
&scope,
222222
mod_path_to_ast(&mod_path, edition),
223223
&ctx.config.insert_use,
224+
edition,
224225
);
225226
}
226227
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ fn apply_references(
375375
edition: Edition,
376376
) {
377377
if let Some((scope, path)) = import {
378-
insert_use(&scope, mod_path_to_ast(&path, edition), &insert_use_cfg);
378+
insert_use(&scope, mod_path_to_ast(&path, edition), &insert_use_cfg, edition);
379379
}
380380
// deep clone to prevent cycle
381381
let path = make::path_from_segments(iter::once(segment.clone_subtree()), false);

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ide_db::imports::{
44
merge_imports::{MergeBehavior, try_merge_imports, try_merge_trees},
55
};
66
use syntax::{
7-
AstNode, SyntaxElement, SyntaxNode,
7+
AstNode, Edition, SyntaxElement, SyntaxNode,
88
algo::neighbor,
99
ast::{self, syntax_factory::SyntaxFactory},
1010
match_ast,
@@ -40,7 +40,12 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
4040

4141
let use_item = tree.syntax().parent().and_then(ast::Use::cast)?;
4242
let mut neighbor = next_prev().find_map(|dir| neighbor(&use_item, dir)).into_iter();
43-
let edits = use_item.try_merge_from(&mut neighbor, &ctx.config.insert_use);
43+
let edition = ctx
44+
.sema
45+
.scope(use_item.syntax())
46+
.map(|it| it.krate().edition(ctx.db()))
47+
.unwrap_or(Edition::CURRENT);
48+
let edits = use_item.try_merge_from(&mut neighbor, &ctx.config.insert_use, edition);
4449
(target, edits?)
4550
} else {
4651
// Merge selected
@@ -53,15 +58,20 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
5358
parent_node.children().filter(|it| selection_range.contains_range(it.text_range()));
5459

5560
let first_selected = selected_nodes.next()?;
61+
let edition = ctx
62+
.sema
63+
.scope(&first_selected)
64+
.map(|it| it.krate().edition(ctx.db()))
65+
.unwrap_or(Edition::CURRENT);
5666
let edits = match_ast! {
5767
match first_selected {
5868
ast::Use(use_item) => {
5969
cov_mark::hit!(merge_with_selected_use_item_neighbors);
60-
use_item.try_merge_from(&mut selected_nodes.filter_map(ast::Use::cast), &ctx.config.insert_use)
70+
use_item.try_merge_from(&mut selected_nodes.filter_map(ast::Use::cast), &ctx.config.insert_use, edition)
6171
},
6272
ast::UseTree(use_tree) => {
6373
cov_mark::hit!(merge_with_selected_use_tree_neighbors);
64-
use_tree.try_merge_from(&mut selected_nodes.filter_map(ast::UseTree::cast), &ctx.config.insert_use)
74+
use_tree.try_merge_from(&mut selected_nodes.filter_map(ast::UseTree::cast), &ctx.config.insert_use, edition)
6575
},
6676
_ => return None,
6777
}
@@ -103,11 +113,12 @@ trait Merge: AstNode + Clone {
103113
self,
104114
items: &mut dyn Iterator<Item = Self>,
105115
cfg: &InsertUseConfig,
116+
edition: Edition,
106117
) -> Option<Vec<Edit>> {
107118
let mut edits = Vec::new();
108119
let mut merged = self.clone();
109120
for item in items {
110-
merged = merged.try_merge(&item, cfg)?;
121+
merged = merged.try_merge(&item, cfg, edition)?;
111122
edits.push(Edit::Remove(item.into_either()));
112123
}
113124
if !edits.is_empty() {
@@ -117,26 +128,26 @@ trait Merge: AstNode + Clone {
117128
None
118129
}
119130
}
120-
fn try_merge(&self, other: &Self, cfg: &InsertUseConfig) -> Option<Self>;
131+
fn try_merge(&self, other: &Self, cfg: &InsertUseConfig, edition: Edition) -> Option<Self>;
121132
fn into_either(self) -> Either<ast::Use, ast::UseTree>;
122133
}
123134

124135
impl Merge for ast::Use {
125-
fn try_merge(&self, other: &Self, cfg: &InsertUseConfig) -> Option<Self> {
136+
fn try_merge(&self, other: &Self, cfg: &InsertUseConfig, edition: Edition) -> Option<Self> {
126137
let mb = match cfg.granularity {
127138
ImportGranularity::One => MergeBehavior::One,
128139
_ => MergeBehavior::Crate,
129140
};
130-
try_merge_imports(self, other, mb)
141+
try_merge_imports(self, other, mb, edition)
131142
}
132143
fn into_either(self) -> Either<ast::Use, ast::UseTree> {
133144
Either::Left(self)
134145
}
135146
}
136147

137148
impl Merge for ast::UseTree {
138-
fn try_merge(&self, other: &Self, _: &InsertUseConfig) -> Option<Self> {
139-
try_merge_trees(self, other, MergeBehavior::Crate)
149+
fn try_merge(&self, other: &Self, _: &InsertUseConfig, edition: Edition) -> Option<Self> {
150+
try_merge_trees(self, other, MergeBehavior::Crate, edition)
140151
}
141152
fn into_either(self) -> Either<ast::Use, ast::UseTree> {
142153
Either::Right(self)
@@ -605,7 +616,7 @@ use foo::$0{
605616
",
606617
r"
607618
use foo::{
608-
bar::baz, FooBar
619+
FooBar, bar::baz,
609620
};
610621
",
611622
)

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use ide_db::imports::merge_imports::try_normalize_import;
2-
use syntax::{AstNode, ast};
2+
use syntax::{AstNode, Edition, ast};
33

44
use crate::{
55
AssistId,
@@ -25,8 +25,13 @@ pub(crate) fn normalize_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
2525
};
2626

2727
let target = use_item.syntax().text_range();
28+
let edition = ctx
29+
.sema
30+
.scope(use_item.syntax())
31+
.map(|it| it.krate().edition(ctx.db()))
32+
.unwrap_or(Edition::CURRENT);
2833
let normalized_use_item =
29-
try_normalize_import(&use_item, ctx.config.insert_use.granularity.into())?;
34+
try_normalize_import(&use_item, ctx.config.insert_use.granularity.into(), edition)?;
3035

3136
acc.add(AssistId::refactor_rewrite("normalize_import"), "Normalize import", target, |builder| {
3237
builder.replace_ast(use_item, normalized_use_item);
@@ -109,8 +114,8 @@ mod tests {
109114
#[test]
110115
fn test_order() {
111116
check_assist_variations!(
112-
"foo::{*, Qux, bar::{Quux, Bar}, baz, FOO_BAZ, self, Baz}",
113-
"foo::{self, bar::{Bar, Quux}, baz, Baz, Qux, FOO_BAZ, *}"
117+
"foo::{*, Qux, bar::{Quux, Bar}, baz, FOO_BAZ, self, Baz, v10, v9, r#aaa}",
118+
"foo::{self, Baz, FOO_BAZ, Qux, r#aaa, bar::{Bar, Quux}, baz, v9, v10, *}"
114119
);
115120
}
116121

@@ -145,29 +150,29 @@ fn main() {
145150

146151
#[test]
147152
fn test_redundant_braces() {
148-
check_assist_variations!("foo::{bar::{baz, Qux}}", "foo::bar::{baz, Qux}");
153+
check_assist_variations!("foo::{bar::{baz, Qux}}", "foo::bar::{Qux, baz}");
149154
check_assist_variations!("foo::{bar::{self}}", "foo::bar::{self}");
150155
check_assist_variations!("foo::{bar::{*}}", "foo::bar::*");
151156
check_assist_variations!("foo::{bar::{Qux as Quux}}", "foo::bar::Qux as Quux");
152157
check_assist_variations!(
153158
"foo::bar::{{FOO_BAZ, Qux, self}, {*, baz}}",
154-
"foo::bar::{self, baz, Qux, FOO_BAZ, *}"
159+
"foo::bar::{self, FOO_BAZ, Qux, baz, *}"
155160
);
156161
check_assist_variations!(
157162
"foo::bar::{{{FOO_BAZ}, {{Qux}, {self}}}, {{*}, {baz}}}",
158-
"foo::bar::{self, baz, Qux, FOO_BAZ, *}"
163+
"foo::bar::{self, FOO_BAZ, Qux, baz, *}"
159164
);
160165
}
161166

162167
#[test]
163168
fn test_merge() {
164169
check_assist_variations!(
165170
"foo::{*, bar, {FOO_BAZ, qux}, bar::{*, baz}, {Quux}}",
166-
"foo::{bar::{self, baz, *}, qux, Quux, FOO_BAZ, *}"
171+
"foo::{FOO_BAZ, Quux, bar::{self, baz, *}, qux, *}"
167172
);
168173
check_assist_variations!(
169174
"foo::{*, bar, {FOO_BAZ, qux}, bar::{*, baz}, {Quux, bar::{baz::Foo}}}",
170-
"foo::{bar::{self, baz::{self, Foo}, *}, qux, Quux, FOO_BAZ, *}"
175+
"foo::{FOO_BAZ, Quux, bar::{self, baz::{self, Foo}, *}, qux, *}"
171176
);
172177
}
173178

@@ -229,15 +234,15 @@ use {
229234
check_assist_not_applicable_variations!("foo::bar");
230235
check_assist_not_applicable_variations!("foo::bar::*");
231236
check_assist_not_applicable_variations!("foo::bar::Qux as Quux");
232-
check_assist_not_applicable_variations!("foo::bar::{self, baz, Qux, FOO_BAZ, *}");
237+
check_assist_not_applicable_variations!("foo::bar::{self, FOO_BAZ, Qux, baz, *}");
233238
check_assist_not_applicable_variations!(
234-
"foo::{self, bar::{Bar, Quux}, baz, Baz, Qux, FOO_BAZ, *}"
239+
"foo::{self, Baz, FOO_BAZ, Qux, bar::{Bar, Quux}, baz, *}"
235240
);
236241
check_assist_not_applicable_variations!(
237-
"foo::{bar::{self, baz, *}, qux, Quux, FOO_BAZ, *}"
242+
"foo::{FOO_BAZ, Quux, bar::{self, baz, *}, qux, *}"
238243
);
239244
check_assist_not_applicable_variations!(
240-
"foo::{bar::{self, baz::{self, Foo}, *}, qux, Quux, FOO_BAZ, *}"
245+
"foo::{bar::{self, FOO_BAZ, Quux, baz::{self, Foo}, *}, qux, *}"
241246
);
242247
}
243248
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub(crate) fn replace_qualified_name_with_use(
9595
Some(qualifier) => make::path_concat(qualifier, path),
9696
None => path,
9797
};
98-
insert_use(&scope, path, &ctx.config.insert_use);
98+
insert_use(&scope, path, &ctx.config.insert_use, edition);
9999
},
100100
)
101101
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use syntax::{
2-
TextRange,
2+
Edition, TextRange,
33
ast::{self, AstNode, HasArgList, prec::ExprPrecedence},
44
};
55

@@ -111,10 +111,20 @@ fn add_import(
111111
import.syntax(),
112112
&ctx.sema,
113113
);
114+
let edition = ctx
115+
.sema
116+
.scope(import.syntax())
117+
.map(|it| it.krate().edition(ctx.db()))
118+
.unwrap_or(Edition::CURRENT);
114119

115120
if let Some(scope) = scope {
116121
let scope = edit.make_import_scope_mut(scope);
117-
ide_db::imports::insert_use::insert_use(&scope, import, &ctx.config.insert_use);
122+
ide_db::imports::insert_use::insert_use(
123+
&scope,
124+
import,
125+
&ctx.config.insert_use,
126+
edition,
127+
);
118128
}
119129
}
120130
}

crates/ide-completion/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ pub fn resolve_completion_edits(
292292
&new_ast,
293293
make::path_from_text_with_edition(&full_import_path, current_edition),
294294
&config.insert_use,
295+
current_edition,
295296
);
296297
});
297298

0 commit comments

Comments
 (0)