Search in sources :

Example 1 with LineFragment

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

the class TextCompareProcessor method findSubFragments.

private ArrayList<LineFragment> findSubFragments(@NotNull DiffString text1, @NotNull DiffString text2) throws FilesTooBigForDiffException {
    DiffFragment[] fragments = new ByWord(myComparisonPolicy).buildFragments(text1, text2);
    fragments = DiffCorrection.ConnectSingleSideToChange.INSTANCE.correct(fragments);
    fragments = UniteSameType.INSTANCE.correct(fragments);
    fragments = PreferWholeLines.INSTANCE.correct(fragments);
    fragments = UniteSameType.INSTANCE.correct(fragments);
    DiffFragment[][] lines = Util.splitByUnchangedLines(fragments);
    lines = Util.uniteFormattingOnly(lines);
    LineFragmentsCollector collector = new LineFragmentsCollector();
    for (DiffFragment[] line : lines) {
        DiffFragment[][] subLines = LineBlockDivider.SINGLE_SIDE.divide(line);
        subLines = Util.uniteFormattingOnly(subLines);
        for (DiffFragment[] subLineFragments : subLines) {
            LineFragment subLine = collector.addDiffFragment(Util.concatenate(subLineFragments));
            if (!subLine.isOneSide()) {
                subLine.setChildren(processInlineFragments(subLineFragments));
            }
        }
    }
    return collector.getFragments();
}
Also used : LineFragment(com.intellij.openapi.diff.impl.fragments.LineFragment) DiffFragment(com.intellij.openapi.diff.ex.DiffFragment)

Example 2 with LineFragment

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

the class TextCompareProcessor method process.

public List<LineFragment> process(@Nullable String text1, @Nullable String text2) throws FilesTooBigForDiffException {
    if (myHighlightMode == HighlightMode.NO_HIGHLIGHTING) {
        return Collections.emptyList();
    }
    text1 = StringUtil.notNullize(text1);
    text2 = StringUtil.notNullize(text2);
    if (text1.isEmpty() || text2.isEmpty()) {
        return new DummyDiffFragmentsProcessor().process(text1, text2);
    }
    DiffString diffText1 = DiffString.create(text1);
    DiffString diffText2 = DiffString.create(text2);
    DiffFragment[] woFormattingBlocks = myDiffPolicy.buildFragments(diffText1, diffText2);
    DiffFragment[] step1lineFragments = new DiffCorrection.TrueLineBlocks(myComparisonPolicy).correctAndNormalize(woFormattingBlocks);
    ArrayList<LineFragment> lineBlocks = new DiffFragmentsProcessor().process(step1lineFragments);
    int badLinesCount = 0;
    if (myHighlightMode == HighlightMode.BY_WORD) {
        for (LineFragment lineBlock : lineBlocks) {
            if (lineBlock.isOneSide() || lineBlock.isEqual())
                continue;
            try {
                DiffString subText1 = lineBlock.getText(diffText1, FragmentSide.SIDE1);
                DiffString subText2 = lineBlock.getText(diffText2, FragmentSide.SIDE2);
                ArrayList<LineFragment> subFragments = findSubFragments(subText1, subText2);
                lineBlock.setChildren(new ArrayList<Fragment>(subFragments));
                lineBlock.adjustTypeFromChildrenTypes();
            } catch (FilesTooBigForDiffException ignore) {
                // If we can't by-word compare two lines - this is not a reason to break entire diff.
                badLinesCount++;
                if (badLinesCount > FilesTooBigForDiffException.MAX_BAD_LINES)
                    break;
            }
        }
    }
    return lineBlocks;
}
Also used : FilesTooBigForDiffException(com.intellij.util.diff.FilesTooBigForDiffException) DiffFragment(com.intellij.openapi.diff.ex.DiffFragment) Fragment(com.intellij.openapi.diff.impl.fragments.Fragment) LineFragment(com.intellij.openapi.diff.impl.fragments.LineFragment) LineFragment(com.intellij.openapi.diff.impl.fragments.LineFragment) DiffFragment(com.intellij.openapi.diff.ex.DiffFragment) DiffString(com.intellij.openapi.diff.impl.string.DiffString)

Example 3 with LineFragment

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

the class DummyDiffFragmentsProcessor method process.

public ArrayList<LineFragment> process(String text1, String text2) {
    ArrayList<LineFragment> lineFragments = new ArrayList<LineFragment>();
    if (text1.isEmpty() && text2.isEmpty()) {
        return lineFragments;
    }
    TextDiffTypeEnum type;
    if (text1.isEmpty()) {
        type = TextDiffTypeEnum.INSERT;
    } else if (text2.isEmpty()) {
        type = TextDiffTypeEnum.DELETED;
    } else {
        type = TextDiffTypeEnum.CHANGED;
    }
    lineFragments.add(new LineFragment(0, countLines(text1), 0, countLines(text2), type, new TextRange(0, text1.length()), new TextRange(0, text2.length())));
    return lineFragments;
}
Also used : LineFragment(com.intellij.openapi.diff.impl.fragments.LineFragment) TextDiffTypeEnum(com.intellij.openapi.diff.impl.util.TextDiffTypeEnum) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange)

Example 4 with LineFragment

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

the class LineFragmentsCollector method addFragment.

@NotNull
private LineFragment addFragment(@Nullable TextDiffTypeEnum type, @Nullable DiffString text1, @Nullable DiffString text2) {
    int lines1 = countLines(text1);
    int lines2 = countLines(text2);
    int endOffset1 = myOffset1 + getLength(text1);
    int endOffset2 = myOffset2 + getLength(text2);
    LineFragment lineFragment = new LineFragment(myLine1, lines1, myLine2, lines2, type, new TextRange(myOffset1, endOffset1), new TextRange(myOffset2, endOffset2));
    myLine1 += lines1;
    myLine2 += lines2;
    myOffset1 = endOffset1;
    myOffset2 = endOffset2;
    myLineFragments.add(lineFragment);
    return lineFragment;
}
Also used : LineFragment(com.intellij.openapi.diff.impl.fragments.LineFragment) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with LineFragment

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

the class DiffMarkup method highlightText.

public void highlightText(@NotNull Fragment fragment, @Nullable GutterIconRenderer gutterIconRenderer) {
    MarkupModel markupModel = getMarkupModel();
    EditorEx editor = getEditor();
    TextDiffTypeEnum diffTypeEnum = fragment.getType();
    if (diffTypeEnum == null || markupModel == null || editor == null) {
        return;
    }
    TextDiffType type = fragment instanceof LineFragment ? DiffUtil.makeTextDiffType((LineFragment) fragment) : TextDiffType.create(diffTypeEnum);
    final TextRange range = fragment.getRange(getSide());
    final TextAttributes attributes = type.getTextAttributes(editor);
    if (attributes == null) {
        return;
    }
    RangeHighlighter rangeMarker;
    if (range.getLength() == 0) {
        final int offset = range.getStartOffset();
        rangeMarker = markupModel.addRangeHighlighter(offset, offset, LAYER, attributes, HighlighterTargetArea.EXACT_RANGE);
        rangeMarker.setCustomRenderer(new CustomHighlighterRenderer() {

            @Override
            public void paint(@NotNull Editor ed, @NotNull RangeHighlighter highlighter, @NotNull Graphics g) {
                g.setColor(attributes.getBackgroundColor());
                Point point = ed.logicalPositionToXY(ed.offsetToLogicalPosition(highlighter.getStartOffset()));
                int endy = point.y + ed.getLineHeight() - 1;
                g.drawLine(point.x, point.y, point.x, endy);
                g.drawLine(point.x - 1, point.y, point.x - 1, endy);
            }
        });
    } else {
        rangeMarker = markupModel.addRangeHighlighter(range.getStartOffset(), range.getEndOffset(), LAYER, attributes, HighlighterTargetArea.EXACT_RANGE);
    }
    if (gutterIconRenderer != null) {
        rangeMarker.setGutterIconRenderer(gutterIconRenderer);
    }
    setLineMarkerRenderer(rangeMarker, fragment, type);
    rangeMarker.setThinErrorStripeMark(true);
    if (DiffUtil.isInlineWrapper(fragment)) {
        rangeMarker.setErrorStripeMarkColor(null);
    }
    saveHighlighter(rangeMarker);
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) TextDiffTypeEnum(com.intellij.openapi.diff.impl.util.TextDiffTypeEnum) TextDiffType(com.intellij.openapi.diff.impl.util.TextDiffType) TextRange(com.intellij.openapi.util.TextRange) LineFragment(com.intellij.openapi.diff.impl.fragments.LineFragment) Editor(com.intellij.openapi.editor.Editor)

Aggregations

LineFragment (com.intellij.openapi.diff.impl.fragments.LineFragment)6 TextRange (com.intellij.openapi.util.TextRange)3 DiffFragment (com.intellij.openapi.diff.ex.DiffFragment)2 TextDiffTypeEnum (com.intellij.openapi.diff.impl.util.TextDiffTypeEnum)2 ArrayList (java.util.ArrayList)2 Fragment (com.intellij.openapi.diff.impl.fragments.Fragment)1 FragmentHighlighterImpl (com.intellij.openapi.diff.impl.fragments.FragmentHighlighterImpl)1 DiffString (com.intellij.openapi.diff.impl.string.DiffString)1 TextDiffType (com.intellij.openapi.diff.impl.util.TextDiffType)1 Editor (com.intellij.openapi.editor.Editor)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1 FilesTooBigForDiffException (com.intellij.util.diff.FilesTooBigForDiffException)1 NotNull (org.jetbrains.annotations.NotNull)1