Search in sources :

Example 16 with ModifiableRootModel

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());
    });
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) ContentEntry(com.intellij.openapi.roots.ContentEntry) PsiClass(com.intellij.psi.PsiClass)

Example 17 with ModifiableRootModel

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();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) IdeaModifiableModelsProvider(com.intellij.openapi.roots.IdeaModifiableModelsProvider) FrameworkSupportNode(com.intellij.ide.util.newProjectWizard.FrameworkSupportNode) OldFrameworkSupportProviderWrapper(com.intellij.ide.util.newProjectWizard.OldFrameworkSupportProviderWrapper) FrameworkSupportInModuleConfigurable(com.intellij.framework.addSupport.FrameworkSupportInModuleConfigurable) FrameworkSupportCommunicator(com.intellij.ide.util.newProjectWizard.impl.FrameworkSupportCommunicator)

Example 18 with ModifiableRootModel

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;
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) ContentEntry(com.intellij.openapi.roots.ContentEntry) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) IOException(java.io.IOException) PsiFile(com.intellij.psi.PsiFile) File(java.io.File)

Example 19 with ModifiableRootModel

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectSdksModel(com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel) HashMap(com.intellij.util.containers.HashMap) THashMap(gnu.trove.THashMap) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) THashMap(gnu.trove.THashMap) ConfigurationException(com.intellij.openapi.options.ConfigurationException) ContentEntry(com.intellij.openapi.roots.ContentEntry) Sdk(com.intellij.openapi.projectRoots.Sdk) HashMap(com.intellij.util.containers.HashMap) THashMap(gnu.trove.THashMap)

Example 20 with ModifiableRootModel

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();
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ContentEntry(com.intellij.openapi.roots.ContentEntry) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager)

Aggregations

ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)101 Module (com.intellij.openapi.module.Module)44 VirtualFile (com.intellij.openapi.vfs.VirtualFile)29 ContentEntry (com.intellij.openapi.roots.ContentEntry)27 File (java.io.File)18 Library (com.intellij.openapi.roots.libraries.Library)15 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)14 ModuleManager (com.intellij.openapi.module.ModuleManager)8 Sdk (com.intellij.openapi.projectRoots.Sdk)8 IOException (java.io.IOException)8 NotNull (org.jetbrains.annotations.NotNull)8 OrderEntry (com.intellij.openapi.roots.OrderEntry)7 Project (com.intellij.openapi.project.Project)6 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)6 Nullable (org.jetbrains.annotations.Nullable)6 ModifiableModuleModel (com.intellij.openapi.module.ModifiableModuleModel)5 ConfigurationException (com.intellij.openapi.options.ConfigurationException)5 THashMap (gnu.trove.THashMap)5 FlexProjectConfigurationEditor (com.intellij.lang.javascript.flex.projectStructure.model.impl.FlexProjectConfigurationEditor)4 AccessToken (com.intellij.openapi.application.AccessToken)4