22import * as vscode from 'vscode' ;
33import * as settings from '../common/configSettings' ;
44import { Commands , PythonLanguage } from '../common/constants' ;
5+ import { EOL } from 'os' ;
56let path = require ( 'path' ) ;
67let terminal : vscode . Terminal ;
78import { 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+ }
2232function execInTerminal ( fileUri ?: vscode . Uri ) {
2333 const terminalShellSettings = vscode . workspace . getConfiguration ( 'terminal.integrated.shell' ) ;
2434 const IS_POWERSHELL = / p o w e r s h e l l / . 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