Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invert assignment direction in Visual Studio [duplicate]

Tags:

I have a bunch of assignment operations in Visual Studio, and I want to reverse them: i.e

i = j;
would become
j = i;

i.e. replacing everything before the equals with what's after the equals, and vice versa

Is there any easy way to do this, say something in the regular expression engine?

Cheers, Ed

like image 632
Ed James Avatar asked Jun 18 '09 14:06

Ed James


2 Answers

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

like image 118
Chris Doggett Avatar answered Oct 01 '22 04:10

Chris Doggett


Just a slight improvement on Chris's answer...

Ctrl+H, then replace:

{:b*}{[^:b]*}:b*=:b*{[^:b]*}:b*; 

with:

\1\3 = \2; 

(better handling of whitespace, esp. at beginning of line)

EDIT: For Visual Studio 2012 and higher (I tried it on 2015): Replace

(\s*)([^\s]+)\s*=\s*([^\s]+)\s*; 

with:

$1$3 = $2; 
like image 20
tintin Avatar answered Oct 01 '22 03:10

tintin