@Nullable
private AnalysisScope getInspectionScopeImpl(@NotNull final DataContext dataContext) {
//Possible scopes: file, directory, package, project, module.
final Project projectContext = PlatformDataKeys.PROJECT_CONTEXT.getData(dataContext);
if (projectContext != null) {
return new AnalysisScope(projectContext);
}
final AnalysisScope analysisScope = AnalyzeUtil.KEY.getData(dataContext);
if (analysisScope != null) {
return analysisScope;
}
final Module moduleContext = LangDataKeys.MODULE_CONTEXT.getData(dataContext);
if (moduleContext != null) {
return new AnalysisScope(moduleContext);
}
final Module [] modulesArray = LangDataKeys.MODULE_CONTEXT_ARRAY.getData(dataContext);
if (modulesArray != null) {
return new AnalysisScope(modulesArray);
}
final PsiFile psiFile = LangDataKeys.PSI_FILE.getData(dataContext);
if (psiFile != null && psiFile.getManager().isInProject(psiFile)) {
final VirtualFile file = psiFile.getVirtualFile();
if (file != null && file.isValid() && file.getFileType() instanceof ArchiveFileType && acceptNonProjectDirectories()) {
final VirtualFile jarRoot = JarFileSystem.getInstance().getJarRootForLocalFile(file);
if (jarRoot != null) {
final PsiDirectory psiDirectory = psiFile.getManager().findDirectory(jarRoot);
if (psiDirectory != null) {
return new AnalysisScope(psiDirectory);
}
}
}
return new AnalysisScope(psiFile);
}
final VirtualFile[] virtualFiles = PlatformDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext);
final Project project = PlatformDataKeys.PROJECT.getData(dataContext);
if (virtualFiles != null && project != null) { //analyze on selection
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
if (virtualFiles.length == 1) {
final PsiDirectory psiDirectory = PsiManager.getInstance(project).findDirectory(virtualFiles[0]);
if (psiDirectory != null && (acceptNonProjectDirectories() || psiDirectory.getManager().isInProject(psiDirectory))) {
return new AnalysisScope(psiDirectory);
}
}
final Set<VirtualFile> files = new HashSet<VirtualFile>();
for (VirtualFile vFile : virtualFiles) {
if (fileIndex.isInContent(vFile)) {
if (vFile instanceof VirtualFileWindow) {
files.add(vFile);
vFile = ((VirtualFileWindow)vFile).getDelegate();
}
collectFilesUnder(vFile, files);
}
}
return new AnalysisScope(project, files);
}
return project == null ? null : new AnalysisScope(project);
}