public static IEnumerable <T> GetAncestorsOrThis <T>(this SyntaxNode node, bool allowStructuredTrivia = false)
            where T : SyntaxNode
        {
            SyntaxNode current = node;

            while (current != null)
            {
                if (current is T)
                {
                    yield return((T)current);
                }

                if (allowStructuredTrivia &&
                    current.IsStructuredTrivia &&
                    current.Parent == null)
                {
                    StructuredTriviaSyntax structuredTrivia = (StructuredTriviaSyntax)current;
                    SyntaxTrivia           parentTrivia     = structuredTrivia.ParentTrivia;
                    current = parentTrivia.Token.Parent;
                }
                else
                {
                    current = current.Parent;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>Adds a new line at the end of a nodes leading trivia.</summary>
        /// <param name="node">The node whose leading trivia the new line should be added.</param>
        /// <param name="triviaSyntax">The structured tricia syntax to add.</param>
        /// <returns>The provided <paramref name="node"/> with a leading trivia including
        /// the original leading trivia and the provided <paramref name="triviaSyntax"/>.</returns>
        private static SyntaxNode AddLineToLeadingTrivia(SyntaxNode node, StructuredTriviaSyntax triviaSyntax)
        {
            SyntaxTriviaList currentLeadingTrivia = node.GetLeadingTrivia();
            SyntaxTrivia     newContentTrivia     = SyntaxFactory.Trivia(triviaSyntax);
            SyntaxTrivia     endOfLineTrivia      = SyntaxFactory.EndOfLine(Environment.NewLine);
            SyntaxTriviaList newLeadingTrivia     = currentLeadingTrivia.Add(newContentTrivia).Add(endOfLineTrivia);

            return(node.WithLeadingTrivia(newLeadingTrivia));
        }
Exemplo n.º 3
0
 internal SyntaxTrivia(SyntaxTree syntaxTree, SyntaxKind kind, string text, TextSpan span, StructuredTriviaSyntax structure, IEnumerable <Diagnostic> diagnostics)
 {
     _syntaxTree = syntaxTree;
     Kind        = kind;
     Text        = text;
     Span        = span;
     Structure   = structure;
     Diagnostics = diagnostics.ToImmutableArray();
 }
Exemplo n.º 4
0
        /// <summary>
        /// Transforms a <see cref="SyntaxTrivia"/> to ensure no formatting operations will be applied to the trivia or
        /// any of its descendants when a <see cref="CodeAction"/> is applied.
        /// </summary>
        /// <param name="trivia">The syntax trivia.</param>
        /// <returns>
        /// A syntax trivia which is equivalent to the input <paramref name="trivia"/>, but which will not be subject to
        /// automatic code formatting operations when applied as part of a <see cref="CodeAction"/>.
        /// </returns>
        public static SyntaxTrivia WithoutFormatting(this SyntaxTrivia trivia)
        {
            // Strategy
            //  1. Replace the structure, if any, with a structure that will not be reformatted
            //  2. Remove formatting from the resulting trivia
            SyntaxTrivia result = trivia;

            if (trivia.HasStructure)
            {
                // GetStructure() returns SyntaxNode instead of StructuredTriviaSyntax. For C# code, this should always
                // be an actual instance of StructuredTriviaSyntax, but we handle the case where it is not by leaving
                // the structure node unaltered rather than throwing some sort of exception.
                StructuredTriviaSyntax structure = trivia.GetStructure() as StructuredTriviaSyntax;
                if (structure != null)
                {
                    result = SyntaxFactory.Trivia(structure.WithoutFormatting());
                }
            }

            return(WithoutFormattingImpl(trivia));
        }
Exemplo n.º 5
0
 public TameStructuredTriviaSyntax(StructuredTriviaSyntax node)
 {
     Node = node;
     AddChildren();
 }
Exemplo n.º 6
0
 internal SyntaxTrivia GetParentTrivia(StructuredTriviaSyntax structuredTrivia)
 {
     return(GetParent <SyntaxTrivia>(structuredTrivia));
 }