Search in sources :

Example 1 with PathMapper

use of com.intellij.util.PathMapper in project intellij-community by JetBrains.

the class PydevConsoleRunnerFactory method createConsoleRunner.

@Override
@NotNull
public PydevConsoleRunnerImpl createConsoleRunner(@NotNull Project project, @Nullable Module contextModule) {
    Pair<Sdk, Module> sdkAndModule = PydevConsoleRunner.findPythonSdkAndModule(project, contextModule);
    Module module = sdkAndModule.second;
    Sdk sdk = sdkAndModule.first;
    assert sdk != null;
    PyConsoleOptions.PyConsoleSettings settingsProvider = PyConsoleOptions.getInstance(project).getPythonConsoleSettings();
    PathMapper pathMapper = PydevConsoleRunner.getPathMapper(project, sdk, settingsProvider);
    String[] setupFragment;
    Collection<String> pythonPath = PythonCommandLineState.collectPythonPath(module, settingsProvider.shouldAddContentRoots(), settingsProvider.shouldAddSourceRoots());
    if (pathMapper != null) {
        pythonPath = pathMapper.convertToRemote(pythonPath);
    }
    String customStartScript = settingsProvider.getCustomStartScript();
    if (customStartScript.trim().length() > 0) {
        customStartScript = "\n" + customStartScript;
    }
    String workingDir = settingsProvider.getWorkingDirectory();
    if (StringUtil.isEmpty(workingDir)) {
        if (module != null && ModuleRootManager.getInstance(module).getContentRoots().length > 0) {
            workingDir = ModuleRootManager.getInstance(module).getContentRoots()[0].getPath();
        } else {
            if (ModuleManager.getInstance(project).getModules().length > 0) {
                VirtualFile[] roots = ModuleRootManager.getInstance(ModuleManager.getInstance(project).getModules()[0]).getContentRoots();
                if (roots.length > 0) {
                    workingDir = roots[0].getPath();
                }
            }
        }
    }
    if (pathMapper != null && workingDir != null) {
        workingDir = pathMapper.convertToRemote(workingDir);
    }
    String selfPathAppend = PydevConsoleRunner.constructPyPathAndWorkingDirCommand(pythonPath, workingDir, customStartScript);
    BuildoutFacet facet = null;
    if (module != null) {
        facet = BuildoutFacet.getInstance(module);
    }
    if (facet != null) {
        List<String> path = facet.getAdditionalPythonPath();
        if (pathMapper != null) {
            path = pathMapper.convertToRemote(path);
        }
        String prependStatement = facet.getPathPrependStatement(path);
        setupFragment = new String[] { prependStatement, selfPathAppend };
    } else {
        setupFragment = new String[] { selfPathAppend };
    }
    Map<String, String> envs = Maps.newHashMap(settingsProvider.getEnvs());
    putIPythonEnvFlag(project, envs);
    Consumer<String> rerunAction = title -> {
        PydevConsoleRunnerImpl runner = createConsoleRunner(project, module);
        runner.setConsoleTitle(title);
        runner.run();
    };
    return createConsoleRunner(project, sdk, workingDir, envs, PyConsoleType.PYTHON, settingsProvider, rerunAction, setupFragment);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModuleManager(com.intellij.openapi.module.ModuleManager) StringUtil(com.intellij.openapi.util.text.StringUtil) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Collection(java.util.Collection) Maps(com.google.common.collect.Maps) Sdk(com.intellij.openapi.projectRoots.Sdk) PythonCommandLineState(com.jetbrains.python.run.PythonCommandLineState) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) PythonEnvUtil(com.jetbrains.python.sdk.PythonEnvUtil) Map(java.util.Map) Pair(com.intellij.openapi.util.Pair) BuildoutFacet(com.jetbrains.python.buildout.BuildoutFacet) Project(com.intellij.openapi.project.Project) PathMapper(com.intellij.util.PathMapper) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull) Consumer(com.intellij.util.Consumer) PathMapper(com.intellij.util.PathMapper) BuildoutFacet(com.jetbrains.python.buildout.BuildoutFacet) Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Maps (com.google.common.collect.Maps)1 Module (com.intellij.openapi.module.Module)1 ModuleManager (com.intellij.openapi.module.ModuleManager)1 Project (com.intellij.openapi.project.Project)1 Sdk (com.intellij.openapi.projectRoots.Sdk)1 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)1 Pair (com.intellij.openapi.util.Pair)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 Consumer (com.intellij.util.Consumer)1 PathMapper (com.intellij.util.PathMapper)1 BuildoutFacet (com.jetbrains.python.buildout.BuildoutFacet)1 PythonCommandLineState (com.jetbrains.python.run.PythonCommandLineState)1 PythonEnvUtil (com.jetbrains.python.sdk.PythonEnvUtil)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1