Search in sources :

Example 1 with SparseFileContentBuilder

use of com.google.gerrit.prettify.common.SparseFileContentBuilder in project gerrit by GerritCodeReview.

the class DiffContentCalculator method packContent.

private DiffContent packContent(TextSource a, TextSource b, boolean ignoredWhitespace, ImmutableList<Edit> edits) {
    SparseFileContentBuilder diffA = new SparseFileContentBuilder(a.size());
    SparseFileContentBuilder diffB = new SparseFileContentBuilder(b.size());
    if (!edits.isEmpty()) {
        EditHunk hunk = new EditHunk(edits, a.size(), b.size());
        while (hunk.next()) {
            if (hunk.isUnmodifiedLine()) {
                String lineA = a.getSourceLine(hunk.getCurA());
                diffA.addLine(hunk.getCurA(), lineA);
                if (ignoredWhitespace) {
                    // If we ignored whitespace in some form, also get the line
                    // from b when it does not exactly match the line from a.
                    // 
                    String lineB = b.getSourceLine(hunk.getCurB());
                    if (!lineA.equals(lineB)) {
                        diffB.addLine(hunk.getCurB(), lineB);
                    }
                }
                hunk.incBoth();
                continue;
            }
            if (hunk.isDeletedA()) {
                a.copyLineTo(diffA, hunk.getCurA());
                hunk.incA();
            }
            if (hunk.isInsertedB()) {
                b.copyLineTo(diffB, hunk.getCurB());
                hunk.incB();
            }
        }
    }
    return new DiffContent(diffA.build(), diffB.build());
}
Also used : SparseFileContentBuilder(com.google.gerrit.prettify.common.SparseFileContentBuilder) EditHunk(com.google.gerrit.prettify.common.EditHunk)

Example 2 with SparseFileContentBuilder

use of com.google.gerrit.prettify.common.SparseFileContentBuilder 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);
}
Also used : SparseFileContentBuilder(com.google.gerrit.prettify.common.SparseFileContentBuilder) Edit(org.eclipse.jgit.diff.Edit) ReplaceEdit(com.google.gerrit.jgit.diff.ReplaceEdit)

Aggregations

SparseFileContentBuilder (com.google.gerrit.prettify.common.SparseFileContentBuilder)2 ReplaceEdit (com.google.gerrit.jgit.diff.ReplaceEdit)1 EditHunk (com.google.gerrit.prettify.common.EditHunk)1 Edit (org.eclipse.jgit.diff.Edit)1