예제 #1
0
        // From a syntax node, create a list of trivia node that encapsulates the same text. We use SkippedTokens trivia
        // to encapsulate the tokens, plus extract trivia from those tokens into the trivia list because:
        //    - We want leading trivia and trailing trivia to be directly visible in the trivia list, not on the tokens
        //      inside the skipped tokens trivia.
        //    - We have to expose structured trivia directives.
        //
        // Several options controls how diagnostics are handled:
        //   "preserveDiagnostics" means existing diagnostics are preserved, otherwise they are thrown away
        //   "addDiagnostic", if not Nothing, is added as a diagnostics
        //   "addDiagnosticsToFirstTokenOnly" means that "addDiagnostics" is attached only to the first token, otherwise
        //    it is attached to all tokens.
        private static SyntaxList <SyntaxNode> CreateSkippedTrivia(SyntaxNode node, bool preserveDiagnostics, bool addDiagnosticToFirstTokenOnly, DiagnosticInfo addDiagnostic)
        {
            if (node.Kind == SyntaxKind.SkippedTokensTrivia)
            {
                // already skipped trivia
                if (addDiagnostic != null)
                {
                    ////node = node.AddError(addDiagnostic); TODO
                }

                return(node);
            }

            IList <DiagnosticInfo> diagnostics = new List <DiagnosticInfo>();
            var tokenListBuilder = SyntaxListBuilder <SyntaxToken> .Create();

            node.CollectConstituentTokensAndDiagnostics(tokenListBuilder, diagnostics);
            // Adjust diagnostics based on input.
            if (!preserveDiagnostics)
            {
                diagnostics.Clear();
            }

            if (addDiagnostic != null)
            {
                diagnostics.Add(addDiagnostic);
            }

            var skippedTriviaBuilder = new SkippedTriviaBuilder(preserveDiagnostics, addDiagnosticToFirstTokenOnly, diagnostics);

            // Get through each token and add it.
            for (int i = 0; i < tokenListBuilder.Count; i++)
            {
                SyntaxToken currentToken = tokenListBuilder[i];
                skippedTriviaBuilder.AddToken(currentToken, isFirst: (i == 0), isLast: (i == tokenListBuilder.Count - 1));
            }

            return(skippedTriviaBuilder.GetTriviaList());
        }