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();
}
}
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();
};
}
}
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);
}
}
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());
}
}
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;
}
}
Aggregations