1
- import { commands , Uri , workspace } from 'vscode' ;
1
+ import {
2
+ commands ,
3
+ NotebookController ,
4
+ Uri ,
5
+ workspace ,
6
+ window ,
7
+ NotebookControllerAffinity ,
8
+ ViewColumn ,
9
+ NotebookEdit ,
10
+ NotebookCellData ,
11
+ NotebookCellKind ,
12
+ WorkspaceEdit ,
13
+ } from 'vscode' ;
2
14
import { Disposable } from 'vscode-jsonrpc' ;
3
- import { Commands } from '../common/constants' ;
15
+ import { Commands , PVSC_EXTENSION_ID } from '../common/constants' ;
4
16
import { IInterpreterService } from '../interpreter/contracts' ;
5
17
import { createReplController , startRepl } from './replController' ;
6
18
19
+ let ourController : NotebookController | undefined ;
20
+
7
21
export function registerReplCommands ( disposables : Disposable [ ] , interpreterService : IInterpreterService ) : void {
8
22
disposables . push (
9
23
commands . registerCommand ( Commands . Exec_In_REPL , async ( uri : Uri ) => {
10
24
const interpreter = await interpreterService . getActiveInterpreter ( uri ) ;
11
25
if ( interpreter ) {
12
26
const interpreterPath = interpreter . path ;
13
27
// How do we get instance of interactive window from Python extension?
14
- const ourController = createReplController ( interpreterPath ) ;
28
+ if ( ! ourController ) {
29
+ ourController = createReplController ( interpreterPath ) ;
30
+ }
15
31
16
32
// How to go from user clicking Run Python --> Run selection/line via Python REPL -> IW opening
17
33
@@ -24,8 +40,77 @@ export function registerReplCommands(disposables: Disposable[], interpreterServi
24
40
// TODO: execute the cell
25
41
}
26
42
// workspace.onDidOpenNotebookDocument;
27
- // await workspace.openNotebookDocument('interactive');
28
- // await window.showNotebookDocument()
43
+ const ourResource = Uri . from ( { scheme : 'untitled' , path : 'repl.interactive' } ) ;
44
+ const notebookDocument = await workspace . openNotebookDocument ( ourResource ) ;
45
+
46
+ const notebookEditor = await window . showNotebookDocument ( notebookDocument , {
47
+ viewColumn : ViewColumn . Beside ,
48
+ } ) ;
49
+
50
+ ourController ! . updateNotebookAffinity ( notebookDocument , NotebookControllerAffinity . Default ) ;
51
+ // await commands.executeCommand(
52
+ // 'interactive.open',
53
+ // // Keep focus on the owning file if there is one
54
+ // { viewColum: 1, preserveFocus: true },
55
+ // ourResource,
56
+ // ourController?.id,
57
+ // 'Python REPL',
58
+ // );
59
+
60
+ await commands . executeCommand ( 'notebook.selectKernel' , {
61
+ notebookEditor,
62
+ id : ourController ?. id ,
63
+ extension : PVSC_EXTENSION_ID ,
64
+ } ) ;
65
+
66
+ const notebookCellData = new NotebookCellData ( NotebookCellKind . Code , 'x=5' , 'python' ) ;
67
+ // keep counter
68
+ const { cellCount } = notebookDocument ;
69
+ const notebookEdit = NotebookEdit . insertCells ( cellCount , [ notebookCellData ] ) ;
70
+ const workspaceEdit = new WorkspaceEdit ( ) ;
71
+ workspaceEdit . set ( notebookDocument . uri , [ notebookEdit ] ) ;
72
+ workspace . applyEdit ( workspaceEdit ) ;
73
+
74
+ const notebookCellExecution = ourController ! . createNotebookCellExecution (
75
+ notebookDocument . cellAt ( cellCount ) ,
76
+ ) ;
77
+ notebookCellExecution . start ( Date . now ( ) ) ;
78
+ // NEED TO TELL TO EXECUTE THE CELL WHICH WILL CALL MY HANDLER
79
+
80
+ // args: [
81
+ // {
82
+ // name: 'showOptions',
83
+ // description: 'Show Options',
84
+ // schema: {
85
+ // type: 'object',
86
+ // properties: {
87
+ // 'viewColumn': {
88
+ // type: 'number',
89
+ // default: -1
90
+ // },
91
+ // 'preserveFocus': {
92
+ // type: 'boolean',
93
+ // default: true
94
+ // }
95
+ // },
96
+ // }
97
+ // },
98
+ // {
99
+ // name: 'resource',
100
+ // description: 'Interactive resource Uri',
101
+ // isOptional: true
102
+ // },
103
+ // {
104
+ // name: 'controllerId',
105
+ // description: 'Notebook controller Id',
106
+ // isOptional: true
107
+ // },
108
+ // {
109
+ // name: 'title',
110
+ // description: 'Notebook editor title',
111
+ // isOptional: true
112
+ // }
113
+ // ]
29
114
} ) ,
30
115
) ;
31
116
}
0 commit comments