question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Clarify which .editorconfig options are supported

See original GitHub issue

Documentation says:

The dotnet-format global tool supports the core set of EditorConfig options* as well as the .NET coding convention settings for EditorConfig.

I tried setting csharp_new_line_before_members_in_object_initializers = true and it did nothing. Documentation is here.

I tried to look into source code but found only whitespace, newline, usings, etc formatters. So does this tool support codestyle rules defined by microsoft? Or does it rely on analyzers installed from SDK/NuGet? (maybe i misunderstand some concepts here…)

Project targets aspnetcore 2.2 SDKs i have installed: 2.2, 3.1, 5.0

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JoeRobichcommented, Oct 11, 2021

@sharwell Since you have a better understanding of the formatting options here, Is there an existing combination of options that achieves the desired formatting?

1reaction
Rast1234commented, Oct 11, 2021

I am sorry to ask more questions, i just have no idea where to find people who actually understand behavior of these rules (please point me if there’s any sort of community)… I swear i have read all docs but can’t figure out how to tune rule combinations properly.

Is it possible to achieve this formatting?

  • auto-properties { get; set;} are on a single line
  • object initializers are forced to be on multiple lines
using System;

namespace Formatting
{

    class Something
    {
        public int Good { get; set; }
        public int Bad1
        {
            get;
            set;
        }
        public int Bad2
        {
            get; set;
        }
        public static void Test()
        {
            var good_formatting = new Something
            {
                Good = 1,
                Bad1 = 2
            };
            var bad_formatting1 = new Something { Good = 1, Bad1 = 2 };
            var bad_formatting2 = new Something
            {
                Good = 1, Bad1 = 2
            };
    }
}

Options i tried:

  • csharp_new_line_before_open_brace seems irrelevant because it only affects { placement, not split-block-into-newlines behavior.
  • csharp_preserve_single_line_blocks = true only ignores blocks if they are on a single line, so there is no way to format var bad_formatting1?
  • csharp_new_line_before_members_in_object_initializers works as you said, fixing var bad_formatting2.

editorconfig combinations:

# fixes bad_formatting2 (expected)
csharp_preserve_single_line_blocks = true
csharp_new_line_before_members_in_object_initializers = true

# does nothing (expected)
csharp_preserve_single_line_blocks = true
csharp_new_line_before_members_in_object_initializers = false

# fixes bad_formatting2, but also formats Good property as Bad2 (get-set are together on a newline)
csharp_preserve_single_line_blocks = false
csharp_new_line_before_members_in_object_initializers = true

# only mangles Good property as Bad2
csharp_preserve_single_line_blocks = false
csharp_new_line_before_members_in_object_initializers = false

So, the first question: is there any way to enforce style like Good property and good_formatting initializer from example above?

Second question: formatter seems to be OK with this existing together, am i missing something?

Prop1
{
    get; set;
}

Prop2
{
    get;
    set;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

EditorConfig | IntelliJ IDEA Documentation
IntelliJ IDEA allows you to manage all code style settings for each individual set of files with EditorConfig support.
Read more >
EditorConfig settings - Visual Studio (Windows)
EditorConfig settings are supported by many code editors and IDEs, including Visual Studio. It's a portable component that travels with your ...
Read more >
Use EditorConfig | ReSharper Documentation
EditorConfig is a configuration file convention that is used to define and maintain consistent code styles between team members working on ...
Read more >
Configuration files for code analysis rules - .NET
EditorConfig options apply only to source files in a project or directory. Files that are included in a project as AdditionalFiles are not ......
Read more >
Why You Should Use EditorConfig to Standardize Code ...
You use EditorConfig to define formatting conventions for textual files in a project. It's great because it's widely supported, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found