public static string Write(this ALMethod method)
        {
            var result = "";

            using (var stringWriter = new StringWriter())
            {
                using (var writer = new IndentedTextWriter(stringWriter))
                {
                    writer.Indent++;
                    writer.WriteLine();

                    var parameterTxt = "";
                    if (method.Parameters.Count > 0)
                    {
                        parameterTxt = String.Join("; ", method.Parameters.Select(s => $"{(s.IsVar ? "var " : "")}{s.Name}: {s.TypeDefinition.Name}"));
                    }

                    string methodType = "procedure";
                    switch (method.MethodKind)
                    {
                    case ALMethodKind.Trigger:
                        methodType = "trigger";
                        break;
                    }

                    writer.WriteLine($"{(method.IsLocal ? "local " : "")}{methodType} {(method.Name.Contains(" ") ? $"\"{method.Name}\"" : method.Name)}({parameterTxt}){(method.ReturnTypeDefinition != null ? ": " + method.ReturnTypeDefinition.Name : "")}{(method.IsMethodDeclaration ? ";" : "")}");

                    if (!method.IsMethodDeclaration)
                    {
                        if (String.IsNullOrEmpty(method.Content))
                        {
                            writer.WriteLine("begin");
                            writer.WriteLine("end;");
                        }
                        else
                        {
                            writer.WriteLine(method.Content);
                        }
                    }

                    writer.Indent--;

                    result = stringWriter.ToString().Replace("}", "").Trim();
                }
            }

            return(result);
        }
 public virtual string OnWriteObjectMethod(IALObject Target, ALMethod method)
 {
     return(method.Write());
 }
Exemple #3
0
        /// <summary>
        /// Parse method of AL Object: triggers and procedures as well
        /// </summary>
        /// <param name="Lines">Array of textlines</param>
        /// <param name="Target">Current ALObject instance</param>
        public void GetMethods(IEnumerable <string> Lines, IALSection Target)
        {
            var pattern    = @"^\s{0,4}(local procedure|procedure|trigger)\s+(.*?)\((.*?)\)\:?(.*)";
            var procedures = Lines
                             .Where(w => Regex.IsMatch(w, pattern))
                             .ToList();

            if (procedures.Count() == 0)
            {
                return;
            }

            Target.Methods = procedures
                             .Select(s =>
            {
                var method = new ALMethod();

                method.IsLocal = s.Trim().StartsWith("local");
                var match      = Regex.Match(s, pattern);
                switch (match.Groups[1].Value.ToLower())
                {
                case "procedure":
                    method.MethodKind = ALMethodKind.Method;
                    break;

                case "trigger":
                    method.MethodKind = ALMethodKind.Trigger;
                    break;
                }
                method.Name = match.Groups[2].Value;

                if (match.Groups.Count > 3)
                {
                    var paramTxt = match.Groups[3].Value.Trim();
                    if (!string.IsNullOrEmpty(paramTxt))
                    {
                        method.Parameters = paramTxt.Split(';').Select(s =>
                        {
                            var result            = new ALParameter();
                            var parts             = s.Split(':');
                            result.IsVar          = parts[0].Trim().StartsWith("var ");
                            result.Name           = parts[0].Replace("var ", "").Trim();
                            result.TypeDefinition = new ALTypeDefinition {
                                Name = parts[1]
                            };

                            return(result);
                        }).ToList();
                    }
                }

                if (match.Groups.Count > 4)
                {
                    method.ReturnTypeDefinition = new ALReturnTypeDefinition {
                        Name = match.Groups[4].Value
                    };
                }

                // Get Method body from var|begin to end;
                var txtLines        = Lines.ToList();
                var start           = txtLines.IndexOf(s) + 1;
                var nextLine        = txtLines.GetRange(start, txtLines.Count() - start - 1).FirstOrDefault(f => Regex.IsMatch(f, pattern));
                var end             = txtLines.IndexOf(nextLine);
                string beginPattern = @"^\s{0,4}(begin)\s*$";
                string beginText    = txtLines.GetRange(start, txtLines.Count() - start - 1).FirstOrDefault(f => Regex.IsMatch(f, beginPattern));
                if (!string.IsNullOrEmpty(beginText))
                {
                    if (end == -1)
                    {
                        end = Lines.Count();
                    }

                    var bodyLines     = txtLines.GetRange(start, end - start - 1);
                    var body          = string.Join("\r\n", bodyLines);
                    var comments      = GetComments(bodyLines);
                    method.MethodBody = new ALMethodBody
                    {
                        Comments     = comments,
                        Content      = body,
                        ContentLines = bodyLines
                    };

                    method.Content = body;
                }
                else
                {
                    method.IsMethodDeclaration = true;
                }
                method.Attributes = new List <ALAttribute>();

                // Check for Attributes
                end            = txtLines.IndexOf(s) - 1;
                start          = txtLines.IndexOf(s) - 1;
                var testMethod = Lines.ElementAt(start);
                while (testMethod.Trim() != String.Empty &&
                       !testMethod.ToLower().Contains("end;") &&
                       !testMethod.ToLower().Contains("{"))
                {
                    if (testMethod.ToLower().Contains("[test"))
                    {
                        method.TestMethod = true;
                        //break;
                    }

                    var attrMatch = Regex.Match(testMethod, @"\[(\w+)(.*)\]");
                    if (attrMatch.Success)
                    {
                        var attr = new ALAttribute
                        {
                            Name    = attrMatch.Groups[1].Value,
                            Content = testMethod.Trim()
                        };
                        attr.IsEvent = attr.Name.ToLower().Contains("event");
                        method.Attributes.Add(attr);
                    }

                    start -= 1;
                    if (start < 0)
                    {
                        break;
                    }
                    testMethod = Lines.ElementAt(start);
                }

                start += 1;
                end   += 1;
                var linesAboveMethod = txtLines
                                       .GetRange(start, end - start)
                                       .Select(s => s.Trim())
                                       .Where(w => !w.StartsWith("["));
                //.Where(w => w.StartsWith("//") || w.StartsWith("/*") || w.EndsWith("*/"));
                var txtAboveMethod = string.Join("\r\n", linesAboveMethod);
                txtAboveMethod     = txtAboveMethod
                                     .Replace("///", "")
                                     .Replace("//", "")
                                     .Replace("/*", "")
                                     .Replace("*/", "");

                var methodComments = new List <ALComment>();
                if (!string.IsNullOrEmpty(txtAboveMethod))
                {
                    methodComments.Add(new ALComment
                    {
                        Content  = txtAboveMethod.Trim(),
                        StartPos = start,
                        EndPos   = end
                    });
                }

                method.Comments = methodComments;

                return(method);
            })
                             .ToList();
        }