Skip to content

Commit 50c385f

Browse files
feat(copilot): export to Language with Copilot VSCODE-573 (#870)
* 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
1 parent ce1f763 commit 50c385f

40 files changed

+1423
-2757
lines changed

.github/dependabot.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ updates:
2222
patterns:
2323
- "@mongodb-js/compass-*"
2424
- mongodb-data-service
25-
- bson-transpilers
2625
- "@mongodb-js/connection-form"
2726
mongosh:
2827
patterns:

package-lock.json

Lines changed: 4 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@
259259
"title": "MongoDB: Change Active Connection"
260260
},
261261
{
262-
"command": "mdb.changeExportToLanguageAddons",
263-
"title": "MongoDB: Change Export To Language Addons"
262+
"command": "mdb.changeDriverSyntax",
263+
"title": "MongoDB: Change Export To Language Driver Syntax"
264264
},
265265
{
266266
"command": "mdb.runSelectedPlaygroundBlocks",
@@ -753,7 +753,8 @@
753753
],
754754
"mdb.copilot": [
755755
{
756-
"command": "mdb.exportCodeToPlayground"
756+
"command": "mdb.exportCodeToPlayground",
757+
"when": "mdb.isPlayground == false"
757758
}
758759
],
759760
"editor/context": [
@@ -872,7 +873,7 @@
872873
"when": "mdb.isPlayground"
873874
},
874875
{
875-
"command": "mdb.changeExportToLanguageAddons",
876+
"command": "mdb.changeDriverSyntax",
876877
"when": "false"
877878
},
878879
{
@@ -1216,7 +1217,6 @@
12161217
"@mongosh/shell-api": "^2.3.3",
12171218
"@segment/analytics-node": "^1.3.0",
12181219
"bson": "^6.8.0",
1219-
"bson-transpilers": "^2.2.0",
12201220
"debug": "^4.3.7",
12211221
"dotenv": "^16.4.5",
12221222
"ejson-shell-parser": "^2.0.1",

src/commands/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ enum EXTENSION_COMMANDS {
2828
MDB_EXPORT_TO_GO = 'mdb.exportToGo',
2929
MDB_EXPORT_TO_RUST = 'mdb.exportToRust',
3030
MDB_EXPORT_TO_PHP = 'mdb.exportToPHP',
31-
MDB_CHANGE_EXPORT_TO_LANGUAGE_ADDONS = 'mdb.changeExportToLanguageAddons',
31+
MDB_CHANGE_DRIVER_SYNTAX = 'mdb.changeDriverSyntax',
3232

3333
MDB_OPEN_MONGODB_DOCUMENT_FROM_CODE_LENS = 'mdb.openMongoDBDocumentFromCodeLens',
3434
MDB_OPEN_MONGODB_DOCUMENT_FROM_TREE = 'mdb.openMongoDBDocumentFromTree',
@@ -84,6 +84,7 @@ enum EXTENSION_COMMANDS {
8484
SELECT_DATABASE_WITH_PARTICIPANT = 'mdb.selectDatabaseWithParticipant',
8585
SELECT_COLLECTION_WITH_PARTICIPANT = 'mdb.selectCollectionWithParticipant',
8686
PARTICIPANT_OPEN_RAW_SCHEMA_OUTPUT = 'mdb.participantViewRawSchemaOutput',
87+
SHOW_EXPORT_TO_LANGUAGE_RESULT = 'mdb.showExportToLanguageResult',
8788
}
8889

8990
export default EXTENSION_COMMANDS;

src/commands/launchMongoShell.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ const openMongoDBShell = (
5252
void vscode.window.showErrorMessage(
5353
'You need to be connected before launching the MongoDB Shell.'
5454
);
55-
5655
return Promise.resolve(false);
5756
}
5857

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

6463
if (!userShell) {
6564
void vscode.window.showErrorMessage(
66-
'Error: No shell found, please set your default shell environment in vscode.'
65+
'No shell found, please set your default shell environment in vscode.'
6766
);
68-
6967
return Promise.resolve(false);
7068
}
7169

src/editors/editDocumentCodeLensProvider.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { DocumentSource } from '../documentSource';
77
import type { EditDocumentInfo } from '../types/editDocumentInfoType';
88
import EXTENSION_COMMANDS from '../commands';
99
import { PLAYGROUND_RESULT_URI } from './playgroundResultProvider';
10-
import type { PlaygroundResult } from '../types/playgroundType';
10+
import type { PlaygroundRunResult } from '../types/playgroundType';
1111

1212
export default class EditDocumentCodeLensProvider
1313
implements vscode.CodeLensProvider
@@ -31,7 +31,7 @@ export default class EditDocumentCodeLensProvider
3131

3232
updateCodeLensesForCollection(data: {
3333
content: Document;
34-
namespace: string | null;
34+
namespace?: string;
3535
uri: vscode.Uri;
3636
}): void {
3737
let resultCodeLensesInfo: EditDocumentInfo[] = [];
@@ -44,7 +44,7 @@ export default class EditDocumentCodeLensProvider
4444
this._codeLensesInfo[data.uri.toString()] = resultCodeLensesInfo;
4545
}
4646

47-
updateCodeLensesForPlayground(playgroundResult: PlaygroundResult): void {
47+
updateCodeLensesForPlayground(playgroundResult: PlaygroundRunResult): void {
4848
const source = DocumentSource.DOCUMENT_SOURCE_PLAYGROUND;
4949
let resultCodeLensesInfo: EditDocumentInfo[] = [];
5050

@@ -71,7 +71,7 @@ export default class EditDocumentCodeLensProvider
7171

7272
_updateCodeLensesForCursor(data: {
7373
content: any;
74-
namespace: string | null;
74+
namespace?: string;
7575
source: DocumentSource;
7676
}): EditDocumentInfo[] {
7777
const resultCodeLensesInfo: EditDocumentInfo[] = [];
@@ -109,7 +109,7 @@ export default class EditDocumentCodeLensProvider
109109

110110
_updateCodeLensesForDocument(data: {
111111
content: any;
112-
namespace: string | null;
112+
namespace?: string;
113113
source: DocumentSource;
114114
}): EditDocumentInfo[] {
115115
const { content, namespace, source } = data;

src/editors/editorsController.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Document } from 'bson';
44

55
import type ActiveConnectionCodeLensProvider from './activeConnectionCodeLensProvider';
66
import type ExportToLanguageCodeLensProvider from './exportToLanguageCodeLensProvider';
7-
import PlaygroundSelectedCodeActionProvider from './playgroundSelectedCodeActionProvider';
7+
import PlaygroundSelectionCodeActionProvider from './playgroundSelectionCodeActionProvider';
88
import PlaygroundDiagnosticsCodeActionProvider from './playgroundDiagnosticsCodeActionProvider';
99
import type ConnectionController from '../connectionController';
1010
import CollectionDocumentsCodeLensProvider from './collectionDocumentsCodeLensProvider';
@@ -38,7 +38,7 @@ const log = createLogger('editors controller');
3838
export function getFileDisplayNameForDocument(
3939
documentId: any,
4040
namespace: string
41-
) {
41+
): string {
4242
let displayName = `${namespace}:${EJSON.stringify(documentId)}`;
4343

4444
// Encode special file uri characters to ensure VSCode handles
@@ -84,7 +84,7 @@ export function getViewCollectionDocumentsUri(
8484
* new editors and the data they need. It also manages active editors.
8585
*/
8686
export default class EditorsController {
87-
_playgroundSelectedCodeActionProvider: PlaygroundSelectedCodeActionProvider;
87+
_playgroundSelectionCodeActionProvider: PlaygroundSelectionCodeActionProvider;
8888
_playgroundDiagnosticsCodeActionProvider: PlaygroundDiagnosticsCodeActionProvider;
8989
_connectionController: ConnectionController;
9090
_playgroundController: PlaygroundController;
@@ -97,7 +97,7 @@ export default class EditorsController {
9797
_documentIdStore: DocumentIdStore;
9898
_mongoDBDocumentService: MongoDBDocumentService;
9999
_telemetryService: TelemetryService;
100-
_playgroundResultViewProvider: PlaygroundResultProvider;
100+
_playgroundResultProvider: PlaygroundResultProvider;
101101
_activeConnectionCodeLensProvider: ActiveConnectionCodeLensProvider;
102102
_exportToLanguageCodeLensProvider: ExportToLanguageCodeLensProvider;
103103
_editDocumentCodeLensProvider: EditDocumentCodeLensProvider;
@@ -109,10 +109,10 @@ export default class EditorsController {
109109
playgroundController,
110110
statusView,
111111
telemetryService,
112-
playgroundResultViewProvider,
112+
playgroundResultProvider,
113113
activeConnectionCodeLensProvider,
114114
exportToLanguageCodeLensProvider,
115-
playgroundSelectedCodeActionProvider,
115+
playgroundSelectionCodeActionProvider,
116116
playgroundDiagnosticsCodeActionProvider,
117117
editDocumentCodeLensProvider,
118118
}: {
@@ -121,10 +121,10 @@ export default class EditorsController {
121121
playgroundController: PlaygroundController;
122122
statusView: StatusView;
123123
telemetryService: TelemetryService;
124-
playgroundResultViewProvider: PlaygroundResultProvider;
124+
playgroundResultProvider: PlaygroundResultProvider;
125125
activeConnectionCodeLensProvider: ActiveConnectionCodeLensProvider;
126126
exportToLanguageCodeLensProvider: ExportToLanguageCodeLensProvider;
127-
playgroundSelectedCodeActionProvider: PlaygroundSelectedCodeActionProvider;
127+
playgroundSelectionCodeActionProvider: PlaygroundSelectionCodeActionProvider;
128128
playgroundDiagnosticsCodeActionProvider: PlaygroundDiagnosticsCodeActionProvider;
129129
editDocumentCodeLensProvider: EditDocumentCodeLensProvider;
130130
}) {
@@ -149,15 +149,15 @@ export default class EditorsController {
149149
statusView: new StatusView(context),
150150
editDocumentCodeLensProvider: this._editDocumentCodeLensProvider,
151151
});
152-
this._playgroundResultViewProvider = playgroundResultViewProvider;
152+
this._playgroundResultProvider = playgroundResultProvider;
153153
this._activeConnectionCodeLensProvider = activeConnectionCodeLensProvider;
154154
this._exportToLanguageCodeLensProvider = exportToLanguageCodeLensProvider;
155155
this._collectionDocumentsCodeLensProvider =
156156
new CollectionDocumentsCodeLensProvider(
157157
this._collectionDocumentsOperationsStore
158158
);
159-
this._playgroundSelectedCodeActionProvider =
160-
playgroundSelectedCodeActionProvider;
159+
this._playgroundSelectionCodeActionProvider =
160+
playgroundSelectionCodeActionProvider;
161161
this._playgroundDiagnosticsCodeActionProvider =
162162
playgroundDiagnosticsCodeActionProvider;
163163

@@ -218,15 +218,14 @@ export default class EditorsController {
218218
}
219219

220220
async saveMongoDBDocument(): Promise<boolean> {
221-
const activeEditor = vscode.window.activeTextEditor;
221+
const editor = vscode.window.activeTextEditor;
222222

223-
if (!activeEditor) {
223+
if (!editor) {
224224
await vscode.commands.executeCommand('workbench.action.files.save');
225-
226225
return false;
227226
}
228227

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

238237
if (
239-
activeEditor.document.uri.scheme !== 'VIEW_DOCUMENT_SCHEME' ||
238+
editor.document.uri.scheme !== 'VIEW_DOCUMENT_SCHEME' ||
240239
!namespace ||
241240
!connectionId ||
242241
// A valid documentId can be false.
243242
documentId === null ||
244243
documentId === undefined
245244
) {
246245
void vscode.window.showErrorMessage(
247-
`The current file can not be saved as a MongoDB document. Invalid URL: ${activeEditor.document.uri.toString()}`
246+
`The current file can not be saved as a MongoDB document. Invalid URL: ${editor.document.uri.toString()}`
248247
);
249248
return false;
250249
}
251250

252251
try {
253-
const newDocument = EJSON.parse(activeEditor.document.getText() || '');
252+
const newDocument = EJSON.parse(editor.document.getText() || '');
254253

255254
await this._mongoDBDocumentService.replaceDocument({
256255
namespace,
@@ -261,7 +260,7 @@ export default class EditorsController {
261260
});
262261

263262
// Save document changes to active editor.
264-
await activeEditor?.document.save();
263+
await editor?.document.save();
265264

266265
void vscode.window.showInformationMessage(
267266
`The document was saved successfully to '${namespace}'`
@@ -317,7 +316,6 @@ export default class EditorsController {
317316
.isCurrentlyFetchingMoreDocuments
318317
) {
319318
void vscode.window.showErrorMessage('Already fetching more documents...');
320-
321319
return Promise.resolve(false);
322320
}
323321

@@ -330,7 +328,6 @@ export default class EditorsController {
330328
void vscode.window.showErrorMessage(
331329
`Unable to view more documents: no longer connected to ${oldConnectionName}`
332330
);
333-
334331
return Promise.resolve(false);
335332
}
336333

@@ -399,7 +396,7 @@ export default class EditorsController {
399396
this._context.subscriptions.push(
400397
vscode.workspace.registerTextDocumentContentProvider(
401398
PLAYGROUND_RESULT_SCHEME,
402-
this._playgroundResultViewProvider
399+
this._playgroundResultProvider
403400
)
404401
);
405402
// REGISTER CODE LENSES PROVIDERS.
@@ -447,10 +444,10 @@ export default class EditorsController {
447444
this._context.subscriptions.push(
448445
vscode.languages.registerCodeActionsProvider(
449446
'javascript',
450-
this._playgroundSelectedCodeActionProvider,
447+
this._playgroundSelectionCodeActionProvider,
451448
{
452449
providedCodeActionKinds:
453-
PlaygroundSelectedCodeActionProvider.providedCodeActionKinds,
450+
PlaygroundSelectionCodeActionProvider.providedCodeActionKinds,
454451
}
455452
)
456453
);

0 commit comments

Comments
 (0)