Skip to content

Commit 6bf6b5f

Browse files
authored
feat: add command to generate objectid (#416)
1 parent c10a890 commit 6bf6b5f

File tree

7 files changed

+117
-5
lines changed

7 files changed

+117
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ If you use Terraform to manage your infrastructure, MongoDB for VS Code helps yo
7171
- `mdb.connectionSaving.hideOptionToChooseWhereToSaveNewConnections`: When a connection is added, a prompt is shown that let's the user decide where the new connection should be saved. When this setting is checked, the prompt is not shown and the default connection saving location setting is used.
7272
- `mdb.connectionSaving.defaultConnectionSavingLocation`: When the setting that hides the option to choose where to save new connections is checked, this setting sets if and where new connections are saved.
7373
- `mdb.useDefaultTemplateForPlayground`: Choose whether to use the default template for playground files or to start with an empty playground editor.
74+
- `mdb.uniqueObjectIdPerCursor`: The default behavior is to generate a single ObjectId and insert it on all cursors. Set to true to generate a unique ObjectId per cursor instead.
7475
- `mdb.sendTelemetry`: Opt-in and opt-out for diagnostic and telemetry collection.
7576

7677
![Settings](resources/screenshots/settings.png)

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
"onCommand:mdb.removeConnection",
7777
"onCommand:mdb.openMongoDBShell",
7878
"onCommand:mdb.saveMongoDBDocument",
79+
"onCommand:mdb.insertObjectIdToEditor",
80+
"onCommand:mdb.generateObjectIdToClipboard",
7981
"onView:mongoDB",
8082
"onView:mongoDBConnectionExplorer",
8183
"onView:mongoDBPlaygroundsExplorer",
@@ -393,6 +395,14 @@
393395
"light": "images/light/plus-circle.svg",
394396
"dark": "images/dark/plus-circle.svg"
395397
}
398+
},
399+
{
400+
"command": "mdb.insertObjectIdToEditor",
401+
"title": "MongoDB: Insert ObjectId to Editor"
402+
},
403+
{
404+
"command": "mdb.generateObjectIdToClipboard",
405+
"title": "MongoDB: Generate ObjectId to Clipboard"
396406
}
397407
],
398408
"menus": {
@@ -896,6 +906,11 @@
896906
"type": "boolean",
897907
"default": true,
898908
"description": "Use default template for playground files."
909+
},
910+
"mdb.uniqueObjectIdPerCursor": {
911+
"type": "boolean",
912+
"default": false,
913+
"description": "The default behavior is to generate a single ObjectId and insert it on all cursors. Set to true to generate a unique ObjectId per cursor instead."
899914
}
900915
}
901916
}

src/commands/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ enum EXTENSION_COMMANDS {
5757
MDB_REFRESH_SCHEMA = 'mdb.refreshSchema',
5858
MDB_COPY_SCHEMA_FIELD_NAME = 'mdb.copySchemaFieldName',
5959
MDB_REFRESH_INDEXES = 'mdb.refreshIndexes',
60-
MDB_CREATE_INDEX_TREE_VIEW = 'mdb.createIndexFromTreeView'
60+
MDB_CREATE_INDEX_TREE_VIEW = 'mdb.createIndexFromTreeView',
61+
MDB_INSERT_OBJECTID_TO_EDITOR = 'mdb.insertObjectIdToEditor',
62+
MDB_GENERATE_OBJECTID_TO_CLIPBOARD = 'mdb.generateObjectIdToClipboard'
6163
}
6264

6365
export default EXTENSION_COMMANDS;

src/mdbExtensionController.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import TelemetryService from './telemetry/telemetryService';
3737
import PlaygroundsTreeItem from './explorer/playgroundsTreeItem';
3838
import PlaygroundResultProvider from './editors/playgroundResultProvider';
3939
import WebviewController from './views/webviewController';
40+
import { createIdFactory, generateId } from './utils/objectIdHelper';
4041

4142
const log = createLogger('commands');
4243

@@ -540,6 +541,37 @@ export default class MDBExtensionController implements vscode.Disposable {
540541
(playgroundsTreeItem: PlaygroundsTreeItem) =>
541542
this._playgroundController.openPlayground(playgroundsTreeItem.filePath)
542543
);
544+
this.registerCommand(
545+
EXTENSION_COMMANDS.MDB_INSERT_OBJECTID_TO_EDITOR,
546+
async (): Promise<boolean> => {
547+
const editor = vscode.window.activeTextEditor;
548+
549+
if (!editor) {
550+
void vscode.window.showInformationMessage('No active editor to insert to.');
551+
return false;
552+
}
553+
554+
const objectIdFactory = createIdFactory();
555+
556+
await editor.edit((editBuilder) => {
557+
const { selections } = editor;
558+
559+
for (const selection of selections) {
560+
editBuilder.replace(selection, objectIdFactory().toHexString());
561+
}
562+
});
563+
return true;
564+
}
565+
);
566+
this.registerCommand(
567+
EXTENSION_COMMANDS.MDB_GENERATE_OBJECTID_TO_CLIPBOARD,
568+
async (): Promise<boolean> => {
569+
await vscode.env.clipboard.writeText(generateId().toHexString());
570+
void vscode.window.showInformationMessage('Copied to clipboard.');
571+
572+
return true;
573+
}
574+
);
543575
}
544576

545577
showOverviewPageIfRecentlyInstalled(): void {

src/test/suite/extension.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,20 @@ suite('Extension Test Suite', () => {
5050
'mdb.copySchemaFieldName',
5151
'mdb.refreshIndexes',
5252
'mdb.createIndexFromTreeView',
53+
'mdb.insertObjectIdToEditor',
54+
'mdb.generateObjectIdToClipboard',
5355

5456
// Editor commands.
5557
'mdb.codeLens.showMoreDocumentsClicked',
5658

5759
...Object.values(EXTENSION_COMMANDS)
5860
];
5961

60-
for (let i = 0; i < expectedCommands.length; i++) {
61-
assert.notEqual(
62-
registeredCommands.indexOf(expectedCommands[i]),
62+
for (const expectedCommand of expectedCommands) {
63+
assert.notStrictEqual(
64+
registeredCommands.indexOf(expectedCommand),
6365
-1,
64-
`command ${expectedCommands[i]} not registered and was expected`
66+
`command ${expectedCommand} not registered and was expected`
6567
);
6668
}
6769
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import vscode from 'vscode';
2+
import { expect } from 'chai';
3+
4+
import { createIdFactory, generateId } from '../../../utils/objectIdHelper';
5+
6+
const CONFIG_SECTION = 'mdb';
7+
const CONFIG_NAME = 'uniqueObjectIdPerCursor';
8+
9+
suite('ObjectId Test Suite', () => {
10+
test('succesfully creates an ObjectId', () => {
11+
expect(() => generateId()).not.to.throw();
12+
});
13+
14+
test('should generate the same ObjectId when config is false', async () => {
15+
await vscode.workspace
16+
.getConfiguration(CONFIG_SECTION)
17+
.update(CONFIG_NAME, false);
18+
19+
const idFactory = createIdFactory();
20+
const ids = [
21+
idFactory(),
22+
idFactory()
23+
];
24+
25+
expect(ids[0]).to.equal(ids[1]);
26+
});
27+
28+
test('should generate unique ObjectIds when config is true', async () => {
29+
await vscode.workspace
30+
.getConfiguration(CONFIG_SECTION)
31+
.update(CONFIG_NAME, true);
32+
33+
const idFactory = createIdFactory();
34+
const ids = [
35+
idFactory(),
36+
idFactory()
37+
];
38+
39+
expect(ids[0]).to.not.equal(ids[1]);
40+
});
41+
});

src/utils/objectIdHelper.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as vscode from 'vscode';
2+
import { ObjectId } from 'bson';
3+
4+
export function createIdFactory(): () => ObjectId {
5+
const uniqueObjectIdPerCursor = vscode.workspace
6+
.getConfiguration('mdb')
7+
.get('uniqueObjectIdPerCursor', false);
8+
9+
if (uniqueObjectIdPerCursor) {
10+
return () => new ObjectId();
11+
}
12+
13+
const staticObjectId = new ObjectId();
14+
return () => staticObjectId;
15+
}
16+
17+
export function generateId(): ObjectId {
18+
return new ObjectId();
19+
}

0 commit comments

Comments
 (0)