Inheritance: WikiFunctions.Hunk
Esempio n. 1
0
        private void RenderContext(Diff.Hunk hunk)
        {
            Range left      = hunk.Left;
            Range right     = hunk.Right;
            int   displayed = 0;

            if (Result.Length > 0) // not the first hunk, adding context for previous change
            {
                displayed = Math.Min(ContextLines, right.Count);
                for (int i = 0; i < displayed; i++)
                {
                    ContextLine(right.Start + i);
                }
            }

            int toDisplay = Math.Min(right.Count - displayed, ContextLines);

            if ((left.End < LeftLines.Length - 1 || right.End < RightLines.Length - 1) && toDisplay > 0)
            // not the last hunk, adding context for next change
            {
                if (right.Count > displayed + toDisplay)
                {
                    ContextHeader(left.End - toDisplay + 1, right.End - toDisplay + 1);
                }
                for (int i = 0; i < toDisplay; i++)
                {
                    ContextLine(right.End - toDisplay + i + 1);
                }
            }
        }
Esempio n. 2
0
        private void RenderDifference(Diff.Hunk hunk)
        {
            Range left  = hunk.Left;
            Range right = hunk.Right;

            if (right.Start == 0)
            {
                ContextHeader(0, 0);
            }
            int changes = Math.Min(left.Count, right.Count);

            for (int i = 0; i < changes; i++)
            {
                LineChanged(left.Start + i, right.Start + i);
            }
            if (left.Count > right.Count)
            {
                for (int i = changes; i < left.Count; i++)
                {
                    LineDeleted(left.Start + i, right.Start + changes);
                }
            }
            else if (left.Count < right.Count)
            {
                for (int i = changes; i < right.Count; i++)
                {
                    LineAdded(right.Start + i);
                }
            }
        }
Esempio n. 3
0
        private static void WriteUnifiedDiffSection(TextWriter writer, ArrayList hunks)
        {
            Diff.Hunk first = (Diff.Hunk)hunks[0];
            Diff.Hunk last  = (Diff.Hunk)hunks[hunks.Count - 1];

            writer.Write("@@ -");
            writer.Write(first.Left.Start + 1);
            writer.Write(",");
            writer.Write(last.Left.End - first.Left.Start + 1);
            writer.Write(" +");
            writer.Write(first.Right.Start + 1);
            writer.Write(",");
            writer.Write(last.Right.End - first.Right.Start + 1);
            writer.WriteLine(" @@");

            foreach (Diff.Hunk hunk in hunks)
            {
                if (hunk.Same)
                {
                    WriteBlock(writer, ' ', hunk.Left);
                    continue;
                }

                WriteBlock(writer, '-', hunk.Left);
                WriteBlock(writer, '+', hunk.Right);
            }
        }
Esempio n. 4
0
        public static void WriteUnifiedDiff(Diff diff, TextWriter writer, string fromfile, string tofile, int context)
        {
            writer.Write("--- ");
            writer.WriteLine(fromfile);
            writer.Write("+++ ");
            writer.WriteLine(tofile);

            ArrayList hunkset = new ArrayList();

            foreach (Diff.Hunk hunk in diff)
            {
                Diff.Hunk lasthunk = null;
                if (hunkset.Count > 0)
                {
                    lasthunk = (Diff.Hunk)hunkset[hunkset.Count - 1];
                }

                if (hunk.Same)
                {
                    // At the start of a hunk set, keep only context lines of context.
                    if (lasthunk == null)
                    {
                        if (hunk.Left.Count > context)
                        {
                            hunkset.Add(hunk.Crop(hunk.Left.Count - context, 0));
                        }
                        else
                        {
                            hunkset.Add(hunk);
                        }
                        // Can't have two same hunks in a row, so the last one was a difference.
                    }
                    else
                    {
                        // Small enough context that this unified diff range will not stop.
                        if (hunk.Left.Count <= context * 2)
                        {
                            hunkset.Add(hunk);

                            // Too much of the same.  Keep context lines and end this section.
                            // And then keep the last context lines as context for the next section.
                        }
                        else
                        {
                            hunkset.Add(hunk.Crop(0, hunk.Left.Count - context));
                            WriteUnifiedDiffSection(writer, hunkset);
                            hunkset.Clear();

                            if (hunk.Left.Count > context)
                            {
                                hunkset.Add(hunk.Crop(hunk.Left.Count - context, 0));
                            }
                            else
                            {
                                hunkset.Add(hunk);
                            }
                        }
                    }
                }
                else
                {
                    hunkset.Add(hunk);
                }
            }

            if (hunkset.Count > 0 && !(hunkset.Count == 1 && ((Diff.Hunk)hunkset[0]).Same))
            {
                WriteUnifiedDiffSection(writer, hunkset);
            }
        }