Is there a shortcut to swap/reorder parameters in visual studio IDE?
Asked Answered
T

4

15

I have a common issue when working with code in the IDE:

string.Concat("foo", "bar");

and I need to change it to:

string.Concat("bar", "foo");

Often I have several of these that need to be swapped at once. I would like to avoid all the typing. Is there a way to automate this? Either a shortcut or some sort of macro would be great if I knew where to start.

Edit: changed to string.Concat to show that you can't always modify the method signature. I am only looking to change the order of the params in the method call, and nothing else.

Tamar answered 20/7, 2010 at 16:42 Comment(0)
S
5

<Ctrl> + <Shift> + <t> will transpose two words, so it would work in your case. Unfortunately I don't see this working (without multiple presses) for functions with larger parameter lists...

Stipulation answered 20/7, 2010 at 16:53 Comment(1)
This comes close, but unfortunately doesn't work for comma delimited params. I end up with something like (,"foo" "bar") or (,foo""bar") perhaps its just the quotes that throw it off. But thanks for the answer.Tamar
P
2

I had a lot of code with this function:

SetInt(comboBox1.Value + 1, "paramName", ...
SetInt(comboBoxOther.Value, "paramName", ...

And I needed to swap only the first two parameters;

I ended up using some text editor with regular expression management (like Scite), and using this one saved me hours:

Find: SetInt(\([.a-z0-9]+[ + 1]*\), \("[a-z0-9]+"\)
Replace: SetInt(\2, \1 
Panpipe answered 26/4, 2022 at 14:51 Comment(0)
G
2

I use an extension that does exactly this one thing.

It describes itself as

Allows arguments (or other comma separated lists contained in brackets, braces, etc.) to be shifted left and right.

https://marketplace.visualstudio.com/items?itemName=Gruntfuggly.shifter

Gormandize answered 2/2 at 18:22 Comment(0)
B
-1

Resharper once again shows superior features over Visual Studio. Since there is no definitive answer and I came here by a Google search, I felt the need to give this information:

Resharper has a refactoring feature called Change Signature refactoring and this works seamlessly together with the shortcuts for Rearrange code elements.

  1. You can simply select the parameter you want to move, then do Ctrl+Shift+Alt + Left/Right
  2. Once done, you can then press Alt+Enter and choose Apply Change Signature refactoring...
Bareheaded answered 11/5, 2023 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.