use of com.intellij.openapi.roots.ProjectRootManager in project android by JetBrains.
the class AndroidPackageUtils method getPackageForPath.
/**
* Return the package associated with the target directory.
*/
@NotNull
public static String getPackageForPath(@NotNull AndroidFacet androidFacet, @NotNull List<AndroidSourceSet> sourceSets, @NotNull VirtualFile targetDirectory) {
if (sourceSets.size() > 0) {
Module module = androidFacet.getModule();
File srcDirectory = sourceSets.get(0).getPaths().getSrcDirectory();
if (srcDirectory != null) {
ProjectRootManager projectManager = ProjectRootManager.getInstance(module.getProject());
String suggestedPackage = projectManager.getFileIndex().getPackageNameByDirectory(targetDirectory);
if (suggestedPackage != null && !suggestedPackage.isEmpty()) {
return suggestedPackage;
}
}
}
return getPackageForApplication(androidFacet);
}
use of com.intellij.openapi.roots.ProjectRootManager in project intellij-community by JetBrains.
the class EmptyDirectoryInspection method getPathRelativeToModule.
@Nullable
private static String getPathRelativeToModule(VirtualFile file, Project project) {
final ProjectRootManager rootManager = ProjectRootManager.getInstance(project);
final VirtualFile[] contentRoots = ReadAction.compute(() -> rootManager.getContentRootsFromAllModules());
for (VirtualFile otherRoot : contentRoots) {
if (VfsUtilCore.isAncestor(otherRoot, file, false)) {
return VfsUtilCore.getRelativePath(file, otherRoot, '/');
}
}
return null;
}
use of com.intellij.openapi.roots.ProjectRootManager in project intellij-community by JetBrains.
the class JavaProjectDataService method importData.
@Override
public void importData(@NotNull final Collection<DataNode<JavaProjectData>> toImport, @Nullable final ProjectData projectData, @NotNull final Project project, @NotNull final IdeModifiableModelsProvider modelsProvider) {
if (toImport.isEmpty() || projectData == null) {
return;
}
if (toImport.size() != 1) {
throw new IllegalArgumentException(String.format("Expected to get a single project but got %d: %s", toImport.size(), toImport));
}
final DataNode<JavaProjectData> javaProjectDataNode = toImport.iterator().next();
final DataNode<ProjectData> projectDataNode = ExternalSystemApiUtil.findParent(javaProjectDataNode, ProjectKeys.PROJECT);
assert projectDataNode != null;
if (!ExternalSystemApiUtil.isOneToOneMapping(project, projectDataNode.getData())) {
return;
}
JavaProjectData javaProjectData = javaProjectDataNode.getData();
// JDK.
JavaSdkVersion version = javaProjectData.getJdkVersion();
JavaSdk javaSdk = JavaSdk.getInstance();
ProjectRootManager rootManager = ProjectRootManager.getInstance(project);
Sdk sdk = rootManager.getProjectSdk();
if (sdk != null) {
JavaSdkVersion currentVersion = javaSdk.getVersion(sdk);
if (currentVersion == null || !currentVersion.isAtLeast(version)) {
updateSdk(project, version);
}
} else {
updateSdk(project, version);
}
// Language level.
setLanguageLevel(javaProjectData.getLanguageLevel(), project);
}
use of com.intellij.openapi.roots.ProjectRootManager in project intellij-community by JetBrains.
the class TempFileSystemTest method testMove.
public void testMove() {
ProjectRootManager rootManager = ProjectRootManager.getInstance(getProject());
VirtualFile sourceRoot = rootManager.getContentSourceRoots()[0];
PsiManager psiManager = PsiManager.getInstance(getProject());
PsiDirectory psiSourceRoot = psiManager.findDirectory(sourceRoot);
PsiFile psiFile = ApplicationManager.getApplication().runWriteAction(new Computable<PsiFile>() {
@Override
public PsiFile compute() {
return psiSourceRoot.createFile("TestDocument.xml");
}
});
PsiDirectory subdirectory = ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() {
@Override
public PsiDirectory compute() {
return psiSourceRoot.createSubdirectory("com");
}
});
PlatformTestCase.move(psiFile.getVirtualFile(), subdirectory.getVirtualFile());
assertTrue(psiFile.isValid());
ApplicationManager.getApplication().runWriteAction(() -> psiFile.delete());
assertFalse(psiFile.isValid());
}
use of com.intellij.openapi.roots.ProjectRootManager in project intellij-community by JetBrains.
the class PackageViewProjectNode method getChildren.
@Override
@NotNull
public Collection<AbstractTreeNode> getChildren() {
if (getSettings().isShowModules()) {
final List<Module> allModules = new ArrayList<>(Arrays.asList(ModuleManager.getInstance(getProject()).getModules()));
for (Iterator<Module> it = allModules.iterator(); it.hasNext(); ) {
final Module module = it.next();
final VirtualFile[] sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots();
if (sourceRoots.length == 0) {
// do not show modules with no source roots configured
it.remove();
}
}
return modulesAndGroups(allModules.toArray(new Module[allModules.size()]));
} else {
final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(myProject);
final PsiManager psiManager = PsiManager.getInstance(myProject);
final List<AbstractTreeNode> children = new ArrayList<>();
final Set<PsiPackage> topLevelPackages = new HashSet<>();
for (final VirtualFile root : projectRootManager.getContentSourceRoots()) {
final PsiDirectory directory = psiManager.findDirectory(root);
if (directory == null) {
continue;
}
final PsiPackage directoryPackage = JavaDirectoryService.getInstance().getPackage(directory);
if (directoryPackage == null || PackageUtil.isPackageDefault(directoryPackage)) {
// add subpackages
final PsiDirectory[] subdirectories = directory.getSubdirectories();
for (PsiDirectory subdirectory : subdirectories) {
final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(subdirectory);
if (aPackage != null && !PackageUtil.isPackageDefault(aPackage)) {
topLevelPackages.add(aPackage);
}
}
// add non-dir items
children.addAll(ProjectViewDirectoryHelper.getInstance(myProject).getDirectoryChildren(directory, getSettings(), false));
} else {
// this is the case when a source root has pakage prefix assigned
topLevelPackages.add(directoryPackage);
}
}
for (final PsiPackage psiPackage : topLevelPackages) {
PackageUtil.addPackageAsChild(children, psiPackage, null, getSettings(), false);
}
if (getSettings().isShowLibraryContents()) {
children.add(new PackageViewLibrariesNode(getProject(), null, getSettings()));
}
return children;
}
}
Aggregations