Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/chatSessions/vscode-node/copilotCLIPythonTerminalService.ts
13399 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import * as vscode from 'vscode';
6
import { ILogService } from '../../../platform/log/common/logService';
7
import { PythonEnvironmentApi } from './copilotCLIPythonEnvironmentApi';
8
9
export class PythonTerminalService {
10
constructor(@ILogService private readonly logService: ILogService,
11
) { }
12
13
private async getEnvExtApi(): Promise<PythonEnvironmentApi | undefined> {
14
const extension = vscode.extensions.getExtension<PythonEnvironmentApi>('ms-python.vscode-python-envs');
15
if (!extension) {
16
return undefined;
17
}
18
if (!extension.isActive) {
19
await extension.activate();
20
}
21
22
return extension.exports;
23
}
24
25
public async createTerminal(options: vscode.TerminalOptions) {
26
try {
27
const workspaceUri = vscode.workspace.workspaceFolders?.length ? vscode.workspace.workspaceFolders[0].uri : undefined;
28
if (!workspaceUri) {
29
return;
30
}
31
32
const api = await this.getEnvExtApi();
33
if (!api) {
34
return;
35
}
36
const env = await api.getEnvironment(workspaceUri);
37
if (!env || !env.sysPrefix.toLowerCase().startsWith(workspaceUri.fsPath.toLowerCase())) {
38
return;
39
}
40
return await api.createTerminal(env, options);
41
} catch (ex) {
42
this.logService.error('Failed to create terminal with Python environment', ex.toString());
43
}
44
}
45
}
46
47