Search in sources :

Example 1 with WordChange

use of com.google.startupos.common.Protos.WordChange 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();
            }
        }
    }
}
Also used : TextDiff(com.google.startupos.common.Protos.TextDiff) ChangeType(com.google.startupos.common.Protos.ChangeType) WordChange(com.google.startupos.common.Protos.WordChange) DiffMatchPatch(com.google.startupos.name.fraser.neil.plaintext.DiffMatchPatch)

Aggregations

ChangeType (com.google.startupos.common.Protos.ChangeType)1 TextDiff (com.google.startupos.common.Protos.TextDiff)1 WordChange (com.google.startupos.common.Protos.WordChange)1 DiffMatchPatch (com.google.startupos.name.fraser.neil.plaintext.DiffMatchPatch)1