コード例 #1
0
        public static Tuple <UIElement, Tuple <int, int, string> > MakePreview(PreviewMap map, IMapValueItem itm, int previwCount, string value)
        {
            var grid = new Grid()
            {
                VerticalAlignment = VerticalAlignment.Stretch
            };
            Tuple <UIElement, Tuple <int, int, string> > res = new Tuple <UIElement, Tuple <int, int, string> >(grid, null);

            grid.ColumnDefinitions.Add(new ColumnDefinition()
            {
                Width = GridLength.Auto
            });
            grid.ColumnDefinitions.Add(new ColumnDefinition() /*{ Width = GridLength.Auto }*/);
            var nums = new StackPanel();

            Grid.SetColumn(nums, 0);
            var vals = new StackPanel();

            Grid.SetColumn(vals, 1);
            grid.Children.Add(vals);

            /*
             * var sb = new ScrollViewer() { Content = vals, HorizontalScrollBarVisibility = ScrollBarVisibility.Auto, VerticalScrollBarVisibility = ScrollBarVisibility.Disabled };
             * Grid.SetColumn(sb, 1);
             * grid.Children.Add(sb);
             * //*/
            grid.Children.Add(nums);
            var changed = !string.IsNullOrWhiteSpace(value);

            var lIdx     = map.LineIndexAt(itm.Start);
            var lIdx2    = map.LineIndexAt(itm.End);
            var lastLine = Math.Min(lIdx2 + previwCount, map.LineStart.Count - 1);

            for (var line = Math.Max(lIdx - previwCount, 0); line <= lastLine; line++)
            {
                var brush = (line >= lIdx && line <= lIdx2) ? Brushes.Green : Brushes.Black;
                AddLine(nums, vals, line + 1, map.LineAt(line), brush, brush);
                if (changed && line == lIdx2)
                {
                    var rIdx = map.LineStart[lIdx];
                    var rLen = map.LineStart[lIdx2] + map.LineLength[lIdx2] - map.LineStart[lIdx];
                    //var str = map.Code.Substring(rIdx, rLen);
                    //try
                    //{
                    //    var iIdx = itm.EditStart - map.LineStart[lIdx];
                    //    var len = itm.EditEnd - itm.EditStart;
                    //    str = str.Remove(iIdx, len);
                    //    str = str.Insert(iIdx, value).Replace("\r\n", "\n");
                    //    str = str.Replace("\r\n", "\n");
                    //}
                    //catch
                    //{
                    //}
                    //string[] lines = str.Split('\n');
                    string[] lines = map.Code.Substring(rIdx, rLen).
                                     Remove(itm.EditStart - map.LineStart[lIdx], itm.EditEnd - itm.EditStart).
                                     Insert(itm.EditStart - map.LineStart[lIdx], value).Replace("\r\n", "\n").Split('\n');
                    if (lines.Length > 1)
                    {
                        var idx = lIdx;
                        var tab = lines[0];
                        //ищем отступ
                        for (int i = 0; i < tab.Length; i++)
                        {
                            if (!char.IsWhiteSpace(tab[i]))
                            {
                                //оригинальный отступ + 2а пробела
                                tab = tab.Substring(0, i) + "  ";
                                break;
                            }
                        }
                        //добавим отступы к строкам
                        for (int i = 1; i < lines.Length; i++)
                        {
                            lines[i] = tab + lines[i];
                        }
                        //выведем строки в предпросмотр
                        foreach (var ln in lines)
                        {
                            AddLine(nums, vals, ++idx, ln, Brushes.Red, Brushes.Red);
                        }
                    }
                    else if (lines.Length == 1)
                    {
                        AddLine(nums, vals, lIdx + 1, lines[0], Brushes.Red, Brushes.Red);
                    }
                    //данные для замены в файле, индекс начала, длина, и текст на замену
                    res = new Tuple <UIElement, Tuple <int, int, string> >(res.Item1, new Tuple <int, int, string>(rIdx, rLen, string.Join("\r\n", lines)));
                }
            }
            return(res);
        }