示例#1
0
 public static Doc Print(VarPatternSyntax node)
 {
     return(Doc.Concat(Token.Print(node.VarKeyword, " "), Node.Print(node.Designation)));
 }
示例#2
0
        public void Align_Should_Print_Basic_Case()
        {
            var doc = Doc.Concat("+ ", Doc.Align(2, Doc.Group("1", Doc.HardLine, "2")));

            PrintedDocShouldBe(doc, $"+ 1{NewLine}  2");
        }
示例#3
0
 public static Doc Print(RefExpressionSyntax node)
 {
     return(Doc.Concat(Token.Print(node.RefKeyword, " "), Node.Print(node.Expression)));
 }
示例#4
0
        public void HardLineIfNoPreviousLine_Does_Not_Blow_Up()
        {
            var doc = Doc.Concat(Doc.HardLineIfNoPreviousLine, "1");

            PrintedDocShouldBe(doc, $"{NewLine}1");
        }
示例#5
0
        public void Conditional_Group_Prints_As_Group()
        {
            var doc = Doc.ConditionalGroup(Doc.Concat("1", Doc.Line, "2"));

            PrintedDocShouldBe(doc, "1 2", 10);
        }
示例#6
0
        public void Trim_Should_Not_Trim_NonWhitespace(string value)
        {
            var doc = Doc.Concat(value, Doc.Trim);

            PrintedDocShouldBe(doc, value);
        }
示例#7
0
        public void HardLineIfNoPreviousLine_Should_Not_Insert_Line_If_There_Is_One()
        {
            var doc = Doc.Concat("1", Doc.HardLine, Doc.HardLineIfNoPreviousLine, "2");

            PrintedDocShouldBe(doc, $"1{NewLine}2");
        }
        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));
        }
示例#9
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
                           )
                       ));
        }
示例#10
0
 public static Doc Print(PrimaryConstructorBaseTypeSyntax node)
 {
     return(Doc.Concat(Node.Print(node.Type), ArgumentList.Print(node.ArgumentList)));
 }
示例#11
0
 public static Doc Print(ContinueStatementSyntax node)
 {
     return(Doc.Concat(Token.Print(node.ContinueKeyword), Token.Print(node.SemicolonToken)));
 }
示例#12
0
 public static Doc Print(PointerTypeSyntax node)
 {
     return(Doc.Concat(Node.Print(node.ElementType), Token.Print(node.AsteriskToken)));
 }
示例#13
0
 public static Doc Print(NullableTypeSyntax node)
 {
     return(Doc.Concat(Node.Print(node.ElementType), Token.Print(node.QuestionToken)));
 }
 public static Doc Print(MemberBindingExpressionSyntax node)
 {
     return(Doc.Concat(Token.Print(node.OperatorToken), Node.Print(node.Name)));
 }
示例#15
0
        public void IfBreak_Should_Print_Flat_Contents_When_GroupId_Does_Not_Break()
        {
            var doc = Doc.Concat(Doc.GroupWithId("1", "1"), Doc.IfBreak("break", "flat", "1"));

            PrintedDocShouldBe(doc, "1flat");
        }
 public static Doc Print(DefaultSwitchLabelSyntax node)
 {
     return(Doc.Concat(Token.Print(node.Keyword), Token.Print(node.ColonToken)));
 }
示例#17
0
        public void Trim_Should_Trim_Current_Line(string indent)
        {
            var doc = Doc.Concat($"1{indent}", Doc.Trim);

            PrintedDocShouldBe(doc, "1");
        }
示例#18
0
        public void Indent_With_Hardline()
        {
            var doc = Doc.Concat("0", Doc.Indent(Doc.Concat(Doc.HardLine, "1", Doc.HardLine, "2")));

            PrintedDocShouldBe(doc, $"0{NewLine}    1{NewLine}    2");
        }
示例#19
0
        public void Concat_With_Line()
        {
            var doc = Doc.Concat("1", Doc.Line, "3");

            PrintedDocShouldBe(doc, $"1{NewLine}3");
        }
示例#20
0
        public void Lines_Allowed()
        {
            var doc = Doc.Concat(Doc.HardLine, "1");

            PrintedDocShouldBe(doc, $"{NewLine}1");
        }
示例#21
0
        public void Group_With_Line()
        {
            var doc = Doc.Group(Doc.Concat("1", Doc.Line, "3"));

            PrintedDocShouldBe(doc, "1 3");
        }
示例#22
0
        public void Lines_Removed_From_Beginning()
        {
            var doc = Doc.Concat(Doc.HardLine, "1");

            PrintedDocShouldBe(doc, "1", trimInitialLines: true);
        }
示例#23
0
        public void HardLineIfNoPreviousLine_Should_Not_Insert_After_Indented_HardLine()
        {
            var doc = Doc.Concat(Doc.Indent("1", Doc.HardLine, Doc.HardLineIfNoPreviousLine, "2"));

            PrintedDocShouldBe(doc, $"1{NewLine}    2");
        }
示例#24
0
        public void Literal_Lines_Removed_From_Beginning()
        {
            var doc = Doc.Concat(Doc.LiteralLine, "1");

            PrintedDocShouldBe(doc, $"1");
        }
示例#25
0
        public void Group_With_Line_And_Hardline()
        {
            var doc = Doc.Group(Doc.Concat("1", Doc.Line, "2", Doc.HardLine, "3"));

            PrintedDocShouldBe(doc, $"1{NewLine}2{NewLine}3");
        }
示例#26
0
        public void LiteralLine_Trims_Space()
        {
            var doc = Doc.Concat("{", Doc.Indent(Doc.HardLine, "indent", Doc.LiteralLine), "}");

            PrintedDocShouldBe(doc, $"{{{NewLine}    indent{NewLine}}}");
        }
示例#27
0
        public void Group_With_Line_And_BreakParent()
        {
            var doc = Doc.Group(Doc.Concat("1", Doc.Line, "2", Doc.Line, "3", Doc.BreakParent));

            PrintedDocShouldBe(doc, $"1{NewLine}2{NewLine}3");
        }
示例#28
0
        public void Basic_Concat()
        {
            var doc = Doc.Concat("1", "2", "3");

            PrintedDocShouldBe(doc, "123");
        }
 public static Doc Print(DeclarationPatternSyntax node)
 {
     return(Doc.Concat(Node.Print(node.Type), " ", Node.Print(node.Designation)));
 }
        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));
        }