Skip to content

Commit 2d906cb

Browse files
committed
Add code execution support via env extension
1 parent eace651 commit 2d906cb

File tree

5 files changed

+69
-31
lines changed

5 files changed

+69
-31
lines changed

src/client/common/terminal/service.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import {
2121
} from './types';
2222
import { traceVerbose } from '../../logging';
2323
import { getConfiguration } from '../vscodeApis/workspaceApis';
24+
import { useEnvExtension } from '../../envExt/api.internal';
25+
import { ensureTerminalLegacy } from '../../envExt/api.legacy';
26+
import { sleep } from '../utils/async';
2427
import { isWindows } from '../utils/platform';
2528

2629
@injectable()
@@ -132,22 +135,29 @@ export class TerminalService implements ITerminalService, Disposable {
132135
if (this.terminal) {
133136
return;
134137
}
135-
this.terminalShellType = this.terminalHelper.identifyTerminalShell(this.terminal);
136-
this.terminal = this.terminalManager.createTerminal({
137-
name: this.options?.title || 'Python',
138-
hideFromUser: this.options?.hideFromUser,
139-
});
140-
this.terminalAutoActivator.disableAutoActivation(this.terminal);
141138

142-
// Sometimes the terminal takes some time to start up before it can start accepting input.
143-
await new Promise((resolve) => setTimeout(resolve, 100));
139+
if (useEnvExtension()) {
140+
this.terminal = await ensureTerminalLegacy(this.options?.resource, {
141+
name: this.options?.title || 'Python',
142+
hideFromUser: this.options?.hideFromUser,
143+
});
144+
} else {
145+
this.terminalShellType = this.terminalHelper.identifyTerminalShell(this.terminal);
146+
this.terminal = this.terminalManager.createTerminal({
147+
name: this.options?.title || 'Python',
148+
hideFromUser: this.options?.hideFromUser,
149+
});
150+
this.terminalAutoActivator.disableAutoActivation(this.terminal);
151+
152+
await sleep(100);
144153

145-
await this.terminalActivator.activateEnvironmentInTerminal(this.terminal, {
146-
resource: this.options?.resource,
147-
preserveFocus,
148-
interpreter: this.options?.interpreter,
149-
hideFromUser: this.options?.hideFromUser,
150-
});
154+
await this.terminalActivator.activateEnvironmentInTerminal(this.terminal, {
155+
resource: this.options?.resource,
156+
preserveFocus,
157+
interpreter: this.options?.interpreter,
158+
hideFromUser: this.options?.hideFromUser,
159+
});
160+
}
151161

152162
if (!this.options?.hideFromUser) {
153163
this.terminal.show(preserveFocus);

src/client/envExt/api.internal.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export async function runInTerminal(
7777
show,
7878
});
7979
}
80-
throw new Error('Python environment not found');
80+
throw new Error('Invalid arguments to run in terminal');
8181
}
8282

8383
export async function runInDedicatedTerminal(
@@ -89,12 +89,12 @@ export async function runInDedicatedTerminal(
8989
const envExtApi = await getEnvExtApi();
9090
const env = await getEnvironment(resource);
9191
const project = resource ? envExtApi.getPythonProject(resource) : undefined;
92-
if (env && project && resource) {
93-
return envExtApi.runInDedicatedTerminal(resource, env, {
92+
if (env && project) {
93+
return envExtApi.runInDedicatedTerminal(resource ?? 'global', env, {
9494
cwd: cwd ?? project.uri,
9595
args,
9696
show,
9797
});
9898
}
99-
throw new Error('Python environment not found');
99+
throw new Error('Invalid arguments to run in dedicated terminal');
100100
}

src/client/envExt/api.legacy.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
import { Uri } from 'vscode';
4+
import { Terminal, Uri } from 'vscode';
55
import { getEnvExtApi, getEnvironment } from './api.internal';
66
import { EnvironmentType, PythonEnvironment as PythonEnvironmentLegacy } from '../pythonEnvironments/info';
7-
import { PythonEnvironment } from './types';
7+
import { PythonEnvironment, PythonTerminalOptions } from './types';
88
import { Architecture } from '../common/utils/platform';
99
import { parseVersion } from '../pythonEnvironments/base/info/pythonVersion';
1010
import { PythonEnvType } from '../pythonEnvironments/base/info';
@@ -149,3 +149,19 @@ export async function resetInterpreterLegacy(uri: Uri | undefined): Promise<void
149149
const api = await getEnvExtApi();
150150
await api.setEnvironment(uri, undefined);
151151
}
152+
153+
export async function ensureTerminalLegacy(
154+
resource: Uri | undefined,
155+
options?: PythonTerminalOptions,
156+
): Promise<Terminal> {
157+
const api = await getEnvExtApi();
158+
const pythonEnv = await api.getEnvironment(resource);
159+
const project = resource ? api.getPythonProject(resource) : undefined;
160+
161+
if (pythonEnv && project) {
162+
const fixedOptions = options ? { ...options } : { cwd: project.uri };
163+
const terminal = await api.createTerminal(pythonEnv, fixedOptions);
164+
return terminal;
165+
}
166+
throw new Error('Invalid arguments to create terminal');
167+
}

src/client/envExt/types.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
import { Uri, Disposable, MarkdownString, Event, LogOutputChannel, ThemeIcon, Terminal, TaskExecution } from 'vscode';
4+
import {
5+
Uri,
6+
Disposable,
7+
MarkdownString,
8+
Event,
9+
LogOutputChannel,
10+
ThemeIcon,
11+
Terminal,
12+
TaskExecution,
13+
TerminalOptions,
14+
} from 'vscode';
515

616
/**
717
* The path to an icon, or a theme-specific configuration of icons.
@@ -243,7 +253,7 @@ export type DidChangeEnvironmentEventArgs = {
243253
/**
244254
* The URI of the environment that changed.
245255
*/
246-
readonly uri: Uri;
256+
readonly uri: Uri | undefined;
247257

248258
/**
249259
* The old Python environment before the change.
@@ -1030,25 +1040,27 @@ export interface PythonProjectModifyApi {
10301040
*/
10311041
export interface PythonProjectApi extends PythonProjectCreationApi, PythonProjectGetterApi, PythonProjectModifyApi {}
10321042

1043+
export interface PythonTerminalOptions extends TerminalOptions {
1044+
/**
1045+
* Whether to show the terminal.
1046+
*/
1047+
disableActivation?: boolean;
1048+
}
1049+
10331050
export interface PythonTerminalCreateApi {
1034-
createTerminal(
1035-
environment: PythonEnvironment,
1036-
cwd: string | Uri,
1037-
envVars?: { [key: string]: string | undefined },
1038-
): Promise<Terminal>;
1051+
createTerminal(environment: PythonEnvironment, options: PythonTerminalOptions): Promise<Terminal>;
10391052
}
10401053

10411054
export interface PythonTerminalExecutionOptions {
10421055
cwd: string | Uri;
10431056
args?: string[];
1044-
10451057
show?: boolean;
10461058
}
10471059

10481060
export interface PythonTerminalRunApi {
10491061
runInTerminal(environment: PythonEnvironment, options: PythonTerminalExecutionOptions): Promise<Terminal>;
10501062
runInDedicatedTerminal(
1051-
terminalKey: Uri,
1063+
terminalKey: Uri | string,
10521064
environment: PythonEnvironment,
10531065
options: PythonTerminalExecutionOptions,
10541066
): Promise<Terminal>;

src/client/terminals/codeExecution/codeExecutionManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export class CodeExecutionManager implements ICodeExecutionManager {
192192
return;
193193
}
194194
const codeExecutionHelper = this.serviceContainer.get<ICodeExecutionHelper>(ICodeExecutionHelper);
195-
const codeToExecute = await codeExecutionHelper.getSelectedTextToExecute(activeEditor!);
195+
const codeToExecute = await codeExecutionHelper.getSelectedTextToExecute(activeEditor);
196196
let wholeFileContent = '';
197197
if (activeEditor && activeEditor.document) {
198198
wholeFileContent = activeEditor.document.getText();
@@ -214,7 +214,7 @@ export class CodeExecutionManager implements ICodeExecutionManager {
214214
noop();
215215
}
216216

217-
await executionService.execute(normalizedCode, activeEditor!.document.uri);
217+
await executionService.execute(normalizedCode, activeEditor.document.uri);
218218
}
219219

220220
private shouldTerminalFocusOnStart(uri: Uri | undefined): boolean {

0 commit comments

Comments
 (0)