Skip to content

Commit

Permalink
feat(copilot): export to Language with Copilot VSCODE-573 (#870)
Browse files Browse the repository at this point in the history
* feat: export to Language with Copilot VSCODE-573

* refactor: rearrange code actions modules

* fear: use driver syntax only

* refactor: lowercase

* refactor: more lowercase

* refactor: clean up

* fix: update export to language prompt

* refactor: clean up

* refactor: address pr comments

* fix: better handling of cancelation requests

* fix: show export to language code lenses only for export to language results

* feat: address pr comments

* test: do not show export to language actions when copilot is missing
  • Loading branch information
alenakhineika authored Nov 21, 2024
1 parent ce1f763 commit 50c385f
Show file tree
Hide file tree
Showing 40 changed files with 1,423 additions and 2,757 deletions.
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.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@
"title": "MongoDB: Change Active Connection"
},
{
"command": "mdb.changeExportToLanguageAddons",
"title": "MongoDB: Change Export To Language Addons"
"command": "mdb.changeDriverSyntax",
"title": "MongoDB: Change Export To Language Driver Syntax"
},
{
"command": "mdb.runSelectedPlaygroundBlocks",
Expand Down Expand Up @@ -753,7 +753,8 @@
],
"mdb.copilot": [
{
"command": "mdb.exportCodeToPlayground"
"command": "mdb.exportCodeToPlayground",
"when": "mdb.isPlayground == false"
}
],
"editor/context": [
Expand Down Expand Up @@ -872,7 +873,7 @@
"when": "mdb.isPlayground"
},
{
"command": "mdb.changeExportToLanguageAddons",
"command": "mdb.changeDriverSyntax",
"when": "false"
},
{
Expand Down Expand Up @@ -1216,7 +1217,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
3 changes: 2 additions & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ enum EXTENSION_COMMANDS {
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_CHANGE_DRIVER_SYNTAX = 'mdb.changeDriverSyntax',

MDB_OPEN_MONGODB_DOCUMENT_FROM_CODE_LENS = 'mdb.openMongoDBDocumentFromCodeLens',
MDB_OPEN_MONGODB_DOCUMENT_FROM_TREE = 'mdb.openMongoDBDocumentFromTree',
Expand Down 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_RESULT = 'mdb.showExportToLanguageResult',
}

export default EXTENSION_COMMANDS;
4 changes: 1 addition & 3 deletions src/commands/launchMongoShell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ const openMongoDBShell = (
void vscode.window.showErrorMessage(
'You need to be connected before launching the MongoDB Shell.'
);

return Promise.resolve(false);
}

Expand All @@ -63,9 +62,8 @@ const openMongoDBShell = (

if (!userShell) {
void vscode.window.showErrorMessage(
'Error: No shell found, please set your default shell environment in vscode.'
'No shell found, please set your default shell environment in vscode.'
);

return Promise.resolve(false);
}

Expand Down
10 changes: 5 additions & 5 deletions src/editors/editDocumentCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DocumentSource } from '../documentSource';
import type { EditDocumentInfo } from '../types/editDocumentInfoType';
import EXTENSION_COMMANDS from '../commands';
import { PLAYGROUND_RESULT_URI } from './playgroundResultProvider';
import type { PlaygroundResult } from '../types/playgroundType';
import type { PlaygroundRunResult } from '../types/playgroundType';

export default class EditDocumentCodeLensProvider
implements vscode.CodeLensProvider
Expand All @@ -31,7 +31,7 @@ export default class EditDocumentCodeLensProvider

updateCodeLensesForCollection(data: {
content: Document;
namespace: string | null;
namespace?: string;
uri: vscode.Uri;
}): void {
let resultCodeLensesInfo: EditDocumentInfo[] = [];
Expand All @@ -44,7 +44,7 @@ export default class EditDocumentCodeLensProvider
this._codeLensesInfo[data.uri.toString()] = resultCodeLensesInfo;
}

updateCodeLensesForPlayground(playgroundResult: PlaygroundResult): void {
updateCodeLensesForPlayground(playgroundResult: PlaygroundRunResult): void {
const source = DocumentSource.DOCUMENT_SOURCE_PLAYGROUND;
let resultCodeLensesInfo: EditDocumentInfo[] = [];

Expand All @@ -71,7 +71,7 @@ export default class EditDocumentCodeLensProvider

_updateCodeLensesForCursor(data: {
content: any;
namespace: string | null;
namespace?: string;
source: DocumentSource;
}): EditDocumentInfo[] {
const resultCodeLensesInfo: EditDocumentInfo[] = [];
Expand Down Expand Up @@ -109,7 +109,7 @@ export default class EditDocumentCodeLensProvider

_updateCodeLensesForDocument(data: {
content: any;
namespace: string | null;
namespace?: string;
source: DocumentSource;
}): EditDocumentInfo[] {
const { content, namespace, source } = data;
Expand Down
45 changes: 21 additions & 24 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 All @@ -97,7 +97,7 @@ export default class EditorsController {
_documentIdStore: DocumentIdStore;
_mongoDBDocumentService: MongoDBDocumentService;
_telemetryService: TelemetryService;
_playgroundResultViewProvider: PlaygroundResultProvider;
_playgroundResultProvider: PlaygroundResultProvider;
_activeConnectionCodeLensProvider: ActiveConnectionCodeLensProvider;
_exportToLanguageCodeLensProvider: ExportToLanguageCodeLensProvider;
_editDocumentCodeLensProvider: EditDocumentCodeLensProvider;
Expand All @@ -109,10 +109,10 @@ export default class EditorsController {
playgroundController,
statusView,
telemetryService,
playgroundResultViewProvider,
playgroundResultProvider,
activeConnectionCodeLensProvider,
exportToLanguageCodeLensProvider,
playgroundSelectedCodeActionProvider,
playgroundSelectionCodeActionProvider,
playgroundDiagnosticsCodeActionProvider,
editDocumentCodeLensProvider,
}: {
Expand All @@ -121,10 +121,10 @@ export default class EditorsController {
playgroundController: PlaygroundController;
statusView: StatusView;
telemetryService: TelemetryService;
playgroundResultViewProvider: PlaygroundResultProvider;
playgroundResultProvider: PlaygroundResultProvider;
activeConnectionCodeLensProvider: ActiveConnectionCodeLensProvider;
exportToLanguageCodeLensProvider: ExportToLanguageCodeLensProvider;
playgroundSelectedCodeActionProvider: PlaygroundSelectedCodeActionProvider;
playgroundSelectionCodeActionProvider: PlaygroundSelectionCodeActionProvider;
playgroundDiagnosticsCodeActionProvider: PlaygroundDiagnosticsCodeActionProvider;
editDocumentCodeLensProvider: EditDocumentCodeLensProvider;
}) {
Expand All @@ -149,15 +149,15 @@ export default class EditorsController {
statusView: new StatusView(context),
editDocumentCodeLensProvider: this._editDocumentCodeLensProvider,
});
this._playgroundResultViewProvider = playgroundResultViewProvider;
this._playgroundResultProvider = playgroundResultProvider;
this._activeConnectionCodeLensProvider = activeConnectionCodeLensProvider;
this._exportToLanguageCodeLensProvider = exportToLanguageCodeLensProvider;
this._collectionDocumentsCodeLensProvider =
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 @@ -317,7 +316,6 @@ export default class EditorsController {
.isCurrentlyFetchingMoreDocuments
) {
void vscode.window.showErrorMessage('Already fetching more documents...');

return Promise.resolve(false);
}

Expand All @@ -330,7 +328,6 @@ export default class EditorsController {
void vscode.window.showErrorMessage(
`Unable to view more documents: no longer connected to ${oldConnectionName}`
);

return Promise.resolve(false);
}

Expand Down Expand Up @@ -399,7 +396,7 @@ export default class EditorsController {
this._context.subscriptions.push(
vscode.workspace.registerTextDocumentContentProvider(
PLAYGROUND_RESULT_SCHEME,
this._playgroundResultViewProvider
this._playgroundResultProvider
)
);
// REGISTER CODE LENSES PROVIDERS.
Expand Down Expand Up @@ -447,10 +444,10 @@ export default class EditorsController {
this._context.subscriptions.push(
vscode.languages.registerCodeActionsProvider(
'javascript',
this._playgroundSelectedCodeActionProvider,
this._playgroundSelectionCodeActionProvider,
{
providedCodeActionKinds:
PlaygroundSelectedCodeActionProvider.providedCodeActionKinds,
PlaygroundSelectionCodeActionProvider.providedCodeActionKinds,
}
)
);
Expand Down
Loading

0 comments on commit 50c385f

Please sign in to comment.