use of com.intellij.openapi.roots.ModifiableRootModel in project intellij-community by JetBrains.
the class FindClassTest method testClassUnderExcludedFolder.
public void testClassUnderExcludedFolder() {
ApplicationManager.getApplication().runWriteAction(() -> {
PsiTestUtil.addExcludedRoot(myModule, myPackDir);
PsiClass psiClass = myJavaFacade.findClass("p.A", GlobalSearchScope.allScope(myProject));
assertNull(psiClass);
ModifiableRootModel rootModel = ModuleRootManager.getInstance(myModule).getModifiableModel();
final ContentEntry content = rootModel.getContentEntries()[0];
content.removeExcludeFolder(content.getExcludeFolders()[0]);
rootModel.commit();
psiClass = myJavaFacade.findClass("p.A", GlobalSearchScope.allScope(myProject));
assertEquals("p.A", psiClass.getQualifiedName());
});
}
use of com.intellij.openapi.roots.ModifiableRootModel in project intellij-community by JetBrains.
the class FrameworkSupportProviderTestCase method addSupport.
protected void addSupport() {
new WriteCommandAction.Simple(getProject()) {
@Override
protected void run() throws Throwable {
final VirtualFile root = getVirtualFile(createTempDir("contentRoot"));
PsiTestUtil.addContentRoot(myModule, root);
final ModifiableRootModel model = ModuleRootManager.getInstance(myModule).getModifiableModel();
try {
List<FrameworkSupportConfigurable> selectedConfigurables = new ArrayList<>();
final IdeaModifiableModelsProvider modelsProvider = new IdeaModifiableModelsProvider();
for (FrameworkSupportNode node : myNodes.values()) {
if (node.isChecked()) {
final FrameworkSupportInModuleConfigurable configurable = getOrCreateConfigurable(node.getUserObject());
configurable.addSupport(myModule, model, modelsProvider);
if (configurable instanceof OldFrameworkSupportProviderWrapper.FrameworkSupportConfigurableWrapper) {
selectedConfigurables.add(((OldFrameworkSupportProviderWrapper.FrameworkSupportConfigurableWrapper) configurable).getConfigurable());
}
}
}
for (FrameworkSupportCommunicator communicator : FrameworkSupportCommunicator.EP_NAME.getExtensions()) {
communicator.onFrameworkSupportAdded(myModule, model, selectedConfigurables, myFrameworkSupportModel);
}
} finally {
model.commit();
}
for (FrameworkSupportInModuleConfigurable configurable : myConfigurables.values()) {
Disposer.dispose(configurable);
}
}
}.execute().throwException();
}
use of com.intellij.openapi.roots.ModifiableRootModel 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.ModifiableRootModel in project intellij-community by JetBrains.
the class ModulesConfigurator method apply.
public void apply() throws ConfigurationException {
// validate content and source roots
final Map<VirtualFile, String> contentRootToModuleNameMap = new HashMap<>();
final Map<VirtualFile, VirtualFile> srcRootsToContentRootMap = new HashMap<>();
for (final ModuleEditor moduleEditor : myModuleEditors.values()) {
final ModifiableRootModel rootModel = moduleEditor.getModifiableRootModel();
final ContentEntry[] contents = rootModel.getContentEntries();
final String moduleName = moduleEditor.getName();
Set<VirtualFile> sourceRoots = new HashSet<>();
for (ContentEntry content : contents) {
for (VirtualFile root : content.getSourceFolderFiles()) {
if (!sourceRoots.add(root)) {
throw new ConfigurationException(ProjectBundle.message("module.paths.validation.duplicate.source.root.in.same.module.error", root.getPresentableUrl(), moduleName));
}
}
}
for (ContentEntry contentEntry : contents) {
final VirtualFile contentRoot = contentEntry.getFile();
if (contentRoot == null) {
continue;
}
final String previousName = contentRootToModuleNameMap.put(contentRoot, moduleName);
if (previousName != null && !previousName.equals(moduleName)) {
throw new ConfigurationException(ProjectBundle.message("module.paths.validation.duplicate.content.error", contentRoot.getPresentableUrl(), previousName, moduleName));
}
for (VirtualFile srcRoot : contentEntry.getSourceFolderFiles()) {
final VirtualFile anotherContentRoot = srcRootsToContentRootMap.put(srcRoot, contentRoot);
if (anotherContentRoot != null) {
final String problematicModule;
final String correctModule;
if (VfsUtilCore.isAncestor(anotherContentRoot, contentRoot, true)) {
problematicModule = contentRootToModuleNameMap.get(anotherContentRoot);
correctModule = contentRootToModuleNameMap.get(contentRoot);
} else {
problematicModule = contentRootToModuleNameMap.get(contentRoot);
correctModule = contentRootToModuleNameMap.get(anotherContentRoot);
}
throw new ConfigurationException(ProjectBundle.message("module.paths.validation.duplicate.source.root.error", problematicModule, srcRoot.getPresentableUrl(), correctModule));
}
}
}
}
// additional validation: directories marked as src roots must belong to the same module as their corresponding content root
for (Map.Entry<VirtualFile, VirtualFile> entry : srcRootsToContentRootMap.entrySet()) {
final VirtualFile srcRoot = entry.getKey();
final VirtualFile correspondingContent = entry.getValue();
final String expectedModuleName = contentRootToModuleNameMap.get(correspondingContent);
for (VirtualFile candidateContent = srcRoot; candidateContent != null && !candidateContent.equals(correspondingContent); candidateContent = candidateContent.getParent()) {
final String moduleName = contentRootToModuleNameMap.get(candidateContent);
if (moduleName != null && !moduleName.equals(expectedModuleName)) {
throw new ConfigurationException(ProjectBundle.message("module.paths.validation.source.root.belongs.to.another.module.error", srcRoot.getPresentableUrl(), expectedModuleName, moduleName));
}
}
}
for (ModuleEditor moduleEditor : myModuleEditors.values()) {
moduleEditor.canApply();
}
final Map<Sdk, Sdk> modifiedToOriginalMap = new THashMap<>();
final ProjectSdksModel projectJdksModel = ProjectStructureConfigurable.getInstance(myProject).getProjectJdksModel();
for (Map.Entry<Sdk, Sdk> entry : projectJdksModel.getProjectSdks().entrySet()) {
modifiedToOriginalMap.put(entry.getValue(), entry.getKey());
}
final Ref<ConfigurationException> exceptionRef = Ref.create();
ApplicationManager.getApplication().runWriteAction(() -> {
final List<ModifiableRootModel> models = new ArrayList<>(myModuleEditors.size());
try {
for (final ModuleEditor moduleEditor : myModuleEditors.values()) {
final ModifiableRootModel model = moduleEditor.apply();
if (model != null) {
if (!model.isSdkInherited()) {
// make sure the sdk is set to original SDK stored in the JDK Table
final Sdk modelSdk = model.getSdk();
if (modelSdk != null) {
final Sdk original = modifiedToOriginalMap.get(modelSdk);
if (original != null) {
model.setSdk(original);
}
}
}
models.add(model);
}
}
myFacetsConfigurator.applyEditors();
} catch (ConfigurationException e) {
exceptionRef.set(e);
return;
}
try {
ModifiableModelCommitter.multiCommit(models, myModuleModel);
myModuleModelCommitted = true;
myFacetsConfigurator.commitFacets();
} finally {
ModuleStructureConfigurable.getInstance(myProject).getFacetEditorFacade().clearMaps(false);
myFacetsConfigurator = createFacetsConfigurator();
myModuleModel = ModuleManager.getInstance(myProject).getModifiableModel();
myModuleModelCommitted = false;
}
});
if (!exceptionRef.isNull()) {
throw exceptionRef.get();
}
myModified = false;
}
use of com.intellij.openapi.roots.ModifiableRootModel 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