Skip to content

Commit

Permalink
Run-in-terminal using python envs ext
Browse files Browse the repository at this point in the history
  • Loading branch information
karthiknadig committed Sep 30, 2024
1 parent 89fae1c commit 809cdc1
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/client/common/application/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Commands as LSCommands } from '../../activation/commands';
import { TensorBoardEntrypoint, TensorBoardEntrypointTrigger } from '../../tensorBoard/constants';
import { Channel, Commands, CommandSource } from '../constants';
import { CreateEnvironmentOptions } from '../../pythonEnvironments/creation/proposed.createEnvApis';
import { EnvsExtensionCommands, TerminalPythonExecutionOptions } from '../../envsExt/commands';

export type CommandsWithoutArgs = keyof ICommandNameWithoutArgumentTypeMapping;

Expand Down Expand Up @@ -115,4 +116,5 @@ export interface ICommandNameArgumentTypeMapping extends ICommandNameWithoutArgu
},
];
['cursorEnd']: [];
[EnvsExtensionCommands.RUN_IN_TERMINAL]: [Uri | TerminalPythonExecutionOptions];
}
15 changes: 15 additions & 0 deletions src/client/envsExt/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable @typescript-eslint/no-namespace */

import { Uri } from 'vscode';
import { PythonEnvironment, PythonProject } from './types';

export namespace EnvsExtensionCommands {
export const RUN_IN_TERMINAL = 'python-envs.runInTerminal';
}

export interface TerminalPythonExecutionOptions {
project: PythonProject;
args: string[];
useDedicatedTerminal?: Uri;
environment?: PythonEnvironment;
}
38 changes: 34 additions & 4 deletions src/client/envsExt/envsExtension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Uri } from 'vscode';
import { getExtension } from '../common/vscodeApis/extensionsApi';
import { PythonEnvironmentApi } from './types';
import { PythonEnvironmentApi, PythonProject } from './types';
import { TerminalPythonExecutionOptions } from './commands';

export const ENVS_EXTENSION_ID = 'ms-python.vscode-python-envs';

Expand All @@ -8,14 +10,42 @@ export async function getEnvsApi(): Promise<PythonEnvironmentApi | undefined> {
if (api) {
return api;
}

const extension = getExtension<PythonEnvironmentApi>(ENVS_EXTENSION_ID);
if (extension) {
if (!extension.isActive) {
await extension.activate();
api = extension.exports;
return api;
}
api = extension.exports;
return api;
}
return undefined;
}

let isInstalled: boolean | undefined;
export function isEnvsExtensionInstalled(): boolean {
if (isInstalled !== undefined) {
return !!isInstalled;
}

const extension = getExtension<PythonEnvironmentApi>(ENVS_EXTENSION_ID);
isInstalled = extension && extension.isActive;
return !!isInstalled;
}

export function getPythonProject(uri: Uri): PythonProject | undefined {
return api?.getPythonProject(uri);
}

export function getRunInTerminalOptions(uri: Uri, useDedicated: boolean): Uri | TerminalPythonExecutionOptions {
if (useDedicated) {
const pr = getPythonProject(uri);
if (pr) {
return {
project: pr,
args: [uri.fsPath],
useDedicatedTerminal: uri,
};
}
}
return uri;
}
4 changes: 4 additions & 0 deletions src/client/extensionActivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import { StopWatch } from './common/utils/stopWatch';
import { registerReplCommands, registerReplExecuteOnEnter, registerStartNativeReplCommand } from './repl/replCommands';
import { registerTriggerForTerminalREPL } from './terminals/codeExecution/terminalReplWatcher';
import { registerPythonStartup } from './terminals/pythonStartup';
import { getEnvsApi } from './envsExt/envsExtension';

export async function activateComponents(
// `ext` is passed to any extra activation funcs.
Expand Down Expand Up @@ -167,6 +168,9 @@ async function activateLegacy(ext: ExtensionState, startupStopWatch: StopWatch):
const interpreterManager = serviceContainer.get<IInterpreterService>(IInterpreterService);
interpreterManager.initialize();
if (!workspaceService.isVirtualWorkspace) {
setImmediate(async () => {
await getEnvsApi();
});
const handlers = serviceManager.getAll<IDebugSessionEventHandlers>(IDebugSessionEventHandlers);
const dispatcher = new DebugSessionEventDispatcher(handlers, DebugService.instance, disposables);
dispatcher.registerEventHandlers();
Expand Down
13 changes: 13 additions & 0 deletions src/client/terminals/codeExecution/codeExecutionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
CreateEnvironmentCheckKind,
triggerCreateEnvironmentCheckNonBlocking,
} from '../../pythonEnvironments/creation/createEnvironmentTrigger';
import { EnvsExtensionCommands } from '../../envsExt/commands';
import { getPythonProject, getRunInTerminalOptions, isEnvsExtensionInstalled } from '../../envsExt/envsExtension';

@injectable()
export class CodeExecutionManager implements ICodeExecutionManager {
Expand All @@ -45,6 +47,17 @@ export class CodeExecutionManager implements ICodeExecutionManager {
this.disposableRegistry.push(
this.commandManager.registerCommand(cmd as any, async (file: Resource) => {
traceVerbose(`Attempting to run Python file`, file?.fsPath);

if (file && isEnvsExtensionInstalled()) {
this.commandManager
.executeCommand(
EnvsExtensionCommands.RUN_IN_TERMINAL,
getRunInTerminalOptions(file, cmd === Commands.Exec_In_Separate_Terminal),
)
.then(noop, noop);
return;
}

const interpreterService = this.serviceContainer.get<IInterpreterService>(IInterpreterService);
const interpreter = await interpreterService.getActiveInterpreter(file);
if (!interpreter) {
Expand Down

0 comments on commit 809cdc1

Please sign in to comment.