use of com.intellij.openapi.roots.ModuleRootManager in project intellij-community by JetBrains.
the class SaveProjectAsTemplateAction method collectStructure.
@NotNull
private static List<LocalArchivedTemplate.RootDescription> collectStructure(Project project, Module moduleToSave) {
List<LocalArchivedTemplate.RootDescription> result = new ArrayList<>();
if (moduleToSave != null) {
PathMacroManager macroManager = PathMacroManager.getInstance(moduleToSave);
ModuleRootManager rootManager = ModuleRootManager.getInstance(moduleToSave);
int i = 0;
for (VirtualFile file : rootManager.getContentRoots()) {
result.add(i, describeRoot(file, i, macroManager));
i++;
}
} else {
PathMacroManager macroManager = PathMacroManager.getInstance(project);
ProjectRootManager rootManager = ProjectRootManager.getInstance(project);
int i = 0;
for (VirtualFile file : rootManager.getContentRoots()) {
result.add(i, describeRoot(file, i, macroManager));
i++;
}
}
return result;
}
use of com.intellij.openapi.roots.ModuleRootManager in project intellij-community by JetBrains.
the class WebModuleBuilder method doGenerate.
private static <T> void doGenerate(@NotNull WebProjectTemplate<T> template, @NotNull Module module) {
WebProjectGenerator.GeneratorPeer<T> peer = template.getPeer();
ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
VirtualFile[] contentRoots = moduleRootManager.getContentRoots();
VirtualFile dir = module.getProject().getBaseDir();
if (contentRoots.length > 0 && contentRoots[0] != null) {
dir = contentRoots[0];
}
template.generateProject(module.getProject(), dir, peer.getSettings(), module);
}
use of com.intellij.openapi.roots.ModuleRootManager in project intellij-community by JetBrains.
the class CodeInsightTestCase method configureByFiles.
protected VirtualFile configureByFiles(@Nullable final File rawProjectRoot, @NotNull final VirtualFile... vFiles) throws IOException {
myFile = null;
myEditor = null;
final File toDirIO = createTempDirectory();
final VirtualFile toDir = getVirtualFile(toDirIO);
ApplicationManager.getApplication().runWriteAction(() -> {
try {
final ModuleRootManager rootManager = ModuleRootManager.getInstance(myModule);
final ModifiableRootModel rootModel = rootManager.getModifiableModel();
if (clearModelBeforeConfiguring()) {
rootModel.clear();
}
// auxiliary files should be copied first
VirtualFile[] reversed = ArrayUtil.reverseArray(vFiles);
Map<VirtualFile, EditorInfo> editorInfos;
if (rawProjectRoot != null) {
final File projectRoot = rawProjectRoot.getCanonicalFile();
FileUtil.copyDir(projectRoot, toDirIO);
VirtualFile fromDir = getVirtualFile(projectRoot);
editorInfos = copyFilesFillingEditorInfos(fromDir, toDir, ContainerUtil.map2Array(reversed, String.class, s -> s.getPath().substring(projectRoot.getPath().length())));
toDir.refresh(false, true);
} else {
editorInfos = new LinkedHashMap<>();
for (final VirtualFile vFile : reversed) {
VirtualFile parent = vFile.getParent();
assert parent.isDirectory() : parent;
editorInfos.putAll(copyFilesFillingEditorInfos(parent, toDir, vFile.getName()));
}
}
boolean sourceRootAdded = false;
if (isAddDirToContentRoot()) {
final ContentEntry contentEntry = rootModel.addContentEntry(toDir);
if (isAddDirToSource()) {
sourceRootAdded = true;
contentEntry.addSourceFolder(toDir, isAddDirToTests());
}
}
doCommitModel(rootModel);
if (sourceRootAdded) {
sourceRootAdded(toDir);
}
openEditorsAndActivateLast(editorInfos);
} catch (IOException e) {
LOG.error(e);
}
});
return toDir;
}
use of com.intellij.openapi.roots.ModuleRootManager 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.ModuleRootManager in project intellij-community by JetBrains.
the class ModuleFixtureBuilderImpl method initModule.
protected void initModule(Module module) {
final ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
final ModifiableRootModel rootModel = rootManager.getModifiableModel();
for (String contentRoot : myContentRoots) {
final VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(contentRoot);
Assert.assertNotNull("cannot find content root: " + contentRoot, virtualFile);
final ContentEntry contentEntry = rootModel.addContentEntry(virtualFile);
for (String sourceRoot : mySourceRoots) {
String s = contentRoot + "/" + sourceRoot;
VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByPath(s);
if (vf == null) {
final VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByPath(sourceRoot);
if (file != null && VfsUtilCore.isAncestor(virtualFile, file, false))
vf = file;
}
// assert vf != null : "cannot find source root: " + sourceRoot;
if (vf != null) {
contentEntry.addSourceFolder(vf, false);
} else {
// files are not created yet
contentEntry.addSourceFolder(VfsUtilCore.pathToUrl(s), false);
}
}
}
setupRootModel(rootModel);
rootModel.commit();
}
Aggregations