Переглянути джерело

Add helper methods to Edit

Exposing isEmpty, getLengthA, getLengthB make it easier to examine
the state of an edit and work with it from higher level code.

The before and after cut routines make it easy to split an edit
that contains another edit, such as to decompose a REPLACE that
contains a common sequence within it.

Change-Id: Id63d6476a7a6b23acb7ab237d414a0a1a7200290
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
tags/v0.9.1
Shawn O. Pearce 13 роки тому
джерело
коміт
fe8fe13349

+ 24
- 1
org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/EditTest.java Переглянути файл

assertEquals(1, e.getEndA()); assertEquals(1, e.getEndA());
assertEquals(3, e.getBeginB()); assertEquals(3, e.getBeginB());
assertEquals(3, e.getEndB()); assertEquals(3, e.getEndB());
assertTrue("is empty", e.isEmpty());
assertSame(Edit.Type.EMPTY, e.getType());
} }


public void testSwap() { public void testSwap() {
public void testType_Insert() { public void testType_Insert() {
final Edit e = new Edit(1, 1, 1, 2); final Edit e = new Edit(1, 1, 1, 2);
assertSame(Edit.Type.INSERT, e.getType()); assertSame(Edit.Type.INSERT, e.getType());
assertFalse("not empty", e.isEmpty());
assertEquals(0, e.getLengthA());
assertEquals(1, e.getLengthB());
} }


public void testType_Delete() { public void testType_Delete() {
final Edit e = new Edit(1, 2, 1, 1); final Edit e = new Edit(1, 2, 1, 1);
assertSame(Edit.Type.DELETE, e.getType()); assertSame(Edit.Type.DELETE, e.getType());
assertFalse("not empty", e.isEmpty());
assertEquals(1, e.getLengthA());
assertEquals(0, e.getLengthB());
} }


public void testType_Replace() { public void testType_Replace() {
final Edit e = new Edit(1, 2, 1, 4); final Edit e = new Edit(1, 2, 1, 4);
assertSame(Edit.Type.REPLACE, e.getType()); assertSame(Edit.Type.REPLACE, e.getType());
assertFalse("not empty", e.isEmpty());
assertEquals(1, e.getLengthA());
assertEquals(3, e.getLengthB());
} }


public void testType_Empty() { public void testType_Empty() {
assertSame(Edit.Type.EMPTY, new Edit(1, 1, 2, 2).getType());
final Edit e = new Edit(1, 1, 2, 2);
assertSame(Edit.Type.EMPTY, e.getType());
assertSame(Edit.Type.EMPTY, new Edit(1, 2).getType()); assertSame(Edit.Type.EMPTY, new Edit(1, 2).getType());
assertTrue("is empty", e.isEmpty());
assertEquals(0, e.getLengthA());
assertEquals(0, e.getLengthB());
} }


public void testToString() { public void testToString() {
e.extendB(); e.extendB();
assertEquals(new Edit(1, 2, 1, 3), e); assertEquals(new Edit(1, 2, 1, 3), e);
} }

public void testBeforeAfterCuts() {
final Edit whole = new Edit(1, 8, 2, 9);
final Edit mid = new Edit(4, 5, 3, 6);

assertEquals(new Edit(1, 4, 2, 3), whole.before(mid));
assertEquals(new Edit(5, 8, 6, 9), whole.after(mid));
}
} }

+ 41
- 0
org.eclipse.jgit/src/org/eclipse/jgit/diff/Edit.java Переглянути файл

return Type.REPLACE; return Type.REPLACE;
} }


/** @return true if the edit is empty (lengths of both a and b is zero). */
public final boolean isEmpty() {
return beginA == endA && beginB == endB;
}

/** @return start point in sequence A. */ /** @return start point in sequence A. */
public final int getBeginA() { public final int getBeginA() {
return beginA; return beginA;
return endB; return endB;
} }


/** @return length of the region in A. */
public final int getLengthA() {
return endA - beginA;
}

/** @return length of the region in B. */
public final int getLengthB() {
return endB - beginB;
}

/**
* Construct a new edit representing the region before cut.
*
* @param cut
* the cut point. The beginning A and B points are used as the
* end points of the returned edit.
* @return an edit representing the slice of {@code this} edit that occurs
* before {@code cut} starts.
*/
public final Edit before(Edit cut) {
return new Edit(beginA, cut.beginA, beginB, cut.beginB);
}

/**
* Construct a new edit representing the region after cut.
*
* @param cut
* the cut point. The ending A and B points are used as the
* starting points of the returned edit.
* @return an edit representing the slice of {@code this} edit that occurs
* after {@code cut} ends.
*/
public final Edit after(Edit cut) {
return new Edit(cut.endA, endA, cut.endB, endB);
}

/** Increase {@link #getEndA()} by 1. */ /** Increase {@link #getEndA()} by 1. */
public void extendA() { public void extendA() {
endA++; endA++;

Завантаження…
Відмінити
Зберегти