@@ -10,6 +10,7 @@ export function activateExecInTerminalProvider(): vscode.Disposable[] {
1010 const disposables : vscode . Disposable [ ] = [ ] ;
1111 disposables . push ( vscode . commands . registerCommand ( Commands . Exec_In_Terminal , execInTerminal ) ) ;
1212 disposables . push ( vscode . commands . registerCommand ( Commands . Exec_Selection_In_Terminal , execSelectionInTerminal ) ) ;
13+ disposables . push ( vscode . commands . registerCommand ( Commands . Exec_Selection_In_Django_Shell , execSelectionInDjangoShell ) ) ;
1314 disposables . push ( vscode . window . onDidCloseTerminal ( ( closedTermina : vscode . Terminal ) => {
1415 if ( terminal === closedTermina ) {
1516 terminal = null ;
@@ -19,9 +20,16 @@ export function activateExecInTerminalProvider(): vscode.Disposable[] {
1920}
2021
2122function execInTerminal ( fileUri ?: vscode . Uri ) {
23+ const terminalShellSettings = vscode . workspace . getConfiguration ( 'terminal.integrated.shell' ) ;
24+ const IS_POWERSHELL = / p o w e r s h e l l / . test ( terminalShellSettings . get < string > ( 'windows' ) ) ;
25+
2226 let pythonSettings = settings . PythonSettings . getInstance ( ) ;
23- const currentPythonPath = pythonSettings . pythonPath ;
2427 let filePath : string ;
28+
29+ let currentPythonPath = pythonSettings . pythonPath ;
30+ if ( currentPythonPath . indexOf ( ' ' ) > 0 ) {
31+ currentPythonPath = `"${ currentPythonPath } "`
32+ }
2533
2634 if ( fileUri === undefined || typeof fileUri . fsPath !== 'string' ) {
2735 const activeEditor = vscode . window . activeTextEditor ;
@@ -48,6 +56,7 @@ function execInTerminal(fileUri?: vscode.Uri) {
4856 if ( filePath . indexOf ( ' ' ) > 0 ) {
4957 filePath = `"${ filePath } "` ;
5058 }
59+
5160 terminal = terminal ? terminal : vscode . window . createTerminal ( `Python` ) ;
5261 if ( pythonSettings . terminal && pythonSettings . terminal . executeInFileDir ) {
5362 const fileDirPath = path . dirname ( filePath ) ;
@@ -57,18 +66,31 @@ function execInTerminal(fileUri?: vscode.Uri) {
5766 }
5867 const launchArgs = settings . PythonSettings . getInstance ( ) . terminal . launchArgs ;
5968 const launchArgsString = launchArgs . length > 0 ? " " . concat ( launchArgs . join ( " " ) ) : "" ;
69+ const command = `${ currentPythonPath } ${ launchArgsString } ${ filePath } `
6070 if ( IS_WINDOWS ) {
61- const cmd = `"${ currentPythonPath } "${ launchArgsString } ${ filePath } ` ;
62- terminal . sendText ( cmd . replace ( / \\ / g, "/" ) ) ;
71+ const commandWin = command . replace ( / \\ / g, "/" ) ;
72+ if ( IS_POWERSHELL ) {
73+ terminal . sendText ( `& ${ commandWin } ` ) ;
74+ }
75+ else {
76+ terminal . sendText ( commandWin ) ;
77+ }
6378 }
6479 else {
65- terminal . sendText ( ` ${ currentPythonPath } ${ launchArgsString } ${ filePath } ` ) ;
80+ terminal . sendText ( command ) ;
6681 }
6782 terminal . show ( ) ;
6883}
6984
7085function execSelectionInTerminal ( ) {
71- const currentPythonPath = settings . PythonSettings . getInstance ( ) . pythonPath ;
86+ const terminalShellSettings = vscode . workspace . getConfiguration ( 'terminal.integrated.shell' ) ;
87+ const IS_POWERSHELL = / p o w e r s h e l l / . test ( terminalShellSettings . get < string > ( 'windows' ) ) ;
88+
89+ let currentPythonPath = settings . PythonSettings . getInstance ( ) . pythonPath ;
90+ if ( currentPythonPath . indexOf ( ' ' ) > 0 ) {
91+ currentPythonPath = `"${ currentPythonPath } "`
92+ }
93+
7294 const activeEditor = vscode . window . activeTextEditor ;
7395 if ( ! activeEditor ) {
7496 return ;
@@ -79,22 +101,81 @@ function execSelectionInTerminal() {
79101 return ;
80102 }
81103 const code = vscode . window . activeTextEditor . document . getText ( new vscode . Range ( selection . start , selection . end ) ) ;
82- terminal = terminal ? terminal : vscode . window . createTerminal ( `Python` ) ;
83104 const launchArgs = settings . PythonSettings . getInstance ( ) . terminal . launchArgs ;
84105 const launchArgsString = launchArgs . length > 0 ? " " . concat ( launchArgs . join ( " " ) ) : "" ;
106+ const command = `${ currentPythonPath } ${ launchArgsString } `
107+ if ( ! terminal ) {
108+ terminal = vscode . window . createTerminal ( `Python` ) ;
109+ if ( IS_WINDOWS ) {
110+ const commandWin = command . replace ( / \\ / g, "/" ) ;
111+ if ( IS_POWERSHELL ) {
112+ terminal . sendText ( `& ${ commandWin } ` ) ;
113+ }
114+ else {
115+ terminal . sendText ( commandWin ) ;
116+ }
117+ }
118+ else {
119+ terminal . sendText ( command ) ;
120+ }
121+ }
122+ const unix_code = code . replace ( / \r \n / g, "\n" )
85123 if ( IS_WINDOWS ) {
86- // Multi line commands don't work the same way on windows terminals as it does on other OS
87- // So just start the Python REPL, then send the commands
88- if ( currentPythonPath . indexOf ( ' ' ) > 0 ) {
89- terminal . sendText ( `"${ currentPythonPath } "${ launchArgsString } ` ) ;
124+ terminal . sendText ( unix_code . replace ( / \n / g, "\r\n" ) ) ;
125+ }
126+ else
127+ {
128+ terminal . sendText ( unix_code )
129+ }
130+ terminal . show ( ) ;
131+ }
132+
133+ function execSelectionInDjangoShell ( ) {
134+ const terminalShellSettings = vscode . workspace . getConfiguration ( 'terminal.integrated.shell' ) ;
135+ const IS_POWERSHELL = / p o w e r s h e l l / . test ( terminalShellSettings . get < string > ( 'windows' ) ) ;
136+
137+ let currentPythonPath = settings . PythonSettings . getInstance ( ) . pythonPath ;
138+ if ( currentPythonPath . indexOf ( ' ' ) > 0 ) {
139+ currentPythonPath = `"${ currentPythonPath } "`
140+ }
141+
142+ const activeEditor = vscode . window . activeTextEditor ;
143+ if ( ! activeEditor ) {
144+ return ;
145+ }
146+
147+ const workspaceRoot = vscode . workspace . rootPath ;
148+ const djangoShellCmd = `"${ workspaceRoot } /manage.py" shell`
149+ const selection = vscode . window . activeTextEditor . selection ;
150+ if ( selection . isEmpty ) {
151+ return ;
152+ }
153+ const code = vscode . window . activeTextEditor . document . getText ( new vscode . Range ( selection . start , selection . end ) ) ;
154+ const launchArgs = settings . PythonSettings . getInstance ( ) . terminal . launchArgs ;
155+ const launchArgsString = launchArgs . length > 0 ? " " . concat ( launchArgs . join ( " " ) ) : "" ;
156+ const command = `${ currentPythonPath } ${ launchArgsString } ${ djangoShellCmd } `
157+ if ( ! terminal ) {
158+ terminal = vscode . window . createTerminal ( `Django Shell` ) ;
159+ if ( IS_WINDOWS ) {
160+ const commandWin = command . replace ( / \\ / g, "/" ) ;
161+ if ( IS_POWERSHELL ) {
162+ terminal . sendText ( `& ${ commandWin } ` ) ;
163+ }
164+ else {
165+ terminal . sendText ( commandWin ) ;
166+ }
90167 }
91168 else {
92- terminal . sendText ( ` ${ currentPythonPath } ${ launchArgsString } ` ) ;
169+ terminal . sendText ( command ) ;
93170 }
94- terminal . sendText ( code ) ;
95171 }
96- else {
97- terminal . sendText ( `${ currentPythonPath } ${ launchArgsString } -c "${ code } "` ) ;
172+ const unix_code = code . replace ( / \r \n / g, "\n" )
173+ if ( IS_WINDOWS ) {
174+ terminal . sendText ( unix_code . replace ( / \n / g, "\r\n" ) ) ;
175+ }
176+ else
177+ {
178+ terminal . sendText ( unix_code )
98179 }
99180 terminal . show ( ) ;
100181}
0 commit comments