Search in sources :

Example 1 with DiffFragment

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

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

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

the class LineStatusMarkerPopup method createEditorComponent.

@Nullable
private EditorFragmentComponent createEditorComponent(@Nullable FileType fileType, @Nullable List<DiffFragment> wordDiff) {
    if (myRange.getType() == Range.INSERTED)
        return null;
    EditorEx uEditor = (EditorEx) EditorFactory.getInstance().createViewer(myTracker.getVcsDocument(), myTracker.getProject());
    uEditor.setColorsScheme(myEditor.getColorsScheme());
    DiffUtil.setEditorCodeStyle(myTracker.getProject(), uEditor, fileType);
    EditorHighlighterFactory highlighterFactory = EditorHighlighterFactory.getInstance();
    uEditor.setHighlighter(highlighterFactory.createEditorHighlighter(myTracker.getProject(), getFileName(myTracker.getDocument())));
    if (wordDiff != null) {
        int vcsStartShift = myTracker.getVcsTextRange(myRange).getStartOffset();
        for (DiffFragment fragment : wordDiff) {
            int vcsStart = vcsStartShift + fragment.getStartOffset1();
            int vcsEnd = vcsStartShift + fragment.getEndOffset1();
            TextDiffType type = getDiffType(fragment);
            DiffDrawUtil.createInlineHighlighter(uEditor, vcsStart, vcsEnd, type);
        }
    }
    EditorFragmentComponent fragmentComponent = EditorFragmentComponent.createEditorFragmentComponent(myEditor.getContentComponent(), uEditor, myRange.getVcsLine1(), myRange.getVcsLine2(), false, false);
    EditorFactory.getInstance().releaseEditor(uEditor);
    return fragmentComponent;
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) DiffFragment(com.intellij.diff.fragments.DiffFragment) EditorFragmentComponent(com.intellij.codeInsight.hint.EditorFragmentComponent) TextDiffType(com.intellij.diff.util.TextDiffType) EditorHighlighterFactory(com.intellij.openapi.editor.highlighter.EditorHighlighterFactory) HintHint(com.intellij.ui.HintHint) LightweightHint(com.intellij.ui.LightweightHint) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with DiffFragment

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

the class SimpleDiffChange method doInstallHighlighterWithInner.

private void doInstallHighlighterWithInner() {
    assert myInnerFragments != null;
    createHighlighter(Side.LEFT, true);
    createHighlighter(Side.RIGHT, true);
    for (DiffFragment fragment : myInnerFragments) {
        createInlineHighlighter(fragment, Side.LEFT);
        createInlineHighlighter(fragment, Side.RIGHT);
    }
}
Also used : DiffFragment(com.intellij.diff.fragments.DiffFragment)

Example 5 with DiffFragment

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

the class ByWord method compareAndSplit.

@NotNull
public static List<LineBlock> compareAndSplit(@NotNull CharSequence text1, @NotNull CharSequence text2, @NotNull ComparisonPolicy policy, @NotNull ProgressIndicator indicator) {
    indicator.checkCanceled();
    // TODO: figure out, what do we exactly want from 'Split' logic
    // -- it is used for trimming of ignored blocks. So we want whitespace-only leading/trailing lines to be separate block.
    // -- old approach: split by matched '\n's
    // TODO: other approach could lead to better results:
    // * Compare words-only
    // * prefer big chunks
    // -- here we can try to minimize number of matched pairs 'pair[i]' and 'pair[i+1]' such that
    //    containsNewline(pair[i].left .. pair[i+1].left) XOR containsNewline(pair[i].right .. pair[i+1].right) == true
    //    ex: "A X C" - "A Y C \n M C" - do not match with last 'C'
    //    ex: "A \n" - "A B \n \n" - do not match with last '\n'
    //    Try some greedy approach ?
    // * split into blocks
    // -- squash blocks with too small unchanged words count (1 matched word out of 40 - is a bad reason to create new block)
    // * match adjustment punctuation
    // * match adjustment whitespaces ('\n' are matched here)
    List<InlineChunk> words1 = getInlineChunks(text1);
    List<InlineChunk> words2 = getInlineChunks(text2);
    FairDiffIterable wordChanges = diff(words1, words2, indicator);
    wordChanges = optimizeWordChunks(text1, text2, words1, words2, wordChanges, indicator);
    List<WordBlock> wordBlocks = new LineFragmentSplitter(text1, text2, words1, words2, wordChanges, indicator).run();
    List<LineBlock> lineBlocks = new ArrayList<>(wordBlocks.size());
    for (WordBlock block : wordBlocks) {
        Range offsets = block.offsets;
        Range words = block.words;
        CharSequence subtext1 = text1.subSequence(offsets.start1, offsets.end1);
        CharSequence subtext2 = text2.subSequence(offsets.start2, offsets.end2);
        List<InlineChunk> subwords1 = words1.subList(words.start1, words.end1);
        List<InlineChunk> subwords2 = words2.subList(words.start2, words.end2);
        FairDiffIterable subiterable = fair(subiterable(wordChanges, words.start1, words.end1, words.start2, words.end2));
        FairDiffIterable delimitersIterable = matchAdjustmentDelimiters(subtext1, subtext2, subwords1, subwords2, subiterable, offsets.start1, offsets.start2, indicator);
        DiffIterable iterable = matchAdjustmentWhitespaces(subtext1, subtext2, delimitersIterable, policy, indicator);
        List<DiffFragment> fragments = convertIntoDiffFragments(iterable);
        int newlines1 = countNewlines(subwords1);
        int newlines2 = countNewlines(subwords2);
        lineBlocks.add(new LineBlock(fragments, offsets, newlines1, newlines2));
    }
    return lineBlocks;
}
Also used : WordBlock(com.intellij.diff.comparison.LineFragmentSplitter.WordBlock) ArrayList(java.util.ArrayList) MergingCharSequence(com.intellij.util.text.MergingCharSequence) FairDiffIterable(com.intellij.diff.comparison.iterables.FairDiffIterable) MergeRange(com.intellij.diff.util.MergeRange) Range(com.intellij.diff.util.Range) FairDiffIterable(com.intellij.diff.comparison.iterables.FairDiffIterable) DiffIterable(com.intellij.diff.comparison.iterables.DiffIterable) DiffFragment(com.intellij.diff.fragments.DiffFragment) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DiffFragment (com.intellij.diff.fragments.DiffFragment)9 ArrayList (java.util.ArrayList)4 HintHint (com.intellij.ui.HintHint)3 LightweightHint (com.intellij.ui.LightweightHint)3 LineFragment (com.intellij.diff.fragments.LineFragment)2 TextDiffType (com.intellij.diff.util.TextDiffType)2 Disposable (com.intellij.openapi.Disposable)2 EditorEx (com.intellij.openapi.editor.ex.EditorEx)2 NotNull (org.jetbrains.annotations.NotNull)2 EditorFragmentComponent (com.intellij.codeInsight.hint.EditorFragmentComponent)1 ComparisonManager (com.intellij.diff.comparison.ComparisonManager)1 DiffTooBigException (com.intellij.diff.comparison.DiffTooBigException)1 WordBlock (com.intellij.diff.comparison.LineFragmentSplitter.WordBlock)1 DiffIterable (com.intellij.diff.comparison.iterables.DiffIterable)1 FairDiffIterable (com.intellij.diff.comparison.iterables.FairDiffIterable)1 MergeRange (com.intellij.diff.util.MergeRange)1 Range (com.intellij.diff.util.Range)1 DocumentEx (com.intellij.openapi.editor.ex.DocumentEx)1 EditorHighlighterFactory (com.intellij.openapi.editor.highlighter.EditorHighlighterFactory)1 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)1