Skip to content

Commit 555dd4a

Browse files
feat: trim selected text for export to language (VSCODE-296) (#369)
* feat: trim selected text for export to language (VSCODE-296) * docs: update readme * refactor: bump some dependencies * chore: bump vscode engine * feat: identify more cases for showing up export to language actions * feat: do not provide driver syntax link for csharp
1 parent 774127f commit 555dd4a

23 files changed

+2096
-1195
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ MongoDB Playgrounds are the most convenient way to prototype and execute CRUD op
2424
- Edit documents returned by your playground.
2525
- Save your playgrounds in your workspace and use them to document how your application interacts with MongoDB.
2626
- Build aggregations quickly with helpful and well-commented stage snippets.
27+
- Export your queries and aggregations to one of the supported languages: Java, Node, C#, and Python 3
2728

2829
![Playgrounds](resources/screenshots/playground.png)
2930

package-lock.json

Lines changed: 1467 additions & 1019 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 68 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"release-draft": "node ./scripts/release-draft.js"
6262
},
6363
"engines": {
64-
"vscode": "^1.58.1",
64+
"vscode": "^1.62.0",
6565
"node": "^14.17.3",
6666
"npm": "^7.19.0"
6767
},
@@ -264,7 +264,7 @@
264264
},
265265
{
266266
"command": "mdb.exportToNode",
267-
"title": "MongoDB: Export To Node"
267+
"title": "MongoDB: Export To Node.js"
268268
},
269269
{
270270
"command": "mdb.addConnection",
@@ -601,6 +601,22 @@
601601
"command": "mdb.runAllPlaygroundBlocks",
602602
"when": "editorLangId == mongodb"
603603
},
604+
{
605+
"command": "mdb.exportToPython",
606+
"when": "editorLangId == mongodb"
607+
},
608+
{
609+
"command": "mdb.exportToJava",
610+
"when": "editorLangId == mongodb"
611+
},
612+
{
613+
"command": "mdb.exportToCsharp",
614+
"when": "editorLangId == mongodb"
615+
},
616+
{
617+
"command": "mdb.exportToNode",
618+
"when": "editorLangId == mongodb"
619+
},
604620
{
605621
"command": "mdb.refreshPlaygroundsFromTreeView",
606622
"when": "false"
@@ -823,7 +839,7 @@
823839
"items": {
824840
"type": "string"
825841
},
826-
"description": "Files and folders to exclude while searching for playground in the the current workspace.",
842+
"description": "Files and folders to exclude while searching for playgrounds in the the current workspace.",
827843
"default": [
828844
"**/.*",
829845
"**/_output/**",
@@ -885,68 +901,68 @@
885901
}
886902
},
887903
"dependencies": {
888-
"@babel/parser": "^7.15.0",
889-
"@babel/traverse": "^7.15.0",
904+
"@babel/parser": "^7.16.4",
905+
"@babel/traverse": "^7.16.3",
890906
"@fortawesome/fontawesome-svg-core": "^1.2.36",
891907
"@fortawesome/free-solid-svg-icons": "^5.15.4",
892-
"@fortawesome/react-fontawesome": "^0.1.15",
893-
"@iconify-icons/codicon": "^1.1.18",
908+
"@fortawesome/react-fontawesome": "^0.1.16",
909+
"@iconify-icons/codicon": "^1.1.25",
894910
"@iconify/react": "^1.1.4",
895911
"@leafygreen-ui/logo": "^6.1.0",
896912
"@leafygreen-ui/toggle": "^7.0.4",
897-
"@mongosh/browser-runtime-electron": "^1.0.4",
898-
"@mongosh/i18n": "^1.0.4",
899-
"@mongosh/service-provider-server": "^1.0.4",
900-
"@mongosh/shell-api": "^1.0.4",
901-
"analytics-node": "^5.0.0",
902-
"bson": "^4.4.1",
913+
"@mongosh/browser-runtime-electron": "^1.1.2",
914+
"@mongosh/i18n": "^1.1.2",
915+
"@mongosh/service-provider-server": "^1.1.2",
916+
"@mongosh/shell-api": "^1.1.2",
917+
"analytics-node": "^5.1.2",
918+
"bson": "^4.5.4",
903919
"bson-transpilers": "^1.2.0",
904920
"classnames": "^2.3.1",
905921
"debug": "^4.3.2",
906-
"dotenv": "^8.2.0",
922+
"dotenv": "^8.6.0",
907923
"micromatch": "^4.0.4",
908-
"mongodb": "^4.1.3",
909-
"mongodb-cloud-info": "^1.1.2",
924+
"mongodb": "^4.2.0",
925+
"mongodb-cloud-info": "^1.1.3",
910926
"mongodb-connection-model": "^21.5.4",
911927
"mongodb-data-service": "^21.5.4",
912928
"mongodb-js-errors": "^0.5.0",
913929
"mongodb-ns": "^2.2.0",
914930
"mongodb-schema": "^9.0.0",
915931
"numeral": "^2.0.6",
916-
"react": "^16.13.1",
917-
"react-dom": "^16.13.1",
918-
"react-redux": "^7.2.4",
919-
"redux": "^4.1.1",
920-
"ts-log": "^2.2.3",
932+
"react": "^16.14.0",
933+
"react-dom": "^16.14.0",
934+
"react-redux": "^7.2.6",
935+
"redux": "^4.1.2",
936+
"ts-log": "^2.2.4",
921937
"uuid": "^8.3.2",
922938
"vscode-languageclient": "^7.0.0",
923939
"vscode-languageserver": "^7.0.0",
924-
"vscode-languageserver-textdocument": "^1.0.1",
925-
"ws": "^7.4.2"
940+
"vscode-languageserver-textdocument": "^1.0.2",
941+
"ws": "^7.5.5"
926942
},
927943
"devDependencies": {
928-
"@types/analytics-node": "^3.1.5",
929-
"@types/babel__core": "^7.1.15",
944+
"@types/analytics-node": "^3.1.7",
945+
"@types/babel__core": "^7.1.16",
930946
"@types/babel__traverse": "^7.14.2",
931-
"@types/chai": "^4.2.21",
947+
"@types/chai": "^4.2.22",
932948
"@types/chai-fs": "^2.0.2",
933949
"@types/chai-json-schema": "^1.4.6",
934950
"@types/classnames": "^2.3.1",
935951
"@types/debug": "^4.1.7",
936-
"@types/enzyme": "^3.10.9",
937-
"@types/glob": "^7.1.4",
952+
"@types/enzyme": "^3.10.10",
953+
"@types/glob": "^7.2.0",
938954
"@types/jest": "^26.0.24",
939-
"@types/mocha": "^8.2.0",
940-
"@types/node": "^14.14.22",
941-
"@types/react": "^17.0.15",
942-
"@types/react-dom": "^17.0.9",
943-
"@types/sinon": "^9.0.10",
944-
"@types/uuid": "^8.3.1",
945-
"@types/vscode": "^1.58.1",
955+
"@types/mocha": "^8.2.3",
956+
"@types/node": "^14.17.33",
957+
"@types/react": "^17.0.35",
958+
"@types/react-dom": "^17.0.11",
959+
"@types/sinon": "^9.0.11",
960+
"@types/uuid": "^8.3.3",
961+
"@types/vscode": "^1.62.0",
946962
"@types/ws": "^7.4.7",
947-
"@typescript-eslint/eslint-plugin": "^4.29.0",
948-
"@typescript-eslint/parser": "^4.29.0",
949-
"autoprefixer": "^9.7.5",
963+
"@typescript-eslint/eslint-plugin": "^4.33.0",
964+
"@typescript-eslint/parser": "^4.33.0",
965+
"autoprefixer": "^9.8.8",
950966
"chai": "^4.3.4",
951967
"chai-as-promised": "^7.1.1",
952968
"chai-fs": "^2.0.0",
@@ -955,26 +971,26 @@
955971
"cli-ux": "^5.6.3",
956972
"context-map-webpack-plugin": "^0.1.0",
957973
"cross-env": "^7.0.3",
958-
"css-loader": "^3.4.2",
974+
"css-loader": "^3.6.0",
959975
"depcheck": "^1.4.2",
960976
"download": "^8.0.0",
961977
"enzyme": "^3.11.0",
962978
"enzyme-adapter-react-16": "^1.15.6",
963979
"eslint": "^6.8.0",
964980
"eslint-config-mongodb-js": "^5.0.3",
965-
"eslint-plugin-mocha": "^8.0.0",
981+
"eslint-plugin-mocha": "^8.2.0",
966982
"execa": "^1.0.0",
967-
"glob": "^7.1.7",
968-
"jest": "^26.5.2",
969-
"jest-junit": "^12.2.0",
983+
"glob": "^7.2.0",
984+
"jest": "^26.6.3",
985+
"jest-junit": "^12.3.0",
970986
"jest-transform-stub": "^2.0.0",
971987
"less": "^3.13.1",
972988
"less-loader": "^5.0.0",
973989
"meow": "^9.0.0",
974990
"mkdirp": "^1.0.4",
975-
"mocha": "^8.2.1",
976-
"mocha-junit-reporter": "^2.0.0",
977-
"mocha-multi": "^1.1.3",
991+
"mocha": "^8.4.0",
992+
"mocha-junit-reporter": "^2.0.2",
993+
"mocha-multi": "^1.1.5",
978994
"mongodb-ace-autocompleter": "^0.8.0",
979995
"mongodb-build-info": "^1.2.0",
980996
"mongodb-runner": "^4.8.3",
@@ -986,15 +1002,15 @@
9861002
"semver": "^7.3.5",
9871003
"sinon": "^9.2.4",
9881004
"sinon-chai": "^3.7.0",
989-
"style-loader": "^1.1.3",
990-
"ts-jest": "^26.5.0",
991-
"ts-loader": "^8.0.14",
1005+
"style-loader": "^1.3.0",
1006+
"ts-jest": "^26.5.6",
1007+
"ts-loader": "^8.3.0",
9921008
"ts-node": "^9.1.1",
993-
"typescript": "^4.3.5",
994-
"vsce": "^1.96.1",
1009+
"typescript": "^4.5.2",
1010+
"vsce": "^1.103.1",
9951011
"vscode-test": "^1.6.1",
9961012
"webpack": "^4.46.0",
997-
"webpack-cli": "^3.3.11",
1013+
"webpack-cli": "^3.3.12",
9981014
"xvfb-maybe": "^0.2.1",
9991015
"yargs-parser": "^20.2.9"
10001016
},

scripts/update-grammar.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ const cli = meow(
5151
ui.succeed(
5252
`Downloaded to ${path.join(cli.flags.dest as string, 'mongodb.tmLanguage.json')}`
5353
);
54-
} catch (err) {
55-
ui.fail(`Download failed: ${err.message}`);
54+
} catch (error) {
55+
const printableError = error as { message: string };
56+
ui.fail(`Download failed: ${printableError.message}`);
5657
}
5758
})();

src/connectionController.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ export default class ConnectionController {
210210
try {
211211
return this.addNewConnectionStringAndConnect(connectionString);
212212
} catch (error) {
213-
void vscode.window.showErrorMessage(error.message);
213+
const printableError = error as { message: string };
214+
void vscode.window.showErrorMessage(printableError.message);
214215

215216
return false;
216217
}
@@ -384,7 +385,7 @@ export default class ConnectionController {
384385
// Override the default connection `appname`.
385386
connectionModel.appname = `${name} ${version}`;
386387

387-
const newDataService: DataServiceType = new DataService(connectionModel);
388+
const newDataService: any = new DataService(connectionModel);
388389
const _connect = promisify(newDataService.connect.bind(newDataService));
389390
let connectError;
390391

@@ -463,7 +464,8 @@ export default class ConnectionController {
463464

464465
return connectResult.successfullyConnected;
465466
} catch (error) {
466-
void vscode.window.showErrorMessage(error.message);
467+
const printableError = error as { message: string };
468+
void vscode.window.showErrorMessage(printableError.message);
467469

468470
return false;
469471
}

src/editors/codeActionProvider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ export default class CodeActionProvider implements vscode.CodeActionProvider {
6464
};
6565
codeActions.push(exportToCsharpCommand);
6666

67-
const exportToJSCommand = new vscode.CodeAction('Export To Node', vscode.CodeActionKind.Empty);
67+
const exportToJSCommand = new vscode.CodeAction('Export To Node.js', vscode.CodeActionKind.Empty);
6868
exportToJSCommand.command = {
6969
command: EXTENSION_COMMANDS.MDB_EXPORT_TO_NODE,
70-
title: 'Export To Node',
71-
tooltip: 'Export To Node'
70+
title: 'Export To Node.js',
71+
tooltip: 'Export To Node.js'
7272
};
7373
codeActions.push(exportToJSCommand);
7474
}

src/editors/exportToLanguageCodeLensProvider.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,17 @@ implements vscode.CodeLensProvider {
5353
};
5454
exportToLanguageCodeLenses.push(importStatementsCodeLens);
5555

56-
driverSyntaxCodeLens.command = {
57-
title: this._exportToLanguageAddons.driverSyntax ? 'Exclude Driver Syntax' : 'Include Driver Syntax',
58-
command: EXTENSION_COMMANDS.MDB_CHANGE_EXPORT_TO_LANGUAGE_ADDONS,
59-
arguments: [{
60-
...this._exportToLanguageAddons,
61-
driverSyntax: !this._exportToLanguageAddons.driverSyntax
62-
}]
63-
};
64-
exportToLanguageCodeLenses.push(driverSyntaxCodeLens);
56+
if (this._exportToLanguageAddons.language !== ExportToLanguages.CSHARP) {
57+
driverSyntaxCodeLens.command = {
58+
title: this._exportToLanguageAddons.driverSyntax ? 'Exclude Driver Syntax' : 'Include Driver Syntax',
59+
command: EXTENSION_COMMANDS.MDB_CHANGE_EXPORT_TO_LANGUAGE_ADDONS,
60+
arguments: [{
61+
...this._exportToLanguageAddons,
62+
driverSyntax: !this._exportToLanguageAddons.driverSyntax
63+
}]
64+
};
65+
exportToLanguageCodeLenses.push(driverSyntaxCodeLens);
66+
}
6567

6668
if (
6769
this._exportToLanguageAddons.language === ExportToLanguages.JAVA &&

src/editors/playgroundController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ export default class PlaygroundController {
315315
}
316316

317317
_getAllText(): string {
318-
return this._activeTextEditor?.document.getText() || '';
318+
return this._activeTextEditor?.document.getText().trim() || '';
319319
}
320320

321321
_getSelectedText(selection: vscode.Range): string {

src/explorer/collectionTreeItem.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,12 @@ export default class CollectionTreeItem extends vscode.TreeItem
317317
this._dataService.estimatedCount(
318318
this.namespace,
319319
{}, // No options.
320-
(err: Error | undefined, count: number) => {
321-
if (err) {
320+
(error: Error | undefined, count: number) => {
321+
if (error) {
322+
const printableError = error as { message: string };
322323
return reject(
323324
new Error(
324-
`Unable to get collection document count: ${err.message}`
325+
`Unable to get collection document count: ${printableError.message}`
325326
)
326327
);
327328
}
@@ -373,9 +374,9 @@ export default class CollectionTreeItem extends vscode.TreeItem
373374
return null;
374375
}
375376
});
376-
} catch (e) {
377+
} catch (error) {
377378
return Promise.reject(
378-
new Error(`An error occured parsing the collection name: ${e}`)
379+
new Error(`An error occured parsing the collection name: ${error}`)
379380
);
380381
}
381382

@@ -386,10 +387,11 @@ export default class CollectionTreeItem extends vscode.TreeItem
386387
return new Promise((resolve) => {
387388
this._dataService.dropCollection(
388389
`${this.databaseName}.${collectionName}`,
389-
(err: Error | null, successfullyDroppedCollection = false) => {
390-
if (err) {
390+
(error: Error | null, successfullyDroppedCollection = false) => {
391+
if (error) {
392+
const printableError = error as { message: string };
391393
void vscode.window.showErrorMessage(
392-
`Drop collection failed: ${err.message}`
394+
`Drop collection failed: ${printableError.message}`
393395
);
394396

395397
return resolve(false);

src/explorer/connectionTreeItem.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ export default class ConnectionTreeItem extends vscode.TreeItem
139139
const runListDatabases = promisify(dataService.listDatabases.bind(dataService));
140140
const dbs = await runListDatabases();
141141
return dbs.map(dbItem => dbItem.name);
142-
} catch (err) {
143-
if (isNotAuthorized(err)) {
142+
} catch (error) {
143+
if (isNotAuthorized(error)) {
144144
// Check for which databases privilages this user has, and list those.
145145
return this.listDatabasesUserHasAccessTo(dataService.client.client);
146146
}
147-
148-
throw new Error(`Unable to list databases: ${err.message}`);
147+
const printableError = error as { message: string };
148+
throw new Error(`Unable to list databases: ${printableError.message}`);
149149
}
150150
}
151151

0 commit comments

Comments
 (0)