Search in sources :

Example 1 with PreferredSdkComparator

use of com.jetbrains.python.sdk.PreferredSdkComparator in project intellij-community by JetBrains.

the class PyPluginCommonOptionsForm method setSdkHome.

public void setSdkHome(String sdkHome) {
    List<Sdk> sdkList = new ArrayList<>();
    sdkList.add(null);
    final List<Sdk> allSdks = PythonSdkType.getAllSdks();
    Collections.sort(allSdks, new PreferredSdkComparator());
    Sdk selection = null;
    for (Sdk sdk : allSdks) {
        String homePath = sdk.getHomePath();
        if (homePath != null && sdkHome != null && FileUtil.pathsEqual(homePath, sdkHome))
            selection = sdk;
        sdkList.add(sdk);
    }
    myInterpreterComboBox.setModel(new CollectionComboBoxModel(sdkList, selection));
}
Also used : PreferredSdkComparator(com.jetbrains.python.sdk.PreferredSdkComparator) ArrayList(java.util.ArrayList) CollectionComboBoxModel(com.intellij.ui.CollectionComboBoxModel) Sdk(com.intellij.openapi.projectRoots.Sdk)

Example 2 with PreferredSdkComparator

use of com.jetbrains.python.sdk.PreferredSdkComparator in project intellij-community by JetBrains.

the class PythonSdkConfigurator method configureProject.

public void configureProject(final Project project, @NotNull final VirtualFile baseDir, Ref<Module> moduleRef) {
    // it it a virtualenv?
    final List<VirtualFile> children = VfsUtil.getChildren(baseDir, new VirtualFileFilter() {

        @Override
        public boolean accept(VirtualFile file) {
            return !Project.DIRECTORY_STORE_FOLDER.equals(file.getName());
        }
    });
    if (children.isEmpty())
        return;
    final PythonSdkType sdkType = PythonSdkType.getInstance();
    //find virtualEnv in project directory
    final List<String> candidates = new ArrayList<>();
    if (project == null)
        return;
    final VirtualFile rootDir = project.getBaseDir();
    if (rootDir != null)
        candidates.addAll(VirtualEnvSdkFlavor.findInDirectory(rootDir));
    if (!candidates.isEmpty()) {
        String filePath = candidates.get(0);
        if (StringUtil.startsWithChar(filePath, '~')) {
            final String home = SystemProperties.getUserHome();
            filePath = home + filePath.substring(1);
        }
        final Sdk virtualEnvSdk = SdkConfigurationUtil.createAndAddSDK(filePath, sdkType);
        if (virtualEnvSdk != null) {
            SdkConfigurationUtil.setDirectoryProjectSdk(project, virtualEnvSdk);
            SdkAdditionalData additionalData = virtualEnvSdk.getSdkAdditionalData();
            if (additionalData == null) {
                additionalData = new PythonSdkAdditionalData(PythonSdkFlavor.getFlavor(virtualEnvSdk.getHomePath()));
                ((ProjectJdkImpl) virtualEnvSdk).setSdkAdditionalData(additionalData);
            }
            ((PythonSdkAdditionalData) additionalData).associateWithProject(project);
            return;
        }
        return;
    }
    final Sdk existingSdk = ProjectRootManager.getInstance(project).getProjectSdk();
    // SdkConfigurationUtil does the same
    if (existingSdk != null && existingSdk.getSdkType() == sdkType)
        return;
    final File executableFile = PythonSdkType.findExecutableFile(new File(project.getBasePath(), "bin"), "python");
    if (executableFile != null) {
        final File virtualEnvRoot = PythonSdkType.getVirtualEnvRoot(executableFile.getPath());
        if (virtualEnvRoot != null) {
            // yes, an unknown virtualenv; set it up as SDK
            final Sdk sdk = SdkConfigurationUtil.createAndAddSDK(executableFile.getPath(), sdkType);
            if (sdk != null) {
                SdkConfigurationUtil.setDirectoryProjectSdk(project, sdk);
                return;
            }
        }
    }
    // default
    SdkConfigurationUtil.configureDirectoryProjectSdk(project, new PreferredSdkComparator(), sdkType);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PreferredSdkComparator(com.jetbrains.python.sdk.PreferredSdkComparator) ArrayList(java.util.ArrayList) PythonSdkAdditionalData(com.jetbrains.python.sdk.PythonSdkAdditionalData) ProjectJdkImpl(com.intellij.openapi.projectRoots.impl.ProjectJdkImpl) VirtualFileFilter(com.intellij.openapi.vfs.VirtualFileFilter) PythonSdkType(com.jetbrains.python.sdk.PythonSdkType) Sdk(com.intellij.openapi.projectRoots.Sdk) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) PythonSdkAdditionalData(com.jetbrains.python.sdk.PythonSdkAdditionalData) SdkAdditionalData(com.intellij.openapi.projectRoots.SdkAdditionalData)

Aggregations

Sdk (com.intellij.openapi.projectRoots.Sdk)2 PreferredSdkComparator (com.jetbrains.python.sdk.PreferredSdkComparator)2 ArrayList (java.util.ArrayList)2 SdkAdditionalData (com.intellij.openapi.projectRoots.SdkAdditionalData)1 ProjectJdkImpl (com.intellij.openapi.projectRoots.impl.ProjectJdkImpl)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 VirtualFileFilter (com.intellij.openapi.vfs.VirtualFileFilter)1 CollectionComboBoxModel (com.intellij.ui.CollectionComboBoxModel)1 PythonSdkAdditionalData (com.jetbrains.python.sdk.PythonSdkAdditionalData)1 PythonSdkType (com.jetbrains.python.sdk.PythonSdkType)1 File (java.io.File)1