Search in sources :

Example 1 with ProjectRootManager

use of com.intellij.openapi.roots.ProjectRootManager in project intellij-community by JetBrains.

the class Generator method createBeanClass.

@NotNull
private static PsiClass createBeanClass(final WizardData wizardData) throws MyException {
    final PsiManager psiManager = PsiManager.getInstance(wizardData.myProject);
    final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(wizardData.myProject);
    final ProjectFileIndex fileIndex = projectRootManager.getFileIndex();
    final VirtualFile sourceRoot = fileIndex.getSourceRootForFile(wizardData.myFormFile);
    if (sourceRoot == null) {
        throw new MyException(UIDesignerBundle.message("error.form.file.is.not.in.source.root"));
    }
    final PsiDirectory rootDirectory = psiManager.findDirectory(sourceRoot);
    LOG.assertTrue(rootDirectory != null);
    final PsiPackage aPackage = JavaPsiFacade.getInstance(psiManager.getProject()).findPackage(wizardData.myPackageName);
    if (aPackage == null) {
        throw new MyException(UIDesignerBundle.message("error.package.does.not.exist", wizardData.myPackageName));
    }
    PsiDirectory targetDir = null;
    final PsiDirectory[] directories = aPackage.getDirectories();
    for (final PsiDirectory psiDirectory : directories) {
        if (PsiTreeUtil.isAncestor(rootDirectory, psiDirectory, false)) {
            targetDir = psiDirectory;
            break;
        }
    }
    if (targetDir == null) {
        // todo
        throw new MyException(UIDesignerBundle.message("error.cannot.find.package", wizardData.myPackageName));
    }
    //noinspection HardCodedStringLiteral
    final String body = "public class " + wizardData.myShortClassName + "{\n" + "public " + wizardData.myShortClassName + "(){}\n" + "}";
    try {
        PsiFile sourceFile = PsiFileFactory.getInstance(psiManager.getProject()).createFileFromText(wizardData.myShortClassName + ".java", body);
        sourceFile = (PsiFile) targetDir.add(sourceFile);
        final PsiClass beanClass = ((PsiJavaFile) sourceFile).getClasses()[0];
        final ArrayList<String> properties = new ArrayList<>();
        final HashMap<String, String> property2fqClassName = new HashMap<>();
        final FormProperty2BeanProperty[] bindings = wizardData.myBindings;
        for (final FormProperty2BeanProperty binding : bindings) {
            if (binding == null || binding.myBeanProperty == null) {
                continue;
            }
            properties.add(binding.myBeanProperty.myName);
            // todo: handle "casts" ?
            final String propertyClassName = binding.myFormProperty.getComponentPropertyClassName();
            property2fqClassName.put(binding.myBeanProperty.myName, propertyClassName);
        }
        generateBean(beanClass, ArrayUtil.toStringArray(properties), property2fqClassName);
        return beanClass;
    } catch (IncorrectOperationException e) {
        throw new MyException(e.getMessage());
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) IncorrectOperationException(com.intellij.util.IncorrectOperationException) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ProjectRootManager

use of com.intellij.openapi.roots.ProjectRootManager in project intellij-community by JetBrains.

the class AddImportHelper method getImportPriority.

@NotNull
public static ImportPriority getImportPriority(@NotNull PsiElement importLocation, @NotNull PsiFileSystemItem toImport) {
    final VirtualFile vFile = toImport.getVirtualFile();
    if (vFile == null) {
        return UNRESOLVED_SYMBOL_PRIORITY;
    }
    final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(toImport.getProject());
    final ProjectFileIndex fileIndex = projectRootManager.getFileIndex();
    if (fileIndex.isInContent(vFile) && !fileIndex.isInLibraryClasses(vFile)) {
        return ImportPriority.PROJECT;
    }
    final Module module = ModuleUtilCore.findModuleForPsiElement(importLocation);
    final Sdk pythonSdk = module != null ? PythonSdkType.findPythonSdk(module) : projectRootManager.getProjectSdk();
    return PythonSdkType.isStdLib(vFile, pythonSdk) ? ImportPriority.BUILTIN : ImportPriority.THIRD_PARTY;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ProjectRootManager

use of com.intellij.openapi.roots.ProjectRootManager in project intellij-community by JetBrains.

the class ElementOnlyUsedFromTestCodeInspection method isUnderTestSources.

private static boolean isUnderTestSources(PsiElement e) {
    final ProjectRootManager rootManager = ProjectRootManager.getInstance(e.getProject());
    final VirtualFile file = e.getContainingFile().getVirtualFile();
    return file != null && rootManager.getFileIndex().isInTestSourceContent(file);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager)

Example 4 with ProjectRootManager

use of com.intellij.openapi.roots.ProjectRootManager in project intellij-community by JetBrains.

the class TodoJavaTreeHelper method addPackagesToChildren.

@Override
public void addPackagesToChildren(final ArrayList<AbstractTreeNode> children, final Module module, final TodoTreeBuilder builder) {
    Project project = getProject();
    final PsiManager psiManager = PsiManager.getInstance(project);
    final List<VirtualFile> sourceRoots = new ArrayList<>();
    if (module == null) {
        final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(project);
        ContainerUtil.addAll(sourceRoots, projectRootManager.getContentSourceRoots());
    } else {
        ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
        ContainerUtil.addAll(sourceRoots, moduleRootManager.getSourceRoots());
    }
    final Set<PsiPackage> topLevelPackages = new HashSet<>();
    for (final VirtualFile root : sourceRoots) {
        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);
                } else {
                    final Iterator<PsiFile> files = builder.getFiles(subdirectory);
                    if (!files.hasNext())
                        continue;
                    TodoDirNode dirNode = new TodoDirNode(project, subdirectory, builder);
                    if (!children.contains(dirNode)) {
                        children.add(dirNode);
                    }
                }
            }
            // add non-dir items
            final Iterator<PsiFile> filesUnderDirectory = builder.getFilesUnderDirectory(directory);
            for (; filesUnderDirectory.hasNext(); ) {
                final PsiFile file = filesUnderDirectory.next();
                TodoFileNode todoFileNode = new TodoFileNode(project, file, builder, false);
                if (!children.contains(todoFileNode)) {
                    children.add(todoFileNode);
                }
            }
        } else {
            // this is the case when a source root has pakage prefix assigned
            topLevelPackages.add(directoryPackage);
        }
    }
    GlobalSearchScope scope = module != null ? GlobalSearchScope.moduleScope(module) : GlobalSearchScope.projectScope(project);
    ArrayList<PsiPackage> packages = new ArrayList<>();
    for (PsiPackage psiPackage : topLevelPackages) {
        final PsiPackage aPackage = findNonEmptyPackage(psiPackage, module, project, builder, scope);
        if (aPackage != null) {
            packages.add(aPackage);
        }
    }
    for (PsiPackage psiPackage : packages) {
        if (!builder.getTodoTreeStructure().getIsFlattenPackages()) {
            PackageElement element = new PackageElement(module, psiPackage, false);
            TodoPackageNode packageNode = new TodoPackageNode(project, element, builder, psiPackage.getQualifiedName());
            if (!children.contains(packageNode)) {
                children.add(packageNode);
            }
        } else {
            Set<PsiPackage> allPackages = new HashSet<>();
            traverseSubPackages(psiPackage, module, builder, project, allPackages);
            for (PsiPackage aPackage : allPackages) {
                TodoPackageNode packageNode = new TodoPackageNode(project, new PackageElement(module, aPackage, false), builder);
                if (!children.contains(packageNode)) {
                    children.add(packageNode);
                }
            }
        }
    }
    final List<VirtualFile> roots = collectContentRoots(module);
    roots.removeAll(sourceRoots);
    addDirsToChildren(roots, children, builder);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PackageElement(com.intellij.ide.projectView.impl.nodes.PackageElement) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager)

Example 5 with ProjectRootManager

use of com.intellij.openapi.roots.ProjectRootManager in project intellij-community by JetBrains.

the class GdkMethodHolder method getHolderForClass.

public static GdkMethodHolder getHolderForClass(final PsiClass categoryClass, final boolean isStatic, final GlobalSearchScope scope) {
    final Project project = categoryClass.getProject();
    Key<CachedValue<GdkMethodHolder>> key = isStatic ? CACHED_STATIC : CACHED_NON_STATIC;
    return CachedValuesManager.getManager(project).getCachedValue(categoryClass, key, () -> {
        GdkMethodHolder result = new GdkMethodHolder(categoryClass, isStatic, scope);
        final ProjectRootManager rootManager = ProjectRootManager.getInstance(project);
        final VirtualFile vfile = categoryClass.getContainingFile().getVirtualFile();
        if (vfile != null && (rootManager.getFileIndex().isInLibraryClasses(vfile) || rootManager.getFileIndex().isInLibrarySource(vfile))) {
            return CachedValueProvider.Result.create(result, rootManager);
        }
        return CachedValueProvider.Result.create(result, PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT, rootManager);
    }, false);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager)

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