com.intellij.openapi.application.ex.ApplicationUtil Java Examples

The following examples show how to use com.intellij.openapi.application.ex.ApplicationUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LSPCompletionContributor.java    From lsp4intellij with Apache License 2.0 6 votes vote down vote up
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);
            }
        }
    });
}
 
Example #2
Source File: TabNineCompletionContributor.java    From tabnine-intellij with MIT License 6 votes vote down vote up
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;
    }
}
 
Example #3
Source File: PsiSearchHelperImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
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;
        }
      }
    });
  }
}
 
Example #4
Source File: BaseApplication.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
  }
}
 
Example #5
Source File: ReadMostlyRWLock.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
  }
}
 
Example #6
Source File: BaseApplication.java    From consulo with Apache License 2.0 4 votes vote down vote up
private boolean checkReadAccessAllowedAndNoPendingWrites() throws ApplicationUtil.CannotRunReadActionException {
  return isWriteThread() || myLock.checkReadLockedByThisThreadAndNoPendingWrites();
}
 
Example #7
Source File: ReadMostlyRWLock.java    From consulo with Apache License 2.0 4 votes vote down vote up
public boolean checkReadLockedByThisThreadAndNoPendingWrites() throws ApplicationUtil.CannotRunReadActionException {
  checkReadThreadAccess();
  Reader status = R.get();
  throwIfImpatient(status);
  return status.readRequested;
}
 
Example #8
Source File: ReadMostlyRWLock.java    From consulo with Apache License 2.0 4 votes vote down vote up
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();
  }
}