From 4869a48fe6d3a503a9769c020ce24fda71c3f230 Mon Sep 17 00:00:00 2001 From: Rhys Howell Date: Tue, 31 Jan 2023 23:49:48 -0500 Subject: [PATCH 1/2] add insert document context action for collections --- package.json | 28 ++++++++++++--- src/commands/index.ts | 1 + src/editors/playgroundController.ts | 12 +++++++ src/mdbExtensionController.ts | 11 ++++++ .../playgroundInsertDocumentTemplate.ts | 13 +++++++ src/test/suite/mdbExtensionController.test.ts | 35 +++++++++++++++++++ 6 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 src/templates/playgroundInsertDocumentTemplate.ts diff --git a/package.json b/package.json index c665bc9b0..c3bad0c20 100644 --- a/package.json +++ b/package.json @@ -377,6 +377,10 @@ "command": "mdb.refreshCollection", "title": "Refresh" }, + { + "command": "mdb.insertDocumentFromTreeView", + "title": "Insert a Document..." + }, { "command": "mdb.refreshSchema", "title": "Refresh" @@ -554,15 +558,20 @@ "group": "1@2" }, { - "command": "mdb.copyCollectionName", + "command": "mdb.insertDocumentFromTreeView", "when": "view == mongoDBConnectionExplorer && viewItem == collectionTreeItem", "group": "2@1" }, { - "command": "mdb.dropCollection", + "command": "mdb.copyCollectionName", "when": "view == mongoDBConnectionExplorer && viewItem == collectionTreeItem", "group": "3@1" }, + { + "command": "mdb.dropCollection", + "when": "view == mongoDBConnectionExplorer && viewItem == collectionTreeItem", + "group": "4@1" + }, { "command": "mdb.searchForDocuments", "when": "view == mongoDBConnectionExplorer && viewItem == documentListTreeItem", @@ -575,17 +584,24 @@ }, { "command": "mdb.viewCollectionDocuments", - "when": "view == mongoDBConnectionExplorer && viewItem == documentListTreeItem" + "when": "view == mongoDBConnectionExplorer && viewItem == documentListTreeItem", + "group": "1@1" }, { "command": "mdb.refreshDocumentList", - "when": "view == mongoDBConnectionExplorer && viewItem == documentListTreeItem" + "when": "view == mongoDBConnectionExplorer && viewItem == documentListTreeItem", + "group": "1@2" }, { "command": "mdb.searchForDocuments", "when": "view == mongoDBConnectionExplorer && viewItem == documentListTreeItem", "group": "2@1" }, + { + "command": "mdb.insertDocumentFromTreeView", + "when": "view == mongoDBConnectionExplorer && viewItem == collectionTreeItem", + "group": "3@1" + }, { "command": "mdb.refreshSchema", "when": "view == mongoDBConnectionExplorer && viewItem == schemaTreeItem" @@ -760,6 +776,10 @@ "command": "mdb.refreshDocumentList", "when": "false" }, + { + "command": "mdb.insertDocumentFromTreeView", + "when": "false" + }, { "command": "mdb.copyCollectionName", "when": "false" diff --git a/src/commands/index.ts b/src/commands/index.ts index 8377284c7..38f53e7be 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -54,6 +54,7 @@ enum EXTENSION_COMMANDS { MDB_VIEW_COLLECTION_DOCUMENTS = 'mdb.viewCollectionDocuments', MDB_REFRESH_COLLECTION = 'mdb.refreshCollection', MDB_REFRESH_DOCUMENT_LIST = 'mdb.refreshDocumentList', + MDB_INSERT_DOCUMENT_FROM_TREE_VIEW = 'mdb.insertDocumentFromTreeView', MDB_REFRESH_SCHEMA = 'mdb.refreshSchema', MDB_COPY_SCHEMA_FIELD_NAME = 'mdb.copySchemaFieldName', MDB_REFRESH_INDEXES = 'mdb.refreshIndexes', diff --git a/src/editors/playgroundController.ts b/src/editors/playgroundController.ts index f19bfd41b..e4149303a 100644 --- a/src/editors/playgroundController.ts +++ b/src/editors/playgroundController.ts @@ -19,6 +19,7 @@ import { LanguageServerController } from '../language'; import playgroundCreateIndexTemplate from '../templates/playgroundCreateIndexTemplate'; import playgroundCreateCollectionTemplate from '../templates/playgroundCreateCollectionTemplate'; import playgroundCloneDocumentTemplate from '../templates/playgroundCloneDocumentTemplate'; +import playgroundInsertDocumentTemplate from '../templates/playgroundInsertDocumentTemplate'; import { PlaygroundResult, ShellExecuteAllResult, @@ -298,6 +299,17 @@ export default class PlaygroundController { return this._createPlaygroundFileWithContent(content); } + createPlaygroundForInsertDocument( + databaseName: string, + collectionName: string + ): Promise { + const content = playgroundInsertDocumentTemplate + .replace('CURRENT_DATABASE', databaseName) + .replace('CURRENT_COLLECTION', collectionName); + + return this._createPlaygroundFileWithContent(content); + } + async createPlayground(): Promise { const useDefaultTemplate = !!vscode.workspace .getConfiguration('mdb') diff --git a/src/mdbExtensionController.ts b/src/mdbExtensionController.ts index 418f50b2b..a65d98cc4 100644 --- a/src/mdbExtensionController.ts +++ b/src/mdbExtensionController.ts @@ -497,6 +497,17 @@ export default class MDBExtensionController implements vscode.Disposable { return Promise.resolve(true); } ); + this.registerCommand( + EXTENSION_COMMANDS.MDB_INSERT_DOCUMENT_FROM_TREE_VIEW, + async ( + documentsListTreeItem: DocumentListTreeItem | CollectionTreeItem + ): Promise => { + return this._playgroundController.createPlaygroundForInsertDocument( + documentsListTreeItem.databaseName, + documentsListTreeItem.collectionName + ); + } + ); this.registerCommand( EXTENSION_COMMANDS.MDB_REFRESH_SCHEMA, (schemaTreeItem: SchemaTreeItem): Promise => { diff --git a/src/templates/playgroundInsertDocumentTemplate.ts b/src/templates/playgroundInsertDocumentTemplate.ts new file mode 100644 index 000000000..f5d347b13 --- /dev/null +++ b/src/templates/playgroundInsertDocumentTemplate.ts @@ -0,0 +1,13 @@ +const template = `// MongoDB Playground +// Use Ctrl+Space inside a snippet or a string literal to trigger completions. + +// The current database to use. +use('CURRENT_DATABASE'); + +// Create a new document in the collection. +db.getCollection('CURRENT_COLLECTION').insertOne({ + +}); +`; + +export default template; diff --git a/src/test/suite/mdbExtensionController.test.ts b/src/test/suite/mdbExtensionController.test.ts index 170757a10..d48191422 100644 --- a/src/test/suite/mdbExtensionController.test.ts +++ b/src/test/suite/mdbExtensionController.test.ts @@ -1660,6 +1660,41 @@ suite('MDBExtensionController Test Suite', function () { assert.strictEqual(namespaceUsed, 'waffle.house'); }); + test('mdb.insertDocumentFromTreeView event should open a playground with an insert document template', async () => { + const collectionTreeItem = new CollectionTreeItem( + { + name: 'pineapple', + type: CollectionTypes.collection, + }, + 'plants', + {}, + false, + false, + null + ); + + const mockCreatePlaygroundForInsertDocument = sinon.fake(); + sinon.replace( + mdbTestExtension.testExtensionController._playgroundController, + 'createPlaygroundForInsertDocument', + mockCreatePlaygroundForInsertDocument + ); + + await vscode.commands.executeCommand( + 'mdb.insertDocumentFromTreeView', + collectionTreeItem + ); + assert.strictEqual(mockCreatePlaygroundForInsertDocument.calledOnce, true); + assert.strictEqual( + mockCreatePlaygroundForInsertDocument.firstCall.args[0], + 'plants' + ); + assert.strictEqual( + mockCreatePlaygroundForInsertDocument.firstCall.args[1], + 'pineapple' + ); + }); + test('mdb.deleteDocumentFromTreeView should not delete a document when the confirmation is cancelled', async () => { const mockDocument = { _id: 'pancakes', From 0365f284fef35ad480591b3099eb00c5335ff2ba Mon Sep 17 00:00:00 2001 From: Rhys Howell Date: Tue, 31 Jan 2023 23:52:58 -0500 Subject: [PATCH 2/2] better action ordering --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index c3bad0c20..3bae561e6 100644 --- a/package.json +++ b/package.json @@ -379,7 +379,7 @@ }, { "command": "mdb.insertDocumentFromTreeView", - "title": "Insert a Document..." + "title": "Insert Document..." }, { "command": "mdb.refreshSchema", @@ -558,12 +558,12 @@ "group": "1@2" }, { - "command": "mdb.insertDocumentFromTreeView", + "command": "mdb.copyCollectionName", "when": "view == mongoDBConnectionExplorer && viewItem == collectionTreeItem", "group": "2@1" }, { - "command": "mdb.copyCollectionName", + "command": "mdb.insertDocumentFromTreeView", "when": "view == mongoDBConnectionExplorer && viewItem == collectionTreeItem", "group": "3@1" }, @@ -599,7 +599,7 @@ }, { "command": "mdb.insertDocumentFromTreeView", - "when": "view == mongoDBConnectionExplorer && viewItem == collectionTreeItem", + "when": "view == mongoDBConnectionExplorer && viewItem == documentListTreeItem", "group": "3@1" }, {