예제 #1
0
        private static Doc PrintAccessorDeclarationSyntax(
            AccessorDeclarationSyntax node,
            Doc separator
            )
        {
            var docs = new List <Doc>();

            if (node.AttributeLists.Count > 0 || node.Body != null || node.ExpressionBody != null)
            {
                docs.Add(Doc.HardLine);
            }
            else
            {
                docs.Add(separator);
            }

            docs.Add(AttributeLists.Print(node, node.AttributeLists));
            docs.Add(Modifiers.Print(node.Modifiers));
            docs.Add(Token.Print(node.Keyword));

            if (node.Body != null)
            {
                docs.Add(Block.Print(node.Body));
            }
            else if (node.ExpressionBody != null)
            {
                docs.Add(ArrowExpressionClause.Print(node.ExpressionBody));
            }

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

            return(Doc.Concat(docs));
        }
예제 #2
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
                           )
                       ));
        }
        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));
        }