Automatic indentation of arguments list on multiple lines in Visual Studio

孤街醉人 提交于 2020-02-21 10:40:26

问题


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 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 re-formatted 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 re-indent multi-line arguments following that pattern when it gets misaligned?


回答1:


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 VS stuff.

http://www.jetbrains.com/resharper/

This particular option is located at:

Update

Resharper > 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




回答2:


For anyone stumbling upon this problem today, in visual studio 2017 I found an option to do this. (not sure if it is available in other versions) Under 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.



来源:https://stackoverflow.com/questions/7053510/automatic-indentation-of-arguments-list-on-multiple-lines-in-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!