use of com.intellij.openapi.roots.ModuleRootManager in project intellij-community by JetBrains.
the class InheritedJdkTest method test2.
public void test2() throws Exception {
final ModuleRootManager rootManager = ModuleRootManager.getInstance(myModule);
ModuleRootModificationUtil.setSdkInherited(myModule);
assertTrue("JDK is inherited after inheritSdk()", rootManager.isSdkInherited());
assertNull("No JDK assigned", rootManager.getSdk());
final Sdk mockJdk = IdeaTestUtil.getMockJdk17("mock 1.4");
ApplicationManager.getApplication().runWriteAction(() -> ProjectJdkTable.getInstance().addJdk(mockJdk));
final ProjectRootManagerEx projectRootManager = ProjectRootManagerEx.getInstanceEx(myProject);
ApplicationManager.getApplication().runWriteAction(() -> projectRootManager.setProjectSdk(mockJdk));
assertTrue(rootManager.isSdkInherited());
assertEquals("mockJdk inherited", mockJdk, rootManager.getSdk());
ApplicationManager.getApplication().runWriteAction(() -> projectRootManager.setProjectSdkName("jdk1"));
assertTrue(rootManager.isSdkInherited());
Assert.assertEquals("Correct non-existing JDK inherited", "jdk1", rootManager.orderEntries().process(new RootPolicy<String>() {
@Override
public String visitInheritedJdkOrderEntry(InheritedJdkOrderEntry inheritedJdkOrderEntry, String s) {
return inheritedJdkOrderEntry.getJdkName();
}
}, null));
assertNull("Non-existing JDK", rootManager.getSdk());
final Sdk jdk1 = IdeaTestUtil.getMockJdk17("jdk1");
ApplicationManager.getApplication().runWriteAction(() -> ProjectJdkTable.getInstance().addJdk(jdk1));
assertTrue(rootManager.isSdkInherited());
assertNotNull("JDK appeared", rootManager.getSdk());
assertEquals("jdk1 found", jdk1, rootManager.getSdk());
}
use of com.intellij.openapi.roots.ModuleRootManager in project intellij-community by JetBrains.
the class MultiModuleEditingTest method testRenaming.
public void testRenaming() throws Exception {
final ModuleManager moduleManager = ModuleManager.getInstance(myProject);
final Module moduleA;
final Module moduleB;
{
final ModifiableModuleModel moduleModel = moduleManager.getModifiableModel();
moduleA = moduleModel.newModule("a.iml", StdModuleTypes.JAVA.getId());
moduleB = moduleModel.newModule("b.iml", StdModuleTypes.JAVA.getId());
final Module moduleC = moduleModel.newModule("c.iml", StdModuleTypes.JAVA.getId());
final ModifiableRootModel rootModelB = ModuleRootManager.getInstance(moduleB).getModifiableModel();
rootModelB.addModuleOrderEntry(moduleC);
moduleModel.disposeModule(moduleC);
ApplicationManager.getApplication().runWriteAction(() -> ModifiableModelCommitter.multiCommit(new ModifiableRootModel[] { rootModelB }, moduleModel));
}
final ModuleRootManager rootManagerB = ModuleRootManager.getInstance(moduleB);
assertEquals(0, rootManagerB.getDependencies().length);
final String[] dependencyModuleNames = rootManagerB.getDependencyModuleNames();
assertEquals(1, dependencyModuleNames.length);
assertEquals("c", dependencyModuleNames[0]);
{
final ModifiableModuleModel moduleModel = moduleManager.getModifiableModel();
moduleModel.renameModule(moduleA, "c");
assertSame(moduleA, moduleModel.findModuleByName("a"));
assertSame(moduleA, moduleManager.findModuleByName("a"));
assertEquals("c", moduleModel.getNewName(moduleA));
assertSame(moduleA, moduleModel.getModuleToBeRenamed("c"));
ApplicationManager.getApplication().runWriteAction(() -> moduleModel.commit());
}
assertEquals(1, rootManagerB.getDependencies().length);
assertEquals(moduleA, rootManagerB.getDependencies()[0]);
assertSame(moduleA, moduleManager.findModuleByName("c"));
assertNull(moduleManager.findModuleByName("a"));
assertEquals("c", moduleA.getName());
moduleManager.disposeModule(moduleA);
moduleManager.disposeModule(moduleB);
}
use of com.intellij.openapi.roots.ModuleRootManager in project intellij-community by JetBrains.
the class FilePathReferenceProvider method getRoots.
@NotNull
public static Collection<PsiFileSystemItem> getRoots(@Nullable final Module thisModule, boolean includingClasses) {
if (thisModule == null)
return Collections.emptyList();
ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(thisModule);
Set<PsiFileSystemItem> result = ContainerUtil.newLinkedHashSet();
final PsiManager psiManager = PsiManager.getInstance(thisModule.getProject());
if (includingClasses) {
VirtualFile[] libraryUrls = moduleRootManager.orderEntries().getAllLibrariesAndSdkClassesRoots();
for (VirtualFile file : libraryUrls) {
PsiDirectory directory = psiManager.findDirectory(file);
if (directory != null) {
result.add(directory);
}
}
}
VirtualFile[] sourceRoots = moduleRootManager.orderEntries().recursively().withoutSdk().withoutLibraries().sources().usingCache().getRoots();
for (VirtualFile root : sourceRoots) {
final PsiDirectory directory = psiManager.findDirectory(root);
if (directory != null) {
final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(directory);
if (aPackage != null && aPackage.getName() != null) {
// package prefix
result.add(PackagePrefixFileSystemItemImpl.create(directory));
} else {
result.add(directory);
}
}
}
return result;
}
use of com.intellij.openapi.roots.ModuleRootManager in project intellij-community by JetBrains.
the class PythonModuleBuilderBase method commitModule.
@Nullable
@Override
public Module commitModule(@NotNull Project project, @Nullable ModifiableModuleModel model) {
Module module = super.commitModule(project, model);
if (module != null && myGenerator != null) {
ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
VirtualFile[] contentRoots = moduleRootManager.getContentRoots();
VirtualFile dir = module.getProject().getBaseDir();
if (contentRoots.length > 0 && contentRoots[0] != null) {
dir = contentRoots[0];
}
myGenerator.generateProject(project, dir, null, module);
}
return module;
}
use of com.intellij.openapi.roots.ModuleRootManager in project intellij-community by JetBrains.
the class PyPackageUtil method findRequirementsTxt.
@Nullable
public static VirtualFile findRequirementsTxt(@NotNull Module module) {
final String requirementsPath = PyPackageRequirementsSettings.getInstance(module).getRequirementsPath();
if (!requirementsPath.isEmpty()) {
final VirtualFile file = LocalFileSystem.getInstance().findFileByPath(requirementsPath);
if (file != null) {
return file;
}
final ModuleRootManager manager = ModuleRootManager.getInstance(module);
for (VirtualFile root : manager.getContentRoots()) {
final VirtualFile fileInRoot = root.findFileByRelativePath(requirementsPath);
if (fileInRoot != null) {
return fileInRoot;
}
}
}
return null;
}
Aggregations