Skip to content

Commit

Permalink
feat: add export to Rust and PHP VSCODE-411 (#676)
Browse files Browse the repository at this point in the history
* feat: add export to Rust and PHP VSCODE-411

* test: update playgroundSelectedCodeActionProvider tests
  • Loading branch information
paula-stacho authored Jan 25, 2024
1 parent 6c32739 commit 4606982
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Select queries and aggregations within your Playground files and translate them
* Python 3
* Ruby
* Go
* Rust
* PHP

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

Expand Down
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@
"command": "mdb.exportToGo",
"title": "MongoDB: Export To Go"
},
{
"command": "mdb.exportToRust",
"title": "MongoDB: Export To Rust"
},
{
"command": "mdb.exportToPHP",
"title": "MongoDB: Export To PHP"
},
{
"command": "mdb.addConnection",
"title": "Add MongoDB Connection",
Expand Down Expand Up @@ -720,6 +728,14 @@
"command": "mdb.exportToGo",
"when": "mdb.isPlayground == true && mdb.connectedToMongoDB == true && mdb.isAtlasStreams == false"
},
{
"command": "mdb.exportToRust",
"when": "mdb.isPlayground == true && mdb.connectedToMongoDB == true && mdb.isAtlasStreams == false"
},
{
"command": "mdb.exportToPHP",
"when": "mdb.isPlayground == true && mdb.connectedToMongoDB == true && mdb.isAtlasStreams == false"
},
{
"command": "mdb.refreshPlaygroundsFromTreeView",
"when": "false"
Expand Down
2 changes: 2 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ enum EXTENSION_COMMANDS {
MDB_EXPORT_TO_NODE = 'mdb.exportToNode',
MDB_EXPORT_TO_RUBY = 'mdb.exportToRuby',
MDB_EXPORT_TO_GO = 'mdb.exportToGo',
MDB_EXPORT_TO_RUST = 'mdb.exportToRust',
MDB_EXPORT_TO_PHP = 'mdb.exportToPHP',
MDB_CHANGE_EXPORT_TO_LANGUAGE_ADDONS = 'mdb.changeExportToLanguageAddons',

MDB_OPEN_MONGODB_DOCUMENT_FROM_CODE_LENS = 'mdb.openMongoDBDocumentFromCodeLens',
Expand Down
22 changes: 22 additions & 0 deletions src/editors/playgroundSelectedCodeActionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,28 @@ export default class PlaygroundSelectedCodeActionProvider
tooltip: 'Export To Go',
};
codeActions.push(exportToGoCommand);

const exportToRustCommand = new vscode.CodeAction(
'Export To Rust',
vscode.CodeActionKind.Empty
);
exportToRustCommand.command = {
command: EXTENSION_COMMANDS.MDB_EXPORT_TO_RUST,
title: 'Export To Rust',
tooltip: 'Export To Rust',
};
codeActions.push(exportToRustCommand);

const exportToPHPCommand = new vscode.CodeAction(
'Export To PHP',
vscode.CodeActionKind.Empty
);
exportToPHPCommand.command = {
command: EXTENSION_COMMANDS.MDB_EXPORT_TO_PHP,
title: 'Export To PHP',
tooltip: 'Export To PHP',
};
codeActions.push(exportToPHPCommand);
}

return codeActions;
Expand Down
6 changes: 6 additions & 0 deletions src/mdbExtensionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ export default class MDBExtensionController implements vscode.Disposable {
this.registerCommand(EXTENSION_COMMANDS.MDB_EXPORT_TO_GO, () =>
this._playgroundController.exportToLanguage(ExportToLanguages.GO)
);
this.registerCommand(EXTENSION_COMMANDS.MDB_EXPORT_TO_RUST, () =>
this._playgroundController.exportToLanguage(ExportToLanguages.RUST)
);
this.registerCommand(EXTENSION_COMMANDS.MDB_EXPORT_TO_PHP, () =>
this._playgroundController.exportToLanguage(ExportToLanguages.PHP)
);

this.registerCommand(
EXTENSION_COMMANDS.MDB_CHANGE_EXPORT_TO_LANGUAGE_ADDONS,
Expand Down
163 changes: 157 additions & 6 deletions src/test/suite/editors/playgroundSelectedCodeActionProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {

const extensionContextStub = new ExtensionContextStub();

const EXPORT_LANGUAGES_CODEACTIONS_COUNT = 8;
const TOTAL_CODEACTIONS_COUNT = EXPORT_LANGUAGES_CODEACTIONS_COUNT + 1;

// The test extension runner.
extensionContextStub.extensionPath = '../../';

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

if (codeActions) {
expect(codeActions.length).to.be.equal(7);
expect(codeActions.length).to.be.equal(TOTAL_CODEACTIONS_COUNT);
const actionCommand = codeActions[2].command;

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

if (codeActions) {
expect(codeActions.length).to.be.equal(7);
expect(codeActions.length).to.be.equal(TOTAL_CODEACTIONS_COUNT);
const actionCommand = codeActions[2].command;

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

if (codeActions) {
expect(codeActions.length).to.be.equal(7);
expect(codeActions.length).to.be.equal(TOTAL_CODEACTIONS_COUNT);
const actionCommand = codeActions[3].command;

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

if (codeActions) {
expect(codeActions.length).to.be.equal(7);
expect(codeActions.length).to.be.equal(TOTAL_CODEACTIONS_COUNT);
const actionCommand = codeActions[1].command;

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

if (codeActions) {
expect(codeActions.length).to.be.equal(7);
expect(codeActions.length).to.be.equal(TOTAL_CODEACTIONS_COUNT);
const actionCommand = codeActions[5].command;

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

if (codeActions) {
expect(codeActions.length).to.be.equal(7);
expect(codeActions.length).to.be.equal(TOTAL_CODEACTIONS_COUNT);
const actionCommand = codeActions[6].command;

if (actionCommand) {
Expand Down Expand Up @@ -547,6 +550,154 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
}
}
});

test('exports to rust and includes driver syntax', async () => {
const textFromEditor = "use('db'); db.coll.find({ name: '22' })";
const selection = {
start: { line: 0, character: 24 },
end: { line: 0, character: 38 },
} as vscode.Selection;
const mode = ExportToLanguageMode.QUERY;
const activeTextEditor = {
document: { getText: () => textFromEditor },
} as vscode.TextEditor;

mdbTestExtension.testExtensionController._playgroundController._selectedText =
"{ name: '22' }";
mdbTestExtension.testExtensionController._playgroundController._playgroundSelectedCodeActionProvider.selection =
selection;
mdbTestExtension.testExtensionController._playgroundController._playgroundSelectedCodeActionProvider.mode =
mode;
mdbTestExtension.testExtensionController._playgroundController._activeTextEditor =
activeTextEditor;

testCodeActionProvider.refresh({ selection, mode });

const codeActions = testCodeActionProvider.provideCodeActions();
expect(codeActions).to.exist;

if (codeActions) {
expect(codeActions.length).to.be.equal(TOTAL_CODEACTIONS_COUNT);
const actionCommand = codeActions[7].command;

if (actionCommand) {
expect(actionCommand.command).to.be.equal('mdb.exportToRust');
expect(actionCommand.title).to.be.equal('Export To Rust');

await vscode.commands.executeCommand(actionCommand.command);

let expectedResult: PlaygroundResult = {
namespace: 'DATABASE_NAME.COLLECTION_NAME',
type: null,
content: 'doc! {\n "name": "22"\n}',
language: 'rust',
};
expect(
mdbTestExtension.testExtensionController._playgroundController
._playgroundResult
).to.be.deep.equal(expectedResult);

const codeLenses =
mdbTestExtension.testExtensionController._playgroundController._exportToLanguageCodeLensProvider.provideCodeLenses();
expect(codeLenses.length).to.be.equal(2);

await vscode.commands.executeCommand(
'mdb.changeExportToLanguageAddons',
{
...mdbTestExtension.testExtensionController._playgroundController
._exportToLanguageCodeLensProvider._exportToLanguageAddons,
driverSyntax: true,
}
);

expectedResult = {
namespace: 'db.coll',
type: null,
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?;`,
language: 'rust',
};

expect(
mdbTestExtension.testExtensionController._playgroundController
._playgroundResult
).to.be.deep.equal(expectedResult);
}
}
});

test('exports to php and includes driver syntax', async () => {
const textFromEditor = "use('db'); db.coll.find({ name: '22' })";
const selection = {
start: { line: 0, character: 24 },
end: { line: 0, character: 38 },
} as vscode.Selection;
const mode = ExportToLanguageMode.QUERY;
const activeTextEditor = {
document: { getText: () => textFromEditor },
} as vscode.TextEditor;

mdbTestExtension.testExtensionController._playgroundController._selectedText =
"{ name: '22' }";
mdbTestExtension.testExtensionController._playgroundController._playgroundSelectedCodeActionProvider.selection =
selection;
mdbTestExtension.testExtensionController._playgroundController._playgroundSelectedCodeActionProvider.mode =
mode;
mdbTestExtension.testExtensionController._playgroundController._activeTextEditor =
activeTextEditor;

testCodeActionProvider.refresh({ selection, mode });

const codeActions = testCodeActionProvider.provideCodeActions();
expect(codeActions).to.exist;

if (codeActions) {
expect(codeActions.length).to.be.equal(TOTAL_CODEACTIONS_COUNT);
const actionCommand = codeActions[8].command;

if (actionCommand) {
expect(actionCommand.command).to.be.equal('mdb.exportToPHP');
expect(actionCommand.title).to.be.equal('Export To PHP');

await vscode.commands.executeCommand(actionCommand.command);

let expectedResult: PlaygroundResult = {
namespace: 'DATABASE_NAME.COLLECTION_NAME',
type: null,
content: "['name' => '22']",
language: 'php',
};
expect(
mdbTestExtension.testExtensionController._playgroundController
._playgroundResult
).to.be.deep.equal(expectedResult);

const codeLenses =
mdbTestExtension.testExtensionController._playgroundController._exportToLanguageCodeLensProvider.provideCodeLenses();
expect(codeLenses.length).to.be.equal(2);

await vscode.commands.executeCommand(
'mdb.changeExportToLanguageAddons',
{
...mdbTestExtension.testExtensionController._playgroundController
._exportToLanguageCodeLensProvider._exportToLanguageAddons,
driverSyntax: true,
}
);

expectedResult = {
namespace: 'db.coll',
type: null,
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']);`,
language: 'php',
};

expect(
mdbTestExtension.testExtensionController._playgroundController
._playgroundResult
).to.be.deep.equal(expectedResult);
}
}
});
});

suite('the regular JS file', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/types/playgroundType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export enum ExportToLanguages {
JAVASCRIPT = 'javascript',
RUBY = 'ruby',
GO = 'go',
RUST = 'rust',
PHP = 'php',
}

export enum ExportToLanguageMode {
Expand Down

0 comments on commit 4606982

Please sign in to comment.