format icon indicating copy to clipboard operation
format copied to clipboard

Weird initializer indentation

Open uladz-zubrycki opened this issue 3 years ago • 5 comments

Indentation of the object/anonymous object initializer block is quite weird in my opinion — one level of indentation is missing. Consider the following example

private static int Foo(object obj, int number)
{
  return 0; 
}

private static void Bar()
{
  const int number = 1;
  var result = Foo(new
    {
      Prop1 = "",
      Prop2 = ""
    },
    number);
}

This is how dotnet-format reformats it

private static int Foo(object obj, int number)
{
  return 0; 
}

private static void Bar()
{
  const int number = 1;
  var result = Foo(new
  {
    Prop1 = "",
    Prop2 = ""
  },
    number);
}

Basically it always removes indentation for the initializer block, thus resulting in inconsistent indentation for the method parameters.

It looks even worse if we add some fluent style method invocations.

private static void Bar()
{
  const int number = 1;
  var result = Foo(new
  {
    Prop1 = "",
    Prop2 = ""
  },
      number)
    .GetHashCode()
    .ToString();
}

Is there a way to keep indentation like in the initial snippet with initializer indented consistently with the rest of the parameters?

E.g resharper formats it the first way and it's more reasonable in general.

dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.101
 Commit:    ef49f6213a

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22000
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.101\

Host (useful for support):
  Version: 6.0.1
  Commit:  3a25a7f1cc

.NET SDKs installed:
  3.1.100 [C:\Program Files\dotnet\sdk]
  3.1.416 [C:\Program Files\dotnet\sdk]
  6.0.100 [C:\Program Files\dotnet\sdk]
  6.0.101 [C:\Program Files\dotnet\sdk]

dotnet format --version
6.0.252703+68bc36719088c86b0ff01334039b0611741b8276

uladz-zubrycki avatar Jan 30 '22 15:01 uladz-zubrycki

I'm facing a similiar issue as well. Rider/Resharper force chops the Methods signature. When I use an object initializer as a parameter, the reformat is messed up as well.

before:

definition.BaseSet = searcherMapper.GetBaseSetFunc(new CriterionSearcherDefinition
                                                   {
                                                       CriterionSearcherFullName = attribute.CriterionSearcher.FullName,
                                                       WithContext = attribute.WithContext
                                                   },
                                                   sourceObjects);

after:

definition.BaseSet = searcherMapper.GetBaseSetFunc(new CriterionSearcherDefinition
{
    CriterionSearcherFullName = attribute.CriterionSearcher.FullName,
    WithContext = attribute.WithContext
 },
                                                   sourceObjects);
editorconfig
root = true

[*.cs]
charset = utf-8
end_of_line = lf
ident_size = 4
ident_style = space
tab_width = 4

[*.sln]
indent_style = tab

[*.{props,targets,csproj}]
indent_style = space
charset = utf-8
indent_size = 2


SimonSchwendele avatar Apr 04 '22 09:04 SimonSchwendele

Any news here? It's almost 4 months passed and no reaction whatsoever.

uladz-zubrycki avatar Apr 19 '22 16:04 uladz-zubrycki

When using method chains, the indentation is even inconsistent.

        list.Select(obj => new
        {
            obj.Name,
            obj.Value
        })
            .First();

        list.OrderBy(obj => obj.Value)
            .Select(obj => new
            {
                obj.Name,
                obj.Value
            })
            .First();

thoenissen avatar Jul 16 '22 20:07 thoenissen

Did the situation change by now ? That ones still annoying...

        var underTest = new AccordionArgumentBuilder<AccordionTestDto>(converter,
                                                                       eleStage,
                                                                       page).WithKey("accordion")
                                                                            .WithData(new AccordionTestDto
                                                                            {
                                                                                FirstName = "Half",
                                                                                Name = "Way",
                                                                                Age = 42,
                                                                                WorksAt = DayOfWeek.Monday
                                                                            },
                                                                                      x => x.FirstName,
                                                                                      x => x.WorksAt)
                                                                            .Build();

SimonSchwendele avatar Jul 03 '23 18:07 SimonSchwendele

I have this problem too, which results in inconsistencies between dotnet format and resharper, and I don't know how to fix it.

Expected:

await Parallel.ForEachAsync(
	clientData,
	new ParallelOptions 
		{ MaxDegreeOfParallelism = _options.Value.MaxDegreeOfParallelism, CancellationToken = ct },
	async (segRecord, token) => await ProcessClient(import.Id, segRecord, token));

Actual:

await Parallel.ForEachAsync(
	clientData,
	new ParallelOptions 
	{ MaxDegreeOfParallelism = _options.Value.MaxDegreeOfParallelism, CancellationToken = ct },
	async (segRecord, token) => await ProcessClient(import.Id, segRecord, token));

stilettk avatar May 11 '24 09:05 stilettk