Beispiel #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);
        }
Beispiel #2
0
 public static Doc Print(ElseClauseSyntax node)
 {
     return(Doc.Concat(
                Token.Print(node.ElseKeyword),
                node.Statement
                is IfStatementSyntax ifStatementSyntax
             ? Doc.Concat(" ", IfStatement.Print(ifStatementSyntax))
             : OptionalBraces.Print(node.Statement)
                ));
 }
Beispiel #3
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)
                        ),
                    OptionalBraces.Print(node.Statement, groupId)
                    )
            };

            return(Doc.Concat(docs));
        }
Beispiel #4
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)
                        ),
                    OptionalBraces.Print(node.Statement, groupId)
                    )
                );

            return(docs);
        }
Beispiel #5
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)
                        ),
                    OptionalBraces.Print(node.Statement, groupId)
                    )
                );

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

            return(Doc.Concat(docs));
        }