From ce56c5dcc979ca415ba61cca02856ad8547d1f83 Mon Sep 17 00:00:00 2001 From: Robin Rosenberg Date: Sat, 12 Jun 2010 15:31:04 +0200 Subject: [PATCH] Extend DiffFormatter API to simplify styling 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 --- .../org/eclipse/jgit/diff/DiffFormatter.java | 102 +++++++++++++++--- 1 file changed, 90 insertions(+), 12 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java b/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java index 2d552d40d9..c40d3b7000 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java @@ -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 edits, final int i) { -- 2.39.5