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