private static SyntaxNode RemoveDeclaration(SyntaxNode root, BaseListSyntax baseList,
                                                    int redundantIndex)
        {
            var newBaseList = baseList
                              .RemoveNode(baseList.Types[redundantIndex], SyntaxRemoveOptions.KeepNoTrivia)
                              .WithAdditionalAnnotations(Formatter.Annotation);

            if (newBaseList.Types.Count != 0)
            {
                return(root.ReplaceNode(baseList, newBaseList));
            }

            var baseTypeHadLineEnding = HasLineEnding(baseList.Types[redundantIndex]);
            var colonHadLineEnding    = HasLineEnding(baseList.ColonToken);
            var typeNameHadLineEnding = HasLineEnding(((BaseTypeDeclarationSyntax)baseList.Parent).Identifier);

            var annotation = new SyntaxAnnotation();
            var newRoot    = root.ReplaceNode(
                baseList.Parent,
                baseList.Parent.WithAdditionalAnnotations(annotation));
            var declaration = (BaseTypeDeclarationSyntax)newRoot.GetAnnotatedNodes(annotation).First();

            newRoot     = newRoot.RemoveNode(declaration.BaseList, SyntaxRemoveOptions.KeepNoTrivia);
            declaration = (BaseTypeDeclarationSyntax)newRoot.GetAnnotatedNodes(annotation).First();

            var needsNewLine = !typeNameHadLineEnding &&
                               (colonHadLineEnding || baseTypeHadLineEnding);

            if (needsNewLine)
            {
                var trivia = SyntaxFactory.TriviaList();
                if (declaration.Identifier.HasTrailingTrivia)
                {
                    trivia = declaration.Identifier.TrailingTrivia;
                }

                trivia = colonHadLineEnding
                    ? trivia.Add(baseList.ColonToken.TrailingTrivia.Last())
                    : trivia.AddRange(baseList.Types[redundantIndex].GetTrailingTrivia());

                newRoot = newRoot.ReplaceToken(
                    declaration.Identifier,
                    declaration.Identifier
                    .WithTrailingTrivia(trivia));
            }

            declaration = (BaseTypeDeclarationSyntax)newRoot.GetAnnotatedNodes(annotation).First();
            return(newRoot.ReplaceNode(
                       declaration,
                       declaration.WithoutAnnotations(annotation)));
        }
        private static SyntaxNode RemoveDeclaration(SyntaxNode root, BaseListSyntax baseList,
            int redundantIndex)
        {
            var newBaseList = baseList
                .RemoveNode(baseList.Types[redundantIndex], SyntaxRemoveOptions.KeepNoTrivia)
                .WithAdditionalAnnotations(Formatter.Annotation);

            if (newBaseList.Types.Count != 0)
            {
                return root.ReplaceNode(baseList, newBaseList);
            }

            var baseTypeHadLineEnding = HasLineEnding(baseList.Types[redundantIndex]);
            var colonHadLineEnding = HasLineEnding(baseList.ColonToken);
            var typeNameHadLineEnding = HasLineEnding(((BaseTypeDeclarationSyntax)baseList.Parent).Identifier);

            var annotation = new SyntaxAnnotation();
            var newRoot = root.ReplaceNode(
                baseList.Parent,
                baseList.Parent.WithAdditionalAnnotations(annotation));
            var declaration = (BaseTypeDeclarationSyntax)newRoot.GetAnnotatedNodes(annotation).First();

            newRoot = newRoot.RemoveNode(declaration.BaseList, SyntaxRemoveOptions.KeepNoTrivia);
            declaration = (BaseTypeDeclarationSyntax)newRoot.GetAnnotatedNodes(annotation).First();

            var needsNewLine = !typeNameHadLineEnding &&
                (colonHadLineEnding || baseTypeHadLineEnding);

            if (needsNewLine)
            {
                var trivia = SyntaxFactory.TriviaList();
                if (declaration.Identifier.HasTrailingTrivia)
                {
                    trivia = declaration.Identifier.TrailingTrivia;
                }

                trivia = colonHadLineEnding
                    ? trivia.Add(baseList.ColonToken.TrailingTrivia.Last())
                    : trivia.AddRange(baseList.Types[redundantIndex].GetTrailingTrivia());

                newRoot = newRoot.ReplaceToken(
                        declaration.Identifier,
                        declaration.Identifier
                            .WithTrailingTrivia(trivia));
            }

            declaration = (BaseTypeDeclarationSyntax)newRoot.GetAnnotatedNodes(annotation).First();
            return newRoot.ReplaceNode(
                declaration,
                declaration.WithoutAnnotations(annotation));
        }
Beispiel #3
0
            public override SyntaxNode VisitBaseList(BaseListSyntax node)
            {
                ChildSyntaxList.Enumerator enumerator = node.ChildNodesAndTokens().GetEnumerator();
                while (enumerator.MoveNext())
                {
                    if (enumerator.Current is { IsNode : true } current)
                    {
                        SyntaxNode currentNode = current.AsNode();
                        if (currentNode is SimpleBaseTypeSyntax nodeToRemove && (nodeToRemove.Type.ToString() == "global::System.Object" || nodeToRemove.Type.ToString() == "System.Object"))
                        {
                            node = node.RemoveNode(nodeToRemove, SyntaxRemoveOptions.KeepNoTrivia);
                            break;
                        }
                    }
                }

                return(node.ChildNodes().Any() ? base.VisitBaseList(node) : null);
            }