diff --git a/src/tools/rust-analyzer/crates/hir/src/lib.rs b/src/tools/rust-analyzer/crates/hir/src/lib.rs index 10365ba0028..83d72dfcf14 100644 --- a/src/tools/rust-analyzer/crates/hir/src/lib.rs +++ b/src/tools/rust-analyzer/crates/hir/src/lib.rs @@ -3781,6 +3781,18 @@ impl Local { } } +impl PartialOrd for Local { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for Local { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.binding_id.cmp(&other.binding_id) + } +} + #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct DeriveHelper { pub(crate) derive: MacroId, diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_function.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_function.rs index ad0be896450..6937d33ebc1 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_function.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_function.rs @@ -18,6 +18,7 @@ use ide_db::{ }, FxIndexSet, RootDatabase, }; +use itertools::Itertools; use syntax::{ ast::{ 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 return; } - let params = - body.extracted_function_params(ctx, &container_info, locals_used.iter().copied()); + let params = body.extracted_function_params(ctx, &container_info, locals_used); let name = make_function_name(&semantics_scope); @@ -1067,9 +1067,11 @@ impl FunctionBody { &self, ctx: &AssistContext<'_>, container_info: &ContainerInfo, - locals: impl Iterator, + locals: FxIndexSet, ) -> Vec { locals + .into_iter() + .sorted() .map(|local| (local, local.primary_source(ctx.db()))) .filter(|(_, src)| is_defined_outside_of_body(ctx, self, src)) .filter_map(|(local, src)| match src.into_ident_pat() { @@ -3167,11 +3169,11 @@ fn foo() { let mut c = C { p: P { n: 0 } }; let mut v = C { p: P { n: 0 } }; let u = C { p: P { n: 0 } }; - fun_name(&mut c, &u, &mut v); + fun_name(&mut c, &mut v, &u); let m = c.p.n + v.p.n + u.p.n; } -fn $0fun_name(c: &mut C, u: &C, v: &mut C) { +fn $0fun_name(c: &mut C, v: &mut C, u: &C) { c.p.n += u.p.n; let r = &mut v.p.n; } @@ -5602,10 +5604,10 @@ fn parent(factor: i32) { fn parent(factor: i32) { let v = &[1, 2, 3]; - fun_name(v, factor); + fun_name(factor, v); } -fn $0fun_name(v: &[i32; 3], factor: i32) { +fn $0fun_name(factor: i32, v: &[i32; 3]) { v.iter().map(|it| it * factor); } "#, @@ -5786,11 +5788,11 @@ struct Struct>(T); impl + Copy> Struct { fn func>(&self, v: V) -> i32 { let t = self.0; - fun_name(t, v) + fun_name(v, t) } } -fn $0fun_name + Copy, V: Into>(t: T, v: V) -> i32 { +fn $0fun_name + Copy, V: Into>(v: V, t: T) -> i32 { t.into() + v.into() } "#, @@ -5815,11 +5817,11 @@ struct Struct, U: Debug>(T, U); impl + Copy, U: Debug> Struct { fn func>(&self, v: V) -> i32 { let t = self.0; - fun_name(t, v) + fun_name(v, t) } } -fn $0fun_name + Copy, V: Into>(t: T, v: V) -> i32 { +fn $0fun_name + Copy, V: Into>(v: V, t: T) -> i32 { t.into() + v.into() } "#, @@ -5844,11 +5846,11 @@ struct Struct(T) where T: Into; impl Struct where T: Into + Copy { fn func(&self, v: V) -> i32 where V: Into { let t = self.0; - fun_name(t, v) + fun_name(v, t) } } -fn $0fun_name(t: T, v: V) -> i32 where T: Into + Copy, V: Into { +fn $0fun_name(v: V, t: T) -> i32 where T: Into + Copy, V: Into { t.into() + v.into() } "#, @@ -5873,11 +5875,11 @@ struct Struct(T, U) where T: Into, U: Debug; impl Struct where T: Into + Copy, U: Debug { fn func(&self, v: V) -> i32 where V: Into { let t = self.0; - fun_name(t, v) + fun_name(v, t) } } -fn $0fun_name(t: T, v: V) -> i32 where T: Into + Copy, V: Into { +fn $0fun_name(v: V, t: T) -> i32 where T: Into + Copy, V: Into { t.into() + v.into() } "#, @@ -6106,6 +6108,31 @@ fn $0fun_name() -> i32 { ); } + #[test] + fn sort_params_in_order() { + check_assist( + extract_function, + r#" +fn existing(a: i32, b: i32, c: i32) { + let x = 32; + + let p = $0x + b + c + a$0; +} +"#, + r#" +fn existing(a: i32, b: i32, c: i32) { + let x = 32; + + let p = fun_name(a, b, c, x); +} + +fn $0fun_name(a: i32, b: i32, c: i32, x: i32) -> i32 { + x + b + c + a +} +"#, + ); + } + #[test] fn in_left_curly_is_not_applicable() { cov_mark::check!(extract_function_in_braces_is_not_applicable);