-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.ts
More file actions
59 lines (56 loc) · 2.17 KB
/
Copy pathindex.ts
File metadata and controls
59 lines (56 loc) · 2.17 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
import * as vscode from "vscode";
import { ActionContext } from "../actions/types";
import { Logger } from "../utilities/logger";
/**
* Get the active text editor and figure out the language ID
* and the executable path from recommended extensions
* If languageID not supported, returns undefined.
*/
export function getActiveEditorExecutablePath(actionContext: ActionContext): string | undefined {
const resource = vscode.window.activeTextEditor;
if (!resource) {
return undefined;
}
Logger.info("Active text document language:", resource.document.languageId);
switch (resource.document.languageId) {
case "python":
return getPythonPath(actionContext, resource.document);
}
return undefined;
}
/**
* Returns python executable path for ms-python.python extension.
*/
function getPythonPath(
actionContext: ActionContext,
document: vscode.TextDocument,
): string | undefined {
try {
const extension = vscode.extensions.getExtension("ms-python.python");
if (!extension) {
Logger.warn("Extension ms-python.python not found.");
return undefined;
}
const usingNewInterpreterStorage =
extension.packageJSON?.featureFlags?.usingNewInterpreterStorage;
if (usingNewInterpreterStorage) {
if (!extension.isActive) {
Logger.info("Python extension not active.");
return undefined;
}
// Support old and new python extension versions. vscode-python issue #11294
const execCommand: string[] = extension.exports.settings.getExecutionDetails
? extension.exports.settings.getExecutionDetails(document.uri).execCommand
: extension.exports.settings.getExecutionCommand(document.uri);
return execCommand.join(" ");
} else {
return actionContext.workspaceManager
.getWorkspaceSettings()
.get<string | undefined>("python.pythonPath");
}
} catch (error) {
const message = "Error while fetching python executable string";
Logger.error(message, error);
return undefined;
}
}