Skip to content

Commit

Permalink
refactor: use enum for export to languages
Browse files Browse the repository at this point in the history
  • Loading branch information
alenakhineika committed Nov 17, 2021
1 parent c83bd8c commit a01b7cf
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 35 deletions.
8 changes: 6 additions & 2 deletions src/editors/exportToLanguageCodeLensProvider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as vscode from 'vscode';

import EXTENSION_COMMANDS from '../commands';
import { ExportToLanguageMode, ExportToLanguageAddons } from '../types/playgroundType';
import {
ExportToLanguageMode,
ExportToLanguageAddons,
ExportToLanguages
} from '../types/playgroundType';

export default class ExportToLanguageCodeLensProvider
implements vscode.CodeLensProvider {
Expand Down Expand Up @@ -60,7 +64,7 @@ implements vscode.CodeLensProvider {
exportToLanguageCodeLenses.push(driverSyntaxCodeLens);

if (
this._exportToLanguageAddons.language === 'java' &&
this._exportToLanguageAddons.language === ExportToLanguages.JAVA &&
this._exportToLanguageAddons.mode === ExportToLanguageMode.QUERY
) {
buildersCodeLens.command = {
Expand Down
31 changes: 10 additions & 21 deletions src/editors/playgroundController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
ShellExecuteAllResult,
ExportToLanguageAddons,
ExportToLanguageMode,
ExportToLanguageNamespace
ExportToLanguageNamespace,
ExportToLanguages
} from '../types/playgroundType';
import PlaygroundResultProvider, {
PLAYGROUND_RESULT_SCHEME,
Expand Down Expand Up @@ -370,27 +371,11 @@ export default class PlaygroundController {

const { type, content } = playgroundResult;

if (type === 'python') {
return 'python';
if (type && type in ExportToLanguages) {
return type;
}

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

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

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

if (typeof content === 'object' && content !== null) {
return 'json';
}

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

async _openPlaygroundResult(): Promise<void> {
Expand Down Expand Up @@ -618,7 +603,11 @@ export default class PlaygroundController {
}

try {
const useBuilders = builders && language === 'java' && mode === ExportToLanguageMode.QUERY;
const useBuilders = (
builders &&
language === ExportToLanguages.JAVA &&
mode === ExportToLanguageMode.QUERY
);
let transpiledExpression = '';
let imports = '';
let namespace: ExportToLanguageNamespace = {
Expand Down
10 changes: 2 additions & 8 deletions src/editors/playgroundResultProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as vscode from 'vscode';

import ConnectionController from '../connectionController';
import EditDocumentCodeLensProvider from './editDocumentCodeLensProvider';
import type { PlaygroundResult } from '../types/playgroundType';
import { PlaygroundResult, ExportToLanguages } from '../types/playgroundType';

export const PLAYGROUND_RESULT_SCHEME = 'PLAYGROUND_RESULT_SCHEME';

Expand Down Expand Up @@ -53,13 +53,7 @@ implements vscode.TextDocumentContentProvider {
return 'undefined';
}

if (
type === 'string' ||
type === 'python' ||
type === 'java' ||
type === 'csharp' ||
type === 'javascript'
) {
if (type && (type === 'string' || type in ExportToLanguages)) {
return this._playgroundResult.content;
}

Expand Down
9 changes: 5 additions & 4 deletions src/mdbExtensionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import TelemetryService from './telemetry/telemetryService';
import PlaygroundsTreeItem from './explorer/playgroundsTreeItem';
import PlaygroundResultProvider from './editors/playgroundResultProvider';
import WebviewController from './views/webviewController';
import { ExportToLanguages } from './types/playgroundType';

const log = createLogger('commands');

Expand Down Expand Up @@ -195,16 +196,16 @@ export default class MDBExtensionController implements vscode.Disposable {
this._playgroundsExplorer.refresh()
);
this.registerCommand(EXTENSION_COMMANDS.MDB_EXPORT_TO_PYTHON, () =>
this._playgroundController.exportToLanguage('python')
this._playgroundController.exportToLanguage(ExportToLanguages.PYTHON)
);
this.registerCommand(EXTENSION_COMMANDS.MDB_EXPORT_TO_JAVA, () =>
this._playgroundController.exportToLanguage('java')
this._playgroundController.exportToLanguage(ExportToLanguages.JAVA)
);
this.registerCommand(EXTENSION_COMMANDS.MDB_EXPORT_TO_CSHARP, () =>
this._playgroundController.exportToLanguage('csharp')
this._playgroundController.exportToLanguage(ExportToLanguages.CSHARP)
);
this.registerCommand(EXTENSION_COMMANDS.MDB_EXPORT_TO_NODE, () =>
this._playgroundController.exportToLanguage('javascript')
this._playgroundController.exportToLanguage(ExportToLanguages.JAVASCRIPT)
);
this.registerCommand(EXTENSION_COMMANDS.MDB_CHANGE_EXPORT_TO_LANGUAGE_ADDONS, (exportToLanguageAddons) =>
this._playgroundController.changeExportToLanguageAddons(exportToLanguageAddons)
Expand Down
7 changes: 7 additions & 0 deletions src/types/playgroundType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ export interface PlaygroundTextAndSelection {
selection: vscode.Selection;
}

export enum ExportToLanguages {
PYTHON = 'python',
JAVA = 'java',
CSHARP = 'csharp',
JAVASCRIPT = 'javascript'
}

export enum ExportToLanguageMode {
QUERY = 'QUERY',
AGGREGATION = 'AGGREGATION',
Expand Down

0 comments on commit a01b7cf

Please sign in to comment.