@@ -12,6 +12,7 @@ import {
1212 workspace ,
1313} from 'vscode' ;
1414import { getExistingReplViewColumn } from './replUtils' ;
15+ import { PVSC_EXTENSION_ID } from '../common/constants' ;
1516
1617/**
1718 * Function that opens/show REPL using IW UI.
@@ -23,29 +24,24 @@ export async function openInteractiveREPL(
2324 notebookController : NotebookController ,
2425 notebookDocument : NotebookDocument | undefined ,
2526) : Promise < NotebookEditor > {
26- let notebookEditor : NotebookEditor | undefined ;
27+ let viewColumn = ViewColumn . Beside ;
2728
2829 // Case where NotebookDocument (REPL document already exists in the tab)
2930 if ( notebookDocument ) {
3031 const existingReplViewColumn = getExistingReplViewColumn ( notebookDocument ) ;
31- const replViewColumn = existingReplViewColumn ?? ViewColumn . Beside ;
32- notebookEditor = await window . showNotebookDocument ( notebookDocument ! , { viewColumn : replViewColumn } ) ;
32+ viewColumn = existingReplViewColumn ?? viewColumn ;
3333 } else if ( ! notebookDocument ) {
34- // Case where NotebookDocument doesnt exist, open new REPL tab
35- const interactiveWindowObject = ( await commands . executeCommand (
36- 'interactive.open' ,
37- {
38- preserveFocus : true ,
39- viewColumn : ViewColumn . Beside ,
40- } ,
41- undefined ,
42- notebookController . id ,
43- 'Python REPL' ,
44- ) ) as { notebookEditor : NotebookEditor } ;
45- notebookEditor = interactiveWindowObject . notebookEditor ;
46- notebookDocument = interactiveWindowObject . notebookEditor . notebook ;
34+ // Case where NotebookDocument doesnt exist, create a blank one.
35+ notebookDocument = await workspace . openNotebookDocument ( 'jupyter-notebook' ) ;
4736 }
48- return notebookEditor ! ;
37+ const editor = window . showNotebookDocument ( notebookDocument ! , { viewColumn, asRepl : 'Python REPL' } ) ;
38+ await commands . executeCommand ( 'notebook.selectKernel' , {
39+ editor,
40+ id : notebookController . id ,
41+ extension : PVSC_EXTENSION_ID ,
42+ } ) ;
43+
44+ return editor ;
4945}
5046
5147/**
@@ -73,13 +69,14 @@ export async function selectNotebookKernel(
7369 * @param code
7470 * @return Promise<void>
7571 */
76- export async function executeNotebookCell ( notebookDocument : NotebookDocument , code : string ) : Promise < void > {
77- const { cellCount } = notebookDocument ;
78- await addCellToNotebook ( notebookDocument , code ) ;
72+ export async function executeNotebookCell ( notebookEditor : NotebookEditor , code : string ) : Promise < void > {
73+ const { notebook, replOptions } = notebookEditor ;
74+ const cellIndex = replOptions ?. appendIndex ?? notebook . cellCount ;
75+ await addCellToNotebook ( notebook , cellIndex , code ) ;
7976 // Execute the cell
8077 commands . executeCommand ( 'notebook.cell.execute' , {
81- ranges : [ { start : cellCount , end : cellCount + 1 } ] ,
82- document : notebookDocument . uri ,
78+ ranges : [ { start : cellIndex , end : cellIndex + 1 } ] ,
79+ document : notebook . uri ,
8380 } ) ;
8481}
8582
@@ -89,11 +86,10 @@ export async function executeNotebookCell(notebookDocument: NotebookDocument, co
8986 * @param code
9087 *
9188 */
92- async function addCellToNotebook ( notebookDocument : NotebookDocument , code : string ) : Promise < void > {
89+ async function addCellToNotebook ( notebookDocument : NotebookDocument , index : number , code : string ) : Promise < void > {
9390 const notebookCellData = new NotebookCellData ( NotebookCellKind . Code , code as string , 'python' ) ;
94- const { cellCount } = notebookDocument ! ;
9591 // Add new cell to interactive window document
96- const notebookEdit = NotebookEdit . insertCells ( cellCount , [ notebookCellData ] ) ;
92+ const notebookEdit = NotebookEdit . insertCells ( index , [ notebookCellData ] ) ;
9793 const workspaceEdit = new WorkspaceEdit ( ) ;
9894 workspaceEdit . set ( notebookDocument ! . uri , [ notebookEdit ] ) ;
9995 await workspace . applyEdit ( workspaceEdit ) ;
0 commit comments