Search in sources :

Example 1 with LineFragment

use of com.intellij.diff.fragments.LineFragment in project intellij-community by JetBrains.

the class SvnPropertiesDiffViewer method setupHighlighting.

private void setupHighlighting(@NotNull DiffChange change, @NotNull Side side) {
    PropertyRecord record = change.getRecord();
    List<? extends LineFragment> fragments = change.getFragments();
    assert fragments != null;
    EditorEx editor = getEditor(side);
    DocumentEx document = editor.getDocument();
    int changeStartLine = change.getStartLine(side);
    for (LineFragment fragment : fragments) {
        List<DiffFragment> innerFragments = fragment.getInnerFragments();
        int startLine = side.getStartLine(fragment) + changeStartLine;
        int endLine = side.getEndLine(fragment) + changeStartLine;
        int start = document.getLineStartOffset(startLine);
        TextDiffType type = DiffUtil.getLineDiffType(fragment);
        DiffDrawUtil.createHighlighter(editor, startLine, endLine, type, innerFragments != null);
        // TODO: we can paint LineMarker here, but it looks ugly for small editors
        if (innerFragments != null) {
            for (DiffFragment innerFragment : innerFragments) {
                int innerStart = side.getStartOffset(innerFragment);
                int innerEnd = side.getEndOffset(innerFragment);
                TextDiffType innerType = DiffUtil.getDiffType(innerFragment);
                innerStart += start;
                innerEnd += start;
                DiffDrawUtil.createInlineHighlighter(editor, innerStart, innerEnd, innerType);
            }
        }
    }
}
Also used : DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) LineFragment(com.intellij.diff.fragments.LineFragment) EditorEx(com.intellij.openapi.editor.ex.EditorEx) DiffFragment(com.intellij.diff.fragments.DiffFragment)

Example 2 with LineFragment

use of com.intellij.diff.fragments.LineFragment 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 LineFragment

use of com.intellij.diff.fragments.LineFragment 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 4 with LineFragment

use of com.intellij.diff.fragments.LineFragment in project intellij-community by JetBrains.

the class TwosideTextDiffProviderBase method compare.

@Nullable
@Override
public List<LineFragment> compare(@NotNull CharSequence text1, @NotNull CharSequence text2, @NotNull ProgressIndicator indicator) {
    IgnorePolicy ignorePolicy = getIgnorePolicy();
    HighlightPolicy highlightPolicy = getHighlightPolicy();
    if (!highlightPolicy.isShouldCompare())
        return null;
    ComparisonPolicy policy = ignorePolicy.getComparisonPolicy();
    boolean innerFragments = highlightPolicy.isFineFragments();
    boolean squashFragments = highlightPolicy.isShouldSquash();
    boolean trimFragments = ignorePolicy.isShouldTrimChunks();
    indicator.checkCanceled();
    List<LineFragment> fragments = doCompare(text1, text2, ignorePolicy, innerFragments, indicator);
    indicator.checkCanceled();
    return ComparisonManager.getInstance().processBlocks(fragments, text1, text2, policy, squashFragments, trimFragments);
}
Also used : IgnorePolicy(com.intellij.diff.tools.util.base.IgnorePolicy) LineFragment(com.intellij.diff.fragments.LineFragment) ComparisonPolicy(com.intellij.diff.comparison.ComparisonPolicy) HighlightPolicy(com.intellij.diff.tools.util.base.HighlightPolicy) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with LineFragment

use of com.intellij.diff.fragments.LineFragment in project android by JetBrains.

the class ParcelableQuickFixTest method lineDiff.

private static String lineDiff(String actual, String expected) {
    List<LineFragment> fragments = myComparisonManager.compareLines(actual, expected, ComparisonPolicy.DEFAULT, INDICATOR);
    if (fragments.isEmpty()) {
        return "";
    }
    StringBuilder diff = new StringBuilder();
    diff.append("\n");
    for (LineFragment fragment : fragments) {
        diff.append(String.format("Expected in line: %s >>>\n", fragment.getStartLine2()));
        diff.append(expected.substring(fragment.getStartOffset2(), fragment.getEndOffset2()));
        diff.append("Actual: >>>\n");
        diff.append(actual.substring(fragment.getStartOffset1(), fragment.getEndOffset1()));
        diff.append(">>>\n\n");
    }
    return diff.toString();
}
Also used : LineFragment(com.intellij.diff.fragments.LineFragment)

Aggregations

LineFragment (com.intellij.diff.fragments.LineFragment)9 DiffTooBigException (com.intellij.diff.comparison.DiffTooBigException)3 DiffFragment (com.intellij.diff.fragments.DiffFragment)2 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)2 ComparisonManager (com.intellij.diff.comparison.ComparisonManager)1 ComparisonPolicy (com.intellij.diff.comparison.ComparisonPolicy)1 DocumentContent (com.intellij.diff.contents.DocumentContent)1 HighlightPolicy (com.intellij.diff.tools.util.base.HighlightPolicy)1 IgnorePolicy (com.intellij.diff.tools.util.base.IgnorePolicy)1 Document (com.intellij.openapi.editor.Document)1 DocumentEx (com.intellij.openapi.editor.ex.DocumentEx)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)1 FileType (com.intellij.openapi.fileTypes.FileType)1 TextRange (com.intellij.openapi.util.TextRange)1 ObjectUtils.assertNotNull (com.intellij.util.ObjectUtils.assertNotNull)1 ArrayList (java.util.ArrayList)1 Nullable (org.jetbrains.annotations.Nullable)1