示例#1
0
        internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format)
        {
            Decorators?.AppendCodeString(res, ast, format);

            format.ReflowComment(res, this.GetPreceedingWhiteSpaceDefaultNull(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.Length != 0)
                    {
                        var commaWhiteSpace = this.GetListWhiteSpace(ast);
                        ParamsToString(res,
                                       ast,
                                       commaWhiteSpace,
                                       format,
                                       format.SpaceWithinFunctionDeclarationParens != null ?
                                       format.SpaceWithinFunctionDeclarationParens.Value ? " " : "" :
                                       null
                                       );
                    }

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

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

                    if (!this.IsMissingCloseGrouping(ast))
                    {
                        res.Append(')');
                    }

                    if (ReturnAnnotation != null)
                    {
                        format.Append(
                            res,
                            format.SpaceAroundAnnotationArrow,
                            " ",
                            string.Empty,
                            this.GetFifthWhiteSpace(ast)
                            );
                        res.Append("->");
                        ReturnAnnotation.AppendCodeString(
                            res,
                            ast,
                            format,
                            format.SpaceAroundAnnotationArrow != null ?
                            format.SpaceAroundAnnotationArrow.Value ? " " : string.Empty :
                            null
                            );
                    }

                    Body?.AppendCodeString(res, ast, format);
                }
            }
        }
        internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format, string leadingWhiteSpace)
        {
            var kwOnlyText = this.GetExtraVerbatimText(ast);

            if (kwOnlyText != null)
            {
                if (leadingWhiteSpace != null)
                {
                    res.Append(leadingWhiteSpace);
                    res.Append(kwOnlyText.TrimStart());
                    leadingWhiteSpace = null;
                }
                else
                {
                    res.Append(kwOnlyText);
                }
            }

            var writeName = true;

            switch (Kind)
            {
            case ParameterKind.Dictionary:
                res.Append(leadingWhiteSpace ?? this.GetPreceedingWhiteSpaceDefaultNull(ast) ?? string.Empty);
                leadingWhiteSpace = null;
                res.Append("**");
                break;

            case ParameterKind.List:
                res.Append(leadingWhiteSpace ?? this.GetPreceedingWhiteSpaceDefaultNull(ast) ?? string.Empty);
                leadingWhiteSpace = null;
                res.Append('*');
                break;

            case ParameterKind.Normal:
                if (this.IsAltForm(ast))
                {
                    res.Append(leadingWhiteSpace ?? this.GetPreceedingWhiteSpaceDefaultNull(ast) ?? string.Empty);
                    leadingWhiteSpace = null;
                    res.Append('(');
                    AppendParameterName(res, ast, format, leadingWhiteSpace);
                    if (!this.IsMissingCloseGrouping(ast))
                    {
                        res.Append(this.GetSecondWhiteSpace(ast));
                        res.Append(')');
                    }
                    writeName = false;
                }
                break;

            case ParameterKind.KeywordOnly:
                break;

            default: throw new InvalidOperationException();
            }

            if (writeName)
            {
                AppendParameterName(res, ast, format, leadingWhiteSpace);
            }

            if (Annotation != null)
            {
                res.Append(this.GetThirdWhiteSpaceDefaultNull(ast) ?? "");
                res.Append(':');
                Annotation.AppendCodeString(res, ast, format);
            }

            if (DefaultValue != null)
            {
                format.Append(
                    res,
                    format.SpaceAroundDefaultValueEquals,
                    " ",
                    string.Empty,
                    NodeAttributes.GetWhiteSpace(this, ast, WhitespacePrecedingAssign)
                    );

                res.Append('=');
                if (format.SpaceAroundDefaultValueEquals != null)
                {
                    DefaultValue.AppendCodeString(res, ast, format, format.SpaceAroundDefaultValueEquals.Value ? " " : string.Empty);
                }
                else
                {
                    DefaultValue.AppendCodeString(res, ast, format);
                }
            }
        }