Skip to content

Commit 7e72cf5

Browse files
committed
fix "+ Code" buttons under text input blocks to insert python blocks
1 parent 4978c77 commit 7e72cf5

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

src/notebooks/deepnote/deepnoteNotebookCommandListener.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ export class DeepnoteNotebookCommandListener implements IExtensionSyncActivation
181181
this.disposableRegistry.push(
182182
commands.registerCommand(Commands.AddButtonBlock, () => this.addInputBlock('button'))
183183
);
184+
185+
// Intercept the built-in insertCodeCellBelow command to fix language for Deepnote notebooks
186+
this.disposableRegistry.push(
187+
commands.registerCommand('notebook.cell.insertCodeCellBelow', () => this.insertCodeCellBelowInterceptor())
188+
);
184189
}
185190

186191
public async addSqlBlock(): Promise<void> {
@@ -368,4 +373,55 @@ export class DeepnoteNotebookCommandListener implements IExtensionSyncActivation
368373
// Enter edit mode on the new cell
369374
await commands.executeCommand('notebook.cell.edit');
370375
}
376+
377+
/**
378+
* Interceptor for the built-in notebook.cell.insertCodeCellBelow command.
379+
* For Deepnote notebooks, ensures that new code cells use Python language instead of
380+
* inheriting plaintext language from input-text/input-textarea blocks.
381+
*/
382+
private async insertCodeCellBelowInterceptor(): Promise<void> {
383+
const editor = window.activeNotebookEditor;
384+
if (!editor || editor.notebook.notebookType !== 'deepnote') {
385+
// Not a Deepnote notebook, use default behavior
386+
await commands.executeCommand('default:notebook.cell.insertCodeCellBelow');
387+
return;
388+
}
389+
390+
// Get the current selection
391+
const selection = editor.selection;
392+
if (!selection) {
393+
// No selection, use default behavior
394+
await commands.executeCommand('default:notebook.cell.insertCodeCellBelow');
395+
return;
396+
}
397+
398+
// Get the current cell to check its language
399+
const currentCell = editor.notebook.cellAt(selection.start);
400+
const currentLanguage = currentCell.document.languageId;
401+
402+
// If the current cell is plaintext (input-text or input-textarea block),
403+
// we need to insert a Python cell instead of inheriting plaintext
404+
if (currentLanguage === 'plaintext') {
405+
// Insert a new Python code cell below
406+
const insertIndex = selection.end;
407+
const document = editor.notebook;
408+
409+
const result = await chainWithPendingUpdates(document, (edit) => {
410+
const newCell = new NotebookCellData(NotebookCellKind.Code, '', 'python');
411+
newCell.metadata = {};
412+
const nbEdit = NotebookEdit.insertCells(insertIndex, [newCell]);
413+
edit.set(document.uri, [nbEdit]);
414+
});
415+
416+
if (result === true) {
417+
// Move selection to the new cell and enter edit mode
418+
const notebookRange = new NotebookRange(insertIndex, insertIndex + 1);
419+
editor.selection = notebookRange;
420+
await commands.executeCommand('notebook.cell.edit');
421+
}
422+
} else {
423+
// For all other languages, use default behavior
424+
await commands.executeCommand('default:notebook.cell.insertCodeCellBelow');
425+
}
426+
}
371427
}

0 commit comments

Comments
 (0)