private static (bool emptyOrWhitespaceTrivia, bool documentationComment, bool emptyLine) AnalyzeLeadingTrivia(SyntaxTriviaList leadingTrivia)
        {
            SyntaxTriviaList.Enumerator en = leadingTrivia.GetEnumerator();

            if (!en.MoveNext())
            {
                return(true, false, false);
            }

            if (en.Current.IsWhitespaceTrivia() &&
                !en.MoveNext())
            {
                return(true, false, false);
            }

            switch (en.Current.Kind())
            {
            case SyntaxKind.SingleLineDocumentationCommentTrivia:
            case SyntaxKind.MultiLineDocumentationCommentTrivia:
            {
                return(false, true, false);
            }

            case SyntaxKind.EndOfLineTrivia:
            {
                while (en.MoveNext())
                {
                    if (!en.Current.IsWhitespaceOrEndOfLineTrivia())
                    {
                        return(default);
                    }
                }
        private static void AnalyzeCompilationUnit(SyntaxNodeAnalysisContext context)
        {
            var compilationUnit = (CompilationUnitSyntax)context.Node;

            if (compilationUnit.Span.Length == 0)
            {
                return;
            }

            SyntaxToken token = compilationUnit.EndOfFileToken;

            if (token.FullSpan.Start > 0)
            {
                token = compilationUnit.GetFirstToken();

                SyntaxDebug.Assert(token.FullSpan.Start == 0, token);

                if (token.FullSpan.Start > 0)
                {
                    return;
                }
            }

            SyntaxTriviaList.Enumerator en = token.LeadingTrivia.GetEnumerator();

            if (en.MoveNext() &&
                en.Current.IsWhitespaceOrEndOfLineTrivia())
            {
                ReportDiagnostic(context, token);
            }
Example #3
0
        private static IEnumerable <SyntaxTrivia> GetTopSingleLineComments(SyntaxTriviaList triviaList)
        {
            SyntaxTriviaList.Enumerator en = triviaList.GetEnumerator();

            while (en.MoveNext())
            {
                if (en.Current.IsSingleLineCommentTrivia())
                {
                    SyntaxTrivia trivia = en.Current;

                    if (en.MoveNext() && en.Current.IsEndOfLineTrivia())
                    {
                        yield return(trivia);

                        yield return(en.Current);
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
        }
Example #4
0
 public override void Visit(SyntaxNode node)
 {
     try {
         bool flag = node != null;
         if (flag)
         {
             if (node.HasLeadingTrivia && !(node is CompilationUnitSyntax))
             {
                 SyntaxTriviaList.Enumerator enumerator = node.GetLeadingTrivia().GetEnumerator();
                 while (enumerator.MoveNext())
                 {
                     SyntaxTrivia current = enumerator.Current;
                     this.VisitTrivia(current, false);
                 }
             }
             try {
                 ((CSharpSyntaxNode)node).Accept(this);
             } catch (Exception ex) {
                 Log(node, "CSharpSyntaxNode.Accept throw exception:{0}\n{1}", ex.Message, ex.StackTrace);
             }
             m_LastComment = string.Empty;
             if (node.HasTrailingTrivia)
             {
                 SyntaxTriviaList.Enumerator enumerator = node.GetTrailingTrivia().GetEnumerator();
                 while (enumerator.MoveNext())
                 {
                     SyntaxTrivia current = enumerator.Current;
                     this.VisitTrivia(current, false);
                 }
             }
         }
     } catch (Exception ex) {
         Log(node, "Visit throw exception:{0}\n{1}", ex.Message, ex.StackTrace);
     }
 }
Example #5
0
        public override void Visit(SyntaxNode node)
        {
            bool flag = node != null;

            if (flag)
            {
                if (node.HasLeadingTrivia && !(node is CompilationUnitSyntax))
                {
                    SyntaxTriviaList.Enumerator enumerator = node.GetLeadingTrivia().GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        SyntaxTrivia current = enumerator.Current;
                        this.VisitTrivia(current, false);
                    }
                }
                ((CSharpSyntaxNode)node).Accept(this);
                m_LastComment = string.Empty;
                if (node.HasTrailingTrivia)
                {
                    SyntaxTriviaList.Enumerator enumerator = node.GetTrailingTrivia().GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        SyntaxTrivia current = enumerator.Current;
                        this.VisitTrivia(current, false);
                    }
                }
            }
        }
        private static bool IsStandardTriviaBetweenLines(SyntaxTriviaList trailingTrivia, SyntaxTriviaList leadingTrivia)
        {
            if (leadingTrivia.Any() &&
                leadingTrivia.All(f => f.IsWhitespaceTrivia()))
            {
                SyntaxTriviaList.Enumerator en = trailingTrivia.GetEnumerator();

                while (en.MoveNext())
                {
                    SyntaxKind kind = en.Current.Kind();

                    if (kind == SyntaxKind.WhitespaceTrivia)
                    {
                        continue;
                    }

                    if (kind == SyntaxKind.EndOfLineTrivia &&
                        !en.MoveNext())
                    {
                        return(true);
                    }

                    break;
                }
            }

            return(false);
        }
Example #7
0
            internal Enumerator(TriviaList triviaList)
            {
                _list1 = triviaList._list1;
                _list2 = triviaList._list2;

                _index      = -1;
                _enumerator = _list1.GetEnumerator();
            }
Example #8
0
            internal Enumerator(TriviaList triviaList)
            {
                this.list1 = triviaList.list1;
                this.list2 = triviaList.list2;

                this.index      = -1;
                this.enumerator = list1.GetEnumerator();
            }
Example #9
0
            public bool MoveNext()
            {
                index++;
                if (index == list1.Count)
                {
                    enumerator = list2.GetEnumerator();
                }

                return(enumerator.MoveNext());
            }
Example #10
0
            public bool MoveNext()
            {
                _index++;
                if (_index == _list1.Count)
                {
                    _enumerator = _list2.GetEnumerator();
                }

                return(_enumerator.MoveNext());
            }
Example #11
0
        public override void VisitEndBlockStatement(EndBlockStatementSyntax node)
        {
            if (Options.IgnoreBlockBoundary)
            {
                TextSpan span = node.Span;

                TextLine line = Lines.GetLineFromPosition(span.Start);

                if (line.IsEmptyOrWhiteSpace(TextSpan.FromBounds(line.Start, span.Start)))
                {
                    if (Options.IncludeComments)
                    {
                        if (line.IsEmptyOrWhiteSpace(TextSpan.FromBounds(span.End, line.End)))
                        {
                            BlockBoundaryLineCount += Lines.GetLineCount(span);
                        }
                    }
                    else if (AnalyzeTrailingTrivia(node.GetTrailingTrivia()))
                    {
                        BlockBoundaryLineCount += Lines.GetLineCount(span);
                    }
                }
            }

            base.VisitEndBlockStatement(node);

            bool AnalyzeTrailingTrivia(in SyntaxTriviaList trailingTrivia)
            {
                SyntaxTriviaList.Enumerator en = trailingTrivia.GetEnumerator();

                while (en.MoveNext())
                {
                    switch (en.Current.Kind())
                    {
                    case SyntaxKind.EndOfLineTrivia:
                    case SyntaxKind.CommentTrivia:
                    {
                        return(true);
                    }

                    case SyntaxKind.WhitespaceTrivia:
                    {
                        break;
                    }

                    default:
                    {
                        return(false);
                    }
                    }
                }

                return(false);
            }
        }
Example #12
0
        public void VisitTrailingTrivia(SyntaxToken token, bool onlyComment)
        {
            bool hasTrailingTrivia = token.HasTrailingTrivia;

            if (hasTrailingTrivia)
            {
                SyntaxTriviaList.Enumerator enumerator = token.TrailingTrivia.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    SyntaxTrivia current = enumerator.Current;
                    this.VisitTrivia(current, onlyComment);
                }
            }
        }
        private static void AnalyzeCompilationUnit(SyntaxNodeAnalysisContext context)
        {
            var compilationUnit = (CompilationUnitSyntax)context.Node;

            SyntaxNode node = compilationUnit.Externs.FirstOrDefault()
                              ?? compilationUnit.Usings.FirstOrDefault()
                              ?? (SyntaxNode)compilationUnit.AttributeLists.FirstOrDefault()
                              ?? compilationUnit.Members.FirstOrDefault();

            if (node == null)
            {
                return;
            }

            SyntaxTriviaList.Enumerator en = node.GetLeadingTrivia().GetEnumerator();

            if (!en.MoveNext())
            {
                return;
            }

            if (en.Current.SpanStart != 0)
            {
                return;
            }

            if (en.Current.IsWhitespaceTrivia() &&
                !en.MoveNext())
            {
                return;
            }

            if (!en.Current.IsKind(SyntaxKind.SingleLineCommentTrivia, SyntaxKind.MultiLineCommentTrivia))
            {
                return;
            }

            if (!en.MoveNext() ||
                !en.Current.IsEndOfLineTrivia() ||
                en.MoveNext())
            {
                return;
            }

            DiagnosticHelpers.ReportDiagnostic(
                context,
                DiagnosticDescriptors.AddEmptyLineAfterTopComment,
                Location.Create(compilationUnit.SyntaxTree, new TextSpan(node.GetLeadingTrivia().Last().SpanStart, 0)));
        }
Example #14
0
        private static void Analyze(
            SyntaxNodeAnalysisContext context,
            UsingDirectiveSyntax usingDirective,
            SyntaxToken nextToken)
        {
            SyntaxTriviaList trailingTrivia = usingDirective.GetTrailingTrivia();

            if (!SyntaxTriviaAnalysis.IsOptionalWhitespaceThenOptionalSingleLineCommentThenEndOfLineTrivia(triviaList: trailingTrivia))
            {
                return;
            }

            SyntaxTriviaList.Enumerator en = nextToken.LeadingTrivia.GetEnumerator();

            if (en.MoveNext())
            {
                if (en.Current.IsWhitespaceTrivia() &&
                    !en.MoveNext())
                {
                    ReportDiagnostic(trailingTrivia.Last().SpanStart);
                }
                else
                {
                    switch (en.Current.Kind())
                    {
                    case SyntaxKind.SingleLineCommentTrivia:
                    case SyntaxKind.SingleLineDocumentationCommentTrivia:
                    case SyntaxKind.MultiLineDocumentationCommentTrivia:
                    {
                        ReportDiagnostic(trailingTrivia.Last().SpanStart);
                        break;
                    }
                    }
                }
            }
            else
            {
                ReportDiagnostic(trailingTrivia.Last().SpanStart);
            }

            void ReportDiagnostic(int position)
            {
                DiagnosticHelpers.ReportDiagnostic(
                    context,
                    DiagnosticRules.AddBlankLineAfterUsingDirectiveList,
                    Location.Create(usingDirective.SyntaxTree, new TextSpan(position, 0)));
            }
        }
        private static void AnalyzeRegionDirectiveTrivia(SyntaxNodeAnalysisContext context)
        {
            var regionDirective = (RegionDirectiveTriviaSyntax)context.Node;

            if (IsFollowedWithEmptyLineOrEndRegionDirective())
            {
                return;
            }

            DiagnosticHelpers.ReportDiagnostic(
                context,
                DiagnosticDescriptors.AddEmptyLineAfterRegionDirective,
                Location.Create(regionDirective.SyntaxTree, regionDirective.EndOfDirectiveToken.Span));

            bool IsFollowedWithEmptyLineOrEndRegionDirective()
            {
                SyntaxTrivia parentTrivia = regionDirective.ParentTrivia;

                SyntaxTriviaList.Enumerator en = parentTrivia.Token.LeadingTrivia.GetEnumerator();

                while (en.MoveNext())
                {
                    if (en.Current == parentTrivia)
                    {
                        if (!en.MoveNext())
                        {
                            return(false);
                        }

                        if (en.Current.IsWhitespaceTrivia() &&
                            !en.MoveNext())
                        {
                            return(false);
                        }

                        if (en.Current.IsKind(SyntaxKind.EndRegionDirectiveTrivia))
                        {
                            return(true);
                        }

                        return(en.Current.IsEndOfLineTrivia());
                    }
                }

                return(false);
            }
        }
Example #16
0
            static bool VerifySpan(TextSpan span, SyntaxTriviaList leading)
            {
                SyntaxTriviaList.Enumerator en = leading.GetEnumerator();

                while (en.MoveNext())
                {
                    if (!en.Current.IsWhitespaceOrEndOfLineTrivia())
                    {
                        continue;
                    }

                    if (en.Current.Span.Contains(span.Start))
                    {
                        if (span.IsEmpty)
                        {
                            return(true);
                        }

                        if (en.Current.Span.End == span.End)
                        {
                            return(true);
                        }

                        span = span.TrimFromStart(en.Current.Span.Length);

                        while (en.MoveNext())
                        {
                            if (en.Current.Span.End == span.End)
                            {
                                return(true);
                            }

                            if (span.End < en.Current.Span.End)
                            {
                                break;
                            }

                            span = span.TrimFromStart(en.Current.Span.Length);
                        }

                        break;
                    }
                }

                return(false);
            }
        private static TextSpan?GetEmptyLineSpan(
            SyntaxTriviaList triviaList,
            bool isEnd)
        {
            SyntaxTriviaList.Enumerator en = triviaList.GetEnumerator();
            while (en.MoveNext())
            {
                switch (en.Current.Kind())
                {
                case SyntaxKind.EndOfLineTrivia:
                {
                    SyntaxTrivia endOfLine = en.Current;

                    if (isEnd)
                    {
                        while (en.MoveNext())
                        {
                            if (!en.Current.IsWhitespaceOrEndOfLineTrivia())
                            {
                                return(null);
                            }
                        }
                    }

                    return(TextSpan.FromBounds(triviaList.Span.Start, endOfLine.Span.End));
                }

                case SyntaxKind.WhitespaceTrivia:
                {
                    break;
                }

                default:
                {
                    return(null);
                }
                }
            }

            return(null);
        }
        private static bool IsOnEmptyLine(TextSpan span, SyntaxTriviaList leadingTrivia)
        {
            SyntaxTriviaList.Enumerator en = leadingTrivia.GetEnumerator();

            while (en.MoveNext())
            {
                if (en.Current.Span.Contains(span))
                {
                    if (en.Current.IsEndOfLineTrivia())
                    {
                        return(true);
                    }

                    return(en.Current.IsWhitespaceTrivia() &&
                           en.MoveNext() &&
                           en.Current.IsEndOfLineTrivia());
                }
            }

            return(false);
        }
Example #19
0
        public static Task <Document> RemoveEmptyLinesBeforeAsync(
            Document document,
            SyntaxToken token,
            CancellationToken cancellationToken = default)
        {
            SyntaxTriviaList leadingTrivia = token.LeadingTrivia;

            int count = 0;

            SyntaxTriviaList.Enumerator en = leadingTrivia.GetEnumerator();
            while (en.MoveNext())
            {
                if (en.Current.IsWhitespaceTrivia())
                {
                    if (!en.MoveNext())
                    {
                        break;
                    }

                    if (!en.Current.IsEndOfLineTrivia())
                    {
                        break;
                    }

                    count += 2;
                }
                else if (en.Current.IsEndOfLineTrivia())
                {
                    count++;
                }
                else
                {
                    break;
                }
            }

            SyntaxToken newToken = token.WithLeadingTrivia(leadingTrivia.RemoveRange(0, count));

            return(document.ReplaceTokenAsync(token, newToken, cancellationToken));
        }