-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathextension.js
More file actions
86 lines (70 loc) · 2.95 KB
/
extension.js
File metadata and controls
86 lines (70 loc) · 2.95 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const { PythonExtension } = require("@vscode/python-extension");
const vscode = require("vscode");
const fs = require("fs");
const path = require("path");
/**
* @param {vscode.ExtensionContext} context
*/
async function activate(context) {
let pythonApi = await PythonExtension.api();
const activeEditor = vscode.window.activeTextEditor;
if (activeEditor) {
await setupPythonEnvironment(activeEditor, pythonApi);
}
let disposable = vscode.window.onDidChangeActiveTextEditor(async (editor) => {
if (editor) {
await setupPythonEnvironment(editor, pythonApi);
}
});
context.subscriptions.push(disposable);
}
async function setupPythonEnvironment(editor, pythonApi) {
let currentDir = path.dirname(editor.document.uri.fsPath);
const root = path.parse(currentDir).root;
const currentWorkspaceFolder = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(editor.document.uri.path));
const currentWorkspaceFolderPath = currentWorkspaceFolder ? currentWorkspaceFolder.uri.path : null;
// Get configuration settings
const config = vscode.workspace.getConfiguration('pythonEnvy');
const venvName = config.get('venvName');
const showNotifications = config.get('showNotifications', true); // Default to true for backward compatibility
while (currentDir !== root) {
const venvPath = path.join(currentDir, venvName);
if (fs.existsSync(venvPath) && fs.lstatSync(venvPath).isDirectory()) {
const currentPythonPath =
pythonApi.environments.getActiveEnvironmentPath(currentWorkspaceFolder.uri);
let pythonPath = path.join(venvPath, "bin", "python");
if (!fs.existsSync(pythonPath)) {
pythonPath = path.join(venvPath, "Scripts", "python.exe");
}
if (currentPythonPath.path !== pythonPath) {
try {
const relativePath = path.relative(currentWorkspaceFolderPath, venvPath);
await pythonApi.environments.updateActiveEnvironmentPath(pythonPath, currentWorkspaceFolder.uri);
// Only show notification if enabled in settings
if (showNotifications) {
vscode.window.showInformationMessage(
`Python Envy: interpreter set to ${relativePath} for ${currentWorkspaceFolder.name}`
);
}
} catch (error) {
vscode.window.showErrorMessage(
`Python Envy: error setting Python interpreter: ${error.message}`
);
}
}
return;
}
if (currentDir === currentWorkspaceFolderPath) {
break;
}
currentDir = path.dirname(currentDir);
if (currentDir === ".") {
currentDir = "";
}
}
}
function deactivate() { }
module.exports = {
activate,
deactivate,
};