Skip to content

Commit 814dc4c

Browse files
VSCODE-158: search for documents (#157)
* feat: search for documents * feat: hide mdb.searchForDocuments from the command palette
1 parent 8a404ed commit 814dc4c

File tree

10 files changed

+137
-14
lines changed

10 files changed

+137
-14
lines changed

images/dark/search-regular.svg

Lines changed: 4 additions & 0 deletions
Loading

images/light/search-regular.svg

Lines changed: 4 additions & 0 deletions
Loading

package.json

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,14 @@
228228
"dark": "images/dark/plus-circle.svg"
229229
}
230230
},
231+
{
232+
"command": "mdb.searchForDocuments",
233+
"title": "Search For Documents...",
234+
"icon": {
235+
"light": "images/light/search-regular.svg",
236+
"dark": "images/dark/search-regular.svg"
237+
}
238+
},
231239
{
232240
"command": "mdb.connectToConnectionTreeItem",
233241
"title": "Connect"
@@ -427,13 +435,25 @@
427435
"when": "view == mongoDB && viewItem == collectionTreeItem",
428436
"group": "3@1"
429437
},
438+
{
439+
"command": "mdb.searchForDocuments",
440+
"when": "view == mongoDB && viewItem == documentListTreeItem",
441+
"group": "inline"
442+
},
430443
{
431444
"command": "mdb.viewCollectionDocuments",
432-
"when": "view == mongoDB && viewItem == documentListTreeItem"
445+
"when": "view == mongoDB && viewItem == documentListTreeItem",
446+
"group": "1@1"
433447
},
434448
{
435449
"command": "mdb.refreshDocumentList",
436-
"when": "view == mongoDB && viewItem == documentListTreeItem"
450+
"when": "view == mongoDB && viewItem == documentListTreeItem",
451+
"group": "1@2"
452+
},
453+
{
454+
"command": "mdb.searchForDocuments",
455+
"when": "view == mongoDB && viewItem == documentListTreeItem",
456+
"group": "2@1"
437457
},
438458
{
439459
"command": "mdb.refreshSchema",
@@ -452,6 +472,10 @@
452472
}
453473
],
454474
"commandPalette": [
475+
{
476+
"command": "mdb.searchForDocuments",
477+
"when": "false"
478+
},
455479
{
456480
"command": "mdb.addConnection",
457481
"when": "false"

src/editors/playgroundController.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ActiveConnectionCodeLensProvider from './activeConnectionCodeLensProvider
88
import PartialExecutionCodeLensProvider from './partialExecutionCodeLensProvider';
99
import { OutputChannel, ProgressLocation, TextEditor } from 'vscode';
1010
import playgroundTemplate from '../templates/playgroundTemplate';
11+
import playgroundSearchTemplate from '../templates/playgroundSearchTemplate';
1112
import { createLogger } from '../logging';
1213

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

156+
public createPlaygroundForSearch(
157+
databaseName: string,
158+
collectionName: string
159+
): Promise<boolean> {
160+
return new Promise((resolve, reject) => {
161+
const content = playgroundSearchTemplate
162+
.replace('CURRENT_DATABASE', databaseName)
163+
.replace('CURRENT_COLLECTION', collectionName);
164+
165+
vscode.workspace
166+
.openTextDocument({
167+
language: 'mongodb',
168+
content
169+
})
170+
.then((document) => {
171+
this._outputChannel.show(true);
172+
vscode.window.showTextDocument(document);
173+
resolve(true);
174+
}, reject);
175+
});
176+
}
177+
155178
public createPlayground(): Promise<boolean> {
156179
const useDefaultTemplate = !!vscode.workspace
157180
.getConfiguration('mdb')

src/explorer/explorerTreeController.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@ import ConnectionController, {
44
DataServiceEventTypes
55
} from '../connectionController';
66
import { DOCUMENT_ITEM } from './documentTreeItem';
7-
import ConnectionTreeItem from './connectionTreeItem';
8-
import DatabaseTreeItem from './databaseTreeItem';
9-
import CollectionTreeItem from './collectionTreeItem';
107
import MDBConnectionsTreeItem from './mdbConnectionsTreeItem';
118

129
import { createLogger } from '../logging';
1310
import { DOCUMENT_LIST_ITEM, CollectionTypes } from './documentListTreeItem';
14-
import TreeItemParentInterface from './treeItemParentInterface';
1511

1612
const log = createLogger('explorer controller');
1713

1814
export default class ExplorerTreeController
19-
implements vscode.TreeDataProvider<vscode.TreeItem> {
15+
implements vscode.TreeDataProvider<vscode.TreeItem> {
2016
private _connectionController: ConnectionController;
2117
private _mdbConnectionsTreeItem: MDBConnectionsTreeItem;
2218

@@ -140,11 +136,7 @@ implements vscode.TreeDataProvider<vscode.TreeItem> {
140136
return element;
141137
}
142138

143-
getChildren(
144-
element?: any
145-
): Thenable<
146-
| any[]
147-
> {
139+
getChildren(element?: any): Thenable<any[]> {
148140
// When no element is present we are at the root.
149141
if (!element) {
150142
// We rebuild the connections tree each time in order to

src/language/mongoDBService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ export default class MongoDBService {
342342

343343
// ------ COMPLETION ------ //
344344

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

src/mdbExtensionController.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,24 @@ export default class MDBExtensionController implements vscode.Disposable {
270270
return successfullyAddedDatabase;
271271
}
272272
);
273+
this.registerCommand(
274+
'mdb.searchForDocuments',
275+
async (element: DocumentListTreeItem): Promise<boolean> => {
276+
if (this._connectionController.isDisconnecting()) {
277+
vscode.window.showErrorMessage(
278+
'Unable to add collection: currently disconnecting.'
279+
);
280+
return false;
281+
}
282+
283+
this._playgroundController.createPlaygroundForSearch(
284+
element.databaseName,
285+
element.collectionName
286+
);
287+
288+
return true;
289+
}
290+
);
273291
this.registerCommand(
274292
'mdb.copyDatabaseName',
275293
async (element: DatabaseTreeItem) => {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const template: string = `// MongoDB Playground
2+
// Use Ctrl+Space inside a snippet or a string literal to trigger completions.
3+
4+
// The current database to use.
5+
use('CURRENT_DATABASE');
6+
7+
// Search for documents in the current collection.
8+
db.getCollection('CURRENT_COLLECTION')
9+
.find(
10+
{
11+
/*
12+
* Filter
13+
* fieldA: value or expression
14+
*/
15+
},
16+
{
17+
/*
18+
* Projection
19+
* _id: 0, // exclude _id
20+
* fieldA: 1 // include field
21+
*/
22+
}
23+
)
24+
.sort({
25+
/*
26+
* fieldA: 1 // ascending
27+
* fieldB: -1 // descending
28+
*/
29+
});
30+
`;
31+
32+
export default template;

src/test/suite/extension.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ suite('Extension Test Suite', () => {
5454
'mdb.addCollection',
5555
'mdb.viewCollectionDocuments',
5656
'mdb.refreshDocumentList',
57+
'mdb.searchForDocuments',
5758
'mdb.copyCollectionName',
5859
'mdb.refreshCollection',
5960
'mdb.refreshSchema',

src/test/suite/mdbExtensionController.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,31 @@ suite('MDBExtensionController Test Suite', () => {
12291229
.then(done, done);
12301230
});
12311231

1232+
test('mdb.searchForDocuments should create a MongoDB playground with search template', async () => {
1233+
const mockOpenTextDocument = sinon.fake.resolves('untitled');
1234+
sinon.replace(vscode.workspace, 'openTextDocument', mockOpenTextDocument);
1235+
1236+
const mockShowTextDocument = sinon.fake.resolves();
1237+
sinon.replace(vscode.window, 'showTextDocument', mockShowTextDocument);
1238+
1239+
await vscode.commands.executeCommand(
1240+
'mdb.searchForDocuments',
1241+
'dbName',
1242+
'collName'
1243+
);
1244+
1245+
assert(mockOpenTextDocument.firstArg.language === 'mongodb');
1246+
assert(
1247+
mockOpenTextDocument.firstArg.content.includes(
1248+
'Search for documents in the current collection.'
1249+
)
1250+
);
1251+
assert(
1252+
mockShowTextDocument.firstArg === 'untitled',
1253+
'Expected it to call vscode to show the playground'
1254+
);
1255+
});
1256+
12321257
test('mdb.createPlayground should create a MongoDB playground with default template', async () => {
12331258
const mockOpenTextDocument = sinon.fake.resolves('untitled');
12341259
sinon.replace(vscode.workspace, 'openTextDocument', mockOpenTextDocument);

0 commit comments

Comments
 (0)