public override void VisitMethodDeclaration(MethodDeclarationSyntax node)
        {
            if (!_buildingSpans && InSpans(node.Span))
            {
                bool isPublic = node.IsPublic();
                bool isStatic = node.IsStatic();

                if (isPublic)
                {
                    // skip methods with ref parameters of multiple out parameters for now
                    int refCount = 0;
                    int outCount = 0;
                    foreach (var parameter in node.ParameterList.Parameters)
                    {
                        foreach (var modifier in parameter.Modifiers)
                        {
                            if (modifier.Text == "ref")
                            {
                                refCount++;
                            }
                            if (modifier.Text == "out")
                            {
                                outCount++;
                            }
                        }
                    }

                    bool useAsReturnType;
                    if (node.IsNonConst(out useAsReturnType) && !useAsReturnType)
                    {
                        outCount++;
                    }

                    if (refCount == 0 && outCount < 3)
                    {
                        TypeDeclarationSyntax tds = node.Parent as TypeDeclarationSyntax;
                        string visitingClass      = ClassName(tds, out string baseClass);

                        var docComment = node.GetLeadingTrivia().Select(i => i.GetStructure()).OfType <DocumentationCommentTriviaSyntax>().FirstOrDefault();
                        var cb         = ClassBuilder.Get(visitingClass);
                        if (cb != null)
                        {
                            cb.Methods.Add(new Tuple <MethodDeclarationSyntax, DocumentationCommentTriviaSyntax>(node, docComment));
                            if (!string.IsNullOrWhiteSpace(baseClass))
                            {
                                cb.BaseClassName = baseClass;
                            }
                        }
                    }
                }
            }
            base.VisitMethodDeclaration(node);
        }
Пример #2
0
        protected override string ToComputeClient(ClassBuilder cb)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine();
            int iMethod = 0;

            foreach (var(method, comment) in cb.Methods)
            {
                string methodName = GetMethodName(method, cb);
                if (string.IsNullOrWhiteSpace(methodName))
                {
                    continue;
                }
                sb.Append($"def {methodName}(");
                List <string> parameters = GetParameterNames(method, cb, out int outParamCount);
                for (int i = 0; i < parameters.Count; i++)
                {
                    sb.Append(parameters[i] + ", ");
                }
                sb.AppendLine("multiple=False):");
                StringBuilder        summary;
                List <ParameterInfo> parameterList;
                ReturnInfo           returnInfo;
                sb.Append(DocCommentToPythonDoc(comment, method, 1, out summary, out parameterList, out returnInfo));
                sb.AppendLine($"{T1}url = \"{cb.EndPoint(method)}\"");
                sb.AppendLine($"{T1}if multiple: url += \"?multiple=true\"");
                var paramList = new StringBuilder();
                for (int i = 0; i < parameters.Count; i++)
                {
                    paramList.Append(parameters[i]);
                    if (i < (parameters.Count - 1))
                    {
                        paramList.Append(", ");
                    }
                }
                sb.AppendLine($"{T1}args = [{paramList.ToString()}]");
                if (parameters.Count == 1)
                {
                    sb.AppendLine($"{T1}if multiple: args = [[item] for item in {parameters[0]}]");
                }
                else
                {
                    sb.AppendLine($"{T1}if multiple: args = zip({paramList.ToString()})");
                }

                string endpoint = method.Identifier.ToString();
                sb.AppendLine($"{T1}response = Util.ComputeFetch(url, args)");

                if (outParamCount == 0)
                {
                    bool   returnIsArray   = returnInfo.Type.EndsWith("[]");
                    string returnClassName = returnIsArray ? returnInfo.Type.Substring(0, returnInfo.Type.Length - 2) : returnInfo.Type;
                    var    returnCB        = ClassBuilder.Get(returnClassName);
                    if (returnCB != null)
                    {
                        var baseClass = returnCB;
                        while (true)
                        {
                            var b = ClassBuilder.Get(baseClass.BaseClassName);
                            if (b != null)
                            {
                                baseClass = b;
                                continue;
                            }
                            break;
                        }
                        if (baseClass.ClassName == "CommonObject" || baseClass.ClassName == "GeometryBase")
                        {
                            sb.AppendLine($"{T1}response = Util.DecodeToCommonObject(response)");
                        }
                        if (baseClass.ClassName == "Point3d" ||
                            baseClass.ClassName == "Vector3d" ||
                            baseClass.ClassName == "Line")
                        {
                            sb.AppendLine($"{T1}response = Util.DecodeTo{baseClass.ClassName}(response)");
                        }
                    }
                }
                sb.AppendLine($"{T1}return response");
                sb.AppendLine();

                iMethod++;
                if (iMethod < cb.Methods.Count)
                {
                    sb.AppendLine();
                }
            }
            return(sb.ToString());
        }