Skip to content

Extract into function should preserve existing parameter order #18639

Closed
@emilk

Description

@emilk

Problem

I often have a list of similar "context" parameters to my functions, and I try to be consistent with their order. For example:

fn foo_ui(ctx: &Context, ui: &mut Ui, foo: &Foo) {}

fn bar_ui(ctx: &Context, ui: &mut Ui, bar: &Bar, option: Option) {}

When calling "Extract to function" on some code, Rust analyzer ignores this parameter order and I often end up with functions where I need to manually rearrange the parameter order to my liking.

Suggested solution

When "Extract to function" decides the parameter order of the new function, it should use the following algorithm:

A) Categorize the arguments based on wether or not they were parameters to the already existing function
B) If yes, put them first, and in the same order as in the existing function
C) if no, put them last, in whatever order (I have no strong opinion here - can use same heuristic as the current code)

Effect

fn existing(a: A, b: B, c: C) {
    let x = 32;
    let y = 1.0;

    long_code_block_that_uses(x,b,a,y) // <--- call "Extract to function" on this code block
}

Results in ->

fn existing(a: A, b: B, c: C) {
    let x = 32;
    let y = 1.0;
    fun(a, b, x, y);
}

fn fun(a: A, b: B, x: i32, y: f32) {
    long_code_block_that_uses(x,b,a,y)
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions