Automatic indentation of arguments list on multiple lines in Visual Studio
Asked Answered
O

2

22

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?

Orgy answered 13/8, 2011 at 21:50 Comment(0)
L
9

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 ToolsOptionsText editorC/C++FormattingIndentation"Within parentheses, align new lines when I type them".

Choose the option "Align contents to opening parentheses".

Visual studio 2019 update.

Mostly the same answer but Formatting seems to have moved under Code style: So: Under menu ToolsOptionsText editorC/C++Code StyleFormattingIndentation"Within parentheses, align new lines when I type them".

Choose the option "Align contents to opening parentheses".

Align contents

Lager answered 19/2, 2020 at 9:56 Comment(1)
Why oh why, for all that is good, is this formatting option only available for C/C++ and not C#!???? Microsoft, why do you constantly find new ways to anger me?Obscuration
M
4

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 OptionsC#Formatting StyleLine Breaks and WrappingWrap long lines.

I don't know if it does exactly what you want, but it does wrap long lines.

Moitoso answered 13/8, 2011 at 21:58 Comment(1)
I am actually using ReSharper already; I'll check if it has an option for that.Orgy

© 2022 - 2024 — McMap. All rights reserved.