Skip to content

Commit 4606982

Browse files
authored
feat: add export to Rust and PHP VSCODE-411 (#676)
* feat: add export to Rust and PHP VSCODE-411 * test: update playgroundSelectedCodeActionProvider tests
1 parent 6c32739 commit 4606982

File tree

7 files changed

+207
-6
lines changed

7 files changed

+207
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ Select queries and aggregations within your Playground files and translate them
4646
* Python 3
4747
* Ruby
4848
* Go
49+
* Rust
50+
* PHP
4951

5052
![Export to language](resources/screenshots/export-to-language.gif)
5153

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@
246246
"command": "mdb.exportToGo",
247247
"title": "MongoDB: Export To Go"
248248
},
249+
{
250+
"command": "mdb.exportToRust",
251+
"title": "MongoDB: Export To Rust"
252+
},
253+
{
254+
"command": "mdb.exportToPHP",
255+
"title": "MongoDB: Export To PHP"
256+
},
249257
{
250258
"command": "mdb.addConnection",
251259
"title": "Add MongoDB Connection",
@@ -720,6 +728,14 @@
720728
"command": "mdb.exportToGo",
721729
"when": "mdb.isPlayground == true && mdb.connectedToMongoDB == true && mdb.isAtlasStreams == false"
722730
},
731+
{
732+
"command": "mdb.exportToRust",
733+
"when": "mdb.isPlayground == true && mdb.connectedToMongoDB == true && mdb.isAtlasStreams == false"
734+
},
735+
{
736+
"command": "mdb.exportToPHP",
737+
"when": "mdb.isPlayground == true && mdb.connectedToMongoDB == true && mdb.isAtlasStreams == false"
738+
},
723739
{
724740
"command": "mdb.refreshPlaygroundsFromTreeView",
725741
"when": "false"

src/commands/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ enum EXTENSION_COMMANDS {
2323
MDB_EXPORT_TO_NODE = 'mdb.exportToNode',
2424
MDB_EXPORT_TO_RUBY = 'mdb.exportToRuby',
2525
MDB_EXPORT_TO_GO = 'mdb.exportToGo',
26+
MDB_EXPORT_TO_RUST = 'mdb.exportToRust',
27+
MDB_EXPORT_TO_PHP = 'mdb.exportToPHP',
2628
MDB_CHANGE_EXPORT_TO_LANGUAGE_ADDONS = 'mdb.changeExportToLanguageAddons',
2729

2830
MDB_OPEN_MONGODB_DOCUMENT_FROM_CODE_LENS = 'mdb.openMongoDBDocumentFromCodeLens',

src/editors/playgroundSelectedCodeActionProvider.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,28 @@ export default class PlaygroundSelectedCodeActionProvider
133133
tooltip: 'Export To Go',
134134
};
135135
codeActions.push(exportToGoCommand);
136+
137+
const exportToRustCommand = new vscode.CodeAction(
138+
'Export To Rust',
139+
vscode.CodeActionKind.Empty
140+
);
141+
exportToRustCommand.command = {
142+
command: EXTENSION_COMMANDS.MDB_EXPORT_TO_RUST,
143+
title: 'Export To Rust',
144+
tooltip: 'Export To Rust',
145+
};
146+
codeActions.push(exportToRustCommand);
147+
148+
const exportToPHPCommand = new vscode.CodeAction(
149+
'Export To PHP',
150+
vscode.CodeActionKind.Empty
151+
);
152+
exportToPHPCommand.command = {
153+
command: EXTENSION_COMMANDS.MDB_EXPORT_TO_PHP,
154+
title: 'Export To PHP',
155+
tooltip: 'Export To PHP',
156+
};
157+
codeActions.push(exportToPHPCommand);
136158
}
137159

138160
return codeActions;

src/mdbExtensionController.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ export default class MDBExtensionController implements vscode.Disposable {
235235
this.registerCommand(EXTENSION_COMMANDS.MDB_EXPORT_TO_GO, () =>
236236
this._playgroundController.exportToLanguage(ExportToLanguages.GO)
237237
);
238+
this.registerCommand(EXTENSION_COMMANDS.MDB_EXPORT_TO_RUST, () =>
239+
this._playgroundController.exportToLanguage(ExportToLanguages.RUST)
240+
);
241+
this.registerCommand(EXTENSION_COMMANDS.MDB_EXPORT_TO_PHP, () =>
242+
this._playgroundController.exportToLanguage(ExportToLanguages.PHP)
243+
);
238244

239245
this.registerCommand(
240246
EXTENSION_COMMANDS.MDB_CHANGE_EXPORT_TO_LANGUAGE_ADDONS,

src/test/suite/editors/playgroundSelectedCodeActionProvider.test.ts

Lines changed: 157 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
2424

2525
const extensionContextStub = new ExtensionContextStub();
2626

27+
const EXPORT_LANGUAGES_CODEACTIONS_COUNT = 8;
28+
const TOTAL_CODEACTIONS_COUNT = EXPORT_LANGUAGES_CODEACTIONS_COUNT + 1;
29+
2730
// The test extension runner.
2831
extensionContextStub.extensionPath = '../../';
2932

@@ -182,7 +185,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
182185
expect(codeActions).to.exist;
183186

184187
if (codeActions) {
185-
expect(codeActions.length).to.be.equal(7);
188+
expect(codeActions.length).to.be.equal(TOTAL_CODEACTIONS_COUNT);
186189
const actionCommand = codeActions[2].command;
187190

188191
if (actionCommand) {
@@ -218,7 +221,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
218221
expect(codeActions).to.exist;
219222

220223
if (codeActions) {
221-
expect(codeActions.length).to.be.equal(7);
224+
expect(codeActions.length).to.be.equal(TOTAL_CODEACTIONS_COUNT);
222225
const actionCommand = codeActions[2].command;
223226

224227
if (actionCommand) {
@@ -283,7 +286,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
283286
expect(codeActions).to.exist;
284287

285288
if (codeActions) {
286-
expect(codeActions.length).to.be.equal(7);
289+
expect(codeActions.length).to.be.equal(TOTAL_CODEACTIONS_COUNT);
287290
const actionCommand = codeActions[3].command;
288291

289292
if (actionCommand) {
@@ -352,7 +355,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
352355
expect(codeActions).to.exist;
353356

354357
if (codeActions) {
355-
expect(codeActions.length).to.be.equal(7);
358+
expect(codeActions.length).to.be.equal(TOTAL_CODEACTIONS_COUNT);
356359
const actionCommand = codeActions[1].command;
357360

358361
if (actionCommand) {
@@ -426,7 +429,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
426429
expect(codeActions).to.exist;
427430

428431
if (codeActions) {
429-
expect(codeActions.length).to.be.equal(7);
432+
expect(codeActions.length).to.be.equal(TOTAL_CODEACTIONS_COUNT);
430433
const actionCommand = codeActions[5].command;
431434

432435
if (actionCommand) {
@@ -500,7 +503,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
500503
expect(codeActions).to.exist;
501504

502505
if (codeActions) {
503-
expect(codeActions.length).to.be.equal(7);
506+
expect(codeActions.length).to.be.equal(TOTAL_CODEACTIONS_COUNT);
504507
const actionCommand = codeActions[6].command;
505508

506509
if (actionCommand) {
@@ -547,6 +550,154 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
547550
}
548551
}
549552
});
553+
554+
test('exports to rust and includes driver syntax', async () => {
555+
const textFromEditor = "use('db'); db.coll.find({ name: '22' })";
556+
const selection = {
557+
start: { line: 0, character: 24 },
558+
end: { line: 0, character: 38 },
559+
} as vscode.Selection;
560+
const mode = ExportToLanguageMode.QUERY;
561+
const activeTextEditor = {
562+
document: { getText: () => textFromEditor },
563+
} as vscode.TextEditor;
564+
565+
mdbTestExtension.testExtensionController._playgroundController._selectedText =
566+
"{ name: '22' }";
567+
mdbTestExtension.testExtensionController._playgroundController._playgroundSelectedCodeActionProvider.selection =
568+
selection;
569+
mdbTestExtension.testExtensionController._playgroundController._playgroundSelectedCodeActionProvider.mode =
570+
mode;
571+
mdbTestExtension.testExtensionController._playgroundController._activeTextEditor =
572+
activeTextEditor;
573+
574+
testCodeActionProvider.refresh({ selection, mode });
575+
576+
const codeActions = testCodeActionProvider.provideCodeActions();
577+
expect(codeActions).to.exist;
578+
579+
if (codeActions) {
580+
expect(codeActions.length).to.be.equal(TOTAL_CODEACTIONS_COUNT);
581+
const actionCommand = codeActions[7].command;
582+
583+
if (actionCommand) {
584+
expect(actionCommand.command).to.be.equal('mdb.exportToRust');
585+
expect(actionCommand.title).to.be.equal('Export To Rust');
586+
587+
await vscode.commands.executeCommand(actionCommand.command);
588+
589+
let expectedResult: PlaygroundResult = {
590+
namespace: 'DATABASE_NAME.COLLECTION_NAME',
591+
type: null,
592+
content: 'doc! {\n "name": "22"\n}',
593+
language: 'rust',
594+
};
595+
expect(
596+
mdbTestExtension.testExtensionController._playgroundController
597+
._playgroundResult
598+
).to.be.deep.equal(expectedResult);
599+
600+
const codeLenses =
601+
mdbTestExtension.testExtensionController._playgroundController._exportToLanguageCodeLensProvider.provideCodeLenses();
602+
expect(codeLenses.length).to.be.equal(2);
603+
604+
await vscode.commands.executeCommand(
605+
'mdb.changeExportToLanguageAddons',
606+
{
607+
...mdbTestExtension.testExtensionController._playgroundController
608+
._exportToLanguageCodeLensProvider._exportToLanguageAddons,
609+
driverSyntax: true,
610+
}
611+
);
612+
613+
expectedResult = {
614+
namespace: 'db.coll',
615+
type: null,
616+
content: `// Requires the MongoDB crate.\n// https://crates.io/crates/mongodb\n\nlet client = Client::with_uri_str(\"mongodb://localhost:27088/?appname=mongodb-vscode+${version}\").await?;\nlet result = client.database(\"db\").collection::<Document>(\"coll\").find(doc! {\n \"name\": \"22\"\n}, None).await?;`,
617+
language: 'rust',
618+
};
619+
620+
expect(
621+
mdbTestExtension.testExtensionController._playgroundController
622+
._playgroundResult
623+
).to.be.deep.equal(expectedResult);
624+
}
625+
}
626+
});
627+
628+
test('exports to php and includes driver syntax', async () => {
629+
const textFromEditor = "use('db'); db.coll.find({ name: '22' })";
630+
const selection = {
631+
start: { line: 0, character: 24 },
632+
end: { line: 0, character: 38 },
633+
} as vscode.Selection;
634+
const mode = ExportToLanguageMode.QUERY;
635+
const activeTextEditor = {
636+
document: { getText: () => textFromEditor },
637+
} as vscode.TextEditor;
638+
639+
mdbTestExtension.testExtensionController._playgroundController._selectedText =
640+
"{ name: '22' }";
641+
mdbTestExtension.testExtensionController._playgroundController._playgroundSelectedCodeActionProvider.selection =
642+
selection;
643+
mdbTestExtension.testExtensionController._playgroundController._playgroundSelectedCodeActionProvider.mode =
644+
mode;
645+
mdbTestExtension.testExtensionController._playgroundController._activeTextEditor =
646+
activeTextEditor;
647+
648+
testCodeActionProvider.refresh({ selection, mode });
649+
650+
const codeActions = testCodeActionProvider.provideCodeActions();
651+
expect(codeActions).to.exist;
652+
653+
if (codeActions) {
654+
expect(codeActions.length).to.be.equal(TOTAL_CODEACTIONS_COUNT);
655+
const actionCommand = codeActions[8].command;
656+
657+
if (actionCommand) {
658+
expect(actionCommand.command).to.be.equal('mdb.exportToPHP');
659+
expect(actionCommand.title).to.be.equal('Export To PHP');
660+
661+
await vscode.commands.executeCommand(actionCommand.command);
662+
663+
let expectedResult: PlaygroundResult = {
664+
namespace: 'DATABASE_NAME.COLLECTION_NAME',
665+
type: null,
666+
content: "['name' => '22']",
667+
language: 'php',
668+
};
669+
expect(
670+
mdbTestExtension.testExtensionController._playgroundController
671+
._playgroundResult
672+
).to.be.deep.equal(expectedResult);
673+
674+
const codeLenses =
675+
mdbTestExtension.testExtensionController._playgroundController._exportToLanguageCodeLensProvider.provideCodeLenses();
676+
expect(codeLenses.length).to.be.equal(2);
677+
678+
await vscode.commands.executeCommand(
679+
'mdb.changeExportToLanguageAddons',
680+
{
681+
...mdbTestExtension.testExtensionController._playgroundController
682+
._exportToLanguageCodeLensProvider._exportToLanguageAddons,
683+
driverSyntax: true,
684+
}
685+
);
686+
687+
expectedResult = {
688+
namespace: 'db.coll',
689+
type: null,
690+
content: `// Requires the MongoDB PHP Driver\n// https://www.mongodb.com/docs/drivers/php/\n\n$client = new Client('mongodb://localhost:27088/?appname=mongodb-vscode+${version}');\n$collection = $client->selectCollection('db', 'coll');\n$cursor = $collection->find(['name' => '22']);`,
691+
language: 'php',
692+
};
693+
694+
expect(
695+
mdbTestExtension.testExtensionController._playgroundController
696+
._playgroundResult
697+
).to.be.deep.equal(expectedResult);
698+
}
699+
}
700+
});
550701
});
551702

552703
suite('the regular JS file', () => {

src/types/playgroundType.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export enum ExportToLanguages {
4646
JAVASCRIPT = 'javascript',
4747
RUBY = 'ruby',
4848
GO = 'go',
49+
RUST = 'rust',
50+
PHP = 'php',
4951
}
5052

5153
export enum ExportToLanguageMode {

0 commit comments

Comments
 (0)