Skip to content

Commit

Permalink
feat: add command to generate objectid (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco authored Jul 21, 2022
1 parent c10a890 commit 6bf6b5f
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ If you use Terraform to manage your infrastructure, MongoDB for VS Code helps yo
- `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.
- `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.
- `mdb.useDefaultTemplateForPlayground`: Choose whether to use the default template for playground files or to start with an empty playground editor.
- `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.
- `mdb.sendTelemetry`: Opt-in and opt-out for diagnostic and telemetry collection.

![Settings](resources/screenshots/settings.png)
Expand Down
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
"onCommand:mdb.removeConnection",
"onCommand:mdb.openMongoDBShell",
"onCommand:mdb.saveMongoDBDocument",
"onCommand:mdb.insertObjectIdToEditor",
"onCommand:mdb.generateObjectIdToClipboard",
"onView:mongoDB",
"onView:mongoDBConnectionExplorer",
"onView:mongoDBPlaygroundsExplorer",
Expand Down Expand Up @@ -393,6 +395,14 @@
"light": "images/light/plus-circle.svg",
"dark": "images/dark/plus-circle.svg"
}
},
{
"command": "mdb.insertObjectIdToEditor",
"title": "MongoDB: Insert ObjectId to Editor"
},
{
"command": "mdb.generateObjectIdToClipboard",
"title": "MongoDB: Generate ObjectId to Clipboard"
}
],
"menus": {
Expand Down Expand Up @@ -896,6 +906,11 @@
"type": "boolean",
"default": true,
"description": "Use default template for playground files."
},
"mdb.uniqueObjectIdPerCursor": {
"type": "boolean",
"default": false,
"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."
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ enum EXTENSION_COMMANDS {
MDB_REFRESH_SCHEMA = 'mdb.refreshSchema',
MDB_COPY_SCHEMA_FIELD_NAME = 'mdb.copySchemaFieldName',
MDB_REFRESH_INDEXES = 'mdb.refreshIndexes',
MDB_CREATE_INDEX_TREE_VIEW = 'mdb.createIndexFromTreeView'
MDB_CREATE_INDEX_TREE_VIEW = 'mdb.createIndexFromTreeView',
MDB_INSERT_OBJECTID_TO_EDITOR = 'mdb.insertObjectIdToEditor',
MDB_GENERATE_OBJECTID_TO_CLIPBOARD = 'mdb.generateObjectIdToClipboard'
}

export default EXTENSION_COMMANDS;
32 changes: 32 additions & 0 deletions src/mdbExtensionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import TelemetryService from './telemetry/telemetryService';
import PlaygroundsTreeItem from './explorer/playgroundsTreeItem';
import PlaygroundResultProvider from './editors/playgroundResultProvider';
import WebviewController from './views/webviewController';
import { createIdFactory, generateId } from './utils/objectIdHelper';

const log = createLogger('commands');

Expand Down Expand Up @@ -540,6 +541,37 @@ export default class MDBExtensionController implements vscode.Disposable {
(playgroundsTreeItem: PlaygroundsTreeItem) =>
this._playgroundController.openPlayground(playgroundsTreeItem.filePath)
);
this.registerCommand(
EXTENSION_COMMANDS.MDB_INSERT_OBJECTID_TO_EDITOR,
async (): Promise<boolean> => {
const editor = vscode.window.activeTextEditor;

if (!editor) {
void vscode.window.showInformationMessage('No active editor to insert to.');
return false;
}

const objectIdFactory = createIdFactory();

await editor.edit((editBuilder) => {
const { selections } = editor;

for (const selection of selections) {
editBuilder.replace(selection, objectIdFactory().toHexString());
}
});
return true;
}
);
this.registerCommand(
EXTENSION_COMMANDS.MDB_GENERATE_OBJECTID_TO_CLIPBOARD,
async (): Promise<boolean> => {
await vscode.env.clipboard.writeText(generateId().toHexString());
void vscode.window.showInformationMessage('Copied to clipboard.');

return true;
}
);
}

showOverviewPageIfRecentlyInstalled(): void {
Expand Down
10 changes: 6 additions & 4 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,20 @@ suite('Extension Test Suite', () => {
'mdb.copySchemaFieldName',
'mdb.refreshIndexes',
'mdb.createIndexFromTreeView',
'mdb.insertObjectIdToEditor',
'mdb.generateObjectIdToClipboard',

// Editor commands.
'mdb.codeLens.showMoreDocumentsClicked',

...Object.values(EXTENSION_COMMANDS)
];

for (let i = 0; i < expectedCommands.length; i++) {
assert.notEqual(
registeredCommands.indexOf(expectedCommands[i]),
for (const expectedCommand of expectedCommands) {
assert.notStrictEqual(
registeredCommands.indexOf(expectedCommand),
-1,
`command ${expectedCommands[i]} not registered and was expected`
`command ${expectedCommand} not registered and was expected`
);
}
});
Expand Down
41 changes: 41 additions & 0 deletions src/test/suite/utils/objectIdHelper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import vscode from 'vscode';
import { expect } from 'chai';

import { createIdFactory, generateId } from '../../../utils/objectIdHelper';

const CONFIG_SECTION = 'mdb';
const CONFIG_NAME = 'uniqueObjectIdPerCursor';

suite('ObjectId Test Suite', () => {
test('succesfully creates an ObjectId', () => {
expect(() => generateId()).not.to.throw();
});

test('should generate the same ObjectId when config is false', async () => {
await vscode.workspace
.getConfiguration(CONFIG_SECTION)
.update(CONFIG_NAME, false);

const idFactory = createIdFactory();
const ids = [
idFactory(),
idFactory()
];

expect(ids[0]).to.equal(ids[1]);
});

test('should generate unique ObjectIds when config is true', async () => {
await vscode.workspace
.getConfiguration(CONFIG_SECTION)
.update(CONFIG_NAME, true);

const idFactory = createIdFactory();
const ids = [
idFactory(),
idFactory()
];

expect(ids[0]).to.not.equal(ids[1]);
});
});
19 changes: 19 additions & 0 deletions src/utils/objectIdHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as vscode from 'vscode';
import { ObjectId } from 'bson';

export function createIdFactory(): () => ObjectId {
const uniqueObjectIdPerCursor = vscode.workspace
.getConfiguration('mdb')
.get('uniqueObjectIdPerCursor', false);

if (uniqueObjectIdPerCursor) {
return () => new ObjectId();
}

const staticObjectId = new ObjectId();
return () => staticObjectId;
}

export function generateId(): ObjectId {
return new ObjectId();
}

0 comments on commit 6bf6b5f

Please sign in to comment.