private static void DrawFileTypeMark(DrawingContext dc, Rect rect, DiffLine diffLine)
        {
            string mark;
            Brush brush;
            if (diffLine.LineType == DiffLine.LineTypes.Add)
            {
                mark = "+";
                brush = Brushes.Green;
            }
            else if (diffLine.LineType == DiffLine.LineTypes.Delete)
            {
                mark = "-";
                brush = Brushes.Red;
            }
            else
                return;

            dc.DrawText(
                new FormattedText(
                    mark,
                    CultureInfo.CurrentCulture,
                    FlowDirection.LeftToRight,
                    new Typeface("Consolas"),
                    14,
                    brush),
                new Point(rect.Left + 4, rect.Top - 1));
        }
        public static void DrawBackground(
            TextView textView, DrawingContext dc, double x, double width, DiffLine[] diffLines,
            bool isLight,
            Action<PerLineDrawArgs> perLineDraw = null)
        {
            if (diffLines == null)
                return;

            foreach (var visualLine in textView.VisualLines)
            {
                var linenum = visualLine.FirstDocumentLine.LineNumber - 1;
                if (linenum >= diffLines.Length)
                    continue;

                var diffLine = diffLines[linenum];
                var brush = default(Brush);

                switch (diffLine.LineType)
                {
                    case DiffLine.LineTypes.ChunkTag:
                        brush = isLight
                            ? Constants.LightChunkTagBackground
                            : Constants.ChunkTagBackground;
                        break;

                    case DiffLine.LineTypes.Add:
                        brush = isLight
                            ? Constants.LightAddBackground
                            : Constants.AddBackground;
                        break;

                    case DiffLine.LineTypes.Delete:
                        brush = isLight
                            ? Constants.LightRemoveBackground
                            : Constants.RemoveBackground;
                        break;
                }

                var perLineDrawArgs = new PerLineDrawArgs
                {
                    TextView = textView,
                    VisualLine = visualLine,
                    DrawingContext = dc,
                    DiffLine = diffLine
                };

                var index = 0;

                foreach (
                    var segRect in BackgroundGeometryBuilder.GetRectsFromVisualSegment(textView, visualLine, 0, 1000))
                {
                    var rect = new Rect(x, segRect.Top, width, segRect.Height + 1);

                    // ReSharper disable once PossibleUnintendedReferenceComparison
                    if (brush != default(Brush))
                        using (new GuidelineSetBlock(dc, rect))
                            dc.DrawRectangle(brush, null, rect);

                    if (perLineDraw != null)
                    {
                        perLineDrawArgs.Rect = rect;
                        perLineDrawArgs.Index = index;

                        perLineDraw.Invoke(perLineDrawArgs);
                    }

                    ++index;
                }
            }
        }
Exemple #3
0
        public void MakeDiff(string patch)
        {
            var sb = new StringBuilder();
            {
                var diffLinesTemp = new List<DiffLine>();

                try
                {
                    var fileDiffs = ParseDiff.Diff.Parse(patch).FirstOrDefault();
                    if (fileDiffs != null)
                    {
                        foreach (var chunck in fileDiffs.Chunks)
                        {
                            sb.AppendLine(chunck.Content.TrimEnd('\r', '\n'));
                            diffLinesTemp.Add(new DiffLine {LineType = DiffLine.LineTypes.ChunckTag});

                            var oldIndex = chunck.OldStart - 1;
                            var newIndex = chunck.NewStart - 1;

                            var addDeletePairs = new Dictionary<int, DiffLine[]>();

                            foreach (var l in chunck.Changes)
                            {
                                switch (l.Type)
                                {
                                    case LineChangeType.Normal:
                                        ++oldIndex;
                                        ++newIndex;
                                        break;

                                    case LineChangeType.Add:
                                        ++newIndex;
                                        break;

                                    case LineChangeType.Delete:
                                        ++oldIndex;
                                        break;
                                }

                                var diffLine =
                                    new DiffLine
                                    {
                                        OldIndex = oldIndex,
                                        NewIndex = newIndex,
                                        LineType = (DiffLine.LineTypes) l.Type,
                                        Index = l.Index,
                                        Content = l.Content.Substring(1)
                                    };

                                diffLinesTemp.Add(diffLine);

                                sb.AppendLine(diffLine.Content.TrimEnd('\r', '\n'));

                                if (l.Type == LineChangeType.Delete)
                                {
                                    Debug.Assert(l.Index != 0);

                                    if (addDeletePairs.ContainsKey(l.Index) == false)
                                        addDeletePairs[l.Index] = new DiffLine[2];

                                    addDeletePairs[l.Index][0] = diffLine;
                                }
                                else if (l.Type == LineChangeType.Add)
                                {
                                    Debug.Assert(l.Index != 0);

                                    if (addDeletePairs.ContainsKey(l.Index) == false)
                                        addDeletePairs[l.Index] = new DiffLine[2];

                                    addDeletePairs[l.Index][1] = diffLine;
                                }
                            }

                            MakeContentDiffs(addDeletePairs);
                        }
                    }
                }
                catch
                {
                    diffLinesTemp.Clear();
                }

                DiffLines = diffLinesTemp.ToArray();
            }

            Diff = sb.ToString().TrimEnd('\r', '\n');
        }
        private static void DrawIndex(DrawingContext dc, Rect rect, DiffLine diffLine)
        {
            switch(diffLine.LineType)
            {
                case DiffLine.LineTypes.ChunkTag:
                    DrawIndexText(dc, rect, "・・・", OldIndexOffset, TextAlignment.Center);
                    DrawIndexText(dc, rect, "・・・", NewIndexOffset, TextAlignment.Center);
                    break;

                case DiffLine.LineTypes.Normal:
                    DrawIndexText(dc, rect, diffLine.OldIndex, OldIndexOffset, TextAlignment.Right);
                    DrawIndexText(dc, rect, diffLine.NewIndex, NewIndexOffset, TextAlignment.Right);
                    break;

                case DiffLine.LineTypes.Add:
                    DrawIndexText(dc, rect, diffLine.NewIndex, NewIndexOffset, TextAlignment.Right);
                    break;

                case DiffLine.LineTypes.Delete:
                    DrawIndexText(dc, rect, diffLine.OldIndex, OldIndexOffset, TextAlignment.Right);
                    break;
            }
        }