views:

290

answers:

6
A: 

what about replace all (CTRL-H)

you can replace for example "i = j;" by "j = i;"

you can use regular expressions in that dialog. I'm not so sure about how you should pop-up help about them however. In that dialog, press F1, then search that page for more information on regular expressions.

I like this dialog because it allows you to go through each replacement. Because the chance of breaking something is high, I think this is a more secure solution

Eric
Ok, how exactly would I do the replace? AFAIK it only allows you to do regex or standard replace, not set one side of something to the other side and vice versa!
Ed Woodcock
A: 

You can do search and replace with regular expressions in Visual Studio, but it would be safer to just do a normal search and replace for each assignment you want to change rather than a bulk change.

Bryan Migliorisi
+1  A: 

Unfortunatly I don't have Visual Studio, so I can't try in the target environment, but if it uses standard regexps, you could probably do it like this: Search for "(:Al) = (:Al);", and replace with "\2 = \1". (\1 and \2 are references to the first and second capture groups, in this case the parenthesises around the \w:s)

EDIT
Ok, not \w... But according to MSDN, we can instead use :Al. Edited above to use that instead.
Also, from the MSDN page I gather that it should work, as the references seem to work as usual.

carlpett
unfortunately, Visual Studio doesn't support \w!
Ed Woodcock
+8  A: 

Select the lines you want to swap, Ctrl+H, then replace:

{:i}:b*=:b*{:i};

with:

\2 = \1;

with "Look in:" set to "Selection"

That only handles C/C++ style identifiers, though (via the ":i"). Replace that with:

{.*}:b*=:b*{.*};

to replace anything on either side of the "=".

Also, since you mentioned in a comment you use ReSharper, you can just highlight the "=", Alt+Enter, and "Reverse assignment".

Chris Doggett
Works like a charm, cheers!
Ed Woodcock
+1  A: 

The robust way to do this is to use a refactoring tool. They know the syntax of the language, so they understand the concept of "assignment statement" and can correctly select the entire expression on either side of the assignment operator rather than be limited to a single identifier, which is what I think all the regular expressions so far have covered. Refactoring tools treat your code as structured code instead of just text. I found mention two Visual Studio add-ins that can do it:

(Inverting assignment isn't technically refactoring since it changes the behavior of the program, but most refactoring tools extend the meaning to include other generic code modifications like that.)

Rob Kennedy
I actually use ReSharper quite a lot, for various other things, but I wouldn't know how to get it to invert the assigment operator, guess I'm just not that competant with it yet!
Ed Woodcock
ReSharper calls them "context actions." Place the cursor at the assignment, click the yellow light bulb, and find the reverse-assignment item. http://www.jetbrains.com/resharper/documentation/help20/AdvEditing/contextActions.html
Rob Kennedy
+1  A: 

Please see this question: Is there a method to swap the left and right hand sides of a set of expressions in Visual Studio?

My answer to that question has a macro that you can use to swap the assignments for a block of code.

Andrew Hare