Search in sources :

Example 86 with ProcessCanceledException

use of com.intellij.openapi.progress.ProcessCanceledException in project intellij-community by JetBrains.

the class DiffActionExecutor method showDiff.

public void showDiff() {
    final Ref<VcsException> exceptionRef = new Ref<>();
    final Ref<DiffRequest> requestRef = new Ref<>();
    final Task.Backgroundable task = new Task.Backgroundable(myProject, VcsBundle.message("show.diff.progress.title.detailed", mySelectedFile.getPresentableUrl()), true) {

        public void run(@NotNull ProgressIndicator indicator) {
            final VcsRevisionNumber revisionNumber = getRevisionNumber();
            try {
                if (revisionNumber == null) {
                    return;
                }
                DiffContent content1 = createRemote(revisionNumber);
                if (content1 == null)
                    return;
                DiffContent content2 = DiffContentFactory.getInstance().create(myProject, mySelectedFile);
                String title = DiffRequestFactory.getInstance().getTitle(mySelectedFile);
                boolean inverted = false;
                String title1;
                String title2;
                final FileStatus status = FileStatusManager.getInstance(myProject).getStatus(mySelectedFile);
                if (status == null || FileStatus.NOT_CHANGED.equals(status) || FileStatus.UNKNOWN.equals(status) || FileStatus.IGNORED.equals(status)) {
                    final VcsRevisionNumber currentRevision = myDiffProvider.getCurrentRevision(mySelectedFile);
                    inverted = revisionNumber.compareTo(currentRevision) > 0;
                    title1 = revisionNumber.asString();
                    title2 = VcsBundle.message("diff.title.local.with.number", currentRevision.asString());
                } else {
                    title1 = revisionNumber.asString();
                    title2 = VcsBundle.message("diff.title.local");
                }
                Integer line = null;
                if (content2 instanceof DocumentContent) {
                    Editor[] editors = EditorFactory.getInstance().getEditors(((DocumentContent) content2).getDocument(), myProject);
                    if (editors.length != 0)
                        line = editors[0].getCaretModel().getLogicalPosition().line;
                }
                if (inverted) {
                    SimpleDiffRequest request = new SimpleDiffRequest(title, content2, content1, title2, title1);
                    if (line != null)
                        request.putUserData(DiffUserDataKeys.SCROLL_TO_LINE, Pair.create(Side.LEFT, line));
                    request.putUserData(DiffUserDataKeys.MASTER_SIDE, Side.LEFT);
                    requestRef.set(request);
                } else {
                    SimpleDiffRequest request = new SimpleDiffRequest(title, content1, content2, title1, title2);
                    if (line != null)
                        request.putUserData(DiffUserDataKeys.SCROLL_TO_LINE, Pair.create(Side.RIGHT, line));
                    request.putUserData(DiffUserDataKeys.MASTER_SIDE, Side.RIGHT);
                    requestRef.set(request);
                }
            } catch (ProcessCanceledException e) {
            //ignore
            } catch (VcsException e) {
                exceptionRef.set(e);
            } catch (IOException e) {
                exceptionRef.set(new VcsException(e));
            }
        }

        @Override
        public void onCancel() {
            onSuccess();
        }

        @Override
        public void onSuccess() {
            myHandler.completed(VcsBackgroundableActions.keyFrom(mySelectedFile));
            if (!exceptionRef.isNull()) {
                AbstractVcsHelper.getInstance(myProject).showError(exceptionRef.get(), VcsBundle.message("message.title.diff"));
                return;
            }
            if (!requestRef.isNull()) {
                DiffManager.getInstance().showDiff(myProject, requestRef.get());
            }
        }
    };
    myHandler.register(VcsBackgroundableActions.keyFrom(mySelectedFile));
    ProgressManager.getInstance().run(task);
}
Also used : SimpleDiffRequest(com.intellij.diff.requests.SimpleDiffRequest) Task(com.intellij.openapi.progress.Task) DiffRequest(com.intellij.diff.requests.DiffRequest) SimpleDiffRequest(com.intellij.diff.requests.SimpleDiffRequest) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) DocumentContent(com.intellij.diff.contents.DocumentContent) VcsRevisionNumber(com.intellij.openapi.vcs.history.VcsRevisionNumber) Editor(com.intellij.openapi.editor.Editor) DiffContent(com.intellij.diff.contents.DiffContent) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 87 with ProcessCanceledException

use of com.intellij.openapi.progress.ProcessCanceledException in project intellij-community by JetBrains.

the class ContributorsBasedGotoByModel method processNames.

@Override
public void processNames(final Processor<String> nameProcessor, final boolean checkBoxState) {
    long start = System.currentTimeMillis();
    List<ChooseByNameContributor> liveContribs = filterDumb(myContributors);
    ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
    Processor<ChooseByNameContributor> processor = new ReadActionProcessor<ChooseByNameContributor>() {

        @Override
        public boolean processInReadAction(@NotNull ChooseByNameContributor contributor) {
            try {
                if (!myProject.isDisposed()) {
                    long contributorStarted = System.currentTimeMillis();
                    final TIntHashSet filter = new TIntHashSet(1000);
                    myContributorToItsSymbolsMap.put(contributor, filter);
                    if (contributor instanceof ChooseByNameContributorEx) {
                        ((ChooseByNameContributorEx) contributor).processNames(s -> {
                            if (nameProcessor.process(s)) {
                                filter.add(s.hashCode());
                            }
                            return true;
                        }, FindSymbolParameters.searchScopeFor(myProject, checkBoxState), getIdFilter(checkBoxState));
                    } else {
                        String[] names = contributor.getNames(myProject, checkBoxState);
                        for (String element : names) {
                            if (nameProcessor.process(element)) {
                                filter.add(element.hashCode());
                            }
                        }
                    }
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(contributor + " for " + (System.currentTimeMillis() - contributorStarted));
                    }
                }
            } catch (ProcessCanceledException | IndexNotReadyException ex) {
            // index corruption detected, ignore
            } catch (Exception ex) {
                LOG.error(ex);
            }
            return true;
        }
    };
    if (!JobLauncher.getInstance().invokeConcurrentlyUnderProgress(liveContribs, indicator, true, processor)) {
        throw new ProcessCanceledException();
    }
    if (indicator != null) {
        indicator.checkCanceled();
    }
    long finish = System.currentTimeMillis();
    if (LOG.isDebugEnabled()) {
        LOG.debug("processNames(): " + (finish - start) + "ms;");
    }
}
Also used : NotNull(org.jetbrains.annotations.NotNull) TIntHashSet(gnu.trove.TIntHashSet) PluginException(com.intellij.diagnostic.PluginException) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) ChooseByNameContributorEx(com.intellij.navigation.ChooseByNameContributorEx) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) ChooseByNameContributor(com.intellij.navigation.ChooseByNameContributor) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 88 with ProcessCanceledException

use of com.intellij.openapi.progress.ProcessCanceledException in project intellij-community by JetBrains.

the class DefaultChooseByNameItemProvider method processNamesByPattern.

private static void processNamesByPattern(@NotNull final ChooseByNameBase base, @NotNull final String[] names, @NotNull final String pattern, final ProgressIndicator indicator, @NotNull final Consumer<MatchResult> consumer) {
    final MinusculeMatcher matcher = buildPatternMatcher(pattern, NameUtil.MatchingCaseSensitivity.NONE);
    Processor<String> processor = name -> {
        ProgressManager.checkCanceled();
        MatchResult result = matches(base, pattern, matcher, name);
        if (result != null) {
            consumer.consume(result);
        }
        return true;
    };
    if (!JobLauncher.getInstance().invokeConcurrentlyUnderProgress(Arrays.asList(names), indicator, false, true, processor)) {
        throw new ProcessCanceledException();
    }
}
Also used : java.util(java.util) JobLauncher(com.intellij.concurrency.JobLauncher) MinusculeMatcher(com.intellij.psi.codeStyle.MinusculeMatcher) PsiProximityComparator(com.intellij.psi.util.proximity.PsiProximityComparator) IdFilter(com.intellij.util.indexing.IdFilter) ContainerUtil(com.intellij.util.containers.ContainerUtil) NameUtil(com.intellij.psi.codeStyle.NameUtil) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) PsiElement(com.intellij.psi.PsiElement) Logger(com.intellij.openapi.diagnostic.Logger) ProgressManager(com.intellij.openapi.progress.ProgressManager) StringUtil(com.intellij.openapi.util.text.StringUtil) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) TextRange(com.intellij.openapi.util.TextRange) PsiCompiledElement(com.intellij.psi.PsiCompiledElement) ProgressIndicatorProvider(com.intellij.openapi.progress.ProgressIndicatorProvider) SmartPointerManager(com.intellij.psi.SmartPointerManager) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) SmartPsiElementPointer(com.intellij.psi.SmartPsiElementPointer) Pair(com.intellij.openapi.util.Pair) FList(com.intellij.util.containers.FList) com.intellij.util(com.intellij.util) NotNull(org.jetbrains.annotations.NotNull) FindSymbolParameters(com.intellij.util.indexing.FindSymbolParameters) MinusculeMatcher(com.intellij.psi.codeStyle.MinusculeMatcher) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 89 with ProcessCanceledException

use of com.intellij.openapi.progress.ProcessCanceledException in project intellij-community by JetBrains.

the class BackgroundTaskUtil method executeAndTryWait.

@NotNull
@CalledInAwt
public static ProgressIndicator executeAndTryWait(@NotNull Function<ProgressIndicator, /*@NotNull*/
Runnable> backgroundTask, @Nullable Runnable onSlowAction, long waitMillis, boolean forceEDT) {
    ModalityState modality = ModalityState.current();
    if (forceEDT) {
        ProgressIndicator indicator = new EmptyProgressIndicator(modality);
        try {
            Runnable callback = backgroundTask.fun(indicator);
            finish(callback, indicator);
        } catch (ProcessCanceledException ignore) {
        } catch (Throwable t) {
            LOG.error(t);
        }
        return indicator;
    } else {
        Pair<Runnable, ProgressIndicator> pair = computeInBackgroundAndTryWait(backgroundTask, (callback, indicator) -> {
            ApplicationManager.getApplication().invokeLater(() -> {
                finish(callback, indicator);
            }, modality);
        }, modality, waitMillis);
        Runnable callback = pair.first;
        ProgressIndicator indicator = pair.second;
        if (callback != null) {
            finish(callback, indicator);
        } else {
            if (onSlowAction != null)
                onSlowAction.run();
        }
        return indicator;
    }
}
Also used : EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ModalityState(com.intellij.openapi.application.ModalityState) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) CalledInAwt(org.jetbrains.annotations.CalledInAwt) NotNull(org.jetbrains.annotations.NotNull)

Example 90 with ProcessCanceledException

use of com.intellij.openapi.progress.ProcessCanceledException in project intellij-community by JetBrains.

the class ProgressIndicatorUtils method runWithWriteActionPriority.

public static boolean runWithWriteActionPriority(@NotNull Runnable action, @NotNull ProgressIndicator progressIndicator) {
    final ApplicationEx application = (ApplicationEx) ApplicationManager.getApplication();
    if (application.isDispatchThread()) {
        throw new IllegalStateException("Must not call from EDT");
    }
    if (application.isWriteActionPending()) {
        // tryRunReadAction below would just run without really checking if a write action is pending
        if (!progressIndicator.isCanceled())
            progressIndicator.cancel();
        return false;
    }
    final ApplicationAdapter listener = new ApplicationAdapter() {

        @Override
        public void beforeWriteActionStart(@NotNull Object action) {
            if (!progressIndicator.isCanceled())
                progressIndicator.cancel();
        }
    };
    boolean succeededWithAddingListener = application.tryRunReadAction(() -> {
        // Even if writeLock.lock() acquisition is in progress at this point then runProcess will block wanting read action which is
        // also ok as last resort.
        application.addApplicationListener(listener);
    });
    if (!succeededWithAddingListener) {
        // second catch: writeLock.lock() acquisition is in progress or already acquired
        if (!progressIndicator.isCanceled())
            progressIndicator.cancel();
        return false;
    }
    final Ref<Boolean> wasCancelled = new Ref<>();
    try {
        ProgressManager.getInstance().runProcess(() -> {
            try {
                action.run();
            } catch (ProcessCanceledException ignore) {
                wasCancelled.set(Boolean.TRUE);
            }
        }, progressIndicator);
    } finally {
        application.removeApplicationListener(listener);
    }
    return wasCancelled.get() != Boolean.TRUE;
}
Also used : Ref(com.intellij.openapi.util.Ref) ApplicationEx(com.intellij.openapi.application.ex.ApplicationEx) ApplicationAdapter(com.intellij.openapi.application.ApplicationAdapter) NotNull(org.jetbrains.annotations.NotNull) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Aggregations

ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)175 NotNull (org.jetbrains.annotations.NotNull)45 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)41 VirtualFile (com.intellij.openapi.vfs.VirtualFile)38 Project (com.intellij.openapi.project.Project)28 Nullable (org.jetbrains.annotations.Nullable)23 IOException (java.io.IOException)20 Task (com.intellij.openapi.progress.Task)16 File (java.io.File)16 Document (com.intellij.openapi.editor.Document)14 Ref (com.intellij.openapi.util.Ref)13 PsiFile (com.intellij.psi.PsiFile)12 IndexNotReadyException (com.intellij.openapi.project.IndexNotReadyException)11 Logger (com.intellij.openapi.diagnostic.Logger)10 StringUtil (com.intellij.openapi.util.text.StringUtil)9 ContainerUtil (com.intellij.util.containers.ContainerUtil)9 ArrayList (java.util.ArrayList)9 NonNls (org.jetbrains.annotations.NonNls)9 ProgressManager (com.intellij.openapi.progress.ProgressManager)8 java.util (java.util)8