Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 516
Neil N Avatar asked Jul 20 '10 16:07

Neil N


People also ask

How do I run a shortcut code in Visual Studio?

1. Ctrl+Shift+P, F1 ⮕ Show Command Palette. This shortcut opens the command palette in the vs-code, where we can search for the other commands. For example, if someone is working on HTML and wants to format the lines of code, but he/she doesn't know the command then the command palette comes in handy to search commands ...

What is change signature in visual studio?

Use Change Signature, instead of Rename, to alter a parameter name in C/C++ so that parameter names in declarations and implementations remain consistent. If you rename a parameter using the Rename command, only the signature and body of an implementation are updated; the declaration is untouched.

How do you duplicate a line in VS code?

Duplicate line. You can make the duplications of the same line using: Mac — Command + Shift + D. Windows/Ubuntu — Ctrl + Shift + D.

How do I select all in Visual Studio?

Ctrl + Shift + L to select all occurrences of current selection. Save this answer.


2 Answers

<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...

like image 110
Oren Avatar answered Nov 15 '22 20:11

Oren


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 
like image 31
Zac Avatar answered Nov 15 '22 20:11

Zac