codemaid icon indicating copy to clipboard operation
codemaid copied to clipboard

Incorrect indentation in C#

Open Sayberix opened this issue 2 years ago • 5 comments

Environment

  • Visual Studio version: Microsoft Visual Studio Enterprise 2019 Version 16.11.16
  • CodeMaid version: 12.0
  • Code language: C#

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] + ", ");_
            }
  • Desired changes are displayed in italics

Sayberix avatar Jul 07 '22 13:07 Sayberix

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 avatar Jul 28 '22 15:07 codecadwallader

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.

codecadwallader avatar Jul 28 '22 15:07 codecadwallader

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

Sayberix avatar Jul 29 '22 16:07 Sayberix

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.

codecadwallader avatar Aug 11 '22 14:08 codecadwallader

OK I understood. Thank you!

Sayberix avatar Aug 13 '22 15:08 Sayberix