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

Incorrect indentation in C# #930

Open Sayberix opened 2 years ago

Sayberix commented 2 years ago

Environment

Description

Incorrect indentation

Current behavior

            for (int i = 0; i < array.Length; i++)
            {
                if (i == 0)
                    Console.Write("[" + array[i] + ", ");
                else
                    if (i == array.Length - 1)
                    Console.Write(array[i] + "]");
                else
                    Console.Write(array[i] + ", ");
            }

Expected behavior

            for (int i = 0; i < array.Length; i++)
            {
                if (i == 0)
                    Console.Write("[" + array[i] + ", ");
                else
                    if (i == array.Length - 1)
                        _Console.Write(array[i] + "]");
                    else
                        Console.Write(array[i] + ", ");_
            }
codecadwallader commented 2 years ago

Thank you for reporting the issue. Indentation is handled natively by Visual Studio, which CodeMaid invokes as part of its cleanup by default. If you run the Edit->Advanced->Format Document command do you see the same behavior?

codecadwallader commented 2 years ago

I took a look at the code a bit further. I think what's happening is that the first else and the following if are effectively an else if unless you include braces. That's why indentation may not work as you expected.

Sayberix commented 2 years ago

Yes you are right. Everything works fine with parentheses. Is it possible to extend the possibilities of correct editing without brackets?

codecadwallader commented 2 years ago

I think to do that would mean changing the C# language definition to consider indentation significant, which I do not think Microsoft would be willing to do. I think it would be better to include the braces as the language expects.

Sayberix commented 2 years ago

OK I understood. Thank you!