Skip to content

Commit 970399c

Browse files
authored
VSCODE-94: Copy field names from schema (#153)
1 parent 8846c94 commit 970399c

File tree

8 files changed

+161
-105
lines changed

8 files changed

+161
-105
lines changed

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@
284284
"command": "mdb.refreshSchema",
285285
"title": "Refresh"
286286
},
287+
{
288+
"command": "mdb.copySchemaFieldName",
289+
"title": "Copy Field Name"
290+
},
287291
{
288292
"command": "mdb.startStreamLanguageServerLogs",
289293
"title": "LSP Inspector: Start Stream LSP Logs"
@@ -434,6 +438,10 @@
434438
{
435439
"command": "mdb.refreshSchema",
436440
"when": "view == mongoDB && viewItem == schemaTreeItem"
441+
},
442+
{
443+
"command": "mdb.copySchemaFieldName",
444+
"when": "view == mongoDB && viewItem == fieldTreeItem"
437445
}
438446
],
439447
"editor/title": [
@@ -535,6 +543,10 @@
535543
{
536544
"command": "mdb.runPlayground",
537545
"when": "false"
546+
},
547+
{
548+
"command": "mdb.copySchemaFieldName",
549+
"when": "false"
538550
}
539551
]
540552
},

src/explorer/fieldTreeItem.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export enum FieldType {
2525
regex = 'Regular Expression',
2626
string = 'String',
2727
timestamp = 'Timestamp',
28-
undefined = 'Undefined'
28+
undefined = 'Undefined',
2929
}
3030

3131
export type SchemaFieldType = {
@@ -120,6 +120,8 @@ export const getIconFileNameForField = (
120120
return null;
121121
};
122122

123+
export const FIELD_TREE_ITEM_CONTEXT_VALUE = 'fieldTreeItem';
124+
123125
export default class FieldTreeItem extends vscode.TreeItem
124126
implements vscode.TreeDataProvider<FieldTreeItem>, TreeItemParent {
125127
// This is a flag which notes that when this tree element is updated,
@@ -133,7 +135,7 @@ export default class FieldTreeItem extends vscode.TreeItem
133135
field: SchemaFieldType;
134136
fieldName: string;
135137

136-
contextValue = 'fieldTreeItem';
138+
contextValue = FIELD_TREE_ITEM_CONTEXT_VALUE;
137139

138140
isExpanded: boolean;
139141

@@ -240,6 +242,10 @@ export default class FieldTreeItem extends vscode.TreeItem
240242
return this._childrenCache;
241243
}
242244

245+
getFieldName(): string {
246+
return this.fieldName;
247+
}
248+
243249
get iconPath():
244250
| string
245251
| vscode.Uri

src/mdbExtensionController.ts

Lines changed: 86 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ import TelemetryController from './telemetry/telemetryController';
1414
import { StatusView } from './views';
1515
import { createLogger } from './logging';
1616
import { StorageController } from './storage';
17-
import DatabaseTreeItem from './explorer/databaseTreeItem';
1817
import ConnectionTreeItem from './explorer/connectionTreeItem';
18+
import DatabaseTreeItem from './explorer/databaseTreeItem';
1919
import SchemaTreeItem from './explorer/schemaTreeItem';
20+
import DocumentListTreeItem from './explorer/documentListTreeItem';
2021
import DocumentTreeItem from './explorer/documentTreeItem';
2122
import WebviewController from './views/webviewController';
22-
import DocumentListTreeItem from './explorer/documentListTreeItem';
23+
import FieldTreeItem from './explorer/fieldTreeItem';
2324

2425
const log = createLogger('commands');
2526

@@ -200,19 +201,15 @@ export default class MDBExtensionController implements vscode.Disposable {
200201
);
201202
this.registerCommand(
202203
'mdb.copyConnectionString',
203-
(element: ConnectionTreeItem) => {
204-
// TODO: Password obfuscation.
204+
async (element: ConnectionTreeItem): Promise<boolean> => {
205205
const connectionString = this._connectionController.getConnectionStringFromConnectionId(
206206
element.connectionId
207207
);
208208

209-
return new Promise((resolve, reject) => {
210-
vscode.env.clipboard.writeText(connectionString).then(() => {
211-
vscode.window.showInformationMessage('Copied to clipboard.');
209+
await vscode.env.clipboard.writeText(connectionString);
210+
vscode.window.showInformationMessage('Copied to clipboard.');
212211

213-
return resolve(true);
214-
}, reject);
215-
});
212+
return true;
216213
}
217214
);
218215
this.registerCommand(
@@ -232,7 +229,7 @@ export default class MDBExtensionController implements vscode.Disposable {
232229
vscode.window.showErrorMessage(
233230
'Please wait for the connection to finish loading before adding a database.'
234231
);
235-
return Promise.resolve(false);
232+
return false;
236233
}
237234

238235
if (
@@ -242,72 +239,63 @@ export default class MDBExtensionController implements vscode.Disposable {
242239
vscode.window.showErrorMessage(
243240
'Please connect to this connection before adding a database.'
244241
);
245-
return Promise.resolve(false);
242+
return false;
246243
}
247244

248245
if (this._connectionController.isDisconnecting()) {
249246
vscode.window.showErrorMessage(
250247
'Unable to add database: currently disconnecting.'
251248
);
252-
return Promise.resolve(false);
249+
return false;
253250
}
254251

255252
if (this._connectionController.isConnecting()) {
256253
vscode.window.showErrorMessage(
257254
'Unable to add database: currently connecting.'
258255
);
259-
return Promise.resolve(false);
256+
return false;
260257
}
261258

262-
return new Promise((resolve, reject) => {
263-
element
264-
.onAddDatabaseClicked(this._context)
265-
.then((successfullyAddedDatabase) => {
266-
if (successfullyAddedDatabase) {
267-
vscode.window.showInformationMessage(
268-
'Database and collection successfully created.'
269-
);
270-
271-
// When we successfully added a database & collection, we need
272-
// to update the explorer view.
273-
this._explorerController.refresh();
274-
}
275-
resolve(successfullyAddedDatabase);
276-
}, reject);
277-
});
259+
const successfullyAddedDatabase = await element.onAddDatabaseClicked(
260+
this._context
261+
);
262+
263+
if (successfullyAddedDatabase) {
264+
vscode.window.showInformationMessage(
265+
'Database and collection successfully created.'
266+
);
267+
268+
// When we successfully added a database & collection, we need
269+
// to update the explorer view.
270+
this._explorerController.refresh();
271+
}
272+
return successfullyAddedDatabase;
278273
}
279274
);
280275
this.registerCommand(
281276
'mdb.copyDatabaseName',
282-
(element: DatabaseTreeItem) => {
283-
return new Promise((resolve, reject) => {
284-
vscode.env.clipboard.writeText(element.databaseName).then(() => {
285-
vscode.window.showInformationMessage('Copied to clipboard.');
286-
return resolve(true);
287-
}, reject);
288-
});
277+
async (element: DatabaseTreeItem) => {
278+
await vscode.env.clipboard.writeText(element.databaseName);
279+
vscode.window.showInformationMessage('Copied to clipboard.');
280+
return true;
289281
}
290282
);
291283
this.registerCommand(
292284
'mdb.dropDatabase',
293-
(element: DatabaseTreeItem): Promise<boolean> => {
294-
return new Promise((resolve, reject) => {
295-
element
296-
.onDropDatabaseClicked()
297-
.then((successfullyDroppedDatabase) => {
298-
if (successfullyDroppedDatabase) {
299-
vscode.window.showInformationMessage(
300-
'Database successfully dropped.'
301-
);
302-
303-
// When we successfully drop a database, we need
304-
// to update the explorer view.
305-
this._explorerController.refresh();
306-
}
307-
308-
resolve(successfullyDroppedDatabase);
309-
}, reject);
310-
});
285+
async (element: DatabaseTreeItem): Promise<boolean> => {
286+
const successfullyDroppedDatabase = await element.onDropDatabaseClicked();
287+
288+
if (successfullyDroppedDatabase) {
289+
vscode.window.showInformationMessage(
290+
'Database successfully dropped.'
291+
);
292+
293+
// When we successfully drop a database, we need
294+
// to update the explorer view.
295+
this._explorerController.refresh();
296+
}
297+
298+
return successfullyDroppedDatabase;
311299
}
312300
);
313301
this.registerCommand(
@@ -324,58 +312,48 @@ export default class MDBExtensionController implements vscode.Disposable {
324312
vscode.window.showErrorMessage(
325313
'Unable to add collection: currently disconnecting.'
326314
);
327-
return Promise.resolve(false);
315+
return false;
328316
}
329317

330-
return new Promise((resolve, reject) => {
331-
element
332-
.onAddCollectionClicked(this._context)
333-
.then((successfullyAddedCollection) => {
334-
if (successfullyAddedCollection) {
335-
vscode.window.showInformationMessage(
336-
'Collection successfully created.'
337-
);
338-
339-
// When we successfully added a collection, we need
340-
// to update the explorer view.
341-
this._explorerController.refresh();
342-
}
343-
resolve(true);
344-
}, reject);
345-
});
318+
const successfullyAddedCollection = await element
319+
.onAddCollectionClicked(this._context);
320+
if (successfullyAddedCollection) {
321+
vscode.window.showInformationMessage(
322+
'Collection successfully created.'
323+
);
324+
325+
// When we successfully added a collection, we need
326+
// to update the explorer view.
327+
this._explorerController.refresh();
328+
}
329+
return true;
346330
}
347331
);
348332
this.registerCommand(
349333
'mdb.copyCollectionName',
350-
(element: CollectionTreeItem): Promise<boolean> => {
351-
return new Promise((resolve, reject) => {
352-
vscode.env.clipboard.writeText(element.collectionName).then(() => {
353-
vscode.window.showInformationMessage('Copied to clipboard.');
354-
return resolve(true);
355-
}, reject);
356-
});
334+
async (element: CollectionTreeItem): Promise<boolean> => {
335+
await vscode.env.clipboard.writeText(element.collectionName);
336+
vscode.window.showInformationMessage('Copied to clipboard.');
337+
338+
return true;
357339
}
358340
);
359341
this.registerCommand(
360342
'mdb.dropCollection',
361-
(element: CollectionTreeItem): Promise<boolean> => {
362-
return new Promise((resolve, reject) => {
363-
element
364-
.onDropCollectionClicked()
365-
.then((successfullyDroppedCollection) => {
366-
if (successfullyDroppedCollection) {
367-
vscode.window.showInformationMessage(
368-
'Collection successfully dropped.'
369-
);
370-
371-
// When we successfully drop a collection, we need
372-
// to update the explorer view.
373-
this._explorerController.refresh();
374-
}
375-
376-
resolve(successfullyDroppedCollection);
377-
}, reject);
378-
});
343+
async (element: CollectionTreeItem): Promise<boolean> => {
344+
const successfullyDroppedCollection = await element.onDropCollectionClicked();
345+
346+
if (successfullyDroppedCollection) {
347+
vscode.window.showInformationMessage(
348+
'Collection successfully dropped.'
349+
);
350+
351+
// When we successfully drop a collection, we need
352+
// to update the explorer view.
353+
this._explorerController.refresh();
354+
}
355+
356+
return successfullyDroppedCollection;
379357
}
380358
);
381359
this.registerCommand(
@@ -417,6 +395,17 @@ export default class MDBExtensionController implements vscode.Disposable {
417395
return this._explorerController.refresh();
418396
}
419397
);
398+
this.registerCommand(
399+
'mdb.copySchemaFieldName',
400+
async (fieldTreeItem: FieldTreeItem): Promise<boolean> => {
401+
await vscode.env.clipboard.writeText(
402+
fieldTreeItem.getFieldName()
403+
);
404+
vscode.window.showInformationMessage('Copied to clipboard.');
405+
406+
return true;
407+
}
408+
);
420409
}
421410

422411
dispose(): void {

src/test/suite/explorer/explorerController.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const sinon = require('sinon');
66

77
import {
88
DefaultSavingLocations,
9-
StorageScope,
9+
StorageScope
1010
} from '../../../storage/storageController';
1111
import { TEST_DATABASE_URI } from '../dbTestHelper';
1212
import { mdbTestExtension } from '../stubbableMdbExtension';
@@ -84,8 +84,8 @@ suite('Explorer Controller Test Suite', function () {
8484
connectionModel: new Connection(),
8585
name: 'testConnectionName',
8686
driverUrl: 'url',
87-
storageLocation: StorageScope.NONE,
88-
},
87+
storageLocation: StorageScope.NONE
88+
}
8989
};
9090
testConnectionController.setConnnectingConnectionId(mockConnectionId);
9191
testConnectionController.setConnnecting(true);
@@ -247,7 +247,7 @@ suite('Explorer Controller Test Suite', function () {
247247
driverUrl: '',
248248
name: 'aaa',
249249
id: 'aaa',
250-
storageLocation: StorageScope.WORKSPACE,
250+
storageLocation: StorageScope.WORKSPACE
251251
};
252252

253253
testConnectionController._connections.zzz = {
@@ -256,7 +256,7 @@ suite('Explorer Controller Test Suite', function () {
256256
driverUrl: '',
257257
name: 'zzz',
258258
id: 'zzz',
259-
storageLocation: StorageScope.WORKSPACE,
259+
storageLocation: StorageScope.WORKSPACE
260260
};
261261

262262
const treeControllerChildren = await treeController.getChildren();

0 commit comments

Comments
 (0)