Esempio n. 1
0
        public static Doc Print(WhileStatementSyntax node)
        {
            var groupId = Guid.NewGuid().ToString();
            var result  = Doc.Concat(
                ExtraNewLines.Print(node),
                Token.PrintLeadingTrivia(node.WhileKeyword),
                Doc.Group(
                    Doc.Group(
                        Token.PrintWithoutLeadingTrivia(node.WhileKeyword),
                        " ",
                        Token.Print(node.OpenParenToken),
                        Doc.GroupWithId(
                            groupId,
                            Doc.Indent(Doc.SoftLine, Node.Print(node.Condition)),
                            Doc.SoftLine
                            ),
                        Token.Print(node.CloseParenToken),
                        Doc.IfBreak(Doc.Null, Doc.SoftLine)
                        ),
                    OptionalBraces.Print(node.Statement, groupId)
                    )
                );

            return(result);
        }
        public static Doc Print(SwitchStatementSyntax node)
        {
            Doc sections = node.Sections.Count == 0
                ? " "
                : Doc.Concat(
                Doc.Indent(
                    Doc.Concat(
                        Doc.HardLine,
                        Doc.Join(Doc.HardLine, node.Sections.Select(Node.Print))
                        )
                    ),
                Doc.HardLine
                );

            return(Doc.Concat(
                       ExtraNewLines.Print(node),
                       Doc.Group(
                           Token.Print(node.SwitchKeyword, " "),
                           Token.Print(node.OpenParenToken),
                           Node.Print(node.Expression),
                           Token.Print(node.CloseParenToken),
                           Doc.Line,
                           Token.Print(node.OpenBraceToken),
                           sections,
                           Token.Print(node.CloseBraceToken)
                           )
                       ));
        }
Esempio n. 3
0
        public static Doc Print(FixedStatementSyntax node)
        {
            var groupId = Guid.NewGuid().ToString();

            return(Doc.Concat(
                       ExtraNewLines.Print(node),
                       Token.PrintLeadingTrivia(node.FixedKeyword),
                       Doc.Group(
                           Token.PrintWithoutLeadingTrivia(node.FixedKeyword),
                           " ",
                           Token.Print(node.OpenParenToken),
                           Doc.GroupWithId(
                               groupId,
                               Doc.Indent(Doc.SoftLine, Node.Print(node.Declaration)),
                               Doc.SoftLine
                               ),
                           Token.Print(node.CloseParenToken),
                           Doc.IfBreak(Doc.Null, Doc.SoftLine)
                           ),
                       node.Statement
                       is BlockSyntax blockSyntax
                    ? Block.PrintWithConditionalSpace(blockSyntax, groupId)
                    : Node.Print(node.Statement)
                       ));
        }
Esempio n. 4
0
 public static Doc Print(ExpressionStatementSyntax node)
 {
     return(Doc.Group(
                ExtraNewLines.Print(node),
                Node.Print(node.Expression),
                Token.Print(node.SemicolonToken)
                ));
 }
Esempio n. 5
0
        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));
        }
 public static Doc Print(GlobalStatementSyntax node)
 {
     return(Doc.Concat(
                ExtraNewLines.Print(node),
                AttributeLists.Print(node, node.AttributeLists),
                Modifiers.Print(node.Modifiers),
                Node.Print(node.Statement)
                ));
 }
Esempio n. 7
0
 public static Doc Print(ExternAliasDirectiveSyntax node)
 {
     return(Doc.Concat(
                ExtraNewLines.Print(node),
                Token.PrintWithSuffix(node.ExternKeyword, " "),
                Token.PrintWithSuffix(node.AliasKeyword, " "),
                Token.Print(node.Identifier),
                Token.Print(node.SemicolonToken)
                ));
 }
Esempio n. 8
0
 public static Doc Print(UsingDirectiveSyntax node)
 {
     return(Doc.Concat(
                ExtraNewLines.Print(node),
                Token.PrintWithSuffix(node.UsingKeyword, " "),
                Token.PrintWithSuffix(node.StaticKeyword, " "),
                node.Alias == null ? Doc.Null : NameEquals.Print(node.Alias),
                Node.Print(node.Name),
                Token.Print(node.SemicolonToken)
                ));
 }
Esempio n. 9
0
 public static Doc Print(LocalDeclarationStatementSyntax node)
 {
     return(Doc.Concat(
                ExtraNewLines.Print(node),
                Token.Print(node.AwaitKeyword, " "),
                Token.Print(node.UsingKeyword, " "),
                Modifiers.Print(node.Modifiers),
                VariableDeclaration.Print(node.Declaration),
                Token.Print(node.SemicolonToken)
                ));
 }
Esempio n. 10
0
 public static Doc Print(ReturnStatementSyntax node)
 {
     return(Doc.Group(
                ExtraNewLines.Print(node),
                Token.PrintWithSuffix(node.ReturnKeyword, node.Expression != null ? " " : Doc.Null),
                node.Expression != null
             ? node.Expression is BinaryExpressionSyntax
                     ? Doc.Indent(Node.Print(node.Expression))
                     : Node.Print(node.Expression)
             : Doc.Null,
                Token.Print(node.SemicolonToken)
                ));
 }
Esempio n. 11
0
        public static Doc Print(ThrowStatementSyntax node)
        {
            Doc expression = node.Expression != null
                ? Doc.Concat(" ", Node.Print(node.Expression))
                : string.Empty;

            return(Doc.Concat(
                       ExtraNewLines.Print(node),
                       Token.Print(node.ThrowKeyword),
                       expression,
                       Token.Print(node.SemicolonToken)
                       ));
        }
Esempio n. 12
0
        public static Doc Print(UsingStatementSyntax node)
        {
            var groupId = Guid.NewGuid().ToString();

            var leadingTrivia = node.AwaitKeyword.Kind() != SyntaxKind.None
                ? Token.PrintLeadingTrivia(node.AwaitKeyword)
                : Token.PrintLeadingTrivia(node.UsingKeyword);

            var docs = new List <Doc>
            {
                ExtraNewLines.Print(node),
                leadingTrivia,
                Doc.Group(
                    Token.PrintWithoutLeadingTrivia(node.AwaitKeyword),
                    node.AwaitKeyword.Kind() != SyntaxKind.None ? " " : Doc.Null,
                    node.AwaitKeyword.Kind() == SyntaxKind.None
                        ? Token.PrintWithoutLeadingTrivia(node.UsingKeyword)
                        : Token.Print(node.UsingKeyword),
                    " ",
                    Token.Print(node.OpenParenToken),
                    Doc.GroupWithId(
                        groupId,
                        Doc.Indent(
                            Doc.SoftLine,
                            node.Declaration != null
                                ? VariableDeclaration.Print(node.Declaration)
                                : Doc.Null,
                            node.Expression != null ? Node.Print(node.Expression) : Doc.Null
                            ),
                        Doc.SoftLine
                        ),
                    Token.Print(node.CloseParenToken),
                    Doc.IfBreak(Doc.Null, Doc.SoftLine)
                    )
            };

            if (node.Statement is UsingStatementSyntax)
            {
                docs.Add(Doc.HardLine, Node.Print(node.Statement));
            }
            else if (node.Statement is BlockSyntax blockSyntax)
            {
                docs.Add(Block.PrintWithConditionalSpace(blockSyntax, groupId));
            }
            else
            {
                docs.Add(Doc.Indent(Doc.HardLine, Node.Print(node.Statement)));
            }

            return(Doc.Concat(docs));
        }
Esempio n. 13
0
 public static Doc Print(DoStatementSyntax node)
 {
     return(Doc.Concat(
                ExtraNewLines.Print(node),
                Token.Print(node.DoKeyword, node.Statement is not BlockSyntax ? " " : Doc.Null),
                Node.Print(node.Statement),
                Doc.HardLine,
                Token.Print(node.WhileKeyword, " "),
                Token.Print(node.OpenParenToken),
                Node.Print(node.Condition),
                Token.Print(node.CloseParenToken),
                Token.Print(node.SemicolonToken)
                ));
 }
Esempio n. 14
0
        public static Doc Print(YieldStatementSyntax node)
        {
            Doc expression = node.Expression != null
                ? Doc.Concat(" ", Node.Print(node.Expression))
                : string.Empty;

            return(Doc.Concat(
                       ExtraNewLines.Print(node),
                       Token.PrintWithSuffix(node.YieldKeyword, " "),
                       Token.Print(node.ReturnOrBreakKeyword),
                       expression,
                       Token.Print(node.SemicolonToken)
                       ));
        }
Esempio n. 15
0
 public static Doc Print(ForEachVariableStatementSyntax node)
 {
     return(Doc.Concat(
                ExtraNewLines.Print(node),
                Token.Print(node.AwaitKeyword, " "),
                Token.Print(node.ForEachKeyword, " "),
                Token.Print(node.OpenParenToken),
                Node.Print(node.Variable),
                " ",
                Token.Print(node.InKeyword, " "),
                Node.Print(node.Expression),
                Token.Print(node.CloseParenToken),
                Node.Print(node.Statement)
                ));
 }
Esempio n. 16
0
        public static Doc Print(GotoStatementSyntax node)
        {
            Doc expression = node.Expression != null
                ? Doc.Concat(" ", Node.Print(node.Expression))
                : string.Empty;

            return(Doc.Concat(
                       ExtraNewLines.Print(node),
                       Token.Print(node.GotoKeyword),
                       node.CaseOrDefaultKeyword.RawKind != 0 ? " " : Doc.Null,
                       Token.Print(node.CaseOrDefaultKeyword),
                       expression,
                       Token.Print(node.SemicolonToken)
                       ));
        }
Esempio n. 17
0
        public static Doc Print(ForEachStatementSyntax node)
        {
            var groupId = Guid.NewGuid().ToString();

            var leadingTrivia = node.AwaitKeyword.Kind() != SyntaxKind.None
                ? Token.PrintLeadingTrivia(node.AwaitKeyword)
                : Token.PrintLeadingTrivia(node.ForEachKeyword);

            var docs = Doc.Concat(
                ExtraNewLines.Print(node),
                leadingTrivia,
                Doc.Group(
                    Doc.Group(
                        Token.PrintWithoutLeadingTrivia(node.AwaitKeyword),
                        node.AwaitKeyword.Kind()
                        is not SyntaxKind.None ? " " : Doc.Null,
                        node.AwaitKeyword.Kind() is SyntaxKind.None
                            ? Token.PrintWithoutLeadingTrivia(node.ForEachKeyword)
                            : Token.Print(node.ForEachKeyword),
                        " ",
                        Token.Print(node.OpenParenToken),
                        Doc.GroupWithId(
                            groupId,
                            Doc.Indent(
                                Doc.SoftLine,
                                Node.Print(node.Type),
                                " ",
                                Token.Print(node.Identifier),
                                " ",
                                Token.Print(node.InKeyword),
                                " ",
                                Node.Print(node.Expression)
                                ),
                            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(docs);
        }
Esempio n. 18
0
        public static Doc Print(BaseFieldDeclarationSyntax node)
        {
            var docs = new List <Doc>
            {
                ExtraNewLines.Print(node),
                AttributeLists.Print(node, node.AttributeLists),
                Modifiers.Print(node.Modifiers)
            };

            if (node is EventFieldDeclarationSyntax eventFieldDeclarationSyntax)
            {
                docs.Add(Token.PrintWithSuffix(eventFieldDeclarationSyntax.EventKeyword, " "));
            }

            docs.Add(Node.Print(node.Declaration), Token.Print(node.SemicolonToken));
            return(Doc.Concat(docs));
        }
Esempio n. 19
0
        public static Doc Print(TryStatementSyntax node)
        {
            var docs = new List <Doc>
            {
                ExtraNewLines.Print(node),
                AttributeLists.Print(node, node.AttributeLists),
                Token.Print(node.TryKeyword),
                Block.Print(node.Block),
                Doc.HardLine,
                Doc.Join(Doc.HardLine, node.Catches.Select(CatchClause.Print))
            };

            if (node.Finally != null)
            {
                docs.Add(Doc.HardLine, FinallyClause.Print(node.Finally));
            }
            return(Doc.Concat(docs));
        }
Esempio n. 20
0
        public static Doc Print(LabeledStatementSyntax node)
        {
            var docs = new List <Doc>
            {
                ExtraNewLines.Print(node),
                AttributeLists.Print(node, node.AttributeLists),
                Token.Print(node.Identifier),
                Token.Print(node.ColonToken)
            };

            if (node.Statement is BlockSyntax blockSyntax)
            {
                docs.Add(Block.Print(blockSyntax));
            }
            else
            {
                docs.Add(Doc.HardLine, Node.Print(node.Statement));
            }
            return(Doc.Concat(docs));
        }
Esempio n. 21
0
        public static Doc Print(IfStatementSyntax node)
        {
            var docs = new List <Doc>();

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

            var groupId = Guid.NewGuid().ToString();

            docs.Add(
                Doc.Group(
                    Token.PrintLeadingTrivia(node.IfKeyword),
                    Doc.Group(
                        Token.PrintWithoutLeadingTrivia(node.IfKeyword),
                        " ",
                        Token.Print(node.OpenParenToken),
                        Doc.GroupWithId(
                            groupId,
                            Doc.Indent(Doc.SoftLine, Node.Print(node.Condition)),
                            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))
                    )
                );

            if (node.Else != null)
            {
                docs.Add(Doc.HardLine, Node.Print(node.Else));
            }

            return(Doc.Concat(docs));
        }
Esempio n. 22
0
        public static Doc Print(DelegateDeclarationSyntax node)
        {
            var docs = new List <Doc>
            {
                ExtraNewLines.Print(node),
                AttributeLists.Print(node, node.AttributeLists),
                Modifiers.Print(node.Modifiers),
                Token.PrintWithSuffix(node.DelegateKeyword, " "),
                Node.Print(node.ReturnType),
                { " ", Token.Print(node.Identifier) }
            };

            if (node.TypeParameterList != null)
            {
                docs.Add(Node.Print(node.TypeParameterList));
            }
            docs.Add(
                Node.Print(node.ParameterList),
                ConstraintClauses.Print(node.ConstraintClauses),
                Token.Print(node.SemicolonToken)
                );
            return(Doc.Concat(docs));
        }
Esempio n. 23
0
        public static Doc Print(NamespaceDeclarationSyntax node)
        {
            var docs = new List <Doc>
            {
                ExtraNewLines.Print(node),
                AttributeLists.Print(node, node.AttributeLists),
                Modifiers.Print(node.Modifiers),
                Token.Print(node.NamespaceKeyword),
                " ",
                Node.Print(node.Name)
            };

            var innerDocs  = new List <Doc>();
            var hasMembers = node.Members.Count > 0;
            var hasUsing   = node.Usings.Count > 0;
            var hasExterns = node.Externs.Count > 0;

            if (hasMembers || hasUsing || hasExterns)
            {
                innerDocs.Add(Doc.HardLine);
                if (hasExterns)
                {
                    innerDocs.Add(
                        Doc.Join(Doc.HardLine, node.Externs.Select(ExternAliasDirective.Print)),
                        Doc.HardLine
                        );
                }
                if (hasUsing)
                {
                    innerDocs.Add(
                        Doc.Join(Doc.HardLine, node.Usings.Select(UsingDirective.Print)),
                        Doc.HardLine
                        );
                }
                if (hasMembers)
                {
                    innerDocs.Add(
                        Doc.Join(Doc.HardLine, node.Members.Select(Node.Print)),
                        Doc.HardLine
                        );
                }

                innerDocs.RemoveAt(innerDocs.Count - 1);
            }
            else
            {
                innerDocs.Add(" ");
            }

            DocUtilities.RemoveInitialDoubleHardLine(innerDocs);

            docs.Add(
                Doc.Group(
                    Doc.Line,
                    Token.Print(node.OpenBraceToken),
                    Doc.Indent(innerDocs),
                    hasMembers || hasUsing || hasExterns ? Doc.HardLine : Doc.Null,
                    Token.Print(node.CloseBraceToken),
                    Token.Print(node.SemicolonToken)
                    )
                );
            return(Doc.Concat(docs));
        }
        public static Doc Print(CSharpSyntaxNode node)
        {
            SyntaxList <AttributeListSyntax>?attributeLists = null;
            SyntaxTokenList?modifiers  = null;
            TypeSyntax?     returnType = null;
            ExplicitInterfaceSpecifierSyntax?explicitInterfaceSpecifier = null;
            TypeParameterListSyntax?         typeParameterList          = null;
            Doc identifier        = Doc.Null;
            var constraintClauses = Enumerable.Empty <TypeParameterConstraintClauseSyntax>();
            ParameterListSyntax?         parameterList          = null;
            ConstructorInitializerSyntax?constructorInitializer = null;
            BlockSyntax?body = null;
            ArrowExpressionClauseSyntax?expressionBody = null;
            SyntaxToken?semicolonToken = null;
            string?     groupId        = null;

            if (node is BaseMethodDeclarationSyntax baseMethodDeclarationSyntax)
            {
                attributeLists = baseMethodDeclarationSyntax.AttributeLists;
                modifiers      = baseMethodDeclarationSyntax.Modifiers;
                parameterList  = baseMethodDeclarationSyntax.ParameterList;
                body           = baseMethodDeclarationSyntax.Body;
                expressionBody = baseMethodDeclarationSyntax.ExpressionBody;
                if (node is MethodDeclarationSyntax methodDeclarationSyntax)
                {
                    returnType = methodDeclarationSyntax.ReturnType;
                    explicitInterfaceSpecifier = methodDeclarationSyntax.ExplicitInterfaceSpecifier;
                    identifier        = Token.Print(methodDeclarationSyntax.Identifier);
                    typeParameterList = methodDeclarationSyntax.TypeParameterList;
                    constraintClauses = methodDeclarationSyntax.ConstraintClauses;
                }
                else if (node is DestructorDeclarationSyntax destructorDeclarationSyntax)
                {
                    identifier = Doc.Concat(
                        Token.Print(destructorDeclarationSyntax.TildeToken),
                        Token.Print(destructorDeclarationSyntax.Identifier)
                        );
                }
                else if (node is ConstructorDeclarationSyntax constructorDeclarationSyntax)
                {
                    identifier             = Token.Print(constructorDeclarationSyntax.Identifier);
                    constructorInitializer = constructorDeclarationSyntax.Initializer;
                }

                semicolonToken = baseMethodDeclarationSyntax.SemicolonToken;
            }
            else if (node is LocalFunctionStatementSyntax localFunctionStatementSyntax)
            {
                attributeLists    = localFunctionStatementSyntax.AttributeLists;
                modifiers         = localFunctionStatementSyntax.Modifiers;
                returnType        = localFunctionStatementSyntax.ReturnType;
                identifier        = Token.Print(localFunctionStatementSyntax.Identifier);
                typeParameterList = localFunctionStatementSyntax.TypeParameterList;
                parameterList     = localFunctionStatementSyntax.ParameterList;
                constraintClauses = localFunctionStatementSyntax.ConstraintClauses;
                body           = localFunctionStatementSyntax.Body;
                expressionBody = localFunctionStatementSyntax.ExpressionBody;
                semicolonToken = localFunctionStatementSyntax.SemicolonToken;
            }

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

            if (attributeLists.HasValue)
            {
                docs.Add(AttributeLists.Print(node, attributeLists.Value));
            }

            var declarationGroup = new List <Doc>();

            if (modifiers.HasValue)
            {
                declarationGroup.Add(Modifiers.PrintWithoutLeadingTrivia(modifiers.Value));
            }

            if (returnType != null)
            {
                if (!(modifiers.HasValue && modifiers.Value.Count > 0))
                {
                    Token.ShouldSkipNextLeadingTrivia = true;
                }

                declarationGroup.Add(Node.Print(returnType), " ");
                Token.ShouldSkipNextLeadingTrivia = false;
            }

            if (explicitInterfaceSpecifier != null)
            {
                declarationGroup.Add(
                    Node.Print(explicitInterfaceSpecifier.Name),
                    Token.Print(explicitInterfaceSpecifier.DotToken)
                    );
            }

            if (identifier != Doc.Null)
            {
                declarationGroup.Add(identifier);
            }

            if (node is ConversionOperatorDeclarationSyntax conversionOperatorDeclarationSyntax)
            {
                declarationGroup.Add(
                    Token.PrintWithSuffix(
                        conversionOperatorDeclarationSyntax.ImplicitOrExplicitKeyword,
                        " "
                        ),
                    Token.Print(conversionOperatorDeclarationSyntax.OperatorKeyword, " "),
                    Node.Print(conversionOperatorDeclarationSyntax.Type)
                    );
            }
            else if (node is OperatorDeclarationSyntax operatorDeclarationSyntax)
            {
                declarationGroup.Add(
                    Node.Print(operatorDeclarationSyntax.ReturnType),
                    " ",
                    Token.Print(operatorDeclarationSyntax.OperatorKeyword, " "),
                    Token.Print(operatorDeclarationSyntax.OperatorToken)
                    );
            }

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

            if (parameterList != null)
            {
                // if there are no parameters, but there is a super long method name, a groupId
                // will cause SpaceBrace when it isn't wanted.
                if (parameterList.Parameters.Count > 0)
                {
                    groupId = Guid.NewGuid().ToString();
                }
                declarationGroup.Add(ParameterList.Print(parameterList, groupId));
                declarationGroup.Add(Doc.IfBreak(Doc.Null, Doc.SoftLine));
            }

            if (constructorInitializer != null)
            {
                declarationGroup.Add(ConstructorInitializer.Print(constructorInitializer));
            }

            if (modifiers.HasValue && modifiers.Value.Count > 0)
            {
                docs.Add(Token.PrintLeadingTrivia(modifiers.Value[0]));
            }
            else if (returnType != null)
            {
                docs.Add(Token.PrintLeadingTrivia(returnType.GetLeadingTrivia()));
            }

            docs.Add(Doc.Group(declarationGroup));

            docs.Add(ConstraintClauses.Print(constraintClauses));
            if (body != null)
            {
                docs.Add(
                    groupId != null
                        ? Block.PrintWithConditionalSpace(body, groupId)
                        : Block.Print(body)
                    );
            }
            else
            {
                if (expressionBody != null)
                {
                    docs.Add(ArrowExpressionClause.Print(expressionBody));
                }
            }

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

            return(Doc.Group(docs));
        }
Esempio n. 25
0
        public static Doc Print(BasePropertyDeclarationSyntax node)
        {
            EqualsValueClauseSyntax?         initializer = null;
            ExplicitInterfaceSpecifierSyntax?explicitInterfaceSpecifierSyntax = null;
            Doc identifier   = Doc.Null;
            Doc eventKeyword = Doc.Null;
            ArrowExpressionClauseSyntax?expressionBody = null;
            SyntaxToken?semicolonToken = null;

            if (node is PropertyDeclarationSyntax propertyDeclarationSyntax)
            {
                expressionBody = propertyDeclarationSyntax.ExpressionBody;
                initializer    = propertyDeclarationSyntax.Initializer;
                explicitInterfaceSpecifierSyntax = propertyDeclarationSyntax.ExplicitInterfaceSpecifier;
                identifier     = Token.Print(propertyDeclarationSyntax.Identifier);
                semicolonToken = propertyDeclarationSyntax.SemicolonToken;
            }
            else if (node is IndexerDeclarationSyntax indexerDeclarationSyntax)
            {
                expressionBody = indexerDeclarationSyntax.ExpressionBody;
                explicitInterfaceSpecifierSyntax = indexerDeclarationSyntax.ExplicitInterfaceSpecifier;
                identifier = Doc.Concat(
                    Token.Print(indexerDeclarationSyntax.ThisKeyword),
                    Node.Print(indexerDeclarationSyntax.ParameterList)
                    );
                semicolonToken = indexerDeclarationSyntax.SemicolonToken;
            }
            else if (node is EventDeclarationSyntax eventDeclarationSyntax)
            {
                eventKeyword = Token.Print(eventDeclarationSyntax.EventKeyword, " ");
                explicitInterfaceSpecifierSyntax = eventDeclarationSyntax.ExplicitInterfaceSpecifier;
                identifier     = Token.Print(eventDeclarationSyntax.Identifier);
                semicolonToken = eventDeclarationSyntax.SemicolonToken;
            }

            Doc contents = string.Empty;

            if (node.AccessorList != null)
            {
                Doc separator = " ";
                if (
                    node.AccessorList.Accessors.Any(
                        o =>
                        o.Body != null ||
                        o.ExpressionBody != null ||
                        o.Modifiers.Any() ||
                        o.AttributeLists.Any()
                        )
                    )
                {
                    separator = Doc.Line;
                }

                contents = Doc.Group(
                    Doc.Concat(
                        separator,
                        Token.Print(node.AccessorList.OpenBraceToken),
                        Doc.Indent(
                            node.AccessorList.Accessors.Select(
                                o => PrintAccessorDeclarationSyntax(o, separator)
                                )
                            .ToArray()
                            ),
                        separator,
                        Token.Print(node.AccessorList.CloseBraceToken)
                        )
                    );
            }
            else if (expressionBody != null)
            {
                contents = Doc.Concat(ArrowExpressionClause.Print(expressionBody));
            }

            var docs = new List <Doc>();

            docs.Add(ExtraNewLines.Print(node));
            docs.Add(AttributeLists.Print(node, node.AttributeLists));

            return(Doc.Group(
                       Doc.Concat(
                           Doc.Concat(docs),
                           Modifiers.Print(node.Modifiers),
                           eventKeyword,
                           Node.Print(node.Type),
                           " ",
                           explicitInterfaceSpecifierSyntax != null
                        ? Doc.Concat(
                               Node.Print(explicitInterfaceSpecifierSyntax.Name),
                               Token.Print(explicitInterfaceSpecifierSyntax.DotToken)
                               )
                        : Doc.Null,
                           identifier,
                           contents,
                           initializer != null ? EqualsValueClause.Print(initializer) : Doc.Null,
                           semicolonToken.HasValue ? Token.Print(semicolonToken.Value) : Doc.Null
                           )
                       ));
        }
Esempio n. 26
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));
        }
Esempio n. 27
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));
        }