Invert assignment direction in Visual Studio [dupl

2019-01-30 19:53发布

This question already has an answer here:

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

9条回答
冷血范
2楼-- · 2019-01-30 20:37

I've improved the expression a little.

Replace

(\t+)(\s*)(\S*) = (\S*);

with

$1$2$4 = $3;

The reason is, it will look for lines starting with tab (\t). It will skip the lines starting with definition. E.g.:

        TestClass tc = new TestClass();
        int a = 75;
        int b = 76;
        int c = 77;

        a = tc.a;
        b = tc.b;
        a = tc.c;

Would ignore the int a, int b and int c and swap only the assignments.

查看更多
The star\"
3楼-- · 2019-01-30 20:40

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.

查看更多
叛逆
4楼-- · 2019-01-30 20:41

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

查看更多
登录 后发表回答