use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-community by JetBrains.
the class PsiJavaDirectoryFactory method isPackage.
@Override
public boolean isPackage(@NotNull PsiDirectory directory) {
ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myManager.getProject()).getFileIndex();
VirtualFile virtualFile = directory.getVirtualFile();
return fileIndex.isUnderSourceRootOfType(virtualFile, JavaModuleSourceRootTypes.SOURCES) && fileIndex.getPackageNameByDirectory(virtualFile) != null;
}
use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-community by JetBrains.
the class CreateTemplateInPackageAction method isAvailable.
@Override
protected boolean isAvailable(final DataContext dataContext) {
final Project project = CommonDataKeys.PROJECT.getData(dataContext);
final IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
if (project == null || view == null || view.getDirectories().length == 0) {
return false;
}
if (mySourceRootTypes == null) {
return true;
}
ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
for (PsiDirectory dir : view.getDirectories()) {
if (projectFileIndex.isUnderSourceRootOfType(dir.getVirtualFile(), mySourceRootTypes) && checkPackageExists(dir)) {
return true;
}
}
return false;
}
use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-community by JetBrains.
the class CopyReferenceAction method getVirtualFileFqn.
private static String getVirtualFileFqn(@NotNull VirtualFile virtualFile, @NotNull Project project) {
final LogicalRoot logicalRoot = LogicalRootsManager.getLogicalRootsManager(project).findLogicalRoot(virtualFile);
VirtualFile logicalRootFile = logicalRoot != null ? logicalRoot.getVirtualFile() : null;
if (logicalRootFile != null && !virtualFile.equals(logicalRootFile)) {
return ObjectUtils.assertNotNull(VfsUtilCore.getRelativePath(virtualFile, logicalRootFile, '/'));
}
VirtualFile outerMostRoot = null;
VirtualFile each = virtualFile;
ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
while (each != null && (each = index.getContentRootForFile(each, false)) != null) {
outerMostRoot = each;
each = each.getParent();
}
if (outerMostRoot != null && !outerMostRoot.equals(virtualFile)) {
String relative = VfsUtilCore.getRelativePath(virtualFile, outerMostRoot, '/');
if (relative != null) {
return relative;
}
}
return virtualFile.getPath();
}
use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-community by JetBrains.
the class JavaAnalysisScope method getNarrowedComplementaryScope.
@Override
@NotNull
public AnalysisScope getNarrowedComplementaryScope(@NotNull Project defaultProject) {
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(defaultProject).getFileIndex();
if (myType == FILE) {
if (myElement instanceof PsiJavaFile && !FileTypeUtils.isInServerPageFile(myElement)) {
PsiJavaFile psiJavaFile = (PsiJavaFile) myElement;
final PsiClass[] classes = psiJavaFile.getClasses();
boolean onlyPackLocalClasses = true;
for (final PsiClass aClass : classes) {
if (aClass.hasModifierProperty(PsiModifier.PUBLIC)) {
onlyPackLocalClasses = false;
}
}
if (onlyPackLocalClasses) {
final PsiDirectory psiDirectory = psiJavaFile.getContainingDirectory();
if (psiDirectory != null) {
return new JavaAnalysisScope(JavaDirectoryService.getInstance().getPackage(psiDirectory), null);
}
}
}
} else if (myType == PACKAGE) {
final PsiDirectory[] directories = ((PsiPackage) myElement).getDirectories();
final HashSet<Module> modules = new HashSet<>();
for (PsiDirectory directory : directories) {
modules.addAll(getAllInterestingModules(fileIndex, directory.getVirtualFile()));
}
return collectScopes(defaultProject, modules);
}
return super.getNarrowedComplementaryScope(defaultProject);
}
use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-community by JetBrains.
the class MoveClassesOrPackagesUtil method chooseDestinationPackage.
@Nullable
public static PsiDirectory chooseDestinationPackage(Project project, String packageName, @Nullable PsiDirectory baseDir) {
final PsiManager psiManager = PsiManager.getInstance(project);
final PackageWrapper packageWrapper = new PackageWrapper(psiManager, packageName);
final PsiPackage aPackage = JavaPsiFacade.getInstance(project).findPackage(packageName);
PsiDirectory directory;
PsiDirectory[] directories = aPackage != null ? aPackage.getDirectories() : null;
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
final VirtualFile baseDirVirtualFile = baseDir != null ? baseDir.getVirtualFile() : null;
final boolean isBaseDirInTestSources = baseDirVirtualFile != null && fileIndex.isInTestSourceContent(baseDirVirtualFile);
if (directories != null && directories.length == 1 && (baseDirVirtualFile == null || fileIndex.isInTestSourceContent(directories[0].getVirtualFile()) == isBaseDirInTestSources)) {
directory = directories[0];
} else {
final List<VirtualFile> contentSourceRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(project);
if (contentSourceRoots.size() == 1 && (baseDirVirtualFile == null || fileIndex.isInTestSourceContent(contentSourceRoots.get(0)) == isBaseDirInTestSources)) {
directory = ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() {
@Override
public PsiDirectory compute() {
return RefactoringUtil.createPackageDirectoryInSourceRoot(packageWrapper, contentSourceRoots.get(0));
}
});
} else {
final VirtualFile sourceRootForFile = chooseSourceRoot(packageWrapper, contentSourceRoots, baseDir);
if (sourceRootForFile == null)
return null;
directory = ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() {
@Override
public PsiDirectory compute() {
return new AutocreatingSingleSourceRootMoveDestination(packageWrapper, sourceRootForFile).getTargetDirectory((PsiDirectory) null);
}
});
}
}
return directory;
}
Aggregations