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 all 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.

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 @@ -2,7 +2,7 @@

import type ConnectionController from '../connectionController';

const launchMongoDBShellWithEnv = ({

Check warning on line 5 in src/commands/launchMongoShell.ts

View workflow job for this annotation

GitHub Actions / Test and Build (ubuntu-latest)

Missing return type on function

Check warning on line 5 in src/commands/launchMongoShell.ts

View workflow job for this annotation

GitHub Actions / Test and Build (macos-latest)

Missing return type on function
shellCommand,
mdbConnectionString,
envVariableString,
Expand All @@ -29,19 +29,19 @@
mongoDBShell.show();
};

const getPowershellEnvString = () => {

Check warning on line 32 in src/commands/launchMongoShell.ts

View workflow job for this annotation

GitHub Actions / Test and Build (ubuntu-latest)

Missing return type on function

Check warning on line 32 in src/commands/launchMongoShell.ts

View workflow job for this annotation

GitHub Actions / Test and Build (macos-latest)

Missing return type on function
return '$Env:MDB_CONNECTION_STRING';
};

const getCmdEnvString = () => {

Check warning on line 36 in src/commands/launchMongoShell.ts

View workflow job for this annotation

GitHub Actions / Test and Build (ubuntu-latest)

Missing return type on function

Check warning on line 36 in src/commands/launchMongoShell.ts

View workflow job for this annotation

GitHub Actions / Test and Build (macos-latest)

Missing return type on function
return '%MDB_CONNECTION_STRING%';
};

const getGitBashEnvString = () => {

Check warning on line 40 in src/commands/launchMongoShell.ts

View workflow job for this annotation

GitHub Actions / Test and Build (ubuntu-latest)

Missing return type on function

Check warning on line 40 in src/commands/launchMongoShell.ts

View workflow job for this annotation

GitHub Actions / Test and Build (macos-latest)

Missing return type on function
return '$MDB_CONNECTION_STRING';
};

const getBashEnvString = () => {

Check warning on line 44 in src/commands/launchMongoShell.ts

View workflow job for this annotation

GitHub Actions / Test and Build (ubuntu-latest)

Missing return type on function

Check warning on line 44 in src/commands/launchMongoShell.ts

View workflow job for this annotation

GitHub Actions / Test and Build (macos-latest)

Missing return type on function
return '$MDB_CONNECTION_STRING';
};

Expand All @@ -52,7 +52,6 @@
void vscode.window.showErrorMessage(
'You need to be connected before launching the MongoDB Shell.'
);

return Promise.resolve(false);
}

Expand All @@ -63,9 +62,8 @@

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
Loading