forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathpythonStartup.ts
More file actions
48 lines (44 loc) · 2.19 KB
/
pythonStartup.ts
File metadata and controls
48 lines (44 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { ExtensionContext, MarkdownString, Uri } from 'vscode';
import * as path from 'path';
import { copy, createDirectory, getConfiguration, onDidChangeConfiguration } from '../common/vscodeApis/workspaceApis';
import { EXTENSION_ROOT_DIR } from '../constants';
import { Interpreters } from '../common/utils/localize';
async function applyPythonStartupSetting(context: ExtensionContext): Promise<void> {
const config = getConfiguration('python');
const pythonrcSetting = config.get<boolean>('terminal.shellIntegration.enabled');
if (pythonrcSetting) {
const storageUri = context.storageUri || context.globalStorageUri;
try {
await createDirectory(storageUri);
} catch {
// already exists, most likely
}
const destPath = Uri.joinPath(storageUri, 'pythonrc.py');
const sourcePath = path.join(EXTENSION_ROOT_DIR, 'python_files', 'pythonrc.py');
await copy(Uri.file(sourcePath), destPath, { overwrite: true });
context.environmentVariableCollection.replace('PYTHONSTARTUP', destPath.fsPath);
// When shell integration is enabled, we disable PyREPL from cpython.
context.environmentVariableCollection.replace('PYTHON_BASIC_REPL', '1');
context.environmentVariableCollection.description = new MarkdownString(
Interpreters.shellIntegrationEnvVarCollectionDescription,
);
} else {
context.environmentVariableCollection.delete('PYTHONSTARTUP');
context.environmentVariableCollection.delete('PYTHON_BASIC_REPL');
context.environmentVariableCollection.description = new MarkdownString(
Interpreters.shellIntegrationDisabledEnvVarCollectionDescription,
);
}
}
export async function registerPythonStartup(context: ExtensionContext): Promise<void> {
await applyPythonStartupSetting(context);
context.subscriptions.push(
onDidChangeConfiguration(async (e) => {
if (e.affectsConfiguration('python.terminal.shellIntegration.enabled')) {
await applyPythonStartupSetting(context);
}
}),
);
}