use of com.google.startupos.common.Protos.ChangeType 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;
}
use of com.google.startupos.common.Protos.ChangeType in project startup-os by google.
the class TextDifferencer method addWordChanges.
// Adds WordChanges, from segmentStart until segmentEnd (inclusive).
private void addWordChanges(List<DiffLine> leftLines, List<DiffLine> rightLines, int segmentStart, int segmentEnd) {
DiffMatchPatch diffMatchPatch = new DiffMatchPatch();
LinkedList<DiffMatchPatch.Diff> diffs = diffMatchPatch.diff_main(getMultilineText(leftLines, segmentStart, segmentEnd), getMultilineText(rightLines, segmentStart, segmentEnd));
diffMatchPatch.diff_cleanupSemantic(diffs);
// Split multi-lines
int leftLineCounter = 0;
int rightLineCounter = 0;
int leftCharCounter = 0;
int rightCharCounter = 0;
for (DiffMatchPatch.Diff diff : diffs) {
String[] diffLines = diff.text.split("\n");
ChangeType type = getChangeType(diff.operation);
for (int i = 0; i < diffLines.length; i++) {
if (i > 0) {
// This means we had a newline
leftCharCounter = 0;
rightCharCounter = 0;
if (type == ChangeType.DELETE || type == ChangeType.NO_CHANGE) {
leftLineCounter++;
}
if (type == ChangeType.ADD || type == ChangeType.NO_CHANGE) {
rightLineCounter++;
}
}
String line = diffLines[i];
int charCounter = type == ChangeType.DELETE ? leftCharCounter : rightCharCounter;
WordChange wordChange = WordChange.newBuilder().setText(line).setStartIndex(charCounter).setEndIndex(charCounter + line.length()).setType(type).build();
if (type == ChangeType.DELETE) {
int index = segmentStart + leftLineCounter;
if (!wordChangeIsWholeLine(leftLines.get(index), wordChange)) {
leftLines.set(index, leftLines.get(index).toBuilder().addWordChange(wordChange).build());
}
} else if (type == ChangeType.ADD) {
int index = segmentStart + rightLineCounter;
if (!wordChangeIsWholeLine(rightLines.get(index), wordChange)) {
rightLines.set(index, rightLines.get(index).toBuilder().addWordChange(wordChange).build());
}
}
if (type == ChangeType.DELETE || type == ChangeType.NO_CHANGE) {
leftCharCounter += line.length();
}
if (type == ChangeType.ADD || type == ChangeType.NO_CHANGE) {
rightCharCounter += line.length();
}
}
}
}
Aggregations