Search in sources :

Example 16 with ProjectRootManager

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);
}
Also used : Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with ProjectRootManager

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with ProjectRootManager

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);
}
Also used : JavaSdkVersion(com.intellij.openapi.projectRoots.JavaSdkVersion) JavaSdk(com.intellij.openapi.projectRoots.JavaSdk) JavaSdk(com.intellij.openapi.projectRoots.JavaSdk) Sdk(com.intellij.openapi.projectRoots.Sdk) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) ProjectData(com.intellij.openapi.externalSystem.model.project.ProjectData)

Example 19 with ProjectRootManager

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());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiDirectory(com.intellij.psi.PsiDirectory) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager)

Example 20 with ProjectRootManager

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;
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiManager(com.intellij.psi.PsiManager) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) PsiPackage(com.intellij.psi.PsiPackage) PsiDirectory(com.intellij.psi.PsiDirectory) Module(com.intellij.openapi.module.Module) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ProjectRootManager (com.intellij.openapi.roots.ProjectRootManager)29 VirtualFile (com.intellij.openapi.vfs.VirtualFile)22 Project (com.intellij.openapi.project.Project)8 Module (com.intellij.openapi.module.Module)7 Sdk (com.intellij.openapi.projectRoots.Sdk)6 NotNull (org.jetbrains.annotations.NotNull)6 Nullable (org.jetbrains.annotations.Nullable)6 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)4 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)4 File (java.io.File)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)2 NullableComputable (com.intellij.openapi.util.NullableComputable)2 PsiDirectory (com.intellij.psi.PsiDirectory)2 PsiManager (com.intellij.psi.PsiManager)2 PackageWrapper (com.intellij.refactoring.PackageWrapper)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 CheckStylePlugin (org.infernus.idea.checkstyle.CheckStylePlugin)2 ScanScope (org.infernus.idea.checkstyle.model.ScanScope)2