codecadwallader / codemaid

CodeMaid is an open source Visual Studio extension to cleanup and simplify our C#, C++, F#, VB, PHP, PowerShell, JSON, XAML, XML, ASP, HTML, CSS, LESS, SCSS, JavaScript and TypeScript coding.
http://www.codemaid.net
GNU Lesser General Public License v3.0
1.89k stars 356 forks source link

Empty constructor and method formatting #899

Open flamewave opened 2 years ago

flamewave commented 2 years ago

Environment

Description

Ever since upgrading to v12, auto-formatting files has started moving the brackets of empty constructors and methods to a new line instead of keeping them on a single line like it used to. I'm not sure if this is a bug/regression or if I am just missing a setting somewhere.

Example:

public class MyClass
{
    public MyClass() { }

    public void MyMethod() { }
}

Will get formatted as:

public class MyClass
{
    public MyClass()
    { }

    public void MyMethod()
    { }
}

Using the "Format Document" feature of Visual Studio does not do this, so it appears to be something with CodeMaid formatting.

Steps to recreate

See example above.

Current behavior

Formatting places brackets of empty constructors and methods on their own line.

Expected behavior

Brackets of empty constructors and methods should be left on the same line as the method definition.

asbjornb commented 2 years ago

I would also love a toggle for this similar to the one for single line methods (SA1502).

codecadwallader commented 2 years ago

Thanks for reporting the issue. I have been able to reproduce it.

konrad-gora commented 2 years ago

I would also love a toggle for this similar to the one for single line methods (SA1502).

It would be best if there could also be a possibility to turn on formatting classes/methods to a single line if the body is empty. (opposite behavior to actual)

ahaleiii commented 1 year ago

Would also like the ability to configure this behavior to leave empty constructor braces on the same line.

VS version: 2022 Community

codecadwallader commented 1 year ago

If you change the option at CodeMaid->Options->Cleaning->Update->Update single line methods by placing braces on separate lines (SA1502), do you still the same behavior?

ahaleiii commented 1 year ago

I have that option checked in my settings. For this instance, regardless of whether it was checked or unchecked, the empty constructor braces got moved to the next line.

TimothyMakkison commented 1 year ago

This is a similar problem to #983. Disabling Code maid-> Options-> Cleaning-> Insert-> Insert explicit access modifier fields on methods, will fix this. Note if Update->Update single line methods by placing braces on separate lines (SA1502), is enabled then brackets will be moved to separate lines. If Insert explicit method is enabled then (SA1502) will not do anything as it overriden.

// Insert explicit disabled, SA1502 enabled.
public class MyClass
{
    public MyClass()
    {
    }

    public void MyMethod()
    {
    }
}
// Insert explicit disabled, SA1502 disabled.
public class MyClass
{
    public MyClass() { }

    public void MyMethod() { }
}
ahaleiii commented 1 year ago

Yes, thank you @TimothyMakkison, that worked. Disabling both of these left the empty constructor braces on the same line.

cytoph commented 6 months ago

Sorry necroing this issue, but in my opinion this is a bug that should be looked into. The explicit access modifiers have nothing to do with putting those curly braces to a separate line, and for my case (and I think many of the other users, too) I want CodeMaid to yes, insert explicit access modifiers, but don't move the empty curly braces to a separate line.