Java Code Examples for com.intellij.openapi.application.Application#isReadAccessAllowed()

The following examples show how to use com.intellij.openapi.application.Application#isReadAccessAllowed() . 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: BlazeTypeScriptConfigServiceImpl.java    From intellij with Apache License 2.0 6 votes vote down vote up
/**
 * Checks for modifications to the tsconfig files for the project.
 *
 * <p>This uses multiple file operations to check timestamps and reload the files list, so should
 * not be called on the EDT or with a read lock.
 */
void update(ImmutableMap<Label, File> tsconfigs) {
  Application application = ApplicationManager.getApplication();
  if (application.isDispatchThread() || application.isReadAccessAllowed()) {
    logger.error("Updating tsconfig files on EDT or with a read lock.");
    return;
  }
  configs =
      tsconfigs.entrySet().parallelStream()
          .map(
              entry ->
                  BlazeTypeScriptConfig.getInstance(project, entry.getKey(), entry.getValue()))
          .filter(Objects::nonNull)
          .collect(
              ImmutableMap.toImmutableMap(TypeScriptConfig::getConfigFile, Functions.identity()));
  for (TypeScriptConfigsChangedListener listener : listeners) {
    listener.afterUpdate(configs.keySet());
  }
  restartServiceIfConfigsChanged();
}
 
Example 2
Source File: LightFilePointer.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void refreshFile() {
  VirtualFile file = myFile;
  if (file != null && file.isValid()) return;
  VirtualFileManager vfManager = VirtualFileManager.getInstance();
  VirtualFile virtualFile = vfManager.findFileByUrl(myUrl);
  if (virtualFile == null && !myRefreshed) {
    myRefreshed = true;
    Application application = ApplicationManager.getApplication();
    if (application.isDispatchThread() || !application.isReadAccessAllowed()) {
      virtualFile = vfManager.refreshAndFindFileByUrl(myUrl);
    }
    else {
      application.executeOnPooledThread(() -> vfManager.refreshAndFindFileByUrl(myUrl));
    }
  }

  myFile = virtualFile != null && virtualFile.isValid() ? virtualFile : null;
}
 
Example 3
Source File: ProgressIndicatorUtils.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Ensure the current EDT activity finishes in case it requires many write actions, with each being delayed a bit
 * by background thread read action (until its first checkCanceled call). Shouldn't be called from under read action.
 */
public static void yieldToPendingWriteActions() {
  Application application = ApplicationManager.getApplication();
  if (application.isReadAccessAllowed()) {
    throw new IllegalStateException("Mustn't be called from within read action");
  }
  if (application.isDispatchThread()) {
    throw new IllegalStateException("Mustn't be called from EDT");
  }
  Semaphore semaphore = new Semaphore(1);
  application.invokeLater(semaphore::up, ModalityState.any());
  awaitWithCheckCanceled(semaphore, ProgressIndicatorProvider.getGlobalProgressIndicator());
}
 
Example 4
Source File: CopyPasteUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void contentChanged(final Transferable oldTransferable, final Transferable newTransferable) {
  Application application = ApplicationManager.getApplication();
  if (application == null || application.isReadAccessAllowed()) {
    updateByTransferable(oldTransferable);
    updateByTransferable(newTransferable);
  }
  else {
    application.runReadAction(() -> {
      updateByTransferable(oldTransferable);
      updateByTransferable(newTransferable);
    });
  }
}
 
Example 5
Source File: FileUtils.java    From leetcode-editor with Apache License 2.0 4 votes vote down vote up
public static String getClearCommentFileBody(File file, CodeTypeEnum codeTypeEnum) {

        VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
        if (FileDocumentManager.getInstance().isFileModified(vf)) {
            try {
                ThrowableComputable<Boolean, Throwable> action = new ThrowableComputable<Boolean, Throwable>() {
                    @Override
                    public Boolean compute() throws Throwable {
                        FileDocumentManager.getInstance().saveDocument(FileDocumentManager.getInstance().getDocument(vf));
                        return true;
                    }
                };


                Application application = ApplicationManager.getApplication();
                if (application.isDispatchThread()) {
                    ApplicationManager.getApplication().runWriteAction(action);
                } else {
                    if (application.isReadAccessAllowed()) {
                        LogUtils.LOG.error("Must not start write action from within read action in the other thread - deadlock is coming");
                    }

                    AtomicReference<Boolean> result = new AtomicReference();
                    AtomicReference<Throwable> exception = new AtomicReference();
                    TransactionGuard.getInstance().submitTransactionAndWait(() -> {
                        try {
                            result.set(WriteAction.compute(action));
                        } catch (Throwable var4) {
                            exception.set(var4);
                        }

                    });
                    Throwable t = (Throwable) exception.get();
                    if (t != null) {
                        t.addSuppressed(new RuntimeException());
                        ExceptionUtil.rethrowUnchecked(t);
                        throw t;
                    }
                }
            } catch (Throwable ignore) {
                LogUtils.LOG.error("自动保存文件错误", ignore);
            }

        }
        StringBuffer code = new StringBuffer();
        try {
            String body = VfsUtil.loadText(vf);
            if (StringUtils.isNotBlank(body)) {

                List<String> codeList = new LinkedList<>();
                int codeBegin = -1;
                int codeEnd = -1;
                int lineCount = 0;

                String[] lines = body.split("\r\n|\r|\n");
                for (String line : lines) {
                    if (StringUtils.isNotBlank(line) && trim(line).equals(trim(codeTypeEnum.getComment() + Constant.SUBMIT_REGION_BEGIN))) {
                        codeBegin = lineCount;
                    } else if (StringUtils.isNotBlank(line) && trim(line).equals(trim(codeTypeEnum.getComment() + Constant.SUBMIT_REGION_END))) {
                        codeEnd = lineCount;
                    }
                    codeList.add(line);
                    lineCount++;
                }
                if (codeBegin >= 0 && codeEnd > 0 && codeBegin < codeEnd) {
                    for (int i = codeBegin + 1; i < codeEnd; i++) {
                        code.append(codeList.get(i)).append("\n");
                    }
                } else {
                    Boolean isCode = Boolean.FALSE;
                    for (int i = 0; i < codeList.size(); i++) {
                        String str = codeList.get(i);
                        if (!isCode) {
                            if (StringUtils.isNotBlank(str) && !str.startsWith(codeTypeEnum.getComment())) {
                                isCode = Boolean.TRUE;
                                code.append(str).append("\n");
                            } else {
                                continue;
                            }
                        } else {
                            code.append(str).append("\n");
                        }
                    }
                }
            }
        } catch (IOException id) {

        }
        return code.toString();
    }
 
Example 6
Source File: Invoker.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Returns {@code true} if the current thread allows to process a task.
 *
 * @return {@code true} if the current thread is valid, or {@code false} otherwise
 */
public boolean isValidThread() {
  if (useReadAction != ThreeState.NO) return true;
  Application application = getApplication();
  return application == null || !application.isReadAccessAllowed();
}