Search in sources :

Example 1 with FilesTooBigForDiffException

use of com.intellij.util.diff.FilesTooBigForDiffException in project intellij-community by JetBrains.

the class RearrangeCodeProcessor method prepareTask.

@NotNull
@Override
protected FutureTask<Boolean> prepareTask(@NotNull final PsiFile file, final boolean processChangedTextOnly) {
    return new FutureTask<>(() -> {
        try {
            Collection<TextRange> ranges = getRangesToFormat(file, processChangedTextOnly);
            Document document = PsiDocumentManager.getInstance(myProject).getDocument(file);
            if (document != null && Rearranger.EXTENSION.forLanguage(file.getLanguage()) != null) {
                PsiDocumentManager.getInstance(myProject).doPostponedOperationsAndUnblockDocument(document);
                PsiDocumentManager.getInstance(myProject).commitDocument(document);
                Runnable command = prepareRearrangeCommand(file, ranges);
                try {
                    CommandProcessor.getInstance().executeCommand(myProject, command, COMMAND_NAME, null);
                } finally {
                    PsiDocumentManager.getInstance(myProject).commitDocument(document);
                }
            }
            return true;
        } catch (FilesTooBigForDiffException e) {
            handleFileTooBigException(LOG, e, file);
            return false;
        }
    });
}
Also used : FilesTooBigForDiffException(com.intellij.util.diff.FilesTooBigForDiffException) FutureTask(java.util.concurrent.FutureTask) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with FilesTooBigForDiffException

use of com.intellij.util.diff.FilesTooBigForDiffException in project intellij-community by JetBrains.

the class TestDiscoverySearchHelper method search.

public static Set<String> search(final Project project, final Pair<String, String> position, final String changeList, final String frameworkPrefix) {
    final Set<String> patterns = new LinkedHashSet<>();
    if (position != null) {
        try {
            collectPatterns(project, patterns, position.first, position.second, frameworkPrefix);
        } catch (IOException ignore) {
        }
    }
    final List<VirtualFile> files = getAffectedFiles(changeList, project);
    final PsiManager psiManager = PsiManager.getInstance(project);
    final TestDiscoveryIndex discoveryIndex = TestDiscoveryIndex.getInstance(project);
    for (final VirtualFile file : files) {
        ApplicationManager.getApplication().runReadAction(() -> {
            final PsiFile psiFile = psiManager.findFile(file);
            if (psiFile instanceof PsiClassOwner) {
                if (position != null) {
                    final PsiClass[] classes = ((PsiClassOwner) psiFile).getClasses();
                    if (classes.length == 0 || TestFrameworks.detectFramework(classes[0]) == null)
                        return;
                }
                try {
                    final List<TextRange> changedTextRanges = FormatChangedTextUtil.getInstance().getChangedTextRanges(project, psiFile);
                    for (TextRange textRange : changedTextRanges) {
                        final PsiElement start = psiFile.findElementAt(textRange.getStartOffset());
                        final PsiElement end = psiFile.findElementAt(textRange.getEndOffset());
                        final PsiElement parent = PsiTreeUtil.findCommonParent(new PsiElement[] { start, end });
                        final Collection<PsiMethod> methods = new ArrayList<>(PsiTreeUtil.findChildrenOfType(parent, PsiMethod.class));
                        final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(parent, PsiMethod.class);
                        if (containingMethod != null) {
                            methods.add(containingMethod);
                        }
                        for (PsiMethod changedMethod : methods) {
                            final LinkedHashSet<String> detectedPatterns = position == null ? collectPatterns(changedMethod, frameworkPrefix) : null;
                            if (detectedPatterns != null) {
                                patterns.addAll(detectedPatterns);
                            }
                            final PsiClass containingClass = changedMethod.getContainingClass();
                            if (containingClass != null && containingClass.getParent() == psiFile) {
                                final String classQualifiedName = containingClass.getQualifiedName();
                                final String changedMethodName = changedMethod.getName();
                                try {
                                    if (classQualifiedName != null && (position == null && TestFrameworks.detectFramework(containingClass) != null || position != null && !discoveryIndex.hasTestTrace(frameworkPrefix + classQualifiedName + "-" + changedMethodName))) {
                                        patterns.add(classQualifiedName + "," + changedMethodName);
                                    }
                                } catch (IOException ignore) {
                                }
                            }
                        }
                    }
                } catch (FilesTooBigForDiffException ignore) {
                }
            }
        });
    }
    final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
    final GlobalSearchScope searchScope = GlobalSearchScope.projectScope(project);
    return new HashSet<>(ContainerUtil.filter(patterns, fqn -> ReadAction.compute(() -> psiFacade.findClass(StringUtil.getPackageName(fqn, ','), searchScope) != null)));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) java.util(java.util) Change(com.intellij.openapi.vcs.changes.Change) ChangeListManager(com.intellij.openapi.vcs.changes.ChangeListManager) StringUtil(com.intellij.openapi.util.text.StringUtil) VirtualFile(com.intellij.openapi.vfs.VirtualFile) LocalChangeList(com.intellij.openapi.vcs.changes.LocalChangeList) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ContentRevision(com.intellij.openapi.vcs.changes.ContentRevision) IOException(java.io.IOException) TextRange(com.intellij.openapi.util.TextRange) ContainerUtil(com.intellij.util.containers.ContainerUtil) ReadAction(com.intellij.openapi.application.ReadAction) Nullable(org.jetbrains.annotations.Nullable) TestFrameworks(com.intellij.codeInsight.TestFrameworks) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) FormatChangedTextUtil(com.intellij.codeInsight.actions.FormatChangedTextUtil) FilesTooBigForDiffException(com.intellij.util.diff.FilesTooBigForDiffException) Pair(com.intellij.openapi.util.Pair) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Project(com.intellij.openapi.project.Project) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) FilesTooBigForDiffException(com.intellij.util.diff.FilesTooBigForDiffException) TextRange(com.intellij.openapi.util.TextRange) IOException(java.io.IOException) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Example 3 with FilesTooBigForDiffException

use of com.intellij.util.diff.FilesTooBigForDiffException in project intellij-community by JetBrains.

the class PersistentRangeHighlighterImpl method translatedViaDiff.

private boolean translatedViaDiff(DocumentEvent e, DocumentEventImpl event) {
    try {
        myLine = event.translateLineViaDiff(myLine);
    } catch (FilesTooBigForDiffException ignored) {
        return false;
    }
    if (myLine < 0 || myLine >= getDocument().getLineCount()) {
        invalidate(e);
    } else {
        DocumentEx document = getDocument();
        setIntervalStart(document.getLineStartOffset(myLine));
        setIntervalEnd(document.getLineEndOffset(myLine));
    }
    return true;
}
Also used : DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) FilesTooBigForDiffException(com.intellij.util.diff.FilesTooBigForDiffException)

Example 4 with FilesTooBigForDiffException

use of com.intellij.util.diff.FilesTooBigForDiffException in project intellij-community by JetBrains.

the class MergeList method create.

@NotNull
public static MergeList create(@Nullable Project project, @NotNull Document left, @NotNull Document base, @NotNull Document right) {
    MergeList mergeList;
    String leftText = left.getText();
    String baseText = base.getText();
    String rightText = right.getText();
    @NonNls final Object[] data = { "Left\n", leftText, "\nBase\n", baseText, "\nRight\n", rightText };
    ContextLogger logger = new ContextLogger(LOG, new ContextLogger.SimpleContext(data));
    List<MergeFragment> fragmentList;
    try {
        fragmentList = processText(leftText, baseText, rightText, logger);
        mergeList = new MergeList(project, left, base, right, null);
    } catch (FilesTooBigForDiffException e) {
        fragmentList = Collections.emptyList();
        mergeList = new MergeList(project, left, base, right, e.getMessage());
    }
    ArrayList<Change> leftChanges = new ArrayList<>();
    ArrayList<Change> rightChanges = new ArrayList<>();
    for (MergeFragment mergeFragment : fragmentList) {
        TextRange baseRange = mergeFragment.getBase();
        TextRange leftRange = mergeFragment.getLeft();
        TextRange rightRange = mergeFragment.getRight();
        if (compareSubstring(leftText, leftRange, rightText, rightRange)) {
            MergeNoConflict conflict = new MergeNoConflict(baseRange, leftRange, rightRange, mergeList);
            assert conflict.getLeftChange() != null;
            assert conflict.getRightChange() != null;
            leftChanges.add(conflict.getLeftChange());
            rightChanges.add(conflict.getRightChange());
        } else if (compareSubstring(baseText, baseRange, leftText, leftRange)) {
            rightChanges.add(SimpleChange.fromRanges(baseRange, rightRange, mergeList.myBaseToRightChangeList));
        } else if (compareSubstring(baseText, baseRange, rightText, rightRange)) {
            leftChanges.add(SimpleChange.fromRanges(baseRange, leftRange, mergeList.myBaseToLeftChangeList));
        } else {
            MergeConflict conflict = new MergeConflict(baseRange, leftRange, rightRange, mergeList);
            assert conflict.getLeftChange() != null;
            assert conflict.getRightChange() != null;
            leftChanges.add(conflict.getLeftChange());
            rightChanges.add(conflict.getRightChange());
        }
    }
    mergeList.myBaseToLeftChangeList.setChanges(leftChanges);
    mergeList.myBaseToRightChangeList.setChanges(rightChanges);
    return mergeList;
}
Also used : NonNls(org.jetbrains.annotations.NonNls) FilesTooBigForDiffException(com.intellij.util.diff.FilesTooBigForDiffException) ContextLogger(com.intellij.openapi.diff.impl.util.ContextLogger) ArrayList(java.util.ArrayList) DiffString(com.intellij.openapi.diff.impl.string.DiffString) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with FilesTooBigForDiffException

use of com.intellij.util.diff.FilesTooBigForDiffException 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)

Aggregations

FilesTooBigForDiffException (com.intellij.util.diff.FilesTooBigForDiffException)12 TextRange (com.intellij.openapi.util.TextRange)6 Document (com.intellij.openapi.editor.Document)4 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)3 DiffFragment (com.intellij.openapi.diff.ex.DiffFragment)2 DiffString (com.intellij.openapi.diff.impl.string.DiffString)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 FutureTask (java.util.concurrent.FutureTask)2 TestFrameworks (com.intellij.codeInsight.TestFrameworks)1 FormatChangedTextUtil (com.intellij.codeInsight.actions.FormatChangedTextUtil)1 FileRevisionTimestampComparator (com.intellij.history.FileRevisionTimestampComparator)1 LocalHistory (com.intellij.history.LocalHistory)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 ReadAction (com.intellij.openapi.application.ReadAction)1 Fragment (com.intellij.openapi.diff.impl.fragments.Fragment)1 LineFragment (com.intellij.openapi.diff.impl.fragments.LineFragment)1 DiffCorrection (com.intellij.openapi.diff.impl.processing.DiffCorrection)1 DiffFragmentsProcessor (com.intellij.openapi.diff.impl.processing.DiffFragmentsProcessor)1 LineBlocks (com.intellij.openapi.diff.impl.splitter.LineBlocks)1