I have a switch case in a section of my function, and I need to reorder some of the cases for better code reading.
So the code at the moment looks something like this:
switch(parameter) { case "foo" : {DoSomething; DoSomething; DoSomething; DoSomething; break } case "bar" : {DoSomething; DoSomething; DoSomething; DoSomething; break } .... case "alpha" : {DoSomething; DoSomething; DoSomething; DoSomething; break } case "beta" : {DoSomething; DoSomething; DoSomething; DoSomething; break } }
So I have hundreads of cases inside this switch statement and I need to reorder most of them. And while reordering, for example if I want to put case foo and case bar below cases alpha and beta. A simple Ctrl+c
and Ctrl+v
gives me an output like this:
switch(parameter) { case "alpha" : {DoSomething; DoSomething; DoSomething; DoSomething; break } case "beta" : {DoSomething; DoSomething; DoSomething; DoSomething; break } ...... case "foo" : {DoSomething; DoSomething; DoSomething; DoSomething; break } case "bar" : {DoSomething; DoSomething; DoSomething; DoSomething; break } }
Rearranging this text multiple times is a cumbersome task. Is there a way a can duplicate a line as it is in some other part of the code?
For example I want the entire text to remain in a single line as it was before,
case "foo" : {DoSomething; DoSomething; DoSomething; DoSomething; break } case "bar" : {DoSomething; DoSomething; DoSomething; DoSomething; break }
Ctrl+V, Edit > Paste, Right Click > Paste. It should all just work!
Go to Tools -> Options. type copy in the search box. Under Text Editor -> Advanced ... Check Copy rich text on copy/cut.
As long as your code is syntactically correct (i.e. no stray brackets or End Ifs without matching Ifs), Visual Studio will reformat your whole file with one key chord: Hold down the Control key and then press K, followed by D. Boom! Everything looks pretty again.
You're experiencing a "feature" of Visual Studio that auto-formats code on certain actions (completed statement on ;, completed block on }, or on Paste. Fortunately, these preferences can be changed via the following settings page (they are language-specific):
Tools > Options > Text Editor > C# > Formatting
Then temporarily uncheck the Automatically format on paste option.
In Visual Studio 2017 and 2019, the "Formatting" options moved underneath a new "Code Style" menu and added some extra settings:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With