Java源码示例:com.intellij.openapi.application.ex.ApplicationUtil

示例1
public LSPCompletionContributor() {
    this.extend(CompletionType.BASIC, usePattern(), new CompletionProvider<CompletionParameters>() {
        @Override
        protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet result) {
            try {
                ApplicationUtil.runWithCheckCanceled(() -> {
                    Editor editor = parameters.getEditor();
                    int offset = parameters.getOffset();
                    Position serverPos = DocumentUtils.offsetToLSPPos(editor, offset);

                    EditorEventManager manager = EditorEventManagerBase.forEditor(editor);
                    if (manager != null) {
                        result.addAllElements(manager.completion(serverPos));
                    }
                    return null;
                }, ProgressIndicatorProvider.getGlobalProgressIndicator());
            } catch (ProcessCanceledException ignored) {
                // ProcessCanceledException can be ignored.
            } catch (Exception e) {
                LOG.warn("LSP Completions ended with an error", e);
            }
        }
    });
}
 
示例2
TabNineProcess.AutocompleteResponse retrieveCompletions(CompletionParameters parameters, int max_num_results) {
    try {
        return ApplicationUtil.runWithCheckCanceled(() -> {
            TabNineProcess proc = this.getProcOrPrintError();
            if (proc == null) {
                return null;
            }
            final int MAX_OFFSET = 100000; // 100 KB
            Document doc = parameters.getEditor().getDocument();
            int middle = parameters.getOffset();
            int begin = Integer.max(0, middle - MAX_OFFSET);
            int end = Integer.min(doc.getTextLength(), middle + MAX_OFFSET);
            TabNineProcess.AutocompleteRequest req = new TabNineProcess.AutocompleteRequest();
            req.before = doc.getText(new TextRange(begin, middle));
            req.after = doc.getText(new TextRange(middle, end));
            req.filename = parameters.getOriginalFile().getVirtualFile().getPath();
            req.max_num_results = max_num_results;
            req.region_includes_beginning = (begin == 0);
            req.region_includes_end = (end == doc.getTextLength());
            return proc.request(req);
        }, ProgressManager.getInstance().getProgressIndicator());
    } catch (Exception e) {
        return null;
    }
}
 
示例3
private void processVirtualFile(@Nonnull final VirtualFile vfile, @Nonnull final AtomicBoolean stopped, @Nonnull final Processor<? super PsiFile> localProcessor) throws ApplicationUtil.CannotRunReadActionException {
  final PsiFile file = ApplicationUtil.tryRunReadAction(() -> vfile.isValid() ? myManager.findFile(vfile) : null);
  if (file != null && !(file instanceof PsiBinaryFile)) {
    ApplicationUtil.tryRunReadAction(() -> {
      final Project project = myManager.getProject();
      if (project.isDisposed()) throw new ProcessCanceledException();
      if (DumbService.isDumb(project)) throw ApplicationUtil.CannotRunReadActionException.create();

      List<PsiFile> psiRoots = file.getViewProvider().getAllFiles();
      Set<PsiFile> processed = new THashSet<>(psiRoots.size() * 2, (float)0.5);
      for (final PsiFile psiRoot : psiRoots) {
        ProgressManager.checkCanceled();
        assert psiRoot != null : "One of the roots of file " + file + " is null. All roots: " + psiRoots + "; ViewProvider: " +
                                 file.getViewProvider() + "; Virtual file: " + file.getViewProvider().getVirtualFile();
        if (!processed.add(psiRoot)) continue;
        if (!psiRoot.isValid()) {
          continue;
        }

        if (!localProcessor.process(psiRoot)) {
          stopped.set(true);
          break;
        }
      }
    });
  }
}
 
示例4
/**
 * Executes a {@code runnable} in an "impatient" mode.
 * In this mode any attempt to call {@link #runReadAction(Runnable)}
 * would fail (i.e. throw {@link ApplicationUtil.CannotRunReadActionException})
 * if there is a pending write action.
 */
@Override
public void executeByImpatientReader(@Nonnull Runnable runnable) throws ApplicationUtil.CannotRunReadActionException {
  if (isDispatchThread()) {
    runnable.run();
  }
  else {
    myLock.executeByImpatientReader(runnable);
  }
}
 
示例5
/**
 * Executes a {@code runnable} in an "impatient" mode.
 * In this mode any attempt to grab read lock
 * will fail (i.e. throw {@link ApplicationUtil.CannotRunReadActionException})
 * if there is a pending write lock request.
 */
public void executeByImpatientReader(@Nonnull Runnable runnable) throws ApplicationUtil.CannotRunReadActionException {
  checkReadThreadAccess();
  Reader status = R.get();
  boolean old = status.impatientReads;
  try {
    status.impatientReads = true;
    runnable.run();
  }
  finally {
    status.impatientReads = old;
  }
}
 
示例6
private boolean checkReadAccessAllowedAndNoPendingWrites() throws ApplicationUtil.CannotRunReadActionException {
  return isWriteThread() || myLock.checkReadLockedByThisThreadAndNoPendingWrites();
}
 
示例7
public boolean checkReadLockedByThisThreadAndNoPendingWrites() throws ApplicationUtil.CannotRunReadActionException {
  checkReadThreadAccess();
  Reader status = R.get();
  throwIfImpatient(status);
  return status.readRequested;
}
 
示例8
private void throwIfImpatient(Reader status) throws ApplicationUtil.CannotRunReadActionException {
  // when client explicitly runs in non-cancelable block do not throw from within nested read actions
  if (status.impatientReads && writeRequested && !ProgressManager.getInstance().isInNonCancelableSection() && CoreProgressManager.ENABLED) {
    throw ApplicationUtil.CannotRunReadActionException.create();
  }
}