/*
- * Copyright 2011-2015 Bas Leijdekkers
+ * Copyright 2011-2016 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.reference.RefElement;
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
-import com.intellij.openapi.application.Application;
-import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.project.Project;
-import com.intellij.openapi.roots.ContentIterator;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
-import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
}
final GlobalSearchScope globalSearchScope = (GlobalSearchScope)searchScope;
final PsiManager psiManager = PsiManager.getInstance(project);
- index.iterateContent(new ContentIterator() {
- @Override
- public boolean processFile(final VirtualFile fileOrDir) {
- if (!fileOrDir.isDirectory()) {
- return true;
- }
- if (!globalSearchScope.contains(fileOrDir)) {
- return true;
- }
- if (onlyReportDirectoriesUnderSourceRoots && !index.isInSourceContent(fileOrDir)) {
- return true;
- }
- final VirtualFile[] children = fileOrDir.getChildren();
- if (children.length != 0) {
- return true;
- }
- final Application application = ApplicationManager.getApplication();
- final PsiDirectory directory = application.runReadAction(
- new Computable<PsiDirectory>() {
- @Override
- public PsiDirectory compute() {
- return psiManager.findDirectory(fileOrDir);
- }
- });
- final RefElement refDirectory = context.getRefManager().getReference(directory);
- if (context.shouldCheck(refDirectory, EmptyDirectoryInspection.this)) {
- return true;
- }
- final String relativePath = getPathRelativeToModule(fileOrDir, project);
- if (relativePath == null) {
- return true;
- }
- processor.addProblemElement(refDirectory, manager.createProblemDescriptor(InspectionGadgetsBundle.message(
- "empty.directories.problem.descriptor", relativePath), new EmptyPackageFix(fileOrDir.getUrl(), fileOrDir.getName())));
+ index.iterateContent(fileOrDir -> {
+ if (!fileOrDir.isDirectory()) {
return true;
}
+ if (!globalSearchScope.contains(fileOrDir)) {
+ return true;
+ }
+ if (onlyReportDirectoriesUnderSourceRoots && !index.isInSourceContent(fileOrDir)) {
+ return true;
+ }
+ final VirtualFile[] children = fileOrDir.getChildren();
+ if (children.length != 0) {
+ return true;
+ }
+ final PsiDirectory directory = ReadAction.compute(() -> psiManager.findDirectory(fileOrDir));
+ final RefElement refDirectory = context.getRefManager().getReference(directory);
+ if (refDirectory == null || context.shouldCheck(refDirectory, this)) {
+ return true;
+ }
+ final String relativePath = getPathRelativeToModule(fileOrDir, project);
+ if (relativePath == null) {
+ return true;
+ }
+ processor.addProblemElement(refDirectory, manager.createProblemDescriptor(
+ InspectionGadgetsBundle.message("empty.directories.problem.descriptor", relativePath),
+ new EmptyPackageFix(fileOrDir.getUrl(), fileOrDir.getName())));
+ return true;
});
}
@Nullable
private static String getPathRelativeToModule(VirtualFile file, Project project) {
final ProjectRootManager rootManager = ProjectRootManager.getInstance(project);
- final Application application = ApplicationManager.getApplication();
- final VirtualFile[] contentRoots = application.runReadAction(
- new Computable<VirtualFile[]>() {
- @Override
- public VirtualFile[] compute() {
- return rootManager.getContentRootsFromAllModules();
- }
- });
+ final VirtualFile[] contentRoots = ReadAction.compute(() -> rootManager.getContentRootsFromAllModules());
for (VirtualFile otherRoot : contentRoots) {
if (VfsUtilCore.isAncestor(otherRoot, file, false)) {
return VfsUtilCore.getRelativePath(file, otherRoot, '/');