Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions images/dark/search-regular.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions images/light/search-regular.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 26 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@
"dark": "images/dark/plus-circle.svg"
}
},
{
"command": "mdb.searchForDocuments",
"title": "Search For Documents...",
"icon": {
"light": "images/light/search-regular.svg",
"dark": "images/dark/search-regular.svg"
}
},
{
"command": "mdb.connectToConnectionTreeItem",
"title": "Connect"
Expand Down Expand Up @@ -427,13 +435,25 @@
"when": "view == mongoDB && viewItem == collectionTreeItem",
"group": "3@1"
},
{
"command": "mdb.searchForDocuments",
"when": "view == mongoDB && viewItem == documentListTreeItem",
"group": "inline"
},
{
"command": "mdb.viewCollectionDocuments",
"when": "view == mongoDB && viewItem == documentListTreeItem"
"when": "view == mongoDB && viewItem == documentListTreeItem",
"group": "1@1"
},
{
"command": "mdb.refreshDocumentList",
"when": "view == mongoDB && viewItem == documentListTreeItem"
"when": "view == mongoDB && viewItem == documentListTreeItem",
"group": "1@2"
},
{
"command": "mdb.searchForDocuments",
"when": "view == mongoDB && viewItem == documentListTreeItem",
"group": "2@1"
},
{
"command": "mdb.refreshSchema",
Expand All @@ -452,6 +472,10 @@
}
],
"commandPalette": [
{
"command": "mdb.searchForDocuments",
"when": "false"
},
{
"command": "mdb.addConnection",
"when": "false"
Expand Down
23 changes: 23 additions & 0 deletions src/editors/playgroundController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ActiveConnectionCodeLensProvider from './activeConnectionCodeLensProvider
import PartialExecutionCodeLensProvider from './partialExecutionCodeLensProvider';
import { OutputChannel, ProgressLocation, TextEditor } from 'vscode';
import playgroundTemplate from '../templates/playgroundTemplate';
import playgroundSearchTemplate from '../templates/playgroundSearchTemplate';
import { createLogger } from '../logging';

const log = createLogger('playground controller');
Expand Down Expand Up @@ -152,6 +153,28 @@ export default class PlaygroundController {
return this._languageServerController.disconnectFromServiceProvider();
}

public createPlaygroundForSearch(
databaseName: string,
collectionName: string
): Promise<boolean> {
return new Promise((resolve, reject) => {
const content = playgroundSearchTemplate
.replace('CURRENT_DATABASE', databaseName)
.replace('CURRENT_COLLECTION', collectionName);

vscode.workspace
.openTextDocument({
language: 'mongodb',
content
})
.then((document) => {
this._outputChannel.show(true);
vscode.window.showTextDocument(document);
resolve(true);
}, reject);
});
}

public createPlayground(): Promise<boolean> {
const useDefaultTemplate = !!vscode.workspace
.getConfiguration('mdb')
Expand Down
12 changes: 2 additions & 10 deletions src/explorer/explorerTreeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ import ConnectionController, {
DataServiceEventTypes
} from '../connectionController';
import { DOCUMENT_ITEM } from './documentTreeItem';
import ConnectionTreeItem from './connectionTreeItem';
import DatabaseTreeItem from './databaseTreeItem';
import CollectionTreeItem from './collectionTreeItem';
import MDBConnectionsTreeItem from './mdbConnectionsTreeItem';

import { createLogger } from '../logging';
import { DOCUMENT_LIST_ITEM, CollectionTypes } from './documentListTreeItem';
import TreeItemParentInterface from './treeItemParentInterface';

const log = createLogger('explorer controller');

export default class ExplorerTreeController
implements vscode.TreeDataProvider<vscode.TreeItem> {
implements vscode.TreeDataProvider<vscode.TreeItem> {
private _connectionController: ConnectionController;
private _mdbConnectionsTreeItem: MDBConnectionsTreeItem;

Expand Down Expand Up @@ -140,11 +136,7 @@ implements vscode.TreeDataProvider<vscode.TreeItem> {
return element;
}

getChildren(
element?: any
): Thenable<
| any[]
> {
getChildren(element?: any): Thenable<any[]> {
// When no element is present we are at the root.
if (!element) {
// We rebuild the connections tree each time in order to
Expand Down
4 changes: 2 additions & 2 deletions src/language/mongoDBService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ export default class MongoDBService {

// ------ COMPLETION ------ //

// Check if string is a valid property name
private isValidPropertyName(str): boolean {
// Check if a string is a valid property name.
private isValidPropertyName(str: string): boolean {
return /^(?![0-9])[a-zA-Z0-9$_]+$/.test(str);
}

Expand Down
18 changes: 18 additions & 0 deletions src/mdbExtensionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,24 @@ export default class MDBExtensionController implements vscode.Disposable {
return successfullyAddedDatabase;
}
);
this.registerCommand(
'mdb.searchForDocuments',
async (element: DocumentListTreeItem): Promise<boolean> => {
if (this._connectionController.isDisconnecting()) {
vscode.window.showErrorMessage(
'Unable to add collection: currently disconnecting.'
);
return false;
}

this._playgroundController.createPlaygroundForSearch(
element.databaseName,
element.collectionName
);

return true;
}
);
this.registerCommand(
'mdb.copyDatabaseName',
async (element: DatabaseTreeItem) => {
Expand Down
32 changes: 32 additions & 0 deletions src/templates/playgroundSearchTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const template: string = `// MongoDB Playground
// Use Ctrl+Space inside a snippet or a string literal to trigger completions.

// The current database to use.
use('CURRENT_DATABASE');

// Search for documents in the current collection.
db.getCollection('CURRENT_COLLECTION')
.find(
{
/*
* Filter
* fieldA: value or expression
*/
},
{
/*
* Projection
* _id: 0, // exclude _id
* fieldA: 1 // include field
*/
}
)
.sort({
/*
* fieldA: 1 // ascending
* fieldB: -1 // descending
*/
});
`;

export default template;
1 change: 1 addition & 0 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ suite('Extension Test Suite', () => {
'mdb.addCollection',
'mdb.viewCollectionDocuments',
'mdb.refreshDocumentList',
'mdb.searchForDocuments',
'mdb.copyCollectionName',
'mdb.refreshCollection',
'mdb.refreshSchema',
Expand Down
25 changes: 25 additions & 0 deletions src/test/suite/mdbExtensionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,31 @@ suite('MDBExtensionController Test Suite', () => {
.then(done, done);
});

test('mdb.searchForDocuments should create a MongoDB playground with search template', async () => {
const mockOpenTextDocument = sinon.fake.resolves('untitled');
sinon.replace(vscode.workspace, 'openTextDocument', mockOpenTextDocument);

const mockShowTextDocument = sinon.fake.resolves();
sinon.replace(vscode.window, 'showTextDocument', mockShowTextDocument);

await vscode.commands.executeCommand(
'mdb.searchForDocuments',
'dbName',
'collName'
);

assert(mockOpenTextDocument.firstArg.language === 'mongodb');
assert(
mockOpenTextDocument.firstArg.content.includes(
'Search for documents in the current collection.'
)
);
assert(
mockShowTextDocument.firstArg === 'untitled',
'Expected it to call vscode to show the playground'
);
});

test('mdb.createPlayground should create a MongoDB playground with default template', async () => {
const mockOpenTextDocument = sinon.fake.resolves('untitled');
sinon.replace(vscode.workspace, 'openTextDocument', mockOpenTextDocument);
Expand Down