forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecInTerminalProvider.ts
More file actions
72 lines (66 loc) · 2.92 KB
/
Copy pathexecInTerminalProvider.ts
File metadata and controls
72 lines (66 loc) · 2.92 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
'use strict';
import * as vscode from 'vscode';
import * as settings from '../common/configSettings';
import { Commands, PythonLanguage } from '../common/constants';
let path = require('path');
export function activateExecInTerminalProvider() {
vscode.commands.registerCommand(Commands.Exec_In_Terminal, execInTerminal);
vscode.commands.registerCommand(Commands.Exec_Selection_In_Terminal, execSelectionInTerminal);
}
function execInTerminal(fileUri?: vscode.Uri) {
let pythonSettings = settings.PythonSettings.getInstance();
const currentPythonPath = pythonSettings.pythonPath;
let filePath: string;
if (fileUri === undefined || typeof fileUri.fsPath !== 'string') {
const activeEditor = vscode.window.activeTextEditor;
if (activeEditor !== undefined) {
if (!activeEditor.document.isUntitled) {
if (activeEditor.document.languageId === PythonLanguage.language) {
filePath = activeEditor.document.fileName;
} else {
vscode.window.showErrorMessage('The active file is not a Python source file');
return;
}
} else {
vscode.window.showErrorMessage('The active file needs to be saved before it can be run');
return;
}
} else {
vscode.window.showErrorMessage('No open file to run in terminal');
return;
}
} else {
filePath = fileUri.fsPath;
}
if (filePath.indexOf(' ') > 0) {
filePath = `"${filePath}"`;
}
const terminal = vscode.window.createTerminal(`Python`);
if (pythonSettings.terminal && pythonSettings.terminal.executeInFileDir) {
const fileDirPath = path.dirname(filePath).substring(1);
if (fileDirPath !== vscode.workspace.rootPath) {
terminal.sendText(`cd "${fileDirPath}"`);
}
}
const launchArgs = settings.PythonSettings.getInstance().terminal.launchArgs;
const launchArgsString = launchArgs.length > 0 ? " ".concat(launchArgs.join(" ")) : "";
terminal.sendText(`${currentPythonPath}${launchArgsString} ${filePath}`);
terminal.show();
}
function execSelectionInTerminal() {
const currentPythonPath = settings.PythonSettings.getInstance().pythonPath;
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) {
return;
}
const selection = vscode.window.activeTextEditor.selection;
if (selection.isEmpty) {
return;
}
const code = vscode.window.activeTextEditor.document.getText(new vscode.Range(selection.start, selection.end));
const terminal = vscode.window.createTerminal(`Python`);
const launchArgs = settings.PythonSettings.getInstance().terminal.launchArgs;
const launchArgsString = launchArgs.length > 0 ? " ".concat(launchArgs.join(" ")) : "";
terminal.sendText(`${currentPythonPath}${launchArgsString} -c "${code}"`);
terminal.show();
}