Skip to content

Commit 612478a

Browse files
committed
fix #738
1 parent 2d44da5 commit 612478a

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,12 @@
164164
},
165165
{
166166
"command": "python.execSelectionInTerminal",
167-
"title": "Run Selection in Python Terminal",
167+
"title": "Run Selection/Line in Python Terminal",
168168
"category": "Python"
169169
},
170170
{
171171
"command": "python.execSelectionInDjangoShell",
172-
"title": "Run Selection in Django Shell",
172+
"title": "Run Selection/Line in Django Shell",
173173
"category": "Python"
174174
},
175175
{

src/client/providers/execInTerminalProvider.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ function execInTerminal(fileUri?: vscode.Uri) {
2525

2626
let pythonSettings = settings.PythonSettings.getInstance();
2727
let filePath: string;
28-
28+
2929
let currentPythonPath = pythonSettings.pythonPath;
30-
if (currentPythonPath.indexOf(' ') > 0 ) {
31-
currentPythonPath = `"${currentPythonPath}"`;
30+
if (currentPythonPath.indexOf(' ') > 0) {
31+
currentPythonPath = `"${currentPythonPath}"`;
3232
}
3333

3434
if (fileUri === undefined || typeof fileUri.fsPath !== 'string') {
@@ -87,8 +87,8 @@ function execSelectionInTerminal() {
8787
const IS_POWERSHELL = /powershell/.test(terminalShellSettings.get<string>('windows'));
8888

8989
let currentPythonPath = settings.PythonSettings.getInstance().pythonPath;
90-
if (currentPythonPath.indexOf(' ') > 0 ) {
91-
currentPythonPath = `"${currentPythonPath}"`;
90+
if (currentPythonPath.indexOf(' ') > 0) {
91+
currentPythonPath = `"${currentPythonPath}"`;
9292
}
9393

9494
const activeEditor = vscode.window.activeTextEditor;
@@ -97,10 +97,17 @@ function execSelectionInTerminal() {
9797
}
9898

9999
const selection = vscode.window.activeTextEditor.selection;
100+
let code: string;
100101
if (selection.isEmpty) {
102+
code = vscode.window.activeTextEditor.document.lineAt(selection.start.line).text;
103+
}
104+
else {
105+
let textRange = new vscode.Range(selection.start, selection.end);
106+
code = vscode.window.activeTextEditor.document.getText(textRange);
107+
}
108+
if (code.length === 0) {
101109
return;
102110
}
103-
const code = vscode.window.activeTextEditor.document.getText(new vscode.Range(selection.start, selection.end));
104111
const launchArgs = settings.PythonSettings.getInstance().terminal.launchArgs;
105112
const launchArgsString = launchArgs.length > 0 ? " ".concat(launchArgs.join(" ")) : "";
106113
const command = `${currentPythonPath}${launchArgsString}`;
@@ -123,8 +130,7 @@ function execSelectionInTerminal() {
123130
if (IS_WINDOWS) {
124131
terminal.sendText(unix_code.replace(/\n/g, "\r\n"));
125132
}
126-
else
127-
{
133+
else {
128134
terminal.sendText(unix_code);
129135
}
130136
terminal.show();
@@ -135,8 +141,8 @@ function execSelectionInDjangoShell() {
135141
const IS_POWERSHELL = /powershell/.test(terminalShellSettings.get<string>('windows'));
136142

137143
let currentPythonPath = settings.PythonSettings.getInstance().pythonPath;
138-
if (currentPythonPath.indexOf(' ') > 0 ) {
139-
currentPythonPath = `"${currentPythonPath}"`;
144+
if (currentPythonPath.indexOf(' ') > 0) {
145+
currentPythonPath = `"${currentPythonPath}"`;
140146
}
141147

142148
const activeEditor = vscode.window.activeTextEditor;
@@ -147,10 +153,17 @@ function execSelectionInDjangoShell() {
147153
const workspaceRoot = vscode.workspace.rootPath;
148154
const djangoShellCmd = `"${workspaceRoot}/manage.py" shell`;
149155
const selection = vscode.window.activeTextEditor.selection;
156+
let code: string;
150157
if (selection.isEmpty) {
158+
code = vscode.window.activeTextEditor.document.lineAt(selection.start.line).text;
159+
}
160+
else {
161+
let textRange = new vscode.Range(selection.start, selection.end);
162+
code = vscode.window.activeTextEditor.document.getText(textRange);
163+
}
164+
if (code.length === 0) {
151165
return;
152166
}
153-
const code = vscode.window.activeTextEditor.document.getText(new vscode.Range(selection.start, selection.end));
154167
const launchArgs = settings.PythonSettings.getInstance().terminal.launchArgs;
155168
const launchArgsString = launchArgs.length > 0 ? " ".concat(launchArgs.join(" ")) : "";
156169
const command = `${currentPythonPath}${launchArgsString} ${djangoShellCmd}`;
@@ -173,8 +186,7 @@ function execSelectionInDjangoShell() {
173186
if (IS_WINDOWS) {
174187
terminal.sendText(unix_code.replace(/\n/g, "\r\n"));
175188
}
176-
else
177-
{
189+
else {
178190
terminal.sendText(unix_code);
179191
}
180192
terminal.show();

0 commit comments

Comments
 (0)