Is there a way (like ⌥ + ⌘ + l
) to reformat named arguments in Android Studio to ensure they match the order specified in the function's declaration.
For example:
fun sum(a: Int, b: Int, c: Int) {
val result = a + b + c
println("Sum: $result")
}
// before
sum(
c = 3,
a = 1,
b = 2,
)
// after
sum(
a = 1,
b = 2,
c = 3,
)
You can do this by combining two intention actions.
Put the caret on the parameter list. Open the intention actions menu by the keyboard shortcut alt+enter (Windows) or option+enter (macOS). Select "Remove all argument names". Then do the same thing again, but select "Add names to call arguments" this time.
The idea is that by first removing the argument names, the arguments will be reordered in the correct order. Then you can add the argument names back again.
If you would like the arguments to be on separate lines, there is also the "Put arguments on separate lines" action.
To make this process even quicker, you can assign a custom keyboard shortcut to all of these actions. See more here.