Search in sources :

Example 1 with ProjectSdksModel

use of com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel in project intellij-community by JetBrains.

the class PythonGenerateProjectCallback method addDetectedSdk.

private static Sdk addDetectedSdk(ProjectSpecificSettingsStep settingsStep, Sdk sdk) {
    final Project project = ProjectManager.getInstance().getDefaultProject();
    final ProjectSdksModel model = PyConfigurableInterpreterList.getInstance(project).getModel();
    final String name = sdk.getName();
    VirtualFile sdkHome = ApplicationManager.getApplication().runWriteAction((Computable<VirtualFile>) () -> LocalFileSystem.getInstance().refreshAndFindFileByPath(name));
    sdk = SdkConfigurationUtil.createAndAddSDK(sdkHome.getPath(), PythonSdkType.getInstance());
    if (sdk != null) {
        PythonSdkUpdater.updateOrShowError(sdk, null, project, null);
    }
    model.addSdk(sdk);
    settingsStep.setSdk(sdk);
    try {
        model.apply();
    } catch (ConfigurationException exception) {
        LOG.error("Error adding detected python interpreter " + exception.getMessage());
    }
    return sdk;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) ProjectSdksModel(com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel) ConfigurationException(com.intellij.openapi.options.ConfigurationException)

Example 2 with ProjectSdksModel

use of com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel in project intellij-community by JetBrains.

the class PythonSdkChooserCombo method showOptions.

private void showOptions(final Project project) {
    final PyConfigurableInterpreterList interpreterList = PyConfigurableInterpreterList.getInstance(project);
    final Sdk[] sdks = interpreterList.getModel().getSdks();
    //noinspection unchecked
    final JComboBox<Sdk> comboBox = getComboBox();
    final Sdk oldSelectedSdk = (Sdk) comboBox.getSelectedItem();
    PythonSdkDetailsStep.show(project, sdks, null, this, getButton().getLocationOnScreen(), sdk -> {
        if (sdk == null)
            return;
        final ProjectSdksModel projectSdksModel = interpreterList.getModel();
        if (projectSdksModel.findSdk(sdk) == null) {
            projectSdksModel.addSdk(sdk);
            try {
                projectSdksModel.apply();
            } catch (ConfigurationException e) {
                LOG.error("Error adding new python interpreter " + e.getMessage());
            }
        }
        final List<Sdk> committedSdks = interpreterList.getAllPythonSdks();
        final Sdk copiedSdk = interpreterList.getModel().findSdk(sdk.getName());
        comboBox.setModel(new CollectionComboBoxModel<>(committedSdks, oldSelectedSdk));
        comboBox.setSelectedItem(copiedSdk);
    }, true);
}
Also used : PyConfigurableInterpreterList(com.jetbrains.python.configuration.PyConfigurableInterpreterList) ProjectSdksModel(com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel) ConfigurationException(com.intellij.openapi.options.ConfigurationException) Sdk(com.intellij.openapi.projectRoots.Sdk)

Example 3 with ProjectSdksModel

use of com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel 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 4 with ProjectSdksModel

use of com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel in project intellij-community by JetBrains.

the class GeneralProjectSettingsElement method check.

@Override
public void check(ProjectStructureProblemsHolder problemsHolder) {
    final Project project = myContext.getProject();
    if (containsModuleWithInheritedSdk()) {
        ProjectSdksModel model = ProjectStructureConfigurable.getInstance(project).getProjectJdksModel();
        Sdk sdk = model.getProjectSdk();
        if (sdk == null) {
            PlaceInProjectStructureBase place = new PlaceInProjectStructureBase(project, ProjectStructureConfigurable.getInstance(project).createProjectConfigurablePlace(), this);
            problemsHolder.registerProblem(ProjectBundle.message("project.roots.project.jdk.problem.message"), null, ProjectStructureProblemType.error("project-sdk-not-defined"), place, null);
        }
    }
    List<Chunk<ModuleSourceSet>> sourceSetCycles = ModuleCompilerUtil.computeSourceSetCycles(myContext.getModulesConfigurator());
    List<String> cycles = new ArrayList<>();
    for (Chunk<ModuleSourceSet> chunk : sourceSetCycles) {
        final Set<ModuleSourceSet> sourceSets = chunk.getNodes();
        List<String> names = new ArrayList<>();
        for (ModuleSourceSet sourceSet : sourceSets) {
            String name = sourceSet.getDisplayName();
            names.add(names.isEmpty() ? name : StringUtil.decapitalize(name));
        }
        cycles.add(StringUtil.join(names, ", "));
    }
    if (!cycles.isEmpty()) {
        final PlaceInProjectStructureBase place = new PlaceInProjectStructureBase(project, ProjectStructureConfigurable.getInstance(project).createModulesPlace(), this);
        final String message;
        final String description;
        if (cycles.size() > 1) {
            message = "Circular dependencies";
            @NonNls final String br = "<br>&nbsp;&nbsp;&nbsp;&nbsp;";
            StringBuilder cyclesString = new StringBuilder();
            for (int i = 0; i < cycles.size(); i++) {
                cyclesString.append(br).append(i + 1).append(". ").append(cycles.get(i));
            }
            description = ProjectBundle.message("module.circular.dependency.warning.description", cyclesString);
        } else {
            message = ProjectBundle.message("module.circular.dependency.warning.short", StringUtil.decapitalize(cycles.get(0)));
            description = null;
        }
        problemsHolder.registerProblem(new ProjectStructureProblemDescription(message, description, place, ProjectStructureProblemType.warning("module-circular-dependency"), Collections.<ConfigurationErrorQuickFix>emptyList()));
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) ProjectSdksModel(com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel) ModuleSourceSet(com.intellij.compiler.ModuleSourceSet) Chunk(com.intellij.util.Chunk) Project(com.intellij.openapi.project.Project) Sdk(com.intellij.openapi.projectRoots.Sdk)

Example 5 with ProjectSdksModel

use of com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel in project intellij-community by JetBrains.

the class JdkChooserPanel method fillList.

public void fillList(@Nullable final SdkType type, @Nullable final Sdk[] globalSdks) {
    myListModel.clear();
    final Sdk[] jdks;
    if (myProject == null || myProject.isDefault()) {
        final Sdk[] allJdks = globalSdks != null ? globalSdks : ProjectJdkTable.getInstance().getAllJdks();
        jdks = getCompatibleJdks(type, Arrays.asList(allJdks));
    } else {
        final ProjectSdksModel projectJdksModel = ProjectStructureConfigurable.getInstance(myProject).getProjectJdksModel();
        if (!projectJdksModel.isInitialized()) {
            //should be initialized
            projectJdksModel.reset(myProject);
        }
        final Collection<Sdk> collection = projectJdksModel.getProjectSdks().values();
        jdks = getCompatibleJdks(type, collection);
    }
    Arrays.sort(jdks, (o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName()));
    for (Sdk jdk : jdks) {
        myListModel.addElement(jdk);
    }
}
Also used : ProjectSdksModel(com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel) Sdk(com.intellij.openapi.projectRoots.Sdk)

Aggregations

ProjectSdksModel (com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel)10 Sdk (com.intellij.openapi.projectRoots.Sdk)7 ConfigurationException (com.intellij.openapi.options.ConfigurationException)4 Project (com.intellij.openapi.project.Project)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PyConfigurableInterpreterList (com.jetbrains.python.configuration.PyConfigurableInterpreterList)2 Nullable (org.jetbrains.annotations.Nullable)2 ModuleSourceSet (com.intellij.compiler.ModuleSourceSet)1 ExecutionException (com.intellij.execution.ExecutionException)1 DataManager (com.intellij.ide.DataManager)1 com.intellij.openapi.actionSystem (com.intellij.openapi.actionSystem)1 ProjectBundle (com.intellij.openapi.project.ProjectBundle)1 SdkAdditionalData (com.intellij.openapi.projectRoots.SdkAdditionalData)1 SdkType (com.intellij.openapi.projectRoots.SdkType)1 SdkTypeId (com.intellij.openapi.projectRoots.SdkTypeId)1 ContentEntry (com.intellij.openapi.roots.ContentEntry)1 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)1 OrderEntryAppearanceService (com.intellij.openapi.roots.ui.OrderEntryAppearanceService)1 JdkListConfigurable (com.intellij.openapi.roots.ui.configuration.projectRoot.JdkListConfigurable)1 ComboBoxWithWidePopup (com.intellij.openapi.ui.ComboBoxWithWidePopup)1