]> source.dussan.org Git - jgit.git/commitdiff
Extend DiffFormatter API to simplify styling 28/828/2
authorRobin Rosenberg <robin.rosenberg@dewire.com>
Sat, 12 Jun 2010 13:31:04 +0000 (15:31 +0200)
committerRobin Rosenberg <robin.rosenberg@dewire.com>
Sat, 12 Jun 2010 13:31:04 +0000 (15:31 +0200)
Refactor and extend the internals so users can override and
intervene during formatting, e.g. to colorize output.

Change-Id: Ia1cf40cfd4a5ed7dfb6503f8dfc617237bee0659
Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java

index 2d552d40d9400d5e26c2530a951e5d2485c50edc..c40d3b70006c4032324d0c39876dabf72a99eacb 100644 (file)
@@ -139,15 +139,15 @@ public class DiffFormatter {
 
                        while (aCur < aEnd || bCur < bEnd) {
                                if (aCur < curEdit.getBeginA() || endIdx + 1 < curIdx) {
-                                       writeLine(out, ' ', a, aCur);
+                                       writeContextLine(out, a, aCur, isEndOfLineMissing(a, aCur));
                                        aCur++;
                                        bCur++;
-
                                } else if (aCur < curEdit.getEndA()) {
-                                       writeLine(out, '-', a, aCur++);
-
+                                       writeRemovedLine(out, a, aCur, isEndOfLineMissing(a, aCur));
+                                       aCur++;
                                } else if (bCur < curEdit.getEndB()) {
-                                       writeLine(out, '+', b, bCur++);
+                                       writeAddedLine(out, b, bCur, isEndOfLineMissing(b, bCur));
+                                       bCur++;
                                }
 
                                if (end(curEdit, aCur, bCur) && ++curIdx < edits.size())
@@ -156,12 +156,85 @@ public class DiffFormatter {
                }
        }
 
-       private void writeHunkHeader(final OutputStream out, int aCur, int aEnd,
-                       int bCur, int bEnd) throws IOException {
+       /**
+        * Output a line of diff context
+        *
+        * @param out
+        *            OutputStream
+        * @param text
+        *            RawText for accessing raw data
+        * @param line
+        *            the line number within text
+        * @param endOfLineMissing
+        *            true if we should add the GNU end of line missing warning
+        * @throws IOException
+        */
+       protected void writeContextLine(final OutputStream out, final RawText text,
+                       final int line, boolean endOfLineMissing) throws IOException {
+               writeLine(out, ' ', text, line, endOfLineMissing);
+       }
+
+       private boolean isEndOfLineMissing(final RawText text, final int line) {
+               return line + 1 == text.size() && text.isMissingNewlineAtEnd();
+       }
+
+       /**
+        * Output an added line
+        *
+        * @param out
+        *            OutputStream
+        * @param text
+        *            RawText for accessing raw data
+        * @param line
+        *            the line number within text
+        * @param endOfLineMissing
+        *            true if we should add the gnu end of line missing warning
+        * @throws IOException
+        */
+       protected void writeAddedLine(final OutputStream out, final RawText text, final int line, boolean endOfLineMissing)
+                       throws IOException {
+               writeLine(out, '+', text, line, endOfLineMissing);
+       }
+
+       /**
+        * Output a removed line
+        *
+        * @param out
+        *            OutputStream
+        * @param text
+        *            RawText for accessing raw data
+        * @param line
+        *            the line number within text
+        * @param endOfLineMissing
+        *            true if we should add the gnu end of line missing warning
+        * @throws IOException
+        */
+       protected void writeRemovedLine(final OutputStream out, final RawText text,
+                       final int line, boolean endOfLineMissing) throws IOException {
+               writeLine(out, '-', text, line, endOfLineMissing);
+       }
+
+       /**
+        * Output a hunk header
+        *
+        * @param out
+        *            OutputStream
+        * @param aStartLine
+        *            within first source
+        * @param aEndLine
+        *            within first source
+        * @param bStartLine
+        *            within second source
+        * @param bEndLine
+        *            within second source
+        * @throws IOException
+        */
+       protected void writeHunkHeader(final OutputStream out, int aStartLine, int aEndLine,
+                       int bStartLine, int bEndLine) throws IOException {
                out.write('@');
                out.write('@');
-               writeRange(out, '-', aCur + 1, aEnd - aCur);
-               writeRange(out, '+', bCur + 1, bEnd - bCur);
+               writeRange(out, '-', aStartLine + 1, aEndLine - aStartLine);
+               writeRange(out, '+', bStartLine + 1, bEndLine - bStartLine);
                out.write(' ');
                out.write('@');
                out.write('@');
@@ -199,12 +272,17 @@ public class DiffFormatter {
        }
 
        private static void writeLine(final OutputStream out, final char prefix,
-                       final RawText text, final int cur) throws IOException {
+                       final RawText text, final int cur, boolean noNewLineIndicator) throws IOException {
                out.write(prefix);
                text.writeLine(out, cur);
                out.write('\n');
-               if (cur + 1 == text.size() && text.isMissingNewlineAtEnd())
-                       out.write(noNewLine);
+               if (noNewLineIndicator)
+                       writeNoNewLine(out);
+       }
+
+       private static void writeNoNewLine(final OutputStream out)
+                       throws IOException {
+               out.write(noNewLine);
        }
 
        private int findCombinedEnd(final List<Edit> edits, final int i) {