Skip to content

Commit 08f9e34

Browse files
committed
Added setting to execute files in terminal using the file's directory as the working directory
1 parent e9b96b0 commit 08f9e34

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,12 @@
675675
"type": "string",
676676
"default": "Python Test Log",
677677
"description": "The output window name for the unit test messages, defaults to Python output window."
678+
},
679+
680+
"python.terminal.executeInFileDir": {
681+
"type": "boolean",
682+
"default": false,
683+
"description": "When executing a file in the terminal, whether to use execute in the file's directory, instead of the current open folder."
678684
}
679685
}
680686
}

src/client/common/configSettings.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface IPythonSettings {
1212
formatting: IFormattingSettings;
1313
unitTest: IUnitTestSettings;
1414
autoComplete: IAutoCompeteSettings;
15+
terminal: ITerminalSettings;
1516
}
1617
export interface IUnitTestSettings {
1718
nosetestsEnabled: boolean;
@@ -66,6 +67,10 @@ export interface IFormattingSettings {
6667
export interface IAutoCompeteSettings {
6768
extraPaths: string[];
6869
}
70+
export interface ITerminalSettings {
71+
executeInFileDir: boolean;
72+
}
73+
6974
const systemVariables: SystemVariables = new SystemVariables();
7075
export class PythonSettings extends EventEmitter implements IPythonSettings {
7176
private static pythonSettings: PythonSettings = new PythonSettings();
@@ -135,6 +140,14 @@ export class PythonSettings extends EventEmitter implements IPythonSettings {
135140
this.unitTest.nosetestArgs = this.unitTest.nosetestArgs.map(arg => systemVariables.resolveAny(arg));
136141
this.unitTest.pyTestArgs = this.unitTest.pyTestArgs.map(arg => systemVariables.resolveAny(arg));
137142
this.unitTest.unittestArgs = this.unitTest.unittestArgs.map(arg => systemVariables.resolveAny(arg));
143+
144+
let terminalSettings = systemVariables.resolveAny(pythonSettings.get<ITerminalSettings>('terminal'));
145+
if (this.terminal) {
146+
Object.assign<ITerminalSettings, ITerminalSettings>(this.terminal, terminalSettings);
147+
}
148+
else {
149+
this.terminal = terminalSettings;
150+
}
138151
}
139152

140153
public pythonPath: string;
@@ -143,6 +156,7 @@ export class PythonSettings extends EventEmitter implements IPythonSettings {
143156
public formatting: IFormattingSettings;
144157
public autoComplete: IAutoCompeteSettings;
145158
public unitTest: IUnitTestSettings;
159+
public terminal: ITerminalSettings;
146160
}
147161

148162
function getAbsolutePath(pathToCheck: string, rootDir: String): string {

src/client/providers/execInTerminalProvider.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
import * as vscode from 'vscode';
33
import * as settings from '../common/configSettings';
44
import { Commands } from '../common/constants';
5+
let path = require('path');
56

67
export function activateExecInTerminalProvider() {
78
vscode.commands.registerCommand(Commands.Exec_In_Terminal, execInTerminal);
89
vscode.commands.registerCommand(Commands.Exec_Selection_In_Terminal, execSelectionInTerminal);
910
}
1011

1112
function execInTerminal(fileUri?: vscode.Uri) {
12-
const currentPythonPath = settings.PythonSettings.getInstance().pythonPath;
13+
let pythonSettings = settings.PythonSettings.getInstance()
14+
const currentPythonPath = pythonSettings.pythonPath;
1315
let filePath: string;
1416

1517
if (fileUri === undefined) {
@@ -38,8 +40,13 @@ function execInTerminal(fileUri?: vscode.Uri) {
3840
filePath = `"${filePath}"`;
3941
}
4042
const terminal = vscode.window.createTerminal(`Python`);
43+
if (pythonSettings.terminal.executeInFileDir) {
44+
let fileDirPath = path.dirname(filePath).substring(1);
45+
if (fileDirPath != vscode.workspace.rootPath) {
46+
terminal.sendText(`cd "${fileDirPath}"`);
47+
}
48+
}
4149
terminal.sendText(`${currentPythonPath} ${filePath}`);
42-
4350
}
4451

4552
function execSelectionInTerminal() {

0 commit comments

Comments
 (0)