@@ -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 = / p o w e r s h e l l / . 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 = / p o w e r s h e l l / . 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