Beispiel #1
0
        public static Doc Print(ArrayRankSpecifierSyntax node)
        {
            if (node.Sizes.All(o => o is OmittedArraySizeExpressionSyntax))
            {
                return(Doc.Concat(
                           Token.Print(node.OpenBracketToken),
                           SeparatedSyntaxList.Print(node.Sizes, Node.Print, Doc.Null),
                           Token.Print(node.CloseBracketToken)
                           ));
            }

            return(Doc.Group(
                       Token.Print(node.OpenBracketToken),
                       node.Sizes.Any()
                    ? Doc.Concat(
                           Doc.Indent(
                               Doc.SoftLine,
                               SeparatedSyntaxList.Print(node.Sizes, Node.Print, Doc.Null)
                               ),
                           Doc.SoftLine
                           )
                    : Doc.Null,
                       Token.Print(node.CloseBracketToken)
                       ));
        }
Beispiel #2
0
 public static Doc Print(SwitchExpressionSyntax node)
 {
     return(Doc.Concat(
                Node.Print(node.GoverningExpression),
                " ",
                Token.Print(node.SwitchKeyword),
                Doc.HardLine,
                Token.Print(node.OpenBraceToken),
                Doc.Group(
                    Doc.Indent(
                        Doc.HardLine,
                        SeparatedSyntaxList.Print(
                            node.Arms,
                            o =>
                            Doc.Concat(
                                Node.Print(o.Pattern),
                                " ",
                                o.WhenClause != null
                                 ? Doc.Concat(Node.Print(o.WhenClause), " ")
                                 : Doc.Null,
                                Token.PrintWithSuffix(o.EqualsGreaterThanToken, " "),
                                Node.Print(o.Expression)
                                ),
                            Doc.HardLine
                            )
                        ),
                    Doc.HardLine
                    ),
                Token.Print(node.CloseBraceToken)
                ));
 }
Beispiel #3
0
        public static Doc Print(VariableDeclarationSyntax node)
        {
            if (node.Variables.Count > 1)
            {
                var docs = Doc.Concat(
                    SeparatedSyntaxList.Print(
                        node.Variables,
                        VariableDeclarator.Print,
                        node.Parent is ForStatementSyntax ? Doc.Line : Doc.HardLine
                        )
                    );

                return(Doc.Concat(Node.Print(node.Type), " ", Doc.Indent(docs)));
            }

            var variable    = node.Variables[0];
            var initializer = variable.Initializer;

            return(Doc.Concat(
                       Doc.Group(
                           Node.Print(node.Type),
                           " ",
                           Token.Print(variable.Identifier),
                           variable.ArgumentList != null
                        ? BracketedArgumentList.Print(variable.ArgumentList)
                        : Doc.Null,
                           initializer != null
                        ? Doc.Concat(" ", Token.Print(initializer.EqualsToken))
                        : Doc.Null
                           ),
                       initializer != null ? RightHandSide.Print(initializer.Value) : Doc.Null
                       ));
        }
Beispiel #4
0
 public static Doc Print(FunctionPointerTypeSyntax node)
 {
     return(Doc.Concat(
                Token.Print(node.DelegateKeyword),
                Token.Print(node.AsteriskToken, " "),
                node.CallingConvention != null
             ? PrintCallingConvention(node.CallingConvention)
             : Doc.Null,
                Token.Print(node.ParameterList.LessThanToken),
                Doc.Indent(
                    Doc.Group(
                        Doc.SoftLine,
                        SeparatedSyntaxList.Print(
                            node.ParameterList.Parameters,
                            o =>
                            Doc.Concat(
                                AttributeLists.Print(o, o.AttributeLists),
                                Modifiers.Print(o.Modifiers),
                                Node.Print(o.Type)
                                ),
                            Doc.Line
                            )
                        )
                    ),
                Token.Print(node.ParameterList.GreaterThanToken)
                ));
 }
 public static Doc Print(TypeArgumentListSyntax node)
 {
     return(Doc.Concat(
                Token.Print(node.LessThanToken),
                Doc.Indent(SeparatedSyntaxList.Print(node.Arguments, Node.Print, Doc.Line)),
                Token.Print(node.GreaterThanToken)
                ));
 }
Beispiel #6
0
 public static Doc Print(BracketedParameterListSyntax node)
 {
     return(Doc.Concat(
                Token.Print(node.OpenBracketToken),
                SeparatedSyntaxList.Print(node.Parameters, Parameter.Print, " "),
                Token.Print(node.CloseBracketToken)
                ));
 }
Beispiel #7
0
 public static Doc Print(TupleTypeSyntax node)
 {
     return(Doc.Concat(
                Token.Print(node.OpenParenToken),
                SeparatedSyntaxList.Print(node.Elements, Node.Print, " "),
                Token.Print(node.CloseParenToken)
                ));
 }
        public static Doc Print(ForStatementSyntax node)
        {
            var groupId = Guid.NewGuid().ToString();

            var innerGroup = new List <Doc> {
                Doc.SoftLine
            };

            if (node.Declaration != null)
            {
                innerGroup.Add(VariableDeclaration.Print(node.Declaration));
            }
            innerGroup.Add(SeparatedSyntaxList.Print(node.Initializers, Node.Print, " "));
            innerGroup.Add(Token.Print(node.FirstSemicolonToken));
            if (node.Condition != null)
            {
                innerGroup.Add(Doc.Line, Node.Print(node.Condition));
            }
            else
            {
                innerGroup.Add(Doc.SoftLine);
            }

            innerGroup.Add(Token.Print(node.SecondSemicolonToken));
            if (node.Incrementors.Any())
            {
                innerGroup.Add(Doc.Line);
            }
            else
            {
                innerGroup.Add(Doc.SoftLine);
            }
            innerGroup.Add(
                Doc.Indent(SeparatedSyntaxList.Print(node.Incrementors, Node.Print, Doc.Line))
                );

            var docs = new List <Doc>
            {
                ExtraNewLines.Print(node),
                Token.PrintLeadingTrivia(node.ForKeyword),
                Doc.Group(
                    Doc.Group(
                        Token.PrintWithoutLeadingTrivia(node.ForKeyword),
                        " ",
                        Token.Print(node.OpenParenToken),
                        Doc.GroupWithId(groupId, Doc.Indent(innerGroup), Doc.SoftLine),
                        Token.Print(node.CloseParenToken),
                        Doc.IfBreak(Doc.Null, Doc.SoftLine)
                        ),
                    node.Statement
                    is BlockSyntax blockSyntax
                        ? Block.PrintWithConditionalSpace(blockSyntax, groupId)
                        : Doc.Indent(Doc.Line, Node.Print(node.Statement))
                    )
            };

            return(Doc.Concat(docs));
        }
Beispiel #9
0
 public static Doc Print(TypeParameterConstraintClauseSyntax node)
 {
     return(Doc.Group(
                Token.PrintWithSuffix(node.WhereKeyword, " "),
                Node.Print(node.Name),
                " ",
                Token.PrintWithSuffix(node.ColonToken, " "),
                Doc.Indent(SeparatedSyntaxList.Print(node.Constraints, Node.Print, Doc.Line))
                ));
 }
Beispiel #10
0
 public static Doc Print(BaseListSyntax node)
 {
     return(Doc.Group(
                Doc.Indent(
                    Doc.Line,
                    Token.PrintWithSuffix(node.ColonToken, " "),
                    Doc.Indent(SeparatedSyntaxList.Print(node.Types, Node.Print, Doc.Line))
                    )
                ));
 }
 public static Doc Print(ArrayRankSpecifierSyntax node)
 {
     return(Doc.Concat(
                Token.Print(node.OpenBracketToken),
                node.Sizes.Any()
             ? SeparatedSyntaxList.Print(node.Sizes, Node.Print, Doc.Null)
             : Doc.Null,
                Token.Print(node.CloseBracketToken)
                ));
 }
 public static Doc Print(ParenthesizedVariableDesignationSyntax node)
 {
     return(Doc.Group(
                Token.Print(node.OpenParenToken),
                Doc.Indent(
                    Doc.SoftLine,
                    SeparatedSyntaxList.Print(node.Variables, Node.Print, Doc.Line),
                    Doc.SoftLine
                    ),
                Token.Print(node.CloseParenToken)
                ));
 }
 public static Doc Print(BracketedArgumentListSyntax node)
 {
     return(Doc.Group(
                Token.Print(node.OpenBracketToken),
                Doc.Indent(
                    Doc.SoftLine,
                    SeparatedSyntaxList.Print(node.Arguments, Node.Print, Doc.Line)
                    ),
                Doc.SoftLine,
                Token.Print(node.CloseBracketToken)
                ));
 }
Beispiel #14
0
 public static Doc Print(BaseListSyntax node)
 {
     return(Doc.Group(
                Doc.Indent(
                    Doc.Line,
                    Token.Print(node.ColonToken),
                    " ",
                    Doc.Align(
                        2,
                        Doc.Concat(SeparatedSyntaxList.Print(node.Types, Node.Print, Doc.Line))
                        )
                    )
                ));
 }
 public static Doc Print(OrderByClauseSyntax node)
 {
     return(Doc.Concat(
                Token.Print(node.OrderByKeyword, " "),
                SeparatedSyntaxList.Print(
                    node.Orderings,
                    orderingNode =>
                    Doc.Concat(
                        Node.Print(orderingNode.Expression),
                        " ",
                        Token.Print(orderingNode.AscendingOrDescendingKeyword)
                        ),
                    Doc.Null
                    )
                ));
 }
 public static Doc Print(InitializerExpressionSyntax node)
 {
     var result = Doc.Concat(
         node.Kind()
             is (SyntaxKind.ArrayInitializerExpression
                 or SyntaxKind.ComplexElementInitializerExpression) ? Doc.Null : Doc.Line,
         Token.Print(node.OpenBraceToken),
         Doc.Indent(
             Doc.Line,
             SeparatedSyntaxList.Print(node.Expressions, Node.Print, Doc.Line)
         ),
         Doc.Line,
         Token.Print(node.CloseBraceToken)
     );
     return node.Parent is not ObjectCreationExpressionSyntax ? Doc.Group(result) : result;
 }
Beispiel #17
0
        public static Doc Print(VariableDeclarationSyntax node)
        {
            var docs = Doc.Concat(
                SeparatedSyntaxList.Print(
                    node.Variables,
                    VariableDeclarator.Print,
                    node.Parent is ForStatementSyntax ? Doc.Line : Doc.HardLine
                    )
                );

            return(Doc.Concat(
                       Node.Print(node.Type),
                       " ",
                       node.Variables.Count > 1 ? Doc.Indent(docs) : docs
                       ));
        }
Beispiel #18
0
 public static Doc Print(TypeParameterListSyntax node)
 {
     if (node.Parameters.Count == 0)
     {
         return(Doc.Null);
     }
     return(Doc.Group(
                Token.Print(node.LessThanToken),
                Doc.Indent(
                    Doc.SoftLine,
                    SeparatedSyntaxList.Print(node.Parameters, TypeParameter.Print, Doc.Line)
                    ),
                Doc.SoftLine,
                Token.Print(node.GreaterThanToken)
                ));
 }
 public static Doc Print(AnonymousObjectCreationExpressionSyntax node)
 {
     return(Doc.Group(
                Token.PrintWithSuffix(node.NewKeyword, Doc.Line),
                Token.Print(node.OpenBraceToken),
                Doc.Indent(
                    Doc.Line,
                    SeparatedSyntaxList.Print(
                        node.Initializers,
                        AnonymousObjectMemberDeclarator.Print,
                        Doc.Line
                        )
                    ),
                Doc.Line,
                Token.Print(node.CloseBraceToken)
                ));
 }
Beispiel #20
0
 private static Doc PrintCallingConvention(FunctionPointerCallingConventionSyntax node)
 {
     return(Doc.Concat(
                Token.Print(node.ManagedOrUnmanagedKeyword),
                node.UnmanagedCallingConventionList != null
             ? Doc.Concat(
                    Token.Print(node.UnmanagedCallingConventionList.OpenBracketToken),
                    SeparatedSyntaxList.Print(
                        node.UnmanagedCallingConventionList.CallingConventions,
                        o => Token.Print(o.Name),
                        " "
                        ),
                    Token.Print(node.UnmanagedCallingConventionList.CloseBracketToken)
                    )
             : Doc.Null
                ));
 }
 public static Doc Print(RecursivePatternSyntax node)
 {
     return(Doc.Concat(
                node.Type != null ? Doc.Concat(Node.Print(node.Type), " ") : Doc.Null,
                node.PositionalPatternClause != null
             ? Doc.Concat(
                    Token.Print(node.PositionalPatternClause.OpenParenToken),
                    SeparatedSyntaxList.Print(
                        node.PositionalPatternClause.Subpatterns,
                        subpatternNode =>
                        Doc.Concat(
                            subpatternNode.NameColon != null
                                     ? NameColon.Print(subpatternNode.NameColon)
                                     : string.Empty,
                            Node.Print(subpatternNode.Pattern)
                            ),
                        " "
                        ),
                    Token.Print(node.PositionalPatternClause.CloseParenToken)
                    )
             : string.Empty,
                node.PropertyPatternClause != null
             ? Doc.Concat(
                    Token.PrintWithSuffix(node.PropertyPatternClause.OpenBraceToken, " "),
                    SeparatedSyntaxList.Print(
                        node.PropertyPatternClause.Subpatterns,
                        subpatternNode =>
                        Doc.Concat(
                            subpatternNode.NameColon != null
                                     ? NameColon.Print(subpatternNode.NameColon)
                                     : Doc.Null,
                            Node.Print(subpatternNode.Pattern)
                            ),
                        " "
                        ),
                    node.PropertyPatternClause.Subpatterns.Any() ? " " : Doc.Null,
                    Token.Print(node.PropertyPatternClause.CloseBraceToken)
                    )
             : string.Empty,
                node.Designation != null
             ? Doc.Concat(" ", Node.Print(node.Designation))
             : string.Empty
                ));
 }
        private static Doc Print(InitializerExpressionSyntax node, string?groupId)
        {
            var result = Doc.Concat(
                groupId != null ? Doc.IfBreak(" ", Doc.Line, groupId) : Doc.Null,
                Token.Print(node.OpenBraceToken),
                Doc.Indent(
                    Doc.Line,
                    SeparatedSyntaxList.Print(node.Expressions, Node.Print, Doc.Line)
                    ),
                Doc.Line,
                Token.Print(node.CloseBraceToken)
                );

            return(node.Parent
                   is not(ObjectCreationExpressionSyntax
                          or ArrayCreationExpressionSyntax
                          or ImplicitArrayCreationExpressionSyntax) &&
                   node.Kind()
                   is not SyntaxKind.WithInitializerExpression?Doc.Group(result) : result);
        }
Beispiel #23
0
 public static Doc Print(ParameterListSyntax node, string?groupId = null)
 {
     return(Doc.GroupWithId(
                groupId ?? string.Empty,
                Token.Print(node.OpenParenToken),
                node.Parameters.Count > 0
             ? Doc.Concat(
                    Doc.Indent(
                        Doc.SoftLine,
                        SeparatedSyntaxList.Print(
                            node.Parameters,
                            Parameter.Print,
                            Doc.Line
                            )
                        ),
                    Doc.SoftLine
                    )
             : Doc.Null,
                Token.Print(node.CloseParenToken)
                ));
 }
Beispiel #24
0
        public static Doc Print(AttributeListSyntax node)
        {
            var docs = new List <Doc>();

            if (node.Parent is CompilationUnitSyntax)
            {
                docs.Add(ExtraNewLines.Print(node));
            }

            docs.Add(Token.Print(node.OpenBracketToken));
            if (node.Target != null)
            {
                docs.Add(
                    Token.Print(node.Target.Identifier),
                    Token.PrintWithSuffix(node.Target.ColonToken, " ")
                    );
            }

            var printSeparatedSyntaxList = SeparatedSyntaxList.Print(
                node.Attributes,
                attributeNode =>
            {
                var name = Node.Print(attributeNode.Name);
                if (attributeNode.ArgumentList == null)
                {
                    return(name);
                }

                return(Doc.Group(
                           name,
                           Token.Print(attributeNode.ArgumentList.OpenParenToken),
                           Doc.Indent(
                               Doc.SoftLine,
                               SeparatedSyntaxList.Print(
                                   attributeNode.ArgumentList.Arguments,
                                   attributeArgumentNode =>
                                   Doc.Concat(
                                       attributeArgumentNode.NameEquals != null
                                            ? NameEquals.Print(attributeArgumentNode.NameEquals)
                                            : Doc.Null,
                                       attributeArgumentNode.NameColon != null
                                            ? NameColon.Print(attributeArgumentNode.NameColon)
                                            : Doc.Null,
                                       Node.Print(attributeArgumentNode.Expression)
                                       ),
                                   Doc.Line
                                   )
                               ),
                           Doc.SoftLine,
                           Token.Print(attributeNode.ArgumentList.CloseParenToken)
                           ));
            },
                Doc.Line
                );

            docs.Add(
                node.Attributes.Count > 1
                    ? Doc.Indent(Doc.SoftLine, printSeparatedSyntaxList)
                    : printSeparatedSyntaxList
                );

            docs.Add(
                node.Attributes.Count > 1 ? Doc.SoftLine : Doc.Null,
                Token.Print(node.CloseBracketToken)
                );

            return(Doc.Group(docs));
        }
Beispiel #25
0
        public static Doc Print(BaseTypeDeclarationSyntax node)
        {
            ParameterListSyntax?    parameterList     = null;
            TypeParameterListSyntax?typeParameterList = null;
            var         constraintClauses             = Enumerable.Empty <TypeParameterConstraintClauseSyntax>();
            var         hasMembers     = false;
            SyntaxToken?keyword        = null;
            Doc         members        = Doc.Null;
            SyntaxToken?semicolonToken = null;
            string?     groupId        = null;

            if (node is TypeDeclarationSyntax typeDeclarationSyntax)
            {
                typeParameterList = typeDeclarationSyntax.TypeParameterList;
                constraintClauses = typeDeclarationSyntax.ConstraintClauses;
                hasMembers        = typeDeclarationSyntax.Members.Count > 0;
                if (typeDeclarationSyntax.Members.Count > 0)
                {
                    members = Doc.Indent(
                        Doc.HardLine,
                        Doc.Join(Doc.HardLine, typeDeclarationSyntax.Members.Select(Node.Print))
                        );
                }
                if (node is ClassDeclarationSyntax classDeclarationSyntax)
                {
                    keyword = classDeclarationSyntax.Keyword;
                }
                else if (node is StructDeclarationSyntax structDeclarationSyntax)
                {
                    keyword = structDeclarationSyntax.Keyword;
                }
                else if (node is InterfaceDeclarationSyntax interfaceDeclarationSyntax)
                {
                    keyword = interfaceDeclarationSyntax.Keyword;
                }
                else if (node is RecordDeclarationSyntax recordDeclarationSyntax)
                {
                    keyword       = recordDeclarationSyntax.Keyword;
                    groupId       = Guid.NewGuid().ToString();
                    parameterList = recordDeclarationSyntax.ParameterList;
                }

                semicolonToken = typeDeclarationSyntax.SemicolonToken;
            }
            else if (node is EnumDeclarationSyntax enumDeclarationSyntax)
            {
                members = Doc.Indent(
                    Doc.HardLine,
                    SeparatedSyntaxList.Print(
                        enumDeclarationSyntax.Members,
                        EnumMemberDeclaration.Print,
                        Doc.HardLine
                        )
                    );
                hasMembers     = enumDeclarationSyntax.Members.Count > 0;
                keyword        = enumDeclarationSyntax.EnumKeyword;
                semicolonToken = enumDeclarationSyntax.SemicolonToken;
            }

            var docs = new List <Doc>
            {
                ExtraNewLines.Print(node),
                AttributeLists.Print(node, node.AttributeLists),
                Modifiers.Print(node.Modifiers)
            };

            if (keyword != null)
            {
                docs.Add(Token.PrintWithSuffix(keyword.Value, " "));
            }

            docs.Add(Token.Print(node.Identifier));

            if (typeParameterList != null)
            {
                docs.Add(TypeParameterList.Print(typeParameterList));
            }

            if (parameterList != null)
            {
                docs.Add(ParameterList.Print(parameterList, groupId));
            }

            if (node.BaseList != null)
            {
                docs.Add(BaseList.Print(node.BaseList));
            }

            docs.Add(ConstraintClauses.Print(constraintClauses));

            if (hasMembers)
            {
                DocUtilities.RemoveInitialDoubleHardLine(members);

                var useSpaceBraceWithParameterList = groupId != null && parameterList != null;
                docs.Add(
                    useSpaceBraceWithParameterList
                        ? Doc.IfBreak(" ", Doc.Line, groupId)
                        : Doc.HardLine,
                    Token.Print(node.OpenBraceToken),
                    members,
                    Doc.HardLine,
                    Token.Print(node.CloseBraceToken)
                    );
            }
            else if (node.OpenBraceToken.Kind() != SyntaxKind.None)
            {
                Doc separator = node.CloseBraceToken.LeadingTrivia.Any() ? Doc.Line : " ";

                docs.Add(
                    separator,
                    Token.Print(node.OpenBraceToken),
                    separator,
                    Token.Print(node.CloseBraceToken)
                    );
            }

            if (semicolonToken.HasValue)
            {
                docs.Add(Token.Print(semicolonToken.Value));
            }

            return(Doc.Concat(docs));
        }