From 4199cec3931e2b47ab3a181f804fa6a35f31adbf Mon Sep 17 00:00:00 2001 From: rajeshka Date: Wed, 6 May 2020 11:42:01 -0700 Subject: [PATCH] Add Cell after current active cell (#10203) * Add Cell after current active cell * fixed error * more fixes * Addressed PR * merged with master * Fixed the tests and code * removed try catch and modified the test to check the method does not throw. Co-authored-by: Rajesh Kamath --- .../notebook/browser/notebookActions.ts | 20 +++++++++++-------- .../test/browser/notebookActions.test.ts | 7 +++---- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/sql/workbench/contrib/notebook/browser/notebookActions.ts b/src/sql/workbench/contrib/notebook/browser/notebookActions.ts index b1ae2af10c..05b4ecf092 100644 --- a/src/sql/workbench/contrib/notebook/browser/notebookActions.ts +++ b/src/sql/workbench/contrib/notebook/browser/notebookActions.ts @@ -27,6 +27,7 @@ import { IObjectExplorerService } from 'sql/workbench/services/objectExplorer/br import { TreeUpdateUtils } from 'sql/workbench/services/objectExplorer/browser/treeUpdateUtils'; import { find, firstIndex } from 'vs/base/common/arrays'; import { INotebookEditor } from 'sql/workbench/services/notebook/browser/notebookService'; +import { NotebookComponent } from 'sql/workbench/contrib/notebook/browser/notebook.component'; import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; const msgLoading = localize('loading', "Loading kernels..."); @@ -47,15 +48,18 @@ export class AddCellAction extends Action { ) { super(id, label, cssClass); } - public run(context: INotebookEditor): Promise { - return new Promise((resolve, reject) => { - try { - context.addCell(this.cellType); - resolve(true); - } catch (e) { - reject(e); + public async run(context: INotebookEditor): Promise { + //Add Cell after current selected cell. + let index = 0; + if (context && context.cells) { + let notebookcomponent = context as NotebookComponent; + let id = notebookcomponent.activeCellId; + if (id) { + index = context.cells.findIndex(cell => cell.id === id); + index = index + 1; } - }); + } + context.addCell(this.cellType, index); } } diff --git a/src/sql/workbench/contrib/notebook/test/browser/notebookActions.test.ts b/src/sql/workbench/contrib/notebook/test/browser/notebookActions.test.ts index 7f2ba32007..38bf4362bc 100644 --- a/src/sql/workbench/contrib/notebook/test/browser/notebookActions.test.ts +++ b/src/sql/workbench/contrib/notebook/test/browser/notebookActions.test.ts @@ -26,17 +26,16 @@ suite('Notebook Actions', function (): void { // Normal use case let mockNotebookComponent = TypeMoq.Mock.ofType(NotebookComponentStub); - mockNotebookComponent.setup(c => c.addCell(TypeMoq.It.isAny())).returns(cellType => { + mockNotebookComponent.setup(c => c.addCell(TypeMoq.It.isAny(), TypeMoq.It.isAnyNumber())).returns(cellType => { actualCellType = cellType; }); - let result = await action.run(mockNotebookComponent.object); - assert.ok(result, 'Add Cell Action should succeed'); + assert.doesNotThrow(() => action.run(mockNotebookComponent.object)); assert.strictEqual(actualCellType, testCellType); // Handle error case mockNotebookComponent.reset(); - mockNotebookComponent.setup(c => c.addCell(TypeMoq.It.isAny())).throws(new Error('Test Error')); + mockNotebookComponent.setup(c => c.addCell(TypeMoq.It.isAny(), TypeMoq.It.isAnyNumber())).throws(new Error('Test Error')); await assert.rejects(action.run(mockNotebookComponent.object)); });