Skip to content

Commit 9809d53

Browse files
committed
fix "+ Code" buttons under text input blocks to insert python blocks
1 parent e5bdfa7 commit 9809d53

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
@@ -175,6 +175,11 @@ export class DeepnoteNotebookCommandListener implements IExtensionSyncActivation
175175
this.disposableRegistry.push(
176176
commands.registerCommand(Commands.AddButtonBlock, () => this.addInputBlock('button'))
177177
);
178+
179+
// Intercept the built-in insertCodeCellBelow command to fix language for Deepnote notebooks
180+
this.disposableRegistry.push(
181+
commands.registerCommand('notebook.cell.insertCodeCellBelow', () => this.insertCodeCellBelowInterceptor())
182+
);
178183
}
179184

180185
public async addSqlBlock(): Promise<void> {
@@ -307,4 +312,55 @@ export class DeepnoteNotebookCommandListener implements IExtensionSyncActivation
307312
// Enter edit mode on the new cell
308313
await commands.executeCommand('notebook.cell.edit');
309314
}
315+
316+
/**
317+
* Interceptor for the built-in notebook.cell.insertCodeCellBelow command.
318+
* For Deepnote notebooks, ensures that new code cells use Python language instead of
319+
* inheriting plaintext language from input-text/input-textarea blocks.
320+
*/
321+
private async insertCodeCellBelowInterceptor(): Promise<void> {
322+
const editor = window.activeNotebookEditor;
323+
if (!editor || editor.notebook.notebookType !== 'deepnote') {
324+
// Not a Deepnote notebook, use default behavior
325+
await commands.executeCommand('default:notebook.cell.insertCodeCellBelow');
326+
return;
327+
}
328+
329+
// Get the current selection
330+
const selection = editor.selection;
331+
if (!selection) {
332+
// No selection, use default behavior
333+
await commands.executeCommand('default:notebook.cell.insertCodeCellBelow');
334+
return;
335+
}
336+
337+
// Get the current cell to check its language
338+
const currentCell = editor.notebook.cellAt(selection.start);
339+
const currentLanguage = currentCell.document.languageId;
340+
341+
// If the current cell is plaintext (input-text or input-textarea block),
342+
// we need to insert a Python cell instead of inheriting plaintext
343+
if (currentLanguage === 'plaintext') {
344+
// Insert a new Python code cell below
345+
const insertIndex = selection.end;
346+
const document = editor.notebook;
347+
348+
const result = await chainWithPendingUpdates(document, (edit) => {
349+
const newCell = new NotebookCellData(NotebookCellKind.Code, '', 'python');
350+
newCell.metadata = {};
351+
const nbEdit = NotebookEdit.insertCells(insertIndex, [newCell]);
352+
edit.set(document.uri, [nbEdit]);
353+
});
354+
355+
if (result === true) {
356+
// Move selection to the new cell and enter edit mode
357+
const notebookRange = new NotebookRange(insertIndex, insertIndex + 1);
358+
editor.selection = notebookRange;
359+
await commands.executeCommand('notebook.cell.edit');
360+
}
361+
} else {
362+
// For all other languages, use default behavior
363+
await commands.executeCommand('default:notebook.cell.insertCodeCellBelow');
364+
}
365+
}
310366
}

0 commit comments

Comments
 (0)