public override CSharpNewLineBeforeCatchStyle Update(NodeInformation <CatchClauseSyntax> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            var node = information.Node;

            if (!node.ContainsDiagnostics)
            {
                var parentStatement = node.FindParent <TryStatementSyntax>();
                var parentChildren  = parentStatement.ChildNodes().ToArray();
                var nodeIndex       = Array.IndexOf(parentChildren, node);
                var previousNode    = parentChildren[nodeIndex - 1];

                if (!(previousNode is BlockSyntax block))
                {
                    block = (BlockSyntax)((CatchClauseSyntax)previousNode).ChildNodes().Last();
                }

                var closeToken  = block.ChildTokens().Single(_ => _.RawKind == (int)SyntaxKind.CloseBraceToken);
                var containsEol = closeToken.HasTrailingTrivia &&
                                  closeToken.TrailingTrivia.Any(_ => _.Kind() == SyntaxKind.EndOfLineTrivia);
                return(new CSharpNewLineBeforeCatchStyle(this.Data.Update(containsEol), this.Severity));
            }

            return(new CSharpNewLineBeforeCatchStyle(this.Data, this.Severity));
        }
Example #2
0
        public override CSharpSpaceBetweenParenthesesStyle Update(NodeInformation <SyntaxNode> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            var node = information.Node;

            if (!node.ContainsDiagnostics)
            {
                if (node is ForStatementSyntax || node is ForEachStatementSyntax || node is IfStatementSyntax ||
                    node is SwitchStatementSyntax || node is WhileStatementSyntax)
                {
                    return(new CSharpSpaceBetweenParenthesesStyle(this.Data.UpdateControlFlow(node.HasParenthesisSpacing())));
                }
                else if (node is ParenthesizedExpressionSyntax)
                {
                    return(new CSharpSpaceBetweenParenthesesStyle(this.Data.UpdateExpression(node.HasParenthesisSpacing())));
                }
                else if (node is CastExpressionSyntax)
                {
                    return(new CSharpSpaceBetweenParenthesesStyle(this.Data.UpdateTypeCast(node.HasParenthesisSpacing())));
                }
            }

            return(new CSharpSpaceBetweenParenthesesStyle(this.Data));
        }
        public override CSharpPreferBracesStyle Update(NodeInformation <SyntaxNode> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            var node = information.Node;

            if (!node.ContainsDiagnostics)
            {
                if (node is IfStatementSyntax || node is ElseClauseSyntax ||
                    node is ForStatementSyntax || node is ForEachStatementSyntax ||
                    node is WhileStatementSyntax)
                {
                    if (node.ChildNodes().SingleOrDefault(_ => _.Kind() == SyntaxKind.Block) is BlockSyntax block)
                    {
                        return(block.ChildNodes().Count() == 1 ?
                               new CSharpPreferBracesStyle(this.Data.Update(true), this.Severity) :
                               new CSharpPreferBracesStyle(this.Data, this.Severity));
                    }
                    else
                    {
                        return(new CSharpPreferBracesStyle(this.Data.Update(false), this.Severity));
                    }
                }
            }

            return(new CSharpPreferBracesStyle(this.Data, this.Severity));
        }
Example #4
0
 public override CSharpStyleExpressionBodiedConstructorsStyle Update(NodeInformation <ConstructorDeclarationSyntax> information)
 {
     if (information is null)
     {
         throw new ArgumentNullException(nameof(information));
     }
     return(new CSharpStyleExpressionBodiedConstructorsStyle(information.Node.Examine(this.Data), this.Severity));
 }
        public override DotnetSortSystemDirectivesFirstStyle Update(NodeInformation <CompilationUnitSyntax> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            var node = information.Node;

            if (!node.ContainsDiagnostics)
            {
                var usingNodes = node.DescendantNodes().Where(_ => _.IsKind(SyntaxKind.UsingDirective))
                                 .Select(_ => (UsingDirectiveSyntax)_).ToImmutableArray();

                if (usingNodes.Length > 1)
                {
                    var foundBreakIndex = false;

                    for (var i = 0; i < usingNodes.Length; i++)
                    {
                        var usingNode = usingNodes[i];

                        if (!usingNode.Name.ToString().StartsWith("System"))
                        {
                            foundBreakIndex = true;
                        }
                        else if (foundBreakIndex)
                        {
                            return(new DotnetSortSystemDirectivesFirstStyle(this.Data.Update(false), this.Severity));
                        }
                    }

                    var systemUsingNodes = usingNodes.Where(_ => _.Name.ToString().StartsWith("System")).ToImmutableArray();

                    if (systemUsingNodes.Length > 1)
                    {
                        for (var i = 1; i < systemUsingNodes.Length; i++)
                        {
                            if (systemUsingNodes[i - 1].Name.ToString().CompareTo(
                                    systemUsingNodes[i].Name.ToString()) > 0)
                            {
                                return(new DotnetSortSystemDirectivesFirstStyle(this.Data.Update(false), this.Severity));
                            }
                        }

                        return(new DotnetSortSystemDirectivesFirstStyle(this.Data.Update(true), this.Severity));
                    }
                    else if (systemUsingNodes.Length == 1)
                    {
                        return(new DotnetSortSystemDirectivesFirstStyle(this.Data.Update(true), this.Severity));
                    }
                }
            }

            return(new DotnetSortSystemDirectivesFirstStyle(this.Data, this.Severity));
        }
        public override CSharpSpaceAfterCastStyle Update(NodeInformation <CastExpressionSyntax> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            var node = information.Node;

            if (!node.ContainsDiagnostics)
            {
                var closeParen = node.ChildTokens().Single(_ => _.RawKind == (int)SyntaxKind.CloseParenToken);
                return(new CSharpSpaceAfterCastStyle(this.Data.Update(closeParen.HasTrailingTrivia), this.Severity));
            }

            return(new CSharpSpaceAfterCastStyle(this.Data, this.Severity));
        }
Example #7
0
        public override CSharpStyleVarElsewhereStyle Update(NodeInformation <LocalDeclarationStatementSyntax> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            var node = information.Node;

            if (!node.ContainsDiagnostics)
            {
                var variableDeclaration = node.ChildNodes()
                                          .Single(_ => _.Kind() == SyntaxKind.VariableDeclaration);
                var identifierName = variableDeclaration.ChildNodes()
                                     .SingleOrDefault(_ => _.Kind() == SyntaxKind.IdentifierName);

                return(identifierName is { } ?
Example #8
0
        public override CSharpNewLineBetweenQueryExpressionClausesStyle Update(NodeInformation <QueryExpressionSyntax> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            var node = information.Node;

            if (!node.ContainsDiagnostics)
            {
                var clauseCount = node.DescendantNodes().Count(_ => _ is QueryClauseSyntax || _ is SelectOrGroupClauseSyntax);
                var eolCount    = node.DescendantTrivia().Count(_ => _.IsKind(SyntaxKind.EndOfLineTrivia));
                return(new CSharpNewLineBetweenQueryExpressionClausesStyle(this.Data.Update(eolCount >= clauseCount - 1), this.Severity));
            }

            return(new CSharpNewLineBetweenQueryExpressionClausesStyle(this.Data, this.Severity));
        }
Example #9
0
        public override CSharpPreferredModifierOrderStyle Update(NodeInformation <MemberDeclarationSyntax> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            var node = information.Node;

            if (!node.ContainsDiagnostics)
            {
                return(new CSharpPreferredModifierOrderStyle(
                           this.Data.Update(node.ChildTokens()
                                            .Where(_ => ((SyntaxKind)_.RawKind).ToString().Contains("Keyword"))
                                            .Select(_ => _.ValueText).ToImmutableList())));
            }

            return(new CSharpPreferredModifierOrderStyle(this.Data));
        }
        public override DotnetStyleRequireAccessibilityModifiersStyle Update(NodeInformation <MemberDeclarationSyntax> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            var node = information.Node;

            if (!node.ContainsDiagnostics)
            {
                var modifiers = node.ChildTokens().Where(
                    _ => DotnetStyleRequireAccessibilityModifiersStyle.AccessibilityModifiers.Contains(_.Text)).ToList();
                var(occurence, isFromPublicInterfaceMember) = DotnetStyleRequireAccessibilityModifiersStyle.GetOccurence(node, modifiers);
                return(new DotnetStyleRequireAccessibilityModifiersStyle(this.Data.Update(occurence, isFromPublicInterfaceMember), this.Severity));
            }

            return(new DotnetStyleRequireAccessibilityModifiersStyle(this.Data, this.Severity));
        }
Example #11
0
        public override IndentStyleStyle Update(NodeInformation <SyntaxNode> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            var node = information.Node;

            var leadingTrivia = node.GetLeadingTrivia().Where(_ => _.IsKind(SyntaxKind.WhitespaceTrivia)).ToArray();

            if (leadingTrivia.Length == 1)
            {
                var content = leadingTrivia[0].ToFullString();
                return(new IndentStyleStyle(this.Data.Update(content.Contains("\t"))));
            }

            return(new IndentStyleStyle(this.Data));
        }
        public override CSharpStyleExpressionBodiedPropertiesStyle Update(NodeInformation <PropertyDeclarationSyntax> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            if (information.Node.DescendantNodes()
                .SingleOrDefault(_ => _.Kind() == SyntaxKind.GetAccessorDeclaration) is AccessorDeclarationSyntax getAccessor)
            {
                var accessorStyle = new CSharpStyleExpressionBodiedAccessorsStyle(this.Data, this.Severity);

                return(new CSharpStyleExpressionBodiedPropertiesStyle(accessorStyle.Update(
                                                                          new NodeInformation <AccessorDeclarationSyntax>(getAccessor)).Data, this.Severity));
            }
            else
            {
                return(new CSharpStyleExpressionBodiedPropertiesStyle(information.Node.Examine(this.Data), this.Severity));
            }
        }
        public override CSharpPreserveSingleLineBlocksStyle Update(NodeInformation <AccessorListSyntax> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            var node = information.Node;

            if (!node.ContainsDiagnostics)
            {
                var openBrace = node.ChildTokens().First(_ => _.IsKind(SyntaxKind.OpenBraceToken));

                return(new CSharpPreserveSingleLineBlocksStyle(
                           this.Data.Update(!(openBrace.HasTrailingTrivia &&
                                              openBrace.TrailingTrivia.Any(_ => _.IsKind(SyntaxKind.EndOfLineTrivia)))), this.Severity));
            }

            return(new CSharpPreserveSingleLineBlocksStyle(this.Data, this.Severity));
        }
Example #14
0
        public override CSharpStyleVarWhenTypeIsApparentStyle Update(NodeInformation <LocalDeclarationStatementSyntax> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            var node = information.Node;

            if (!node.ContainsDiagnostics)
            {
                if (node.DescendantNodes()
                    .Any(_ => _.Kind() == SyntaxKind.ObjectCreationExpression))
                {
                    var variableDeclaration = node.ChildNodes()
                                              .Single(_ => _.Kind() == SyntaxKind.VariableDeclaration);
                    var identifierName = variableDeclaration.ChildNodes()
                                         .SingleOrDefault(_ => _.Kind() == SyntaxKind.IdentifierName);

                    return(identifierName is { } ?
Example #15
0
        public override CSharpStyleInlinedVariableDeclarationStyle Update(NodeInformation <ArgumentSyntax> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            var node = information.Node;

            if (!node.ContainsDiagnostics)
            {
                var isOut = node.DescendantTokens().Any(_ => _.RawKind == (int)SyntaxKind.OutKeyword);

                if (isOut)
                {
                    return(new CSharpStyleInlinedVariableDeclarationStyle(this.Data.Update(
                                                                              node.DescendantNodes().Any(_ => _.Kind() == SyntaxKind.DeclarationExpression)), this.Severity));
                }
            }

            return(new CSharpStyleInlinedVariableDeclarationStyle(this.Data, this.Severity));
        }
        public override CSharpNewLineBeforeMembersInObjectInitializersStyle Update(NodeInformation <InitializerExpressionSyntax> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            var node = information.Node;

            if (!node.ContainsDiagnostics)
            {
                var commas = node.ChildTokens().Where(_ => _.Kind() == SyntaxKind.CommaToken).ToArray();

                if (commas.Length > 0)
                {
                    return(new CSharpNewLineBeforeMembersInObjectInitializersStyle(
                               this.Data.Update(commas.Any(_ => _.HasTrailingTrivia && _.TrailingTrivia.Any(t => t.Kind() == SyntaxKind.EndOfLineTrivia))),
                               this.Severity));
                }
            }

            return(new CSharpNewLineBeforeMembersInObjectInitializersStyle(this.Data, this.Severity));
        }
Example #17
0
        public override DotnetStylePreferInferredTupleNamesStyle Update(NodeInformation <TupleExpressionSyntax> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            var node = information.Node;

            if (!node.ContainsDiagnostics)
            {
                var data = this.Data;

                foreach (var argument in node.ChildNodes().OfType <ArgumentSyntax>())
                {
                    data = data.Update(!argument.ChildNodes().Any(_ => _.Kind() == SyntaxKind.NameColon));
                }

                return(new DotnetStylePreferInferredTupleNamesStyle(data, this.Severity));
            }

            return(new DotnetStylePreferInferredTupleNamesStyle(this.Data, this.Severity));
        }
        public override CSharpPreferSimpleDefaultExpressionStyle Update(NodeInformation <ExpressionSyntax> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            var node = information.Node;

            if (!node.ContainsDiagnostics)
            {
                if (node is LiteralExpressionSyntax)
                {
                    return(new CSharpPreferSimpleDefaultExpressionStyle(this.Data.Update(true), this.Severity));
                }
                else if (node is DefaultExpressionSyntax)
                {
                    return(new CSharpPreferSimpleDefaultExpressionStyle(this.Data.Update(false), this.Severity));
                }
            }

            return(new CSharpPreferSimpleDefaultExpressionStyle(this.Data, this.Severity));
        }
        public override CSharpNewLineBeforeElseStyle Update(NodeInformation <ElseClauseSyntax> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            var node = information.Node;

            if (!node.ContainsDiagnostics)
            {
                var parentStatement = node.FindParent <IfStatementSyntax>();
                var parentChildren  = parentStatement.ChildNodes().ToArray();
                var nodeIndex       = Array.IndexOf(parentChildren, node);
                var previousNode    = parentChildren[nodeIndex - 1];

                var lastToken   = previousNode.ChildTokens().Last();
                var containsEol = lastToken.HasTrailingTrivia &&
                                  lastToken.TrailingTrivia.Any(_ => _.Kind() == SyntaxKind.EndOfLineTrivia);
                return(new CSharpNewLineBeforeElseStyle(this.Data.Update(containsEol), this.Severity));
            }

            return(new CSharpNewLineBeforeElseStyle(this.Data, this.Severity));
        }
        public override CSharpSpaceBetweenMethodDeclarationParameterListParenthesesStyle Update(NodeInformation <ParameterListSyntax> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            var node = information.Node;

            if (!node.ContainsDiagnostics)
            {
                return(new CSharpSpaceBetweenMethodDeclarationParameterListParenthesesStyle(
                           this.Data.Update(node.HasParenthesisSpacing())));
            }

            return(new CSharpSpaceBetweenMethodDeclarationParameterListParenthesesStyle(this.Data));
        }
Example #21
0
        public override DotnetStylePreferInferredAnonymousTypeMemberNamesStyle Update(NodeInformation <AnonymousObjectMemberDeclaratorSyntax> information)
        {
            if (information is null)
            {
                throw new ArgumentNullException(nameof(information));
            }

            var node = information.Node;

            if (!node.ContainsDiagnostics)
            {
                return(new DotnetStylePreferInferredAnonymousTypeMemberNamesStyle(
                           this.Data.Update(!node.ChildNodes().Any(_ => _.Kind() == SyntaxKind.NameEquals)), this.Severity));
            }

            return(new DotnetStylePreferInferredAnonymousTypeMemberNamesStyle(this.Data, this.Severity));
        }