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());
}
}
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;
}
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);
}
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);
}
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);
}
Aggregations