Skip to content

Commit 0689366

Browse files
committed
feat: preserve order of parameters in extract_functions
1 parent 9c03cbb commit 0689366

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

Diff for: crates/hir/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -3781,6 +3781,18 @@ impl Local {
37813781
}
37823782
}
37833783

3784+
impl PartialOrd for Local {
3785+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
3786+
Some(self.cmp(other))
3787+
}
3788+
}
3789+
3790+
impl Ord for Local {
3791+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
3792+
self.binding_id.cmp(&other.binding_id)
3793+
}
3794+
}
3795+
37843796
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
37853797
pub struct DeriveHelper {
37863798
pub(crate) derive: MacroId,

Diff for: crates/ide-assists/src/handlers/extract_function.rs

+42-15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use ide_db::{
1818
},
1919
FxIndexSet, RootDatabase,
2020
};
21+
use itertools::Itertools;
2122
use syntax::{
2223
ast::{
2324
self, edit::IndentLevel, edit_in_place::Indent, AstNode, AstToken, HasGenericParams,
@@ -114,8 +115,7 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
114115
return;
115116
}
116117

117-
let params =
118-
body.extracted_function_params(ctx, &container_info, locals_used.iter().copied());
118+
let params = body.extracted_function_params(ctx, &container_info, locals_used);
119119

120120
let name = make_function_name(&semantics_scope);
121121

@@ -1067,9 +1067,11 @@ impl FunctionBody {
10671067
&self,
10681068
ctx: &AssistContext<'_>,
10691069
container_info: &ContainerInfo,
1070-
locals: impl Iterator<Item = Local>,
1070+
locals: FxIndexSet<Local>,
10711071
) -> Vec<Param> {
10721072
locals
1073+
.into_iter()
1074+
.sorted()
10731075
.map(|local| (local, local.primary_source(ctx.db())))
10741076
.filter(|(_, src)| is_defined_outside_of_body(ctx, self, src))
10751077
.filter_map(|(local, src)| match src.into_ident_pat() {
@@ -3167,11 +3169,11 @@ fn foo() {
31673169
let mut c = C { p: P { n: 0 } };
31683170
let mut v = C { p: P { n: 0 } };
31693171
let u = C { p: P { n: 0 } };
3170-
fun_name(&mut c, &u, &mut v);
3172+
fun_name(&mut c, &mut v, &u);
31713173
let m = c.p.n + v.p.n + u.p.n;
31723174
}
31733175
3174-
fn $0fun_name(c: &mut C, u: &C, v: &mut C) {
3176+
fn $0fun_name(c: &mut C, v: &mut C, u: &C) {
31753177
c.p.n += u.p.n;
31763178
let r = &mut v.p.n;
31773179
}
@@ -5602,10 +5604,10 @@ fn parent(factor: i32) {
56025604
fn parent(factor: i32) {
56035605
let v = &[1, 2, 3];
56045606
5605-
fun_name(v, factor);
5607+
fun_name(factor, v);
56065608
}
56075609
5608-
fn $0fun_name(v: &[i32; 3], factor: i32) {
5610+
fn $0fun_name(factor: i32, v: &[i32; 3]) {
56095611
v.iter().map(|it| it * factor);
56105612
}
56115613
"#,
@@ -5786,11 +5788,11 @@ struct Struct<T: Into<i32>>(T);
57865788
impl <T: Into<i32> + Copy> Struct<T> {
57875789
fn func<V: Into<i32>>(&self, v: V) -> i32 {
57885790
let t = self.0;
5789-
fun_name(t, v)
5791+
fun_name(v, t)
57905792
}
57915793
}
57925794
5793-
fn $0fun_name<T: Into<i32> + Copy, V: Into<i32>>(t: T, v: V) -> i32 {
5795+
fn $0fun_name<T: Into<i32> + Copy, V: Into<i32>>(v: V, t: T) -> i32 {
57945796
t.into() + v.into()
57955797
}
57965798
"#,
@@ -5815,11 +5817,11 @@ struct Struct<T: Into<i32>, U: Debug>(T, U);
58155817
impl <T: Into<i32> + Copy, U: Debug> Struct<T, U> {
58165818
fn func<V: Into<i32>>(&self, v: V) -> i32 {
58175819
let t = self.0;
5818-
fun_name(t, v)
5820+
fun_name(v, t)
58195821
}
58205822
}
58215823
5822-
fn $0fun_name<T: Into<i32> + Copy, V: Into<i32>>(t: T, v: V) -> i32 {
5824+
fn $0fun_name<T: Into<i32> + Copy, V: Into<i32>>(v: V, t: T) -> i32 {
58235825
t.into() + v.into()
58245826
}
58255827
"#,
@@ -5844,11 +5846,11 @@ struct Struct<T>(T) where T: Into<i32>;
58445846
impl <T> Struct<T> where T: Into<i32> + Copy {
58455847
fn func<V>(&self, v: V) -> i32 where V: Into<i32> {
58465848
let t = self.0;
5847-
fun_name(t, v)
5849+
fun_name(v, t)
58485850
}
58495851
}
58505852
5851-
fn $0fun_name<T, V>(t: T, v: V) -> i32 where T: Into<i32> + Copy, V: Into<i32> {
5853+
fn $0fun_name<T, V>(v: V, t: T) -> i32 where T: Into<i32> + Copy, V: Into<i32> {
58525854
t.into() + v.into()
58535855
}
58545856
"#,
@@ -5873,11 +5875,11 @@ struct Struct<T, U>(T, U) where T: Into<i32>, U: Debug;
58735875
impl <T, U> Struct<T, U> where T: Into<i32> + Copy, U: Debug {
58745876
fn func<V>(&self, v: V) -> i32 where V: Into<i32> {
58755877
let t = self.0;
5876-
fun_name(t, v)
5878+
fun_name(v, t)
58775879
}
58785880
}
58795881
5880-
fn $0fun_name<T, V>(t: T, v: V) -> i32 where T: Into<i32> + Copy, V: Into<i32> {
5882+
fn $0fun_name<T, V>(v: V, t: T) -> i32 where T: Into<i32> + Copy, V: Into<i32> {
58815883
t.into() + v.into()
58825884
}
58835885
"#,
@@ -6106,6 +6108,31 @@ fn $0fun_name() -> i32 {
61066108
);
61076109
}
61086110

6111+
#[test]
6112+
fn sort_params_in_order() {
6113+
check_assist(
6114+
extract_function,
6115+
r#"
6116+
fn existing(a: i32, b: i32, c: i32) {
6117+
let x = 32;
6118+
6119+
let p = $0x + b + c + a$0;
6120+
}
6121+
"#,
6122+
r#"
6123+
fn existing(a: i32, b: i32, c: i32) {
6124+
let x = 32;
6125+
6126+
let p = fun_name(a, b, c, x);
6127+
}
6128+
6129+
fn $0fun_name(a: i32, b: i32, c: i32, x: i32) -> i32 {
6130+
x + b + c + a
6131+
}
6132+
"#,
6133+
);
6134+
}
6135+
61096136
#[test]
61106137
fn in_left_curly_is_not_applicable() {
61116138
cov_mark::check!(extract_function_in_braces_is_not_applicable);

0 commit comments

Comments
 (0)