예제 #1
0
        public Detail(string code)
        {
            InitializeComponent();
            code += "\n";
            resultView.ItemsSource = currentRenderLayers;
            treeView.ItemsSource   = treeRenderLayers;
            colorScheme            = colorNames.Select(s => (Color)ColorConverter.ConvertFromString(s)).ToArray();
            colorlist = new List <Brush>
            {
                Brushes.Green,
                Brushes.Orange,
                Brushes.Pink,
                Brushes.Purple,
                Brushes.Blue
            };
            TextBlockBorderThickness = 1;
            BigStackPanel.Margin     = new Thickness(TextBlockBorderThickness);
            //Random rnd = new Random();

            /*
             * 使用tempstackpanellist处理临时stackpanel的回收问题
             * 节点范围跨行,即当做方框型
             * 树节点数据结构:father, linenum, left, right
             *  linenum为-1,此为跨行节点,left到right行
             *  否则不跨行,linenum行第left列到right列的token
             */
            string [] input;
            try
            {
                input      = CompilerInvoker.Compile(code).Replace("\r", "").Split('\n').Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
                TreeOutput = CompilerInvoker.Compile(code, true).Replace("\r", "").Split('\n').Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
            }
            catch
            {
                throw;
            }
            TotalNodeNum = Convert.ToInt32(input[0]);
            nodes        = new List <RawNode>();
            for (int i = TotalNodeNum; i > 0; i--)
            {
                nodes.Add(new RawNode()
                {
                    Father = -1, FatherLinkType = "root"
                });
            }
            List <ImportantPos> poslist = new List <ImportantPos>();

            for (int i = 1; i < input.Length; i++)
            {
                var part       = Regex.Split(input[i], @"\s");
                int nowpartpos = 6;
                int nowid      = Convert.ToInt32(part[0]);
                nodes[nowid].TypeMacro = Convert.ToInt32(part[1]);
                nodes[nowid].TypeName  = part[2];
                int          startpos = Convert.ToInt32(part[3]);
                int          endpos   = startpos + Convert.ToInt32(part[4]);
                ImportantPos ipstart  = new ImportantPos()
                {
                    IsStart = true,
                    NodeId  = nowid,
                    Pos     = startpos
                };
                ImportantPos ipend = new ImportantPos()
                {
                    IsStart = false,
                    NodeId  = nowid,
                    Pos     = endpos
                };
                poslist.Add(ipstart);
                poslist.Add(ipend);
                int childnum = Convert.ToInt32(part[5]);
                nodes[nowid].ChildrenCount = childnum;
                for (; childnum > 0; childnum--)
                {
                    int    childid         = Convert.ToInt32(part[nowpartpos++]);
                    string childlinestring = part[nowpartpos++];
                    nodes[childid].Father         = nowid;
                    nodes[childid].FatherLinkType = childlinestring;
                }
            }
            poslist.Sort();
            int codeposoffset = 0, nowpos = 0, nowcodeline = 0;
            var codeslinesplit = Regex.Split(code, @"\n");

            int[] codebelong   = new int[code.Length];
            int[] linestartpos = new int[codeslinesplit.Length + 1];
            linestartpos[codeslinesplit.Length] = code.Length;
            for (int i = 0; i < codebelong.Length; i++)
            {
                codebelong[i] = -1;
            }
            for (;;)
            {
                if (nowpos >= poslist.Count && nowcodeline >= codeslinesplit.Length)
                {
                    break;
                }
                linestartpos[nowcodeline] = codeposoffset;
                int start = codeposoffset, end = codeposoffset + codeslinesplit[nowcodeline].Length + 1;
                for (; nowpos < poslist.Count && poslist[nowpos].Pos < end; nowpos++)
                {
                    var pos = poslist[nowpos];
                    if (pos.IsStart)
                    {
                        nodes[pos.NodeId].LineNumber  = nowcodeline;
                        nodes[pos.NodeId].Left        = pos.Pos - codeposoffset;
                        nodes[pos.NodeId].OneLineLeft = pos.Pos;
                    }
                    else
                    {
                        nodes[pos.NodeId].OneLineRight = pos.Pos;
                        //for (int i = nodes[pos.NodeId].OneLineLeft; i < nodes[pos.NodeId].OneLineRight; i++)
                        //    if (codebelong[i] == -1)
                        //        codebelong[i] = pos.NodeId;
                        if (nowcodeline == nodes[pos.NodeId].LineNumber)
                        {
                            nodes[pos.NodeId].Right = pos.Pos - codeposoffset;
                        }
                        else
                        {
                            nodes[pos.NodeId].Left       = nodes[pos.NodeId].LineNumber;
                            nodes[pos.NodeId].LineNumber = -1;
                            nodes[pos.NodeId].Right      = nowcodeline + 1;
                        }
                    }
                }
                codeposoffset = end;
                nowcodeline++;
            }
            foreach (var pos in poslist)
            {
                if (!pos.IsStart)
                {
                    for (int i = nodes[pos.NodeId].OneLineLeft; i < nodes[pos.NodeId].OneLineRight; i++)
                    {
                        if (codebelong[i] == -1 || IsFather(codebelong[i], pos.NodeId))
                        {
                            codebelong[i] = pos.NodeId;
                        }
                    }
                }
            }
            for (int i = 0; i < codeslinesplit.Length; i++)
            {
                StackPanel       sp       = new StackPanel();
                List <TextBlock> ltb      = new List <TextBlock>();
                List <Border>    lb       = new List <Border>();
                bool             allspace = true;
                int offset = linestartpos[i];
                int lastcolor = offset < codebelong.Length ? codebelong[offset] : -2, startpos = offset;
                for (int j = offset; j < linestartpos[i + 1]; j++)
                {
                    if (codebelong[j] != lastcolor || j == linestartpos[i + 1] - 1 || (allspace && j != startpos))
                    {
                        TextBlock tb = new TextBlock()
                        {
                            Text       = code.Substring(startpos, j - startpos),
                            Tag        = lastcolor,
                            FontFamily = new FontFamily("Courier New")
                        };
                        //tb.Background = colorlist[rnd.Next() % colorlist.Count];
                        //tb.Background = colorlist[rand];
                        //rand = (rand + 1) % colorlist.Count;
                        Border b = new Border()
                        {
                            BorderThickness = new Thickness(0),
                            Margin          = new Thickness(0),
                            BorderBrush     = Brushes.Transparent,
                            Tag             = lastcolor,
                            Child           = tb
                        };
                        if (!allspace)
                        {
                            tb.MouseMove += TextBlockWithBorder_MouseMove;
                            b.MouseMove  += TextBlockWithBorder_MouseMove;
                        }
                        ltb.Add(tb);
                        lb.Add(b);
                        sp.Children.Add(b);
                        lastcolor = codebelong[j];
                        startpos  = j;
                    }
                    if (code[j] != ' ')
                    {
                        allspace = false;
                    }
                }
                sp.Orientation         = Orientation.Horizontal;
                sp.Height              = 22;
                sp.HorizontalAlignment = HorizontalAlignment.Left;
                smallstackpanels.Add(sp);
                textblocks.Add(ltb);
                borders.Add(lb);
            }
            foreach (var i in smallstackpanels)
            {
                BigStackPanel.Children.Add(i);
            }

            VisualizeTreeOutput();

            for (int i = 0; i < codeslinesplit.Length; i++)
            {
                TextBlock tb = new TextBlock()
                {
                    Text       = breakPointString,
                    Foreground = Brushes.Transparent,
                    FontFamily = new FontFamily("新宋体"),
                    Height     = 22
                };
                tb.MouseDown += BreakPoint_MouseDown;
                BreakPointStackPanel.Children.Add(tb);
            }

            //RandBlock(ref BigStackPanel, 0, smallstackpanels.Count, 0);
            Mediator.Instance.Code = code;
            runningProgram         = false;
        }
예제 #2
0
            public int CompareTo(object obj)
            {
                ImportantPos ip = obj as ImportantPos;

                return(Pos.CompareTo(ip.Pos));
            }