When the list of arguments to a method grows to the point where they do not fit comfortably on one line, I like to format code such that each argument is on a separate line (following the StyleCop suggestion), like this:
public void MyMethod(
int someArgument,
double someOtherArgument,
int someMoreArguments)
The problem I run into is that this formatting is "fragile" and does not get automatically reformatted when using Ctrl + K + D. For instance, if I happen to insert some spaces in front of one of the arguments, it doesn't get removed, and I end up doing some tedious manual reformatting.
If I copy a method (say, to provide an overloaded signature), the argument indentation in the copy gets totally messy. I have a similar issue with LINQ statements, which I also like to format on multiple lines, like:
myEnumerable.
.Where(this and that)
.Where(this and that)
.FirstOrDefault();
I realize this is complete obsessive-compulsive formatting, and a very minor issue, but is there a way to make Visual Studio 2010 automatically reindent multi-line arguments following that pattern when it gets misaligned?
If you prefer using [spacebar] to indent your code rather than using [tab], you can select multiple lines by holding the [alt] key and clicking on the beginning of each line you want to indent. Then, you can press [spacebar] and all the selected lines will be affected.
Select the text you want to automatically indent.Click menu Edit → Advanced → *Format Selection, or press Ctrl + K , Ctrl + F . Format Selection applies the smart indenting rules for the language in which you are programming to the selected text.
For anyone searching this problem for later versions, in Visual Studio 2017 I found an option to do this (I am not sure if it is available in other versions).
Under menu Tools → Options → Text editor → C/C++ → Formatting → Indentation → "Within parentheses, align new lines when I type them".
Choose the option "Align contents to opening parentheses".
You may want to look at ReSharper from JetBrains. It has all sorts of rules that can be applied to formatting code that is much better that the built-in Visual Studio stuff.
This particular option is located at:
ReSharper → menu Options → C# → Formatting Style → Line Breaks and Wrapping → Wrap long lines.
I don't know if it does exactly what you want, but it does wrap long lines.
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