Skip to content

Commit 97d4fc1

Browse files
jaboothDonJayamanne
authored andcommitted
Basic exec in terminal support (#261)
1 parent 6a6a4b0 commit 97d4fc1

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
],
3535
"activationEvents": [
3636
"onLanguage:python",
37+
"onCommand:python.execInTerminal",
3738
"onCommand:python.sortImports",
3839
"onCommand:python.runtests",
3940
"onCommand:python.setInterpreter",
@@ -61,6 +62,11 @@
6162
"title": "Run All Unit Tests",
6263
"category": "Python"
6364
},
65+
{
66+
"command": "python.execInTerminal",
67+
"title": "Run Python file in Terminal",
68+
"category": "Python"
69+
},
6470
{
6571
"command": "python.setInterpreter",
6672
"title": "Select Workspace Interpreter",

src/client/extension.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export function activate(context: vscode.ExtensionContext) {
4747

4848
sortImports.activate(context, formatOutChannel);
4949
activateSetInterpreterProvider();
50+
activateExecInTerminalProvider();
5051
activateSimplePythonRefactorProvider(context, formatOutChannel);
5152
context.subscriptions.push(activateFormatOnSaveProvider(PYTHON, pythonSettings, formatOutChannel, vscode.workspace.rootPath));
5253

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"use strict";
2+
import * as child_process from 'child_process';
3+
import * as path from "path";
4+
import * as vscode from "vscode";
5+
import * as settings from "./../common/configSettings";
6+
import * as utils from "./../common/utils";
7+
8+
export function activateExecInTerminalProvider() {
9+
vscode.commands.registerCommand("python.execInTerminal", execInTerminal);
10+
}
11+
12+
function execInTerminal(filePath: string) {
13+
const currentPythonPath = settings.PythonSettings.getInstance().pythonPath;
14+
15+
console.log(filePath);
16+
if (filePath === undefined) {
17+
const activeEditor = vscode.window.activeTextEditor;
18+
if (activeEditor !== undefined) {
19+
if (!activeEditor.document.isUntitled) {
20+
filePath = activeEditor.document.fileName;
21+
} else {
22+
vscode.window.showErrorMessage('The active file needs to be saved before it can be run');
23+
return;
24+
}
25+
} else {
26+
vscode.window.showErrorMessage('No open file to run in terminal');
27+
return;
28+
}
29+
}
30+
31+
const terminal = (<any>vscode.window).createTerminal(`Ext Terminal`);
32+
setTimeout(() => {
33+
terminal.sendText(`${currentPythonPath} ${filePath}`);
34+
}, 1000);
35+
36+
}

0 commit comments

Comments
 (0)