添加继承时删除“:”之前的换行符

Removing newline before ':' when adding inheritance

使用这样的给定输入:

namespace Test
{
    using System;

    public class Test
    {
        public int? OBJECTID { get; set; }
    }
}

我想让这个 class 扩展其他 classes。所以我用这些规则写了我的重写器:

public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
{
    node =
        node.WithBaseList(
            SyntaxFactory.BaseList()
                .WithTypes(
                    SyntaxFactory.SingletonSeparatedList<BaseTypeSyntax>(
                        SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseName("Form"))
                            .WithLeadingTrivia(SyntaxFactory.Space) //Space before 'Form'
                            .WithTrailingTrivia(SyntaxFactory.LineFeed) // NewLine after 'Form'
                        )
                )
            );

    return base.VisitClassDeclaration(node);
}

但是我得到的输出是这样的:

namespace Test
{
    using System;

    public class Test
: Form
    {
        public int? OBJECTID { get; set; }
    }
}

我已经在许多不同的位置尝试了 WithoutTrailingTrivia()WithoutLeadingTrivia(),但我找不到真正的放置位置,删除 ":".[=16 之前的换行符=]

你能帮我解决这个问题吗?

我使用扩展工具中的语法可视化工具查看了示例中的语法树,插入符号就在 class 名称后面。这为我提供了以下语法树:

如您所见,EndOfLineTrivia 与 IdentifierToken 相关联。因此,您可以通过替换标识符来删除它(或替换它,如以下示例所示):

    public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
    {
        node = node.WithBaseList(
            SyntaxFactory.BaseList()
                .WithTypes(
                   SyntaxFactory.SingletonSeparatedList<BaseTypeSyntax>(
                       SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseName("Form"))
                           .WithLeadingTrivia(SyntaxFactory.Space) 
                           .WithTrailingTrivia(SyntaxFactory.LineFeed)
                    )
                )
            );
        node =
            node.WithIdentifier(
                node.Identifier.WithTrailingTrivia
                    (SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, " ")));
        return base.VisitClassDeclaration(node);
    }