Skip to content

Commit

Permalink
fix: separate evaluation type and results language
Browse files Browse the repository at this point in the history
  • Loading branch information
alenakhineika committed Nov 17, 2021
1 parent 3efb643 commit 62a543d
Show file tree
Hide file tree
Showing 14 changed files with 202 additions and 190 deletions.
4 changes: 2 additions & 2 deletions src/editors/codeActionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ExportToLanguageMode } from '../types/playgroundType';
export default class CodeActionProvider implements vscode.CodeActionProvider {
_onDidChangeCodeCodeAction: vscode.EventEmitter<void> = new vscode.EventEmitter<void>();
selection?: vscode.Selection;
mode?: string;
mode?: ExportToLanguageMode;

static readonly providedCodeActionKinds = [vscode.CodeActionKind.QuickFix];

Expand All @@ -19,7 +19,7 @@ export default class CodeActionProvider implements vscode.CodeActionProvider {
readonly onDidChangeCodeLenses: vscode.Event<void> = this
._onDidChangeCodeCodeAction.event;

refresh({ selection, mode }: { selection?: vscode.Selection, mode?: string }): void {
refresh({ selection, mode }: { selection?: vscode.Selection, mode?: ExportToLanguageMode }): void {
this.selection = selection;
this.mode = mode;
this._onDidChangeCodeCodeAction.fire();
Expand Down
23 changes: 5 additions & 18 deletions src/editors/playgroundController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,20 +364,6 @@ export default class PlaygroundController {
}
}

_getDocumentLanguage(playgroundResult: PlaygroundResult): string {
if (!playgroundResult) {
return 'plaintext';
}

const { type, content } = playgroundResult;

if (type && type in ExportToLanguages) {
return type;
}

return (typeof content === 'object' && content !== null) ? 'json' : 'plaintext';
}

async _openPlaygroundResult(): Promise<void> {
this._playgroundResultViewProvider.setPlaygroundResult(
this._playgroundResult
Expand All @@ -392,7 +378,7 @@ export default class PlaygroundController {
await this._showResultAsVirtualDocument();

if (this._playgroundResultTextDocument) {
const language = this._getDocumentLanguage(this._playgroundResult);
const language = this._playgroundResult?.language || 'plaintext';

await vscode.languages.setTextDocumentLanguage(
this._playgroundResultTextDocument,
Expand Down Expand Up @@ -599,7 +585,7 @@ export default class PlaygroundController {
'Please select one or more lines in the playground.'
);

return Promise.resolve(true);
return true;
}

try {
Expand Down Expand Up @@ -645,8 +631,9 @@ export default class PlaygroundController {
namespace: namespace.databaseName && namespace.collectionName
? `${namespace.databaseName}.${namespace.collectionName}`
: null,
type: language,
content: imports ? `${imports}\n\n${transpiledExpression}` : transpiledExpression
type: null,
content: imports ? `${imports}\n\n${transpiledExpression}` : transpiledExpression,
language
};

log.info(`Export to ${language} language result`, this._playgroundResult);
Expand Down
7 changes: 4 additions & 3 deletions src/editors/playgroundResultProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ implements vscode.TextDocumentContentProvider {
this._playgroundResult = {
namespace: null,
type: null,
content: undefined
content: undefined,
language: null
};
}

Expand All @@ -47,13 +48,13 @@ implements vscode.TextDocumentContentProvider {
return 'undefined';
}

const { type, content } = this._playgroundResult;
const { type, content, language } = this._playgroundResult;

if (type === 'undefined') {
return 'undefined';
}

if (type && (type === 'string' || type in ExportToLanguages)) {
if (type === 'string' || (language && Object.values(ExportToLanguages).includes(language as ExportToLanguages))) {
return this._playgroundResult.content;
}

Expand Down
22 changes: 14 additions & 8 deletions src/language/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ const executeAll = async (
outputLines.push({
type,
content: printable,
namespace: null
namespace: null,
language: null
});
}
}
Expand All @@ -52,16 +53,21 @@ const executeAll = async (
source && source.namespace
? `${source.namespace.db}.${source.namespace.collection}`
: null;
const content =
type === 'Cursor' || type === 'AggregationCursor' ?
JSON.parse(EJSON.stringify(printable.documents)) :
typeof printable === 'string'
? printable
: JSON.parse(EJSON.stringify(printable));
let content = '';

if (type === 'Cursor' || type === 'AggregationCursor') {
content = JSON.parse(EJSON.stringify(printable.documents));
} else {
content = typeof printable === 'string'
? printable
: JSON.parse(EJSON.stringify(printable));
}

const result: PlaygroundResult = {
namespace,
type: type ? type : typeof printable,
content
content,
language: (typeof content === 'object' && content !== null) ? 'json' : 'plaintext'
};

return [null, { outputLines, result }];
Expand Down
31 changes: 17 additions & 14 deletions src/test/suite/editors/codeActionProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import CodeActionProvider from '../../../editors/codeActionProvider';
import { ExplorerController } from '../../../explorer';
import { LanguageServerController } from '../../../language';
import { PlaygroundController } from '../../../editors';
import { PlaygroundResult } from '../../../types/playgroundType';
import { PlaygroundResult, ExportToLanguageMode } from '../../../types/playgroundType';

import { mdbTestExtension } from '../stubbableMdbExtension';
import { TestExtensionContext } from '../stubs';
Expand Down Expand Up @@ -104,7 +104,7 @@ suite('Code Action Provider Test Suite', function () {
} as vscode.Selection;
const testCodeActionProvider = new CodeActionProvider();

testCodeActionProvider.refresh({ selection, mode: 'OTHER' });
testCodeActionProvider.refresh({ selection, mode: ExportToLanguageMode.OTHER });

const codeActions = testCodeActionProvider.provideCodeActions();

Expand All @@ -120,7 +120,7 @@ suite('Code Action Provider Test Suite', function () {

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

const expectedResult = { namespace: null, type: 'number', content: 123 };
const expectedResult = { namespace: null, type: 'number', content: 123, language: 'plaintext' };
expect(mdbTestExtension.testExtensionController._playgroundController._playgroundResult).to.be.deep.equal(expectedResult);
expect(mdbTestExtension.testExtensionController._playgroundController._isPartialRun).to.be.equal(true);
}
Expand All @@ -133,7 +133,7 @@ suite('Code Action Provider Test Suite', function () {
start: { line: 0, character: 0 },
end: { line: 0, character: 14 }
} as vscode.Selection;
const mode = 'QUERY';
const mode = ExportToLanguageMode.QUERY;

mdbTestExtension.testExtensionController._playgroundController._selectedText = textFromEditor;
mdbTestExtension.testExtensionController._playgroundController._codeActionProvider.selection = selection;
Expand Down Expand Up @@ -163,12 +163,12 @@ suite('Code Action Provider Test Suite', function () {

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

const expectedResult = { namespace: null, type: 'java', content: 'new Document("name", "22")' };
const expectedResult = { namespace: null, type: null, content: 'new Document("name", "22")', language: 'java' };

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

// Only java supports builders.
// Only java queries supports builders.
await vscode.commands.executeCommand('mdb.changeExportToLanguageAddons', {
...mdbTestExtension.testExtensionController._playgroundController._exportToLanguageCodeLensProvider._exportToLanguageAddons,
builders: true
Expand All @@ -186,7 +186,7 @@ suite('Code Action Provider Test Suite', function () {
start: { line: 0, character: 0 },
end: { line: 0, character: 14 }
} as vscode.Selection;
const mode = 'QUERY';
const mode = ExportToLanguageMode.QUERY;

mdbTestExtension.testExtensionController._playgroundController._selectedText = textFromEditor;
mdbTestExtension.testExtensionController._playgroundController._codeActionProvider.selection = selection;
Expand Down Expand Up @@ -218,8 +218,9 @@ suite('Code Action Provider Test Suite', function () {

const expectedResult = {
namespace: null,
type: 'csharp',
content: 'new BsonDocument(\"name\", \"22\")'
type: null,
content: 'new BsonDocument(\"name\", \"22\")',
language: 'csharp'
};
expect(mdbTestExtension.testExtensionController._playgroundController._playgroundResult).to.be.deep.equal(expectedResult);

Expand All @@ -244,7 +245,7 @@ suite('Code Action Provider Test Suite', function () {
start: { line: 0, character: 24 },
end: { line: 0, character: 38 }
} as vscode.Selection;
const mode = 'QUERY';
const mode = ExportToLanguageMode.QUERY;

mdbTestExtension.testExtensionController._playgroundController._selectedText = "{ name: '22' }";
mdbTestExtension.testExtensionController._playgroundController._codeActionProvider.selection = selection;
Expand Down Expand Up @@ -276,8 +277,9 @@ suite('Code Action Provider Test Suite', function () {

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

Expand All @@ -291,8 +293,9 @@ suite('Code Action Provider Test Suite', function () {

expectedResult = {
namespace: 'db.coll',
type: 'python',
content: "# Requires the PyMongo package.\n# https://api.mongodb.com/python/current\n\nclient = MongoClient('mongodb://localhost:27018/?readPreference=primary&appname=mongodb-vscode+0.0.0-dev.0&directConnection=true&ssl=false')\nresult = client['db']['coll'].aggregate({\n 'name': '22'\n})"
type: null,
content: "# Requires the PyMongo package.\n# https://api.mongodb.com/python/current\n\nclient = MongoClient('mongodb://localhost:27018/?readPreference=primary&appname=mongodb-vscode+0.0.0-dev.0&directConnection=true&ssl=false')\nresult = client['db']['coll'].aggregate({\n 'name': '22'\n})",
language: 'python'
};

expect(mdbTestExtension.testExtensionController._playgroundController._playgroundResult).to.be.deep.equal(expectedResult);
Expand Down
9 changes: 6 additions & 3 deletions src/test/suite/editors/editDocumentCodeLensProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ suite('Edit Document Code Lens Provider Test Suite', () => {
testCodeLensProvider.updateCodeLensesForPlayground({
namespace: 'db.coll',
type: 'Document',
content: { _id: '93333a0d-83f6-4e6f-a575-af7ea6187a4a' }
content: { _id: '93333a0d-83f6-4e6f-a575-af7ea6187a4a' },
language: 'json'
});

const codeLens = testCodeLensProvider.provideCodeLenses();
Expand Down Expand Up @@ -164,7 +165,8 @@ suite('Edit Document Code Lens Provider Test Suite', () => {
content: [
{ _id: '93333a0d-83f6-4e6f-a575-af7ea6187a4a' },
{ _id: '21333a0d-83f6-4e6f-a575-af7ea6187444' }
]
],
language: 'json'
});

const codeLens = testCodeLensProvider.provideCodeLenses();
Expand Down Expand Up @@ -226,7 +228,8 @@ suite('Edit Document Code Lens Provider Test Suite', () => {
$date: '2014-04-04T21:23:13.331Z'
}
}
]
],
language: 'json'
});

const codeLens = testCodeLensProvider.provideCodeLenses();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { beforeEach } from 'mocha';
import chai from 'chai';

import ExportToLanguageCodeLensProvider from '../../../editors/exportToLanguageCodeLensProvider';
import { ExportToLanguageMode } from '../../../types/playgroundType';

const expect = chai.expect;

Expand Down Expand Up @@ -55,7 +56,7 @@ suite('Export To Language Code Lens Provider Test Suite', function () {
});

test('has the use builders code lens when builders is false, language is java, and mode is query', () => {
testExportToLanguageCodeLensProvider.refresh({ ...defaults, mode: 'QUERY', language: 'java' });
testExportToLanguageCodeLensProvider.refresh({ ...defaults, mode: ExportToLanguageMode.QUERY, language: 'java' });

const codeLenses = testExportToLanguageCodeLensProvider.provideCodeLenses();

Expand All @@ -64,7 +65,7 @@ suite('Export To Language Code Lens Provider Test Suite', function () {
});

test('has the use raw query code lens when builders is true, language is java, and mode is query', () => {
testExportToLanguageCodeLensProvider.refresh({ ...defaults, builders: true, mode: 'QUERY', language: 'java' });
testExportToLanguageCodeLensProvider.refresh({ ...defaults, builders: true, mode: ExportToLanguageMode.QUERY, language: 'java' });

const codeLenses = testExportToLanguageCodeLensProvider.provideCodeLenses();

Expand All @@ -73,7 +74,7 @@ suite('Export To Language Code Lens Provider Test Suite', function () {
});

test('does not have the use raw query code lens when builders is true, language is java, and mode is plain text', () => {
testExportToLanguageCodeLensProvider.refresh({ ...defaults, builders: true, mode: 'OTHER', language: 'java' });
testExportToLanguageCodeLensProvider.refresh({ ...defaults, builders: true, mode: ExportToLanguageMode.OTHER, language: 'java' });

const codeLenses = testExportToLanguageCodeLensProvider.provideCodeLenses();

Expand Down
Loading

0 comments on commit 62a543d

Please sign in to comment.