Skip to content

Commit efeabe4

Browse files
committed
feat: Refactor Deepnote notebook command listener to support async operations and improve error handling
- Changed private methods to public async methods for adding SQL, big number chart, and input blocks, enhancing the ability to handle asynchronous operations. - Improved error handling by throwing errors when no active notebook editor is found or when cell insertion fails. - Updated the logic for cell insertion to ensure proper selection and editing of newly created cells. - Enhanced unit tests to cover new async behavior and error scenarios. Signed-off-by: Tomas Kislan <tomas@kislan.sk>
1 parent edc39b7 commit efeabe4

File tree

2 files changed

+717
-54
lines changed

2 files changed

+717
-54
lines changed

src/notebooks/deepnote/deepnoteNotebookCommandListener.ts

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
DeepnoteSqlMetadata
3131
} from './deepnoteSchemas';
3232

33-
type InputBlockType =
33+
export type InputBlockType =
3434
| 'input-text'
3535
| 'input-textarea'
3636
| 'input-select'
@@ -175,10 +175,10 @@ export class DeepnoteNotebookCommandListener implements IExtensionSyncActivation
175175
);
176176
}
177177

178-
private addSqlBlock(): void {
178+
public async addSqlBlock(): Promise<void> {
179179
const editor = window.activeNotebookEditor;
180180
if (!editor) {
181-
return;
181+
throw new Error('No active notebook editor found');
182182
}
183183
const document = editor.notebook;
184184
const selection = editor.selection;
@@ -194,7 +194,7 @@ export class DeepnoteNotebookCommandListener implements IExtensionSyncActivation
194194
// Determine the index where to insert the new cell (below current selection or at the end)
195195
const insertIndex = selection ? selection.end : document.cellCount;
196196

197-
chainWithPendingUpdates(document, (edit) => {
197+
const result = await chainWithPendingUpdates(document, (edit) => {
198198
// Create a SQL cell with SQL language for syntax highlighting
199199
// This matches the SqlBlockConverter representation
200200
const newCell = new NotebookCellData(NotebookCellKind.Code, '', 'sql');
@@ -207,26 +207,22 @@ export class DeepnoteNotebookCommandListener implements IExtensionSyncActivation
207207
};
208208
const nbEdit = NotebookEdit.insertCells(insertIndex, [newCell]);
209209
edit.set(document.uri, [nbEdit]);
210-
}).then(
211-
() => {
212-
const notebookRange = new NotebookRange(insertIndex, insertIndex + 1);
213-
editor.revealRange(notebookRange, NotebookEditorRevealType.Default);
214-
editor.selection = notebookRange;
215-
// Enter edit mode on the new cell
216-
commands
217-
.executeCommand('notebook.cell.edit')
218-
.then(undefined, (error) => logger.error('Error entering edit mode', error));
219-
},
220-
(error) => {
221-
logger.error('Error inserting SQL block', error);
222-
}
223-
);
210+
});
211+
if (result !== true) {
212+
throw new Error('Failed to insert SQL block');
213+
}
214+
215+
const notebookRange = new NotebookRange(insertIndex, insertIndex + 1);
216+
editor.revealRange(notebookRange, NotebookEditorRevealType?.Default ?? 0);
217+
editor.selection = notebookRange;
218+
// Enter edit mode on the new cell
219+
await commands.executeCommand('notebook.cell.edit');
224220
}
225221

226-
private addBigNumberChartBlock(): void {
222+
public async addBigNumberChartBlock(): Promise<void> {
227223
const editor = window.activeNotebookEditor;
228224
if (!editor) {
229-
return;
225+
throw new Error('No active notebook editor found');
230226
}
231227
const document = editor.notebook;
232228
const selection = editor.selection;
@@ -243,7 +239,7 @@ export class DeepnoteNotebookCommandListener implements IExtensionSyncActivation
243239
}
244240
};
245241

246-
chainWithPendingUpdates(document, (edit) => {
242+
const result = await chainWithPendingUpdates(document, (edit) => {
247243
const newCell = new NotebookCellData(
248244
NotebookCellKind.Code,
249245
JSON.stringify(bigNumberMetadata, null, 2),
@@ -252,26 +248,22 @@ export class DeepnoteNotebookCommandListener implements IExtensionSyncActivation
252248
newCell.metadata = metadata;
253249
const nbEdit = NotebookEdit.insertCells(insertIndex, [newCell]);
254250
edit.set(document.uri, [nbEdit]);
255-
}).then(
256-
() => {
257-
const notebookRange = new NotebookRange(insertIndex, insertIndex + 1);
258-
editor.revealRange(notebookRange, NotebookEditorRevealType.Default);
259-
editor.selection = notebookRange;
260-
// Enter edit mode on the new cell
261-
commands
262-
.executeCommand('notebook.cell.edit')
263-
.then(undefined, (error) => logger.error('Error entering edit mode', error));
264-
},
265-
(error) => {
266-
logger.error('Error inserting big number chart block', error);
267-
}
268-
);
251+
});
252+
if (result !== true) {
253+
throw new Error('Failed to insert big number chart block');
254+
}
255+
256+
const notebookRange = new NotebookRange(insertIndex, insertIndex + 1);
257+
editor.revealRange(notebookRange, NotebookEditorRevealType?.Default ?? 0);
258+
editor.selection = notebookRange;
259+
// Enter edit mode on the new cell
260+
await commands.executeCommand('notebook.cell.edit');
269261
}
270262

271-
private addInputBlock(blockType: InputBlockType): void {
263+
public async addInputBlock(blockType: InputBlockType): Promise<void> {
272264
const editor = window.activeNotebookEditor;
273265
if (!editor) {
274-
return;
266+
throw new Error('No active notebook editor found');
275267
}
276268
const document = editor.notebook;
277269
const selection = editor.selection;
@@ -291,7 +283,7 @@ export class DeepnoteNotebookCommandListener implements IExtensionSyncActivation
291283
}
292284
};
293285

294-
chainWithPendingUpdates(document, (edit) => {
286+
const result = await chainWithPendingUpdates(document, (edit) => {
295287
const newCell = new NotebookCellData(
296288
NotebookCellKind.Code,
297289
JSON.stringify(defaultMetadata, null, 2),
@@ -300,19 +292,16 @@ export class DeepnoteNotebookCommandListener implements IExtensionSyncActivation
300292
newCell.metadata = metadata;
301293
const nbEdit = NotebookEdit.insertCells(insertIndex, [newCell]);
302294
edit.set(document.uri, [nbEdit]);
303-
}).then(
304-
() => {
305-
const notebookRange = new NotebookRange(insertIndex, insertIndex + 1);
306-
editor.revealRange(notebookRange, NotebookEditorRevealType.Default);
307-
editor.selection = notebookRange;
308-
// Enter edit mode on the new cell
309-
commands
310-
.executeCommand('notebook.cell.edit')
311-
.then(undefined, (error) => logger.error('Error entering edit mode', error));
312-
},
313-
(error) => {
314-
logger.error('Error inserting input block', error);
315-
}
316-
);
295+
});
296+
if (result !== true) {
297+
throw new Error('Failed to insert input block');
298+
}
299+
300+
const notebookRange = new NotebookRange(insertIndex, insertIndex + 1);
301+
// editor.revealRange(notebookRange, NotebookEditorRevealType.Default);
302+
editor.revealRange(notebookRange, 0);
303+
editor.selection = notebookRange;
304+
// Enter edit mode on the new cell
305+
await commands.executeCommand('notebook.cell.edit');
317306
}
318307
}

0 commit comments

Comments
 (0)