Skip to content
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,17 @@
"type": "string",
"default": "Python Test Log",
"description": "The output window name for the unit test messages, defaults to Python output window."
},

"python.terminal.executeInFileDir": {
"type": "boolean",
"default": false,
"description": "When executing a file in the terminal, whether to use execute in the file's directory, instead of the current open folder."
},
"python.terminal.launchArgs": {
"type": "array",
"default": [],
"description": "Python launch arguments to use when executing a file in the terminal."
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/client/common/configSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface IPythonSettings {
formatting: IFormattingSettings;
unitTest: IUnitTestSettings;
autoComplete: IAutoCompeteSettings;
terminal: ITerminalSettings;
}
export interface IUnitTestSettings {
nosetestsEnabled: boolean;
Expand Down Expand Up @@ -66,6 +67,11 @@ export interface IFormattingSettings {
export interface IAutoCompeteSettings {
extraPaths: string[];
}
export interface ITerminalSettings {
executeInFileDir: boolean;
launchArgs: string[];
}

const systemVariables: SystemVariables = new SystemVariables();
export class PythonSettings extends EventEmitter implements IPythonSettings {
private static pythonSettings: PythonSettings = new PythonSettings();
Expand Down Expand Up @@ -135,6 +141,14 @@ export class PythonSettings extends EventEmitter implements IPythonSettings {
this.unitTest.nosetestArgs = this.unitTest.nosetestArgs.map(arg => systemVariables.resolveAny(arg));
this.unitTest.pyTestArgs = this.unitTest.pyTestArgs.map(arg => systemVariables.resolveAny(arg));
this.unitTest.unittestArgs = this.unitTest.unittestArgs.map(arg => systemVariables.resolveAny(arg));

let terminalSettings = systemVariables.resolveAny(pythonSettings.get<ITerminalSettings>('terminal'));
if (this.terminal) {
Object.assign<ITerminalSettings, ITerminalSettings>(this.terminal, terminalSettings);
}
else {
this.terminal = terminalSettings;
}
}

public pythonPath: string;
Expand All @@ -143,6 +157,7 @@ export class PythonSettings extends EventEmitter implements IPythonSettings {
public formatting: IFormattingSettings;
public autoComplete: IAutoCompeteSettings;
public unitTest: IUnitTestSettings;
public terminal: ITerminalSettings;
}

function getAbsolutePath(pathToCheck: string, rootDir: String): string {
Expand Down
23 changes: 18 additions & 5 deletions src/client/providers/execInTerminalProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
import * as vscode from 'vscode';
import * as settings from '../common/configSettings';
import { Commands } 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) {
const currentPythonPath = settings.PythonSettings.getInstance().pythonPath;
let pythonSettings = settings.PythonSettings.getInstance();
const currentPythonPath = pythonSettings.pythonPath;
let filePath: string;

if (fileUri === undefined) {
Expand Down Expand Up @@ -38,8 +40,16 @@ function execInTerminal(fileUri?: vscode.Uri) {
filePath = `"${filePath}"`;
}
const terminal = vscode.window.createTerminal(`Python`);
terminal.sendText(`${currentPythonPath} ${filePath}`);

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() {
Expand All @@ -55,5 +65,8 @@ function execSelectionInTerminal() {
}
const code = vscode.window.activeTextEditor.document.getText(new vscode.Range(selection.start, selection.end));
const terminal = vscode.window.createTerminal(`Python`);
terminal.sendText(`${currentPythonPath} -c "${code}"`);
}
const launchArgs = settings.PythonSettings.getInstance().terminal.launchArgs;
const launchArgsString = launchArgs.length > 0 ? " ".concat(launchArgs.join(" ")) : "";
terminal.sendText(`${currentPythonPath}${launchArgsString} -c "${code}"`);
terminal.show();
}