use of com.google.startupos.common.Protos.DiffLine in project startup-os by google.
the class TextDifferencer method getTextDiff.
public TextDiff getTextDiff(String leftText, String rightText, String diffString) {
TextDiff.Builder result = TextDiff.newBuilder().setLeftFileContents(leftText).setRightFileContents(rightText);
if (diffString.isEmpty()) {
return result.build();
}
String[] diffLines = diffString.split("\n");
List<DiffLine> leftLines = new ArrayList();
List<DiffLine> rightLines = new ArrayList();
int leftCodeLineNumber = getLeftStartLine(diffLines[0]);
int leftDiffLineNumber = leftCodeLineNumber;
int rightCodeLineNumber = getRightStartLine(diffLines[0]);
int rightDiffLineNumber = rightCodeLineNumber;
for (int diffIndex = 1; diffIndex < diffLines.length; diffIndex++) {
if (diffLines[diffIndex].charAt(0) == '\\') {
// This is not a real code line, probably "\ No newline at end of file".
continue;
}
ChangeType type = CHANGE_TYPE_MAP.get(diffLines[diffIndex].charAt(0));
if (type == null) {
throw new IllegalStateException("Diff line " + diffIndex + " does not start with a diff character (+- ):\n" + diffLines[diffIndex] + "\nFor diffString:\n" + diffString);
}
if (type == ChangeType.NO_CHANGE) {
// On NO_CHANGE lines, we know that both diff line indices should be the same. We add
// placeholder DiffLines to fill in the gaps:
fillPlaceholderGap(leftLines, rightLines, leftDiffLineNumber, rightDiffLineNumber);
// Now gap is filled, so set both line numbers to the maximum:
leftDiffLineNumber = Math.max(leftDiffLineNumber, rightDiffLineNumber);
rightDiffLineNumber = leftDiffLineNumber;
// On to the next line:
leftCodeLineNumber++;
rightCodeLineNumber++;
leftDiffLineNumber++;
rightDiffLineNumber++;
continue;
}
String text = diffLines[diffIndex].substring(1);
int codeLineNumber = type == ChangeType.DELETE ? leftCodeLineNumber : rightCodeLineNumber;
int diffLineNumber = type == ChangeType.DELETE ? leftDiffLineNumber : rightDiffLineNumber;
DiffLine diffLine = DiffLine.newBuilder().setText(text).setType(type).setCodeLineNumber(codeLineNumber).setDiffLineNumber(diffLineNumber).build();
if (type == ChangeType.DELETE) {
leftLines.add(diffLine);
leftCodeLineNumber++;
leftDiffLineNumber++;
} else {
rightLines.add(diffLine);
rightCodeLineNumber++;
rightDiffLineNumber++;
}
}
// Fill any last section:
fillPlaceholderGap(leftLines, rightLines, leftDiffLineNumber, rightDiffLineNumber);
addWordChanges(leftLines, rightLines);
result.addAllLeftDiffLine(leftLines);
result.addAllRightDiffLine(rightLines);
TextDiff textDiff = result.build();
return textDiff;
}
Aggregations