dotnet/roslyn

"Pull Member Up" produces an incorrect level of indentation when Base class already has members

Aperta

#54.847 aperta il 15 lug 2021

 (0 commenti) (0 reazioni) (0 assegnatari)C# (4257 fork)batch import
Area-IDEBugIDE-Formatterhelp wanted

Metriche repository

Star
 (20.414 stelle)
Metriche merge PR
 (Merge medio 6g 17h) (256 PR mergiate in 30 g)

Descrizione

Steps to Reproduce: When we have a file such as this, where the derived class has a different indentation level than the base class (from namespaces or otherwise):

namespace TestNs1
{
    public class Base
    {
    }
}
namespace TestNs2
{
    namespace InnerNs
    {
        public class Derived : Base
        {
            public int TestMethod()
            {
                return 5;
            }
        }
    }
}

And we use Ctrl + . or the lightbulb to open Quick Actions on TestMethod() and Pull Member up to Base, we get the correct level of indentation for TestMethod(), i.e. one level more than Base. However, if we produce a file with at least one existing base member as follows:

namespace TestNs1
{
    using System;

    public class Base
    {
        public int Num { get; set; }
    }
}
namespace TestNs2
{
    namespace InnerNs
    {
        public class Derived : Base
        {
            public int TestMethod()
            {
                return 5;
            }
        }
    }
}

Then TestMethod(), when moved to Base will have the same indentation level as it did in Derived, which is not the correct indentation level for a member of Base. Expected Behavior:

namespace TestNs1
{
    using System;

    public class Base
    {
        public int Num { get; set; }
        public int TestMethod()
        {
            return 5;
        }
    }
}
namespace TestNs2
{
    namespace InnerNs
    {
        public class Derived : Base
        {
        }
    }
}

Actual Behavior:

namespace TestNs1
{
    using System;

    public class Base
    {
        public int Num { get; set; }
            public int TestMethod()
            {
                return 5;
            }
    }
}
namespace TestNs2
{
    namespace InnerNs
    {
        public class Derived : Base
        {
        }
    }
}

Note how TestMethod() is indented one more than Num.

I believe this is caused by the formatter annotation not being applied when the member list is larger than 1. In CSharpCodeGenerationHelpers, there is a method which only adds a formatter annotation if there is exactly one member. It is called near the very end of the member adding process. I suspect the formatter annotation would find the correct indentation, which is why moving a single member has the correct indentation, while multiple member moves or moves to types with existing members keeps indentation as before.

This may be an issue with other code generation services which add members or otherwise call this helper.

Guida contributor