Beispiel #1
0
        public static string Format(AphidExpression statement, string name)
        {
            if (statement == null ||
                string.IsNullOrWhiteSpace(statement.Code) ||
                statement.Index < 0)
            {
                return(null);
            }

            var code = TokenHelper.GetCodeExcerpt(
                statement.Code
                .Insert(statement.Index + statement.Length, End)
                .Insert(statement.Index, Start),
                statement.Index + Start.Length,
                AphidConfig.Current.FrameExcerptLines / 2);

            var highlighted = SyntaxHighlightingFormatter.Format(code);

            return(!string.IsNullOrWhiteSpace(highlighted) ? highlighted : null);
        }
Beispiel #2
0
 public static IEnumerable <Lazy <string> > Format(
     AphidInterpreter interpreter,
     AphidPair[] locals,
     List <AphidObject> scopes,
     int index = 0) =>
 locals
 .Select(x => new Lazy <string>(() =>
                                LineNumberFormatter.PrefixIndex(index++,
                                                                SyntaxHighlightingFormatter.Format(
                                                                    string.Format(
                                                                        "{0} {1} = {2}",
                                                                        AphidCli.GetAphidObjectTypeName(x.Value),
                                                                        x.Key,
                                                                        SerializingFormatter.Format(
                                                                            interpreter,
                                                                            x.Value,
                                                                            ignoreNull: false,
                                                                            ignoreClrObj: false,
                                                                            scopes: scopes)
                                                                        .Indent(new string(' ', 5))
                                                                        .TrimStart())))));
        public static IEnumerable <ColoredText> Highlight(string text)
        {
            var state = 0;
            var strs  = new List <(bool, string)>();
            var sb    = new StringBuilder();

            void push(bool highlight)
            {
                strs.Add((highlight, sb.ToString()));
                sb.Clear();
            }

            void append(string line, bool highlight)
            {
                if (line != null)
                {
                    sb.AppendLine(line);
                }

                push(highlight);
            }

            foreach (var t in text.SplitLines())
            {
                if (state == 0)
                {
                    if (_stackTraceRegex.IsMatch(t))
                    {
                        if (sb.Length != 0)
                        {
                            push(false);
                        }

                        sb.AppendLine(t);
                        state = 1;
                    }
                    else
                    {
                        if (_varRegex.IsMatch(t))
                        {
                            if (sb.Length != 0)
                            {
                                push(false);
                            }

                            append(t, true);
                        }
                        else
                        {
                            sb.AppendLine(t);
                        }
                    }
                }
                else if (state == 1)
                {
                    if (_stackTraceEndRegex.IsMatch(t))
                    {
                        append(t, true);
                        state = 0;
                    }
                    else
                    {
                        sb.AppendLine(t);
                    }
                }
            }

            if (sb.Length != 0)
            {
                push(state != 0);
            }

            var c = strs
                    .SelectMany(x => x.Item1 ?
                                SyntaxHighlightingFormatter.Highlight(x.Item2) :
                                new[] { new ColoredText(new DefaultAphidColorTheme().GetColor(Lexer.AphidTokenType.Text), x.Item2) })
                    .ToArray();

            return(c);
        }