public SyntaxRemover(
     SyntaxNode[] nodesToRemove,
     SyntaxRemoveOptions options)
     : base(nodesToRemove.Any(n => n.IsPartOfStructuredTrivia()))
 {
     _nodesToRemove  = new HashSet <SyntaxNode>(nodesToRemove);
     _options        = options;
     _searchSpan     = ComputeTotalSpan(nodesToRemove);
     _residualTrivia = SyntaxTriviaListBuilder.Create();
 }
        public void TestStructuredTrivia()
        {
            var spaceTrivia = SyntaxTriviaListBuilder.Create().Add(SyntaxFactory.Whitespace(" ")).ToList();
            var emptyTrivia = SyntaxTriviaListBuilder.Create().ToList();

            var name            = "goo";
            var xmlStartElement = SyntaxFactory.XmlElementStartTag(
                SyntaxFactory.Token(spaceTrivia, SyntaxKind.LessThanToken, default(SyntaxTriviaList)),
                SyntaxFactory.XmlName(null,
                                      SyntaxFactory.Identifier(name)),
                default(SyntaxList <XmlAttributeSyntax>),
                SyntaxFactory.Token(default(SyntaxTriviaList), SyntaxKind.GreaterThanToken, spaceTrivia));

            var xmlEndElement = SyntaxFactory.XmlElementEndTag(
                SyntaxFactory.Token(SyntaxKind.LessThanSlashToken),
                SyntaxFactory.XmlName(null,
                                      SyntaxFactory.Identifier(name)),
                SyntaxFactory.Token(default(SyntaxTriviaList), SyntaxKind.GreaterThanToken, spaceTrivia));

            var xmlElement = SyntaxFactory.XmlElement(xmlStartElement, default(SyntaxList <XmlNodeSyntax>), xmlEndElement);

            Assert.Equal(" <goo> </goo> ", xmlElement.ToFullString());
            Assert.Equal("<goo> </goo>", xmlElement.ToString());

            var docComment = SyntaxFactory.DocumentationCommentTrivia(SyntaxKind.SingleLineDocumentationCommentTrivia).WithContent(new SyntaxList <XmlNodeSyntax>(xmlElement));

            Assert.Equal(" <goo> </goo> ", docComment.ToFullString());
            // Assert.Equal("<goo> </goo>", docComment.GetText());
            var child = (XmlElementSyntax)docComment.ChildNodesAndTokens()[0];

            Assert.Equal(" <goo> </goo> ", child.ToFullString());
            Assert.Equal("<goo> </goo>", child.ToString());
            Assert.Equal(" <goo> ", child.StartTag.ToFullString());
            Assert.Equal("<goo>", child.StartTag.ToString());

            var sTrivia = SyntaxFactory.Trivia(docComment);

            Assert.NotEqual(default(SyntaxTrivia), sTrivia);
            var ident = SyntaxFactory.Identifier(SyntaxTriviaList.Create(sTrivia), "banana", spaceTrivia);

            Assert.Equal(" <goo> </goo> banana ", ident.ToFullString());
            Assert.Equal("banana", ident.ToString());
            Assert.Equal(" <goo> </goo> ", ident.LeadingTrivia[0].ToFullString());
            // Assert.Equal("<goo> </goo>", ident.LeadingTrivia[0].GetText());

            var identExpr = SyntaxFactory.IdentifierName(ident);

            // make sure FindLeaf digs into the structured trivia.
            var result = identExpr.FindToken(3, true);

            Assert.Equal(SyntaxKind.IdentifierToken, result.Kind());
            Assert.Equal("goo", result.ToString());

            var trResult = identExpr.FindTrivia(6, SyntaxTrivia.Any);

            Assert.Equal(SyntaxKind.WhitespaceTrivia, trResult.Kind());
            Assert.Equal(" ", trResult.ToString());

            var foundDocComment = result.Parent.Parent.Parent.Parent;

            Assert.Null(foundDocComment.Parent);

            var identTrivia = identExpr.GetLeadingTrivia()[0];
            var foundTrivia = ((DocumentationCommentTriviaSyntax)foundDocComment).ParentTrivia;

            Assert.Equal(identTrivia, foundTrivia);

            // make sure FindLeafNodesOverlappingWithSpan does not dig into the structured trivia.
            var resultList = identExpr.DescendantTokens(t => t.FullSpan.OverlapsWith(new TextSpan(3, 18)));

            Assert.Equal(1, resultList.Count());
        }
Beispiel #3
0
 /// <summary>
 /// Creates a list of trivia.
 /// </summary>
 /// <param name="trivias">A sequence of trivia.</param>
 public SyntaxTriviaList(IEnumerable <SyntaxTrivia> trivias)
     : this(default, SyntaxTriviaListBuilder.Create(trivias).Node, 0, 0)
 {
 }