Esempio n. 1
0
        internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format)
        {
            var decorateWhiteSpace = this.GetNamesWhiteSpace(ast);

            if (Decorators != null)
            {
                Decorators.AppendCodeString(res, ast, format);
            }
            format.ReflowComment(res, this.GetProceedingWhiteSpaceDefaultNull(ast));
            if (IsCoroutine)
            {
                res.Append("async");
                res.Append(NodeAttributes.GetWhiteSpace(this, ast, WhitespaceAfterAsync));
            }
            res.Append("def");
            var name = this.GetVerbatimImage(ast) ?? Name;

            if (!String.IsNullOrEmpty(name))
            {
                res.Append(this.GetSecondWhiteSpace(ast));
                res.Append(name);
                if (!this.IsIncompleteNode(ast))
                {
                    format.Append(
                        res,
                        format.SpaceBeforeFunctionDeclarationParen,
                        " ",
                        "",
                        this.GetThirdWhiteSpaceDefaultNull(ast)
                        );

                    res.Append('(');
                    if (Parameters.Count != 0)
                    {
                        var commaWhiteSpace = this.GetListWhiteSpace(ast);
                        ParamsToString(res,
                                       ast,
                                       commaWhiteSpace,
                                       format,
                                       format.SpaceWithinFunctionDeclarationParens != null ?
                                       format.SpaceWithinFunctionDeclarationParens.Value ? " " : "" :
                                       null
                                       );
                    }

                    string namedOnly = this.GetExtraVerbatimText(ast);
                    if (namedOnly != null)
                    {
                        res.Append(namedOnly);
                    }

                    if (!this.IsMissingCloseGrouping(ast))
                    {
                        format.Append(
                            res,
                            Parameters.Count != 0 ?
                            format.SpaceWithinFunctionDeclarationParens :
                            format.SpaceWithinEmptyParameterList,
                            " ",
                            "",
                            this.GetFourthWhiteSpaceDefaultNull(ast)
                            );

                        res.Append(')');
                    }
                    if (ReturnAnnotation != null)
                    {
                        format.Append(
                            res,
                            format.SpaceAroundAnnotationArrow,
                            " ",
                            "",
                            this.GetFifthWhiteSpace(ast)
                            );
                        res.Append("->");
                        _returnAnnotation.AppendCodeString(
                            res,
                            ast,
                            format,
                            format.SpaceAroundAnnotationArrow != null ?
                            format.SpaceAroundAnnotationArrow.Value ? " " : "" :
                            null
                            );
                    }
                    if (Body != null)
                    {
                        Body.AppendCodeString(res, ast, format);
                    }
                }
            }
        }
        internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format)
        {
            if (Decorators != null)
            {
                Decorators.AppendCodeString(res, ast, format);
            }

            format.ReflowComment(res, this.GetProceedingWhiteSpace(ast));
            res.Append("class");
            res.Append(this.GetSecondWhiteSpace(ast));
            res.Append(this.GetVerbatimImage(ast) ?? Name);

            if (!this.IsAltForm(ast))
            {
                format.Append(
                    res,
                    format.SpaceBeforeClassDeclarationParen,
                    " ",
                    "",
                    this.GetThirdWhiteSpace(ast)
                    );

                res.Append('(');
            }

            if (Bases.Count != 0)
            {
                ListExpression.AppendItems(
                    res,
                    ast,
                    format,
                    "",
                    "",
                    this,
                    this.Bases.Count,
                    (i, sb) => {
                    if (format.SpaceWithinClassDeclarationParens != null && i == 0)
                    {
                        // need to remove any leading whitespace which was preserved for
                        // the 1st param, and then force the correct whitespace.
                        Bases[i].AppendCodeString(sb, ast, format, format.SpaceWithinClassDeclarationParens.Value ? " " : "");
                    }
                    else
                    {
                        Bases[i].AppendCodeString(sb, ast, format);
                    }
                }
                    );
            }
            else if (!this.IsAltForm(ast))
            {
                if (format.SpaceWithinEmptyBaseClassList != null && format.SpaceWithinEmptyBaseClassList.Value)
                {
                    res.Append(' ');
                }
            }

            if (!this.IsAltForm(ast) && !this.IsMissingCloseGrouping(ast))
            {
                if (Bases.Count != 0 ||
                    format.SpaceWithinEmptyBaseClassList == null ||
                    !String.IsNullOrWhiteSpace(this.GetFourthWhiteSpace(ast)))
                {
                    format.Append(
                        res,
                        format.SpaceWithinClassDeclarationParens,
                        " ",
                        "",
                        this.GetFourthWhiteSpace(ast)
                        );
                }

                res.Append(')');
            }

            _body.AppendCodeString(res, ast, format);
        }