Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(copilot): export to Language with Copilot VSCODE-573 #870

Merged
merged 16 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ updates:
patterns:
- "@mongodb-js/compass-*"
- mongodb-data-service
- bson-transpilers
- "@mongodb-js/connection-form"
mongosh:
patterns:
Expand Down
55 changes: 4 additions & 51 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,6 @@
"@mongosh/shell-api": "^2.3.3",
"@segment/analytics-node": "^1.3.0",
"bson": "^6.8.0",
"bson-transpilers": "^2.2.0",
"debug": "^4.3.7",
"dotenv": "^16.4.5",
"ejson-shell-parser": "^2.0.1",
Expand Down
1 change: 1 addition & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ enum EXTENSION_COMMANDS {
SELECT_DATABASE_WITH_PARTICIPANT = 'mdb.selectDatabaseWithParticipant',
SELECT_COLLECTION_WITH_PARTICIPANT = 'mdb.selectCollectionWithParticipant',
PARTICIPANT_OPEN_RAW_SCHEMA_OUTPUT = 'mdb.participantViewRawSchemaOutput',
SHOW_EXPORT_TO_LANGUAGE_RESULTS = 'mdb.showExportToLanguageResults',
}

export default EXTENSION_COMMANDS;
33 changes: 16 additions & 17 deletions src/editors/editorsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Document } from 'bson';

import type ActiveConnectionCodeLensProvider from './activeConnectionCodeLensProvider';
import type ExportToLanguageCodeLensProvider from './exportToLanguageCodeLensProvider';
import PlaygroundSelectedCodeActionProvider from './playgroundSelectedCodeActionProvider';
import PlaygroundSelectionCodeActionProvider from './playgroundSelectionCodeActionProvider';
import PlaygroundDiagnosticsCodeActionProvider from './playgroundDiagnosticsCodeActionProvider';
import type ConnectionController from '../connectionController';
import CollectionDocumentsCodeLensProvider from './collectionDocumentsCodeLensProvider';
Expand Down Expand Up @@ -38,7 +38,7 @@ const log = createLogger('editors controller');
export function getFileDisplayNameForDocument(
documentId: any,
namespace: string
) {
): string {
let displayName = `${namespace}:${EJSON.stringify(documentId)}`;

// Encode special file uri characters to ensure VSCode handles
Expand Down Expand Up @@ -84,7 +84,7 @@ export function getViewCollectionDocumentsUri(
* new editors and the data they need. It also manages active editors.
*/
export default class EditorsController {
_playgroundSelectedCodeActionProvider: PlaygroundSelectedCodeActionProvider;
_playgroundSelectionCodeActionProvider: PlaygroundSelectionCodeActionProvider;
_playgroundDiagnosticsCodeActionProvider: PlaygroundDiagnosticsCodeActionProvider;
_connectionController: ConnectionController;
_playgroundController: PlaygroundController;
Expand Down Expand Up @@ -112,7 +112,7 @@ export default class EditorsController {
playgroundResultViewProvider,
activeConnectionCodeLensProvider,
exportToLanguageCodeLensProvider,
playgroundSelectedCodeActionProvider,
playgroundSelectionCodeActionProvider,
playgroundDiagnosticsCodeActionProvider,
editDocumentCodeLensProvider,
}: {
Expand All @@ -124,7 +124,7 @@ export default class EditorsController {
playgroundResultViewProvider: PlaygroundResultProvider;
activeConnectionCodeLensProvider: ActiveConnectionCodeLensProvider;
exportToLanguageCodeLensProvider: ExportToLanguageCodeLensProvider;
playgroundSelectedCodeActionProvider: PlaygroundSelectedCodeActionProvider;
playgroundSelectionCodeActionProvider: PlaygroundSelectionCodeActionProvider;
playgroundDiagnosticsCodeActionProvider: PlaygroundDiagnosticsCodeActionProvider;
editDocumentCodeLensProvider: EditDocumentCodeLensProvider;
}) {
Expand Down Expand Up @@ -156,8 +156,8 @@ export default class EditorsController {
new CollectionDocumentsCodeLensProvider(
this._collectionDocumentsOperationsStore
);
this._playgroundSelectedCodeActionProvider =
playgroundSelectedCodeActionProvider;
this._playgroundSelectionCodeActionProvider =
playgroundSelectionCodeActionProvider;
this._playgroundDiagnosticsCodeActionProvider =
playgroundDiagnosticsCodeActionProvider;

Expand Down Expand Up @@ -218,15 +218,14 @@ export default class EditorsController {
}

async saveMongoDBDocument(): Promise<boolean> {
const activeEditor = vscode.window.activeTextEditor;
const editor = vscode.window.activeTextEditor;

if (!activeEditor) {
if (!editor) {
await vscode.commands.executeCommand('workbench.action.files.save');

return false;
}

const uriParams = new URLSearchParams(activeEditor.document.uri.query);
const uriParams = new URLSearchParams(editor.document.uri.query);
const namespace = uriParams.get(NAMESPACE_URI_IDENTIFIER);
const connectionId = uriParams.get(CONNECTION_ID_URI_IDENTIFIER);
const documentIdReference = uriParams.get(DOCUMENT_ID_URI_IDENTIFIER) || '';
Expand All @@ -236,21 +235,21 @@ export default class EditorsController {
) as DocumentSource;

if (
activeEditor.document.uri.scheme !== 'VIEW_DOCUMENT_SCHEME' ||
editor.document.uri.scheme !== 'VIEW_DOCUMENT_SCHEME' ||
!namespace ||
!connectionId ||
// A valid documentId can be false.
documentId === null ||
documentId === undefined
) {
void vscode.window.showErrorMessage(
`The current file can not be saved as a MongoDB document. Invalid URL: ${activeEditor.document.uri.toString()}`
`The current file can not be saved as a MongoDB document. Invalid URL: ${editor.document.uri.toString()}`
);
return false;
}

try {
const newDocument = EJSON.parse(activeEditor.document.getText() || '');
const newDocument = EJSON.parse(editor.document.getText() || '');

await this._mongoDBDocumentService.replaceDocument({
namespace,
Expand All @@ -261,7 +260,7 @@ export default class EditorsController {
});

// Save document changes to active editor.
await activeEditor?.document.save();
await editor?.document.save();

void vscode.window.showInformationMessage(
`The document was saved successfully to '${namespace}'`
Expand Down Expand Up @@ -447,10 +446,10 @@ export default class EditorsController {
this._context.subscriptions.push(
vscode.languages.registerCodeActionsProvider(
'javascript',
this._playgroundSelectedCodeActionProvider,
this._playgroundSelectionCodeActionProvider,
{
providedCodeActionKinds:
PlaygroundSelectedCodeActionProvider.providedCodeActionKinds,
PlaygroundSelectionCodeActionProvider.providedCodeActionKinds,
}
)
);
Expand Down
43 changes: 2 additions & 41 deletions src/editors/exportToLanguageCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import * as vscode from 'vscode';

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

export default class ExportToLanguageCodeLensProvider
alenakhineika marked this conversation as resolved.
Show resolved Hide resolved
implements vscode.CodeLensProvider
Expand All @@ -19,9 +16,8 @@ export default class ExportToLanguageCodeLensProvider

constructor() {
this._exportToLanguageAddons = {
importStatements: false,
codeToTranspile: '',
driverSyntax: false,
builders: false,
language: 'shell',
};

Expand All @@ -40,29 +36,13 @@ export default class ExportToLanguageCodeLensProvider
}

provideCodeLenses(): vscode.CodeLens[] {
const importStatementsCodeLens = this.createCodeLens();
const driverSyntaxCodeLens = this.createCodeLens();
const buildersCodeLens = this.createCodeLens();
const exportToLanguageCodeLenses: vscode.CodeLens[] = [];

if (['json', 'plaintext'].includes(this._exportToLanguageAddons.language)) {
return [];
}

importStatementsCodeLens.command = {
title: this._exportToLanguageAddons.importStatements
? 'Exclude Import Statements'
: 'Include Import Statements',
command: EXTENSION_COMMANDS.MDB_CHANGE_EXPORT_TO_LANGUAGE_ADDONS,
arguments: [
{
...this._exportToLanguageAddons,
importStatements: !this._exportToLanguageAddons.importStatements,
},
],
};
exportToLanguageCodeLenses.push(importStatementsCodeLens);

if (this._exportToLanguageAddons.language !== ExportToLanguages.CSHARP) {
driverSyntaxCodeLens.command = {
title: this._exportToLanguageAddons.driverSyntax
Expand All @@ -79,25 +59,6 @@ export default class ExportToLanguageCodeLensProvider
exportToLanguageCodeLenses.push(driverSyntaxCodeLens);
}

if (
this._exportToLanguageAddons.language === ExportToLanguages.JAVA &&
this._exportToLanguageAddons.mode === ExportToLanguageMode.QUERY
) {
buildersCodeLens.command = {
title: this._exportToLanguageAddons.builders
? 'Use Raw Query'
: 'Use Builders',
command: EXTENSION_COMMANDS.MDB_CHANGE_EXPORT_TO_LANGUAGE_ADDONS,
arguments: [
{
...this._exportToLanguageAddons,
builders: !this._exportToLanguageAddons.builders,
},
],
};
exportToLanguageCodeLenses.push(buildersCodeLens);
}

return exportToLanguageCodeLenses;
}
}
Loading
Loading