use of org.eclipse.jgit.diff.Edit in project gerrit by GerritCodeReview.
the class DiffContentCalculator method calculateDiffContent.
/**
* Gather information necessary to display line-by-line difference between 2 texts.
*
* <p>The method returns instance of {@link DiffCalculatorResult} with the following data:
*
* <ul>
* <li>All changed lines
* <li>Additional lines to be displayed above and below the changed lines
* <li>All changed and unchanged lines with comments
* <li>Additional lines to be displayed above and below lines with commentsEdits with special
* "fake" edits for unchanged lines with comments
* </ul>
*
* <p>More details can be found in {@link DiffCalculatorResult}.
*
* @param srcA Original text content
* @param srcB New text content
* @param edits List of edits which was applied to srcA to produce srcB
* @return an instance of {@link DiffCalculatorResult}.
*/
DiffCalculatorResult calculateDiffContent(TextSource srcA, TextSource srcB, ImmutableList<Edit> edits) {
if (srcA.src == srcB.src && edits.isEmpty()) {
// Odd special case; the files are identical (100% rename or copy)
// and the user has asked for context that is larger than the file.
// Send them the entire file, with an empty edit after the last line.
//
SparseFileContentBuilder diffA = new SparseFileContentBuilder(srcA.size());
for (int i = 0; i < srcA.size(); i++) {
srcA.copyLineTo(diffA, i);
}
DiffContent diffContent = new DiffContent(diffA.build(), SparseFileContent.create(ImmutableList.of(), srcB.size()));
Edit emptyEdit = new Edit(srcA.size(), srcA.size());
return new DiffCalculatorResult(diffContent, ImmutableList.of(emptyEdit));
}
ImmutableList<Edit> sortedEdits = correctForDifferencesInNewlineAtEnd(srcA, srcB, edits);
DiffContent diffContent = packContent(srcA, srcB, diffPrefs.ignoreWhitespace != Whitespace.IGNORE_NONE, sortedEdits);
return new DiffCalculatorResult(diffContent, sortedEdits);
}
use of org.eclipse.jgit.diff.Edit in project gerrit by GerritCodeReview.
the class IntraLineDiff method readObject.
private void readObject(ObjectInputStream in) throws IOException {
status = readEnum(in, Status.values());
int editCount = readVarInt32(in);
Edit[] editArray = new Edit[editCount];
for (int i = 0; i < editCount; i++) {
editArray[i] = readEdit(in);
int innerCount = readVarInt32(in);
if (0 < innerCount) {
Edit[] inner = new Edit[innerCount];
for (int j = 0; j < innerCount; j++) {
inner[j] = readEdit(in);
}
editArray[i] = new ReplaceEdit(editArray[i], asList(inner));
}
}
edits = ImmutableList.copyOf(editArray);
}
use of org.eclipse.jgit.diff.Edit in project gerrit by GerritCodeReview.
the class IntraLineLoader method applyEditsToString.
/**
* Apply edits to the {@code target} string. Replace edits are applied to target and replaced with
* a substring from {@code from}. Delete edits are applied to target. Insert edits are removed
* from target.
*
* @return Optional containing the transformed string, or empty if the transformation fails (due
* to index out of bounds).
*/
private static Optional<String> applyEditsToString(StringBuilder target, String from, List<Edit> edits) {
// Process edits right to left to avoid re-computation of indices when characters are removed.
try {
for (int i = edits.size() - 1; i >= 0; i--) {
Edit edit = edits.get(i);
if (edit.getType() == Edit.Type.REPLACE) {
boundaryCheck(target, edit.getBeginA(), edit.getEndA() - 1);
boundaryCheck(from, edit.getBeginB(), edit.getEndB() - 1);
target.replace(edit.getBeginA(), edit.getEndA(), from.substring(edit.getBeginB(), edit.getEndB()));
} else if (edit.getType() == Edit.Type.DELETE) {
boundaryCheck(target, edit.getBeginA(), edit.getEndA() - 1);
target.delete(edit.getBeginA(), edit.getEndA());
} else if (edit.getType() == Edit.Type.INSERT) {
boundaryCheck(target, edit.getBeginB(), edit.getEndB() - 1);
target.delete(edit.getBeginB(), edit.getEndB());
}
}
return Optional.of(target.toString());
} catch (StringIndexOutOfBoundsException unused) {
return Optional.empty();
}
}
use of org.eclipse.jgit.diff.Edit in project gerrit by GerritCodeReview.
the class IntraLineLoader method combineLineEdits.
private static void combineLineEdits(List<Edit> edits, ImmutableSet<Edit> editsDueToRebase, Text a, Text b) {
for (int j = 0; j < edits.size() - 1; ) {
Edit c = edits.get(j);
Edit n = edits.get(j + 1);
if (editsDueToRebase.contains(c) || editsDueToRebase.contains(n)) {
// Don't combine any edits which were identified as being introduced by a rebase as we would
// lose that information because of the combination.
j++;
continue;
}
// Combine edits that are really close together. Right now our rule
// is, coalesce two line edits which are only one line apart if that
// common context line is either a "pointless line", or is identical
// on both sides and starts a new block of code. These are mostly
// block reindents to add or remove control flow operators.
//
final int ad = n.getBeginA() - c.getEndA();
final int bd = n.getBeginB() - c.getEndB();
if ((1 <= ad && isBlankLineGap(a, c.getEndA(), n.getBeginA())) || (1 <= bd && isBlankLineGap(b, c.getEndB(), n.getBeginB())) || (ad == 1 && bd == 1 && isControlBlockStart(a, c.getEndA()))) {
int ab = c.getBeginA();
int ae = n.getEndA();
int bb = c.getBeginB();
int be = n.getEndB();
edits.set(j, new Edit(ab, ae, bb, be));
edits.remove(j + 1);
continue;
}
j++;
}
}
use of org.eclipse.jgit.diff.Edit in project gerrit by GerritCodeReview.
the class PatchListEntry method readEditArray.
private static Edit[] readEditArray(InputStream in) throws IOException {
int numEdits = readVarInt32(in);
Edit[] edits = new Edit[numEdits];
for (int i = 0; i < numEdits; i++) {
int beginA = readVarInt32(in);
int endA = readVarInt32(in);
int beginB = readVarInt32(in);
int endB = readVarInt32(in);
edits[i] = new Edit(beginA, endA, beginB, endB);
}
return edits;
}
Aggregations