Skip to content

Commit 02164f1

Browse files
committed
fix #758
1 parent 8ec5d64 commit 02164f1

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

src/client/providers/execInTerminalProvider.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import * as vscode from 'vscode';
33
import * as settings from '../common/configSettings';
44
import { Commands, PythonLanguage } from '../common/constants';
5+
import { EOL } from 'os';
56
let path = require('path');
67
let terminal: vscode.Terminal;
78
import { IS_WINDOWS } from '../common/utils';
@@ -19,6 +20,15 @@ export function activateExecInTerminalProvider(): vscode.Disposable[] {
1920
return disposables;
2021
}
2122

23+
function removeBlankLines(code: string): string {
24+
let codeLines = code.split(/\r?\n/g)
25+
let codeLinesWithoutEmptyLines = codeLines.filter(line => line.trim().length > 0);
26+
let lastLineIsEmpty = codeLines.length > 0 && codeLines[codeLines.length - 1].trim().length === 0;
27+
if (lastLineIsEmpty) {
28+
codeLinesWithoutEmptyLines.unshift('');
29+
}
30+
return codeLinesWithoutEmptyLines.join(EOL);
31+
}
2232
function execInTerminal(fileUri?: vscode.Uri) {
2333
const terminalShellSettings = vscode.workspace.getConfiguration('terminal.integrated.shell');
2434
const IS_POWERSHELL = /powershell/.test(terminalShellSettings.get<string>('windows'));
@@ -108,6 +118,7 @@ function execSelectionInTerminal() {
108118
if (code.length === 0) {
109119
return;
110120
}
121+
code = removeBlankLines(code);
111122
const launchArgs = settings.PythonSettings.getInstance().terminal.launchArgs;
112123
const launchArgsString = launchArgs.length > 0 ? " ".concat(launchArgs.join(" ")) : "";
113124
const command = `${currentPythonPath}${launchArgsString}`;

0 commit comments

Comments
 (0)