Search in sources :

Example 26 with Range

use of com.intellij.diff.util.Range in project intellij-community by JetBrains.

the class TextPatchBuilder method buildModifiedFile.

@Nullable
private TextFilePatch buildModifiedFile(@NotNull AirContentRevision beforeRevision, @NotNull AirContentRevision afterRevision) throws VcsException {
    String beforeContent = getContent(beforeRevision);
    String afterContent = getContent(afterRevision);
    TextFilePatch patch = buildPatchHeading(beforeRevision, afterRevision);
    if (beforeContent.equals(afterContent)) {
        if (beforeRevision.getPath().getPath().equals(afterRevision.getPath().getPath()))
            return null;
        // movement
        patch.addHunk(new PatchHunk(0, 0, 0, 0));
        return patch;
    }
    if (beforeContent.isEmpty()) {
        patch.addHunk(createWholeFileHunk(afterContent, true, true));
        return patch;
    }
    if (afterContent.isEmpty()) {
        patch.addHunk(createWholeFileHunk(beforeContent, false, true));
        return patch;
    }
    List<String> beforeLines = tokenize(beforeContent);
    List<String> afterLines = tokenize(afterContent);
    boolean beforeNoNewlineAtEOF = !beforeContent.endsWith("\n");
    boolean afterNoNewlineAtEOF = !afterContent.endsWith("\n");
    List<Range> fragments;
    try {
        fragments = compareLines(beforeLines, afterLines, beforeNoNewlineAtEOF, afterNoNewlineAtEOF);
    } catch (DiffTooBigException e) {
        throw new VcsException("File '" + myBasePath + "' is too big and there are too many changes to build diff", e);
    }
    int hunkStart = 0;
    while (hunkStart < fragments.size()) {
        List<Range> hunkFragments = getAdjacentFragments(fragments, hunkStart);
        patch.addHunk(createHunk(hunkFragments, beforeLines, afterLines, beforeNoNewlineAtEOF, afterNoNewlineAtEOF));
        hunkStart += hunkFragments.size();
    }
    return patch;
}
Also used : VcsException(com.intellij.openapi.vcs.VcsException) DiffTooBigException(com.intellij.diff.comparison.DiffTooBigException) Range(com.intellij.diff.util.Range) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Range (com.intellij.diff.util.Range)26 NotNull (org.jetbrains.annotations.NotNull)21 MergeRange (com.intellij.diff.util.MergeRange)8 ArrayList (java.util.ArrayList)7 TextRange (com.intellij.openapi.util.TextRange)5 FairDiffIterable (com.intellij.diff.comparison.iterables.FairDiffIterable)3 Iterator (java.util.Iterator)3 DiffTooBigException (com.intellij.diff.comparison.DiffTooBigException)2 DiffIterable (com.intellij.diff.comparison.iterables.DiffIterable)2 DiffIterableUtil (com.intellij.diff.comparison.iterables.DiffIterableUtil)2 DiffUtil (com.intellij.diff.util.DiffUtil)2 IntPair (com.intellij.diff.util.IntPair)2 TIntArrayList (gnu.trove.TIntArrayList)2 Nullable (org.jetbrains.annotations.Nullable)2 WordBlock (com.intellij.diff.comparison.LineFragmentSplitter.WordBlock)1 DiffIterableUtil.fair (com.intellij.diff.comparison.iterables.DiffIterableUtil.fair)1 com.intellij.diff.fragments (com.intellij.diff.fragments)1 DiffFragment (com.intellij.diff.fragments.DiffFragment)1 Side (com.intellij.diff.util.Side)1 Logger (com.intellij.openapi.diagnostic.Logger)1