Search in sources :

Example 6 with DiffFragment

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

the class DiffDrawUtil method createUnifiedChunkHighlighters.

//
// Highlighters
//
// TODO: desync of range and 'border' line markers on typing
@NotNull
public static List<RangeHighlighter> createUnifiedChunkHighlighters(@NotNull Editor editor, @NotNull LineRange deleted, @NotNull LineRange inserted, @Nullable List<DiffFragment> innerFragments) {
    boolean ignored = innerFragments != null;
    List<RangeHighlighter> list = new ArrayList<>();
    if (!inserted.isEmpty() && !deleted.isEmpty()) {
        list.addAll(createHighlighter(editor, deleted.start, deleted.end, TextDiffType.DELETED, ignored));
        list.addAll(createHighlighter(editor, inserted.start, inserted.end, TextDiffType.INSERTED, ignored));
    } else if (!inserted.isEmpty()) {
        list.addAll(createHighlighter(editor, inserted.start, inserted.end, TextDiffType.INSERTED, ignored));
    } else if (!deleted.isEmpty()) {
        list.addAll(createHighlighter(editor, deleted.start, deleted.end, TextDiffType.DELETED, ignored));
    }
    if (innerFragments != null) {
        int deletedStartOffset = editor.getDocument().getLineStartOffset(deleted.start);
        int insertedStartOffset = editor.getDocument().getLineStartOffset(inserted.start);
        for (DiffFragment fragment : innerFragments) {
            int deletedWordStart = deletedStartOffset + fragment.getStartOffset1();
            int deletedWordEnd = deletedStartOffset + fragment.getEndOffset1();
            list.addAll(createInlineHighlighter(editor, deletedWordStart, deletedWordEnd, TextDiffType.DELETED));
            int insertedWordStart = insertedStartOffset + fragment.getStartOffset2();
            int insertedWordEnd = insertedStartOffset + fragment.getEndOffset2();
            list.addAll(createInlineHighlighter(editor, insertedWordStart, insertedWordEnd, TextDiffType.INSERTED));
        }
    }
    return list;
}
Also used : DiffFragment(com.intellij.diff.fragments.DiffFragment) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with DiffFragment

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

the class LineStatusMarkerPopup method showHintAt.

public void showHintAt(@Nullable Point mousePosition) {
    if (!myTracker.isValid())
        return;
    final Disposable disposable = Disposer.newDisposable();
    FileType fileType = getFileType();
    List<DiffFragment> wordDiff = computeWordDiff();
    installMasterEditorHighlighters(wordDiff, disposable);
    JComponent editorComponent = createEditorComponent(fileType, wordDiff);
    ActionToolbar toolbar = buildToolbar(mousePosition, disposable);
    // we need valid ActionToolbar.getPreferredSize() to calc size of popup
    toolbar.updateActionsImmediately();
    toolbar.setReservePlaceAutoPopupIcon(false);
    PopupPanel popupPanel = new PopupPanel(myEditor, toolbar, editorComponent);
    LightweightHint hint = new LightweightHint(popupPanel);
    HintListener closeListener = new HintListener() {

        public void hintHidden(final EventObject event) {
            Disposer.dispose(disposable);
        }
    };
    hint.addHintListener(closeListener);
    int line = myEditor.getCaretModel().getLogicalPosition().line;
    Point point = HintManagerImpl.getHintPosition(hint, myEditor, new LogicalPosition(line, 0), HintManager.UNDER);
    if (mousePosition != null) {
        // show right after the nearest line
        int lineHeight = myEditor.getLineHeight();
        int delta = (point.y - mousePosition.y) % lineHeight;
        if (delta < 0)
            delta += lineHeight;
        point.y = mousePosition.y + delta;
    }
    // align main editor with the one in popup
    point.x -= popupPanel.getEditorTextOffset();
    int flags = HintManager.HIDE_BY_ANY_KEY | HintManager.HIDE_BY_TEXT_CHANGE | HintManager.HIDE_BY_SCROLLING;
    HintManagerImpl.getInstanceImpl().showEditorHint(hint, myEditor, point, flags, -1, false, new HintHint(myEditor, point));
    if (!hint.isVisible()) {
        closeListener.hintHidden(null);
    }
}
Also used : Disposable(com.intellij.openapi.Disposable) LightweightHint(com.intellij.ui.LightweightHint) EventObject(java.util.EventObject) HintHint(com.intellij.ui.HintHint) LightweightHint(com.intellij.ui.LightweightHint) HintListener(com.intellij.ui.HintListener) HintHint(com.intellij.ui.HintHint) DiffFragment(com.intellij.diff.fragments.DiffFragment) PlainTextFileType(com.intellij.openapi.fileTypes.PlainTextFileType) FileType(com.intellij.openapi.fileTypes.FileType)

Example 8 with DiffFragment

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

the class LineStatusMarkerPopup method installMasterEditorHighlighters.

private void installMasterEditorHighlighters(@Nullable List<DiffFragment> wordDiff, @NotNull Disposable parentDisposable) {
    if (wordDiff == null)
        return;
    final List<RangeHighlighter> highlighters = new ArrayList<>();
    int currentStartShift = myTracker.getCurrentTextRange(myRange).getStartOffset();
    for (DiffFragment fragment : wordDiff) {
        int currentStart = currentStartShift + fragment.getStartOffset2();
        int currentEnd = currentStartShift + fragment.getEndOffset2();
        TextDiffType type = getDiffType(fragment);
        highlighters.addAll(DiffDrawUtil.createInlineHighlighter(myEditor, currentStart, currentEnd, type));
    }
    Disposer.register(parentDisposable, new Disposable() {

        @Override
        public void dispose() {
            for (RangeHighlighter highlighter : highlighters) {
                highlighter.dispose();
            }
        }
    });
}
Also used : Disposable(com.intellij.openapi.Disposable) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) DiffFragment(com.intellij.diff.fragments.DiffFragment) ArrayList(java.util.ArrayList) TextDiffType(com.intellij.diff.util.TextDiffType) HintHint(com.intellij.ui.HintHint) LightweightHint(com.intellij.ui.LightweightHint)

Example 9 with DiffFragment

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

the class DiffUtil method compareThreesideInner.

@Nullable
public static MergeInnerDifferences compareThreesideInner(@NotNull List<CharSequence> chunks, @NotNull ComparisonPolicy comparisonPolicy, @NotNull ProgressIndicator indicator) {
    // ---
    if (chunks.get(0) == null && chunks.get(1) == null && chunks.get(2) == null)
        return null;
    if (comparisonPolicy == ComparisonPolicy.IGNORE_WHITESPACES) {
        if (isChunksEquals(chunks.get(0), chunks.get(1), comparisonPolicy) && isChunksEquals(chunks.get(0), chunks.get(2), comparisonPolicy)) {
            // whitespace-only changes, ex: empty lines added/removed
            return new MergeInnerDifferences(Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
        }
    }
    if (chunks.get(0) == null && chunks.get(1) == null || chunks.get(0) == null && chunks.get(2) == null || chunks.get(1) == null && chunks.get(2) == null) {
        // =--, -=-, --=
        return null;
    }
    if (chunks.get(0) != null && chunks.get(1) != null && chunks.get(2) != null) {
        // ===
        List<DiffFragment> fragments1 = ByWord.compare(chunks.get(1), chunks.get(0), comparisonPolicy, indicator);
        List<DiffFragment> fragments2 = ByWord.compare(chunks.get(1), chunks.get(2), comparisonPolicy, indicator);
        List<TextRange> left = new ArrayList<>();
        List<TextRange> base = new ArrayList<>();
        List<TextRange> right = new ArrayList<>();
        for (DiffFragment wordFragment : fragments1) {
            base.add(new TextRange(wordFragment.getStartOffset1(), wordFragment.getEndOffset1()));
            left.add(new TextRange(wordFragment.getStartOffset2(), wordFragment.getEndOffset2()));
        }
        for (DiffFragment wordFragment : fragments2) {
            base.add(new TextRange(wordFragment.getStartOffset1(), wordFragment.getEndOffset1()));
            right.add(new TextRange(wordFragment.getStartOffset2(), wordFragment.getEndOffset2()));
        }
        return new MergeInnerDifferences(left, base, right);
    }
    // ==-, =-=, -==
    final ThreeSide side1 = chunks.get(0) != null ? ThreeSide.LEFT : ThreeSide.BASE;
    final ThreeSide side2 = chunks.get(2) != null ? ThreeSide.RIGHT : ThreeSide.BASE;
    CharSequence chunk1 = side1.select(chunks);
    CharSequence chunk2 = side2.select(chunks);
    List<DiffFragment> wordConflicts = ByWord.compare(chunk1, chunk2, comparisonPolicy, indicator);
    List<List<TextRange>> textRanges = ThreeSide.map(side -> {
        if (side == side1) {
            return ContainerUtil.map(wordConflicts, fragment -> new TextRange(fragment.getStartOffset1(), fragment.getEndOffset1()));
        }
        if (side == side2) {
            return ContainerUtil.map(wordConflicts, fragment -> new TextRange(fragment.getStartOffset2(), fragment.getEndOffset2()));
        }
        return null;
    });
    return new MergeInnerDifferences(textRanges.get(0), textRanges.get(1), textRanges.get(2));
}
Also used : DiffFragment(com.intellij.diff.fragments.DiffFragment) List(java.util.List)

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