Skip to content

Commit

Permalink
refactor registerReplCommands
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonykim1 committed May 23, 2024
1 parent a6036cd commit 57b2bdd
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/client/repl/replCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export async function registerReplCommands(
notebookController = createReplController(interpreterPath, disposables);
}
const activeEditor = window.activeTextEditor as TextEditor;

const code = await getSelectedTextToExecute(activeEditor);

// We want to keep notebookEditor, whenever we want to run.
Expand Down Expand Up @@ -112,25 +111,33 @@ export async function registerReplCommands(
extension: PVSC_EXTENSION_ID,
});

const notebookCellData = new NotebookCellData(NotebookCellKind.Code, code as string, 'python');
const { cellCount } = notebookDocument;
// Add new cell to interactive window document
const notebookEdit = NotebookEdit.insertCells(cellCount, [notebookCellData]);
const workspaceEdit = new WorkspaceEdit();
workspaceEdit.set(notebookDocument.uri, [notebookEdit]);
await workspace.applyEdit(workspaceEdit);

await addCellToNotebook(code as string);
// Execute the cell
commands.executeCommand('notebook.cell.execute', {
ranges: [{ start: cellCount, end: cellCount + 1 }],
// document: ourResource,
document: notebookDocument.uri,
});
}
}
}),
);
}
/**
* Function that adds cell to notebook.
* This function will only get called when notebook document is defined.
* @param code
*
*/
async function addCellToNotebook(code: string): Promise<void> {
const notebookCellData = new NotebookCellData(NotebookCellKind.Code, code as string, 'python');
const { cellCount } = notebookDocument!;
// Add new cell to interactive window document
const notebookEdit = NotebookEdit.insertCells(cellCount, [notebookCellData]);
const workspaceEdit = new WorkspaceEdit();
workspaceEdit.set(notebookDocument!.uri, [notebookEdit]);
await workspace.applyEdit(workspaceEdit);
}

/**
* Command triggered for 'Enter': Conditionally call interactive.execute OR insert \n in text input box.
Expand Down

0 comments on commit 57b2bdd

Please sign in to comment.