private static IEnumerable <StatementSyntax> GetNewNodes(StatementSyntax statement)
        {
            SyntaxTriviaList leadingTrivia = statement.Parent.GetLeadingTrivia();

            if (statement.IsKind(SyntaxKind.ElseClause))
            {
                leadingTrivia.Insert(0, SyntaxHelper.NewLine);
            }

            if (statement.IsKind(SyntaxKind.Block))
            {
                SyntaxList <StatementSyntax> .Enumerator en = ((BlockSyntax)statement).Statements.GetEnumerator();

                if (en.MoveNext())
                {
                    yield return(en.Current.WithLeadingTrivia(leadingTrivia));

                    while (en.MoveNext())
                    {
                        yield return(en.Current);
                    }
                }
            }
            else
            {
                yield return(statement.WithLeadingTrivia(leadingTrivia));
            }
        }
예제 #2
0
        private void FindSelectedNodes()
        {
            SyntaxList <TNode> .Enumerator en = UnderlyingList.GetEnumerator();

            if (en.MoveNext())
            {
                int i = 0;

                while (Span.Start >= en.Current.FullSpan.End &&
                       en.MoveNext())
                {
                    i++;
                }

                if (Span.Start >= en.Current.FullSpan.Start &&
                    Span.Start <= en.Current.Span.Start)
                {
                    int j = i;

                    while (Span.End > en.Current.FullSpan.End &&
                           en.MoveNext())
                    {
                        j++;
                    }

                    if (Span.End >= en.Current.Span.End &&
                        Span.End <= en.Current.FullSpan.End)
                    {
                        _firstIndex = i;
                        _lastIndex  = j;
                    }
                }
            }
        }
        private static void AnalyzeSwitchStatement(SyntaxNodeAnalysisContext context)
        {
            var switchStatement = (SwitchStatementSyntax)context.Node;

            SyntaxList <SwitchSectionSyntax> sections = switchStatement.Sections;

            SyntaxList <SwitchSectionSyntax> .Enumerator en = sections.GetEnumerator();

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

            SwitchSectionSyntax previousSection = en.Current;

            while (en.MoveNext())
            {
                SyntaxTriviaList leadingTrivia = en.Current.Labels[0].GetLeadingTrivia();

                if (SyntaxTriviaAnalysis.IsEmptyOrSingleWhitespaceTrivia(leadingTrivia))
                {
                    SyntaxTriviaList trailingTrivia = previousSection.GetTrailingTrivia();

                    if (SyntaxTriviaAnalysis.IsOptionalWhitespaceThenOptionalSingleLineCommentThenEndOfLineTrivia(trailingTrivia))
                    {
                        DiagnosticHelpers.ReportDiagnostic(
                            context,
                            DiagnosticDescriptors.AddEmptyLineBetweenSwitchSections,
                            Location.Create(switchStatement.SyntaxTree, trailingTrivia.Last().Span.WithLength(0)));
                    }
                }

                previousSection = en.Current;
            }
        }
예제 #4
0
        public static SelectionResult Create <TNode>(SyntaxList <TNode> list, TextSpan span) where TNode : SyntaxNode
        {
            SyntaxList <TNode> .Enumerator en = list.GetEnumerator();

            if (en.MoveNext())
            {
                int i = 0;

                while (span.Start >= en.Current.FullSpan.End &&
                       en.MoveNext())
                {
                    i++;
                }

                if (span.Start >= en.Current.FullSpan.Start &&
                    span.Start <= en.Current.SpanStart)
                {
                    int j = i;

                    while (span.End > en.Current.FullSpan.End &&
                           en.MoveNext())
                    {
                        j++;
                    }

                    if (span.End >= en.Current.Span.End &&
                        span.End <= en.Current.FullSpan.End)
                    {
                        return(new SelectionResult(i, j));
                    }
                }
            }

            return(Fail);
        }
예제 #5
0
        protected static IndexPair GetIndexes(SyntaxList <TNode> list, TextSpan span)
        {
            SyntaxList <TNode> .Enumerator en = list.GetEnumerator();

            if (en.MoveNext())
            {
                int i = 0;

                while (span.Start >= en.Current.FullSpan.End &&
                       en.MoveNext())
                {
                    i++;
                }

                if (span.Start >= en.Current.FullSpan.Start &&
                    span.Start <= en.Current.Span.Start)
                {
                    int j = i;

                    while (span.End > en.Current.FullSpan.End &&
                           en.MoveNext())
                    {
                        j++;
                    }

                    if (span.End >= en.Current.Span.End &&
                        span.End <= en.Current.FullSpan.End)
                    {
                        return(new IndexPair(i, j));
                    }
                }
            }

            return(new IndexPair(-1, -1));
        }
        public static IEnumerable <AttributeListSyntax> GetSelectedAttributeLists(SyntaxList <AttributeListSyntax> attributeLists, TextSpan span)
        {
            if (attributeLists.Count > 0)
            {
                SyntaxList <AttributeListSyntax> .Enumerator en = attributeLists.GetEnumerator();

                while (en.MoveNext())
                {
                    if (span.Start >= en.Current.FullSpan.Start)
                    {
                        if (span.Start <= en.Current.Span.Start)
                        {
                            do
                            {
                                if (span.End >= en.Current.Span.End)
                                {
                                    yield return(en.Current);
                                }
                                else
                                {
                                    yield break;
                                }
                            } while (en.MoveNext());
                        }
                        else if (span.Start <= en.Current.Span.End)
                        {
                            yield break;
                        }
                    }
                }
            }
        }
        private static IEnumerable <StatementSyntax> GetNewNodes(StatementSyntax statement)
        {
            List <SyntaxTrivia> list;

            if (statement.IsParentKind(SyntaxKind.ElseClause))
            {
                list = new List <SyntaxTrivia>()
                {
                    CSharpFactory.NewLine()
                };
            }
            else
            {
                list = statement.Parent.GetLeadingTrivia()
                       .Reverse()
                       .SkipWhile(f => f.IsWhitespaceTrivia())
                       .Reverse()
                       .ToList();
            }

            if (statement.IsKind(SyntaxKind.Block))
            {
                SyntaxList <StatementSyntax> .Enumerator en = ((BlockSyntax)statement).Statements.GetEnumerator();

                if (en.MoveNext())
                {
                    list.AddRange(en.Current.GetLeadingTrivia());

                    yield return(en.Current.WithLeadingTrivia(list));

                    while (en.MoveNext())
                    {
                        yield return(en.Current);
                    }
                }
            }
            else
            {
                list.AddRange(statement.GetLeadingTrivia());

                yield return(statement.WithLeadingTrivia(list));
            }
        }
예제 #8
0
        private SyntaxList <TNode> RemoveRange <TNode>(
            SyntaxList <TNode> list,
            int index,
            int count,
            Func <TNode, bool> predicate) where TNode : SyntaxNode
        {
            return(SyntaxFactory.List(RemoveRange()));

            IEnumerable <TNode> RemoveRange()
            {
                SyntaxList <TNode> .Enumerator en = list.GetEnumerator();

                int i = 0;

                while (i < index &&
                       en.MoveNext())
                {
                    yield return(en.Current);

                    i++;
                }

                int endIndex = index + count;

                while (i < endIndex &&
                       en.MoveNext())
                {
                    if (!predicate(en.Current))
                    {
                        yield return(en.Current);
                    }

                    i++;
                }

                while (en.MoveNext())
                {
                    yield return(en.Current);
                }
            }
        }
        private static void AnalyzeSwitchStatement(SyntaxNodeAnalysisContext context)
        {
            var switchStatement = (SwitchStatementSyntax)context.Node;

            SyntaxList <SwitchSectionSyntax> sections = switchStatement.Sections;

            SyntaxList <SwitchSectionSyntax> .Enumerator en = sections.GetEnumerator();

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

            SwitchSectionSyntax previousSection = en.Current;

            var previousBlock = previousSection.Statements.SingleOrDefault(shouldThrow: false) as BlockSyntax;

            while (en.MoveNext())
            {
                SyntaxTriviaList leadingTrivia = en.Current.Labels[0].GetLeadingTrivia();

                if (SyntaxTriviaAnalysis.IsEmptyOrSingleWhitespaceTrivia(leadingTrivia))
                {
                    SyntaxTriviaList trailingTrivia = previousSection.GetTrailingTrivia();

                    if (SyntaxTriviaAnalysis.IsOptionalWhitespaceThenOptionalSingleLineCommentThenEndOfLineTrivia(trailingTrivia) &&
                        (context.GetBlankLineBetweenClosingBraceAndSwitchSection() != false ||
                         previousBlock == null))
                    {
                        DiagnosticHelpers.ReportDiagnostic(
                            context,
                            DiagnosticRules.AddBlankLineBetweenSwitchSections,
                            Location.Create(switchStatement.SyntaxTree, trailingTrivia.Last().Span.WithLength(0)));
                    }
                }

                previousSection = en.Current;

                previousBlock = en.Current.Statements.SingleOrDefault(shouldThrow: false) as BlockSyntax;
            }
        }
        private static CompilationUnitSyntax AddUsingDirective(this CompilationUnitSyntax compilationUnit, string @namespace)
        {
            if (compilationUnit == null)
            {
                throw new ArgumentNullException(nameof(compilationUnit));
            }

            UsingDirectiveSyntax usingDirective = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(@namespace));

            int index = 0;

            SyntaxList <UsingDirectiveSyntax> .Enumerator en = compilationUnit.Usings.GetEnumerator();

            bool isSkippingSystem = true;

            while (en.MoveNext())
            {
                int result = string.Compare(
                    en.Current.Name.ToString(),
                    usingDirective.Name.ToString(),
                    StringComparison.Ordinal);

                if (result == 0)
                {
                    return(compilationUnit);
                }

                if (isSkippingSystem)
                {
                    if (en.Current.IsSystem())
                    {
                        index++;
                        continue;
                    }
                    else
                    {
                        isSkippingSystem = false;
                    }
                }

                if (result > 0)
                {
                    index++;
                }
            }

            SyntaxList <UsingDirectiveSyntax> usings = compilationUnit.Usings.Insert(index, usingDirective);

            return(compilationUnit.WithUsings(usings));
        }
        public static void AnalyzerSwitchSection(SyntaxNodeAnalysisContext context)
        {
            var switchSection = (SwitchSectionSyntax)context.Node;

            if (!(switchSection.Statements.SingleOrDefault(shouldThrow: false) is BlockSyntax block))
            {
                return;
            }

            SyntaxList <StatementSyntax> statements = block.Statements;

            SyntaxList <StatementSyntax> .Enumerator en = statements.GetEnumerator();

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

            do
            {
                if (en.Current.IsKind(SyntaxKind.LocalDeclarationStatement))
                {
                    var localDeclaration = (LocalDeclarationStatementSyntax)en.Current;

                    if (localDeclaration.UsingKeyword.IsKind(SyntaxKind.UsingKeyword))
                    {
                        return;
                    }
                }
            } while (en.MoveNext());

            SyntaxToken openBrace = block.OpenBraceToken;

            if (!AnalyzeTrivia(openBrace.LeadingTrivia))
            {
                return;
            }

            if (!AnalyzeTrivia(openBrace.TrailingTrivia))
            {
                return;
            }

            if (!AnalyzeTrivia(statements[0].GetLeadingTrivia()))
            {
                return;
            }

            if (!AnalyzeTrivia(statements.Last().GetTrailingTrivia()))
            {
                return;
            }

            SyntaxToken closeBrace = block.CloseBraceToken;

            if (!AnalyzeTrivia(closeBrace.LeadingTrivia))
            {
                return;
            }

            if (!AnalyzeTrivia(closeBrace.TrailingTrivia))
            {
                return;
            }

            DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.RemoveUnnecessaryBraces, openBrace);
            DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.RemoveUnnecessaryBracesFadeOut, closeBrace);