Search in sources :

Example 1 with DiffTooBigException

use of com.intellij.diff.comparison.DiffTooBigException in project intellij-community by JetBrains.

the class ChangesDiffCalculator method calculateDiff.

public static List<TextRange> calculateDiff(@NotNull Document beforeDocument, @NotNull Document currentDocument) {
    CharSequence beforeText = beforeDocument.getCharsSequence();
    CharSequence currentText = currentDocument.getCharsSequence();
    try {
        ComparisonManager manager = ComparisonManager.getInstance();
        List<LineFragment> lineFragments = manager.compareLinesInner(beforeText, currentText, ComparisonPolicy.DEFAULT, DumbProgressIndicator.INSTANCE);
        List<TextRange> modifiedRanges = new ArrayList<>();
        for (LineFragment lineFragment : lineFragments) {
            int fragmentStartOffset = lineFragment.getStartOffset2();
            int fragmentEndOffset = lineFragment.getEndOffset2();
            List<DiffFragment> innerFragments = lineFragment.getInnerFragments();
            if (innerFragments != null) {
                for (DiffFragment innerFragment : innerFragments) {
                    int innerFragmentStartOffset = fragmentStartOffset + innerFragment.getStartOffset2();
                    int innerFragmentEndOffset = fragmentStartOffset + innerFragment.getEndOffset2();
                    modifiedRanges.add(calculateChangeHighlightRange(currentText, innerFragmentStartOffset, innerFragmentEndOffset));
                }
            } else {
                modifiedRanges.add(calculateChangeHighlightRange(currentText, fragmentStartOffset, fragmentEndOffset));
            }
        }
        return modifiedRanges;
    } catch (DiffTooBigException e) {
        LOG.info(e);
        return Collections.emptyList();
    }
}
Also used : ComparisonManager(com.intellij.diff.comparison.ComparisonManager) LineFragment(com.intellij.diff.fragments.LineFragment) DiffFragment(com.intellij.diff.fragments.DiffFragment) ArrayList(java.util.ArrayList) DiffTooBigException(com.intellij.diff.comparison.DiffTooBigException) TextRange(com.intellij.openapi.util.TextRange)

Example 2 with DiffTooBigException

use of com.intellij.diff.comparison.DiffTooBigException in project intellij-community by JetBrains.

the class UnifiedDiffViewer method performRediff.

@Override
@NotNull
protected Runnable performRediff(@NotNull final ProgressIndicator indicator) {
    try {
        indicator.checkCanceled();
        final Document document1 = getContent1().getDocument();
        final Document document2 = getContent2().getDocument();
        final CharSequence[] texts = ReadAction.compute(() -> {
            return new CharSequence[] { document1.getImmutableCharSequence(), document2.getImmutableCharSequence() };
        });
        final List<LineFragment> fragments = myTextDiffProvider.compare(texts[0], texts[1], indicator);
        final DocumentContent content1 = getContent1();
        final DocumentContent content2 = getContent2();
        indicator.checkCanceled();
        TwosideDocumentData data = ReadAction.compute(() -> {
            indicator.checkCanceled();
            UnifiedFragmentBuilder builder = new UnifiedFragmentBuilder(fragments, document1, document2, myMasterSide);
            builder.exec();
            indicator.checkCanceled();
            EditorHighlighter highlighter = buildHighlighter(myProject, content1, content2, texts[0], texts[1], builder.getRanges(), builder.getText().length());
            UnifiedEditorRangeHighlighter rangeHighlighter = new UnifiedEditorRangeHighlighter(myProject, document1, document2, builder.getRanges());
            return new TwosideDocumentData(builder, highlighter, rangeHighlighter);
        });
        UnifiedFragmentBuilder builder = data.getBuilder();
        FileType fileType = content2.getContentType() == null ? content1.getContentType() : content2.getContentType();
        LineNumberConvertor convertor1 = builder.getConvertor1();
        LineNumberConvertor convertor2 = builder.getConvertor2();
        List<LineRange> changedLines = builder.getChangedLines();
        boolean isContentsEqual = builder.isEqual();
        CombinedEditorData editorData = new CombinedEditorData(builder.getText(), data.getHighlighter(), data.getRangeHighlighter(), fileType, convertor1.createConvertor(), convertor2.createConvertor());
        return apply(editorData, builder.getBlocks(), convertor1, convertor2, changedLines, isContentsEqual);
    } catch (DiffTooBigException e) {
        return () -> {
            clearDiffPresentation();
            myPanel.setTooBigContent();
        };
    } catch (ProcessCanceledException e) {
        throw e;
    } catch (Throwable e) {
        LOG.error(e);
        return () -> {
            clearDiffPresentation();
            myPanel.setErrorContent();
        };
    }
}
Also used : LineFragment(com.intellij.diff.fragments.LineFragment) FileType(com.intellij.openapi.fileTypes.FileType) DocumentContent(com.intellij.diff.contents.DocumentContent) DiffTooBigException(com.intellij.diff.comparison.DiffTooBigException) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 3 with DiffTooBigException

use of com.intellij.diff.comparison.DiffTooBigException in project intellij-community by JetBrains.

the class Block method createPreviousBlock.

@NotNull
public Block createPreviousBlock(@NotNull String[] prevContent) {
    try {
        FairDiffIterable iterable = ByLine.compare(Arrays.asList(prevContent), Arrays.asList(mySource), ComparisonPolicy.IGNORE_WHITESPACES, DumbProgressIndicator.INSTANCE);
        // empty range should not be transferred to the non-empty range
        boolean greedy = myStart != myEnd;
        int start = myStart;
        int end = myEnd;
        int shift = 0;
        for (Range range : iterable.iterateChanges()) {
            int changeStart = range.start2 + shift;
            int changeEnd = range.end2 + shift;
            int changeShift = (range.end1 - range.start1) - (range.end2 - range.start2);
            DiffUtil.UpdatedLineRange updatedRange = DiffUtil.updateRangeOnModification(start, end, changeStart, changeEnd, changeShift, greedy);
            start = updatedRange.startLine;
            end = updatedRange.endLine;
            shift += changeShift;
        }
        if (start < 0 || end > prevContent.length || end < start) {
            LOG.error("Invalid block range: [" + start + ", " + end + "); length - " + prevContent.length);
        }
        // intern strings, reducing memory usage
        for (Range range : iterable.iterateUnchanged()) {
            int count = range.end1 - range.start1;
            for (int i = 0; i < count; i++) {
                int prevIndex = range.start1 + i;
                int sourceIndex = range.start2 + i;
                if (prevContent[prevIndex].equals(mySource[sourceIndex])) {
                    prevContent[prevIndex] = mySource[sourceIndex];
                }
            }
        }
        return new Block(prevContent, start, end);
    } catch (DiffTooBigException e) {
        return new Block(prevContent, 0, 0);
    }
}
Also used : DiffUtil(com.intellij.diff.util.DiffUtil) DiffTooBigException(com.intellij.diff.comparison.DiffTooBigException) FairDiffIterable(com.intellij.diff.comparison.iterables.FairDiffIterable) Range(com.intellij.diff.util.Range) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with DiffTooBigException

use of com.intellij.diff.comparison.DiffTooBigException in project intellij-community by JetBrains.

the class SimpleDiffViewer method performRediff.

@Override
@NotNull
protected Runnable performRediff(@NotNull final ProgressIndicator indicator) {
    try {
        indicator.checkCanceled();
        final Document document1 = getContent1().getDocument();
        final Document document2 = getContent2().getDocument();
        CharSequence[] texts = ReadAction.compute(() -> {
            return new CharSequence[] { document1.getImmutableCharSequence(), document2.getImmutableCharSequence() };
        });
        List<LineFragment> lineFragments = myTextDiffProvider.compare(texts[0], texts[1], indicator);
        boolean isContentsEqual = (lineFragments == null || lineFragments.isEmpty()) && StringUtil.equals(texts[0], texts[1]);
        return apply(new CompareData(lineFragments, isContentsEqual));
    } catch (DiffTooBigException e) {
        return applyNotification(DiffNotifications.createDiffTooBig());
    } catch (ProcessCanceledException e) {
        throw e;
    } catch (Throwable e) {
        LOG.error(e);
        return applyNotification(DiffNotifications.createError());
    }
}
Also used : LineFragment(com.intellij.diff.fragments.LineFragment) DiffTooBigException(com.intellij.diff.comparison.DiffTooBigException) Document(com.intellij.openapi.editor.Document) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) ObjectUtils.assertNotNull(com.intellij.util.ObjectUtils.assertNotNull)

Example 5 with DiffTooBigException

use of com.intellij.diff.comparison.DiffTooBigException in project intellij-community by JetBrains.

the class ApplyPatchChange method calcPatchInnerDifferences.

@Nullable
private static List<DiffFragment> calcPatchInnerDifferences(@NotNull PatchChangeBuilder.Hunk hunk, @NotNull ApplyPatchViewer viewer) {
    LineRange deletionRange = hunk.getPatchDeletionRange();
    LineRange insertionRange = hunk.getPatchInsertionRange();
    if (deletionRange.isEmpty() || insertionRange.isEmpty())
        return null;
    try {
        DocumentEx patchDocument = viewer.getPatchEditor().getDocument();
        CharSequence deleted = DiffUtil.getLinesContent(patchDocument, deletionRange.start, deletionRange.end);
        CharSequence inserted = DiffUtil.getLinesContent(patchDocument, insertionRange.start, insertionRange.end);
        return ByWord.compare(deleted, inserted, ComparisonPolicy.DEFAULT, DumbProgressIndicator.INSTANCE);
    } catch (DiffTooBigException ignore) {
        return null;
    }
}
Also used : DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) DiffTooBigException(com.intellij.diff.comparison.DiffTooBigException) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

DiffTooBigException (com.intellij.diff.comparison.DiffTooBigException)7 LineFragment (com.intellij.diff.fragments.LineFragment)3 ComparisonManager (com.intellij.diff.comparison.ComparisonManager)2 Range (com.intellij.diff.util.Range)2 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 FairDiffIterable (com.intellij.diff.comparison.iterables.FairDiffIterable)1 DocumentContent (com.intellij.diff.contents.DocumentContent)1 DiffFragment (com.intellij.diff.fragments.DiffFragment)1 DiffUtil (com.intellij.diff.util.DiffUtil)1 Document (com.intellij.openapi.editor.Document)1 DocumentEx (com.intellij.openapi.editor.ex.DocumentEx)1 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)1 FileType (com.intellij.openapi.fileTypes.FileType)1 EmptyRunnable (com.intellij.openapi.util.EmptyRunnable)1 TextRange (com.intellij.openapi.util.TextRange)1 VcsException (com.intellij.openapi.vcs.VcsException)1 ObjectUtils.assertNotNull (com.intellij.util.ObjectUtils.assertNotNull)1 ArrayList (java.util.ArrayList)1