/** * Formats a list of edits in unified diff format * * @param edits some differences which have been calculated between A and B * @param a the text A which was compared * @param b the text B which was compared * @throws IOException */ public void format(final EditList edits, final RawText a, final RawText b) throws IOException { for (int curIdx = 0; curIdx < edits.size(); ) { Edit curEdit = edits.get(curIdx); final int endIdx = findCombinedEnd(edits, curIdx); final Edit endEdit = edits.get(endIdx); int aCur = Math.max(0, curEdit.getBeginA() - context); int bCur = Math.max(0, curEdit.getBeginB() - context); final int aEnd = Math.min(a.size(), endEdit.getEndA() + context); final int bEnd = Math.min(b.size(), endEdit.getEndB() + context); writeHunkHeader(aCur, aEnd, bCur, bEnd); while (aCur < aEnd || bCur < bEnd) { if (aCur < curEdit.getBeginA() || endIdx + 1 < curIdx) { writeContextLine(a, aCur); if (isEndOfLineMissing(a, aCur)) out.write(noNewLine); aCur++; bCur++; } else if (aCur < curEdit.getEndA()) { writeRemovedLine(a, aCur); if (isEndOfLineMissing(a, aCur)) out.write(noNewLine); aCur++; } else if (bCur < curEdit.getEndB()) { writeAddedLine(b, bCur); if (isEndOfLineMissing(b, bCur)) out.write(noNewLine); bCur++; } if (end(curEdit, aCur, bCur) && ++curIdx < edits.size()) curEdit = edits.get(curIdx); } } }
@Test public void testCreate() { final Edit e = new Edit(1, 2, 3, 4); assertEquals(1, e.getBeginA()); assertEquals(2, e.getEndA()); assertEquals(3, e.getBeginB()); assertEquals(4, e.getEndB()); }
@Test public void testCreateEmpty() { final Edit e = new Edit(1, 3); assertEquals(1, e.getBeginA()); assertEquals(1, e.getEndA()); assertEquals(3, e.getBeginB()); assertEquals(3, e.getEndB()); assertTrue("is empty", e.isEmpty()); assertSame(Edit.Type.EMPTY, e.getType()); }
private static boolean end(final Edit edit, final int a, final int b) { return edit.getEndA() <= a && edit.getEndB() <= b; }