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

The following examples show how to use com.intellij.openapi.application.Application#runReadAction() . 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: AbstractTreeBuilder.java    From consulo with Apache License 2.0 6 votes vote down vote up
protected void runBackgroundLoading(@Nonnull final Runnable runnable) {
  if (isDisposed()) return;

  final Application app = ApplicationManager.getApplication();
  if (app != null) {
    app.runReadAction(new TreeRunnable("AbstractTreeBuilder.runBackgroundLoading") {
      @Override
      public void perform() {
        runnable.run();
      }
    });
  }
  else {
    runnable.run();
  }
}
 
Example 2
Source File: CompilerPathsImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * The same as {@link #getModuleOutputDirectory} but returns String.
 * The method still returns a non-null value if the output path is specified in Settings but does not exist on disk.
 */
@javax.annotation.Nullable
@Deprecated
public static String getModuleOutputPath(final Module module, final boolean forTestClasses) {
  final String outPathUrl;
  final Application application = ApplicationManager.getApplication();
  final ModuleCompilerPathsManager pathsManager = ModuleCompilerPathsManager.getInstance(module);

  if (application.isDispatchThread()) {
    outPathUrl = pathsManager.getCompilerOutputUrl(
      forTestClasses ? TestContentFolderTypeProvider.getInstance() : ProductionContentFolderTypeProvider.getInstance());
  }
  else {
    outPathUrl = application.runReadAction(new Computable<String>() {
      @Override
      public String compute() {
        return pathsManager.getCompilerOutputUrl(
          forTestClasses ? TestContentFolderTypeProvider.getInstance() : ProductionContentFolderTypeProvider.getInstance());
      }
    });
  }

  return outPathUrl != null ? VirtualFileManager.extractPath(outPathUrl) : null;
}
 
Example 3
Source File: CompilerPathsImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@javax.annotation.Nullable
public static String getModuleOutputPath(final Module module, final ContentFolderTypeProvider contentFolderType) {
  final String outPathUrl;
  final Application application = ApplicationManager.getApplication();
  final ModuleCompilerPathsManager pathsManager = ModuleCompilerPathsManager.getInstance(module);

  if (application.isDispatchThread()) {
    outPathUrl = pathsManager.getCompilerOutputUrl(contentFolderType);
  }
  else {
    outPathUrl = application.runReadAction(new Computable<String>() {
      @Override
      public String compute() {
        return pathsManager.getCompilerOutputUrl(contentFolderType);
      }
    });
  }

  return outPathUrl != null ? VirtualFileManager.extractPath(outPathUrl) : null;
}
 
Example 4
Source File: InferredTypesService.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
public static void queryForSelectedTextEditor(@NotNull Project project) {
    try {
        Editor selectedTextEditor = FileEditorManager.getInstance(project).getSelectedTextEditor();
        if (selectedTextEditor != null) {
            Document document = selectedTextEditor.getDocument();
            PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);
            if (psiFile instanceof FileBase && !FileHelper.isInterface(psiFile.getFileType())) {
                // Try to get the inferred types cached at the psi file user data
                VirtualFile sourceFile = psiFile.getVirtualFile();
                Application application = ApplicationManager.getApplication();

                SignatureProvider.InferredTypesWithLines sigContext = psiFile.getUserData(SignatureProvider.SIGNATURE_CONTEXT);
                InferredTypes signatures = sigContext == null ? null : sigContext.getTypes();
                if (signatures == null) {
                    FileType fileType = sourceFile.getFileType();
                    if (FileHelper.isCompilable(fileType)) {
                        InsightManager insightManager = ServiceManager.getService(project, InsightManager.class);

                        if (!DumbService.isDumb(project)) {
                            LOG.debug("Reading types from file", psiFile);
                            PsiFile cmtFile = findCmtFileFromSource(project, sourceFile.getNameWithoutExtension());
                            if (cmtFile != null) {
                                Path cmtPath = FileSystems.getDefault().getPath(cmtFile.getVirtualFile().getPath());
                                insightManager.queryTypes(sourceFile, cmtPath, types -> application
                                        .runReadAction(() -> annotatePsiFile(project, psiFile.getLanguage(), sourceFile, types)));
                            }
                        }
                    }
                } else {
                    LOG.debug("Signatures found in user data cache");
                    application.runReadAction(() -> annotatePsiFile(project, psiFile.getLanguage(), sourceFile, signatures));
                }
            }
        }
    } catch (Error e) {
        // might produce an AssertionError when project is being disposed, but the invokeLater still process that code
    }
}
 
Example 5
Source File: GradleDslFile.java    From ok-gradle with Apache License 2.0 5 votes vote down vote up
protected GradleDslFile(@NotNull VirtualFile file,
                        @NotNull Project project,
                        @NotNull String moduleName,
                        @NotNull BuildModelContext context) {
  super(null, null, GradleNameElement.fake(moduleName));
  myFile = file;
  myProject = project;
  myBuildModelContext = context;

  Application application = ApplicationManager.getApplication();
  PsiFile psiFile = application.runReadAction((Computable<PsiFile>)() -> PsiManager.getInstance(myProject).findFile(myFile));

  // Pick the language that should be used by this GradleDslFile, we do this by selecting the parser implementation.
  GroovyFile groovyPsiFile;
  if (psiFile instanceof GroovyFile) {
    groovyPsiFile = (GroovyFile)psiFile;
    myGradleDslParser = new GroovyDslParser(groovyPsiFile, this);
    myGradleDslWriter = new GroovyDslWriter();
  }
  else {
    // If we don't support the language we ignore the PsiElement and set stubs for the writer and parser.
    // This means this file will produce an empty model.
    myGradleDslParser = new GradleDslParser.Adapter();
    myGradleDslWriter = new GradleDslWriter.Adapter();
    return;
  }

  setPsiElement(groovyPsiFile);
}
 
Example 6
Source File: ModuleCompilerUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void sortModules(final Project project, final List<Module> modules) {
  final Application application = ApplicationManager.getApplication();
  Runnable sort = new Runnable() {
    public void run() {
      Comparator<Module> comparator = ModuleManager.getInstance(project).moduleDependencyComparator();
      Collections.sort(modules, comparator);
    }
  };
  if (application.isDispatchThread()) {
    sort.run();
  }
  else {
    application.runReadAction(sort);
  }
}
 
Example 7
Source File: MessageBusUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static <T> void runOnSyncPublisher(final Project project, final Topic<T> topic, final Consumer<T> listener) {
  final Application application = ApplicationManager.getApplication();
  final Runnable runnable = createPublisherRunnable(project, topic, listener);
  if (application.isDispatchThread()) {
    runnable.run();
  } else {
    application.runReadAction(runnable);
  }
}
 
Example 8
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 9
Source File: JavaGetImportCandidatesHandler.java    From ijaas with Apache License 2.0 4 votes vote down vote up
@Override
protected Response handle(Request request) {
  Project project = findProject(request.file);
  if (project == null) {
    throw new RuntimeException("Cannot find the target project");
  }
  Application application = ApplicationManager.getApplication();
  Response response = new Response();
  application.runReadAction(
      () -> {
        PsiFile psiFile =
            PsiFileFactory.getInstance(project)
                .createFileFromText(JavaLanguage.INSTANCE, request.text);
        if (!(psiFile instanceof PsiJavaFile)) {
          throw new RuntimeException("Cannot parse as Java file");
        }
        PsiJavaFile psiJavaFile = (PsiJavaFile) psiFile;

        Set<String> processed = new HashSet<>();
        for (PsiClass psiClass : psiJavaFile.getClasses()) {
          psiClass.accept(
              new JavaRecursiveElementWalkingVisitor() {
                @Override
                public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
                  try {
                    if (reference.getQualifier() != null) {
                      return;
                    }
                    String name = reference.getReferenceName();
                    if (processed.contains(name)) {
                      return;
                    }
                    processed.add(name);

                    Set<String> candidates = new HashSet<>();
                    for (PsiClass t : new ImportClassFix(reference).getClassesToImport()) {
                      candidates.add(String.format("import %s;", t.getQualifiedName()));
                    }
                    if (!candidates.isEmpty()) {
                      response.choices.add(candidates.stream().sorted().collect(toList()));
                    }
                  } finally {
                    super.visitReferenceElement(reference);
                  }
                }
              });
        }
      });
  return response;
}