Skip to content

Commit 7feb9f0

Browse files
VSCODE-225: Associate results with connection (#234)
* refactor: fix eslint errors * test: update tests to reflect refactoring * feat: associate playground results with the connection id which generated those results * refactor: remove unnecessary await * refactor: clean some code * refactor: use shorter versions of some code blocks * refactor: fix error messages text
1 parent 6e96bfa commit 7feb9f0

22 files changed

+696
-441
lines changed

src/connectionController.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -635,17 +635,18 @@ export default class ConnectionController {
635635
if (
636636
this._connections[connectionId].storageLocation === StorageScope.GLOBAL
637637
) {
638-
await this._storageController
639-
.saveConnectionToGlobalStore(this._connections[connectionId]);
638+
await this._storageController.saveConnectionToGlobalStore(
639+
this._connections[connectionId]
640+
);
640641
return true;
641642
}
642643

643644
if (
644-
this._connections[connectionId].storageLocation ===
645-
StorageScope.WORKSPACE
645+
this._connections[connectionId].storageLocation === StorageScope.WORKSPACE
646646
) {
647-
await this._storageController
648-
.saveConnectionToWorkspaceStore(this._connections[connectionId]);
647+
await this._storageController.saveConnectionToWorkspaceStore(
648+
this._connections[connectionId]
649+
);
649650
return true;
650651
}
651652

@@ -822,9 +823,7 @@ export default class ConnectionController {
822823
}
823824
},
824825
...Object.values(this._connections)
825-
.sort((connectionA: {
826-
name: string
827-
}, connectionB: any) =>
826+
.sort((connectionA: { name: string }, connectionB: any) =>
828827
(connectionA.name || '').localeCompare(connectionB.name || '')
829828
)
830829
.map((item: any) => ({
@@ -849,9 +848,7 @@ export default class ConnectionController {
849848
return true;
850849
}
851850

852-
if (
853-
selectedQuickPickItem.data.type === NewConnectionType.NEW_CONNECTION
854-
) {
851+
if (selectedQuickPickItem.data.type === NewConnectionType.NEW_CONNECTION) {
855852
return this.connectWithURI();
856853
}
857854

src/connectionModelType.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { EJSON } from 'bson';
12
import { Host } from './views/webview-app/connection-model/connection-model';
23
import SSH_TUNNEL_TYPES from './views/webview-app/connection-model/constants/ssh-tunnel-types';
34

45
type ConnectionAttributes = {
56
driverUrl: string;
67
driverUrlWithSsh: string;
7-
driverOptions: any;
8+
driverOptions: EJSON.SerializableTypes;
89
sshTunnelOptions: {
910
host?: string;
1011
port?: number;
@@ -20,6 +21,6 @@ export type ConnectionModelType = {
2021
driverUrl: string;
2122
driverUrlWithSsh: string;
2223
sshTunnel?: SSH_TUNNEL_TYPES;
23-
getAttributes(options?: object): ConnectionAttributes;
24+
getAttributes(options?: EJSON.SerializableTypes): ConnectionAttributes;
2425
disconnect(callback: (n: Error | undefined) => void): void;
2526
};

src/editors/editDocumentCodeLensProvider.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import * as vscode from 'vscode';
2-
import { EJSON } from 'bson';
32
import EXTENSION_COMMANDS from '../commands';
4-
import type { DocCodeLensesInfo } from '../utils/types';
5-
import type { OutputItem } from '../utils/types';
3+
import type { OutputItem, ResultCodeLensInfo } from '../utils/types';
4+
import ConnectionController from '../connectionController';
65

76
export default class EditDocumentCodeLensProvider
87
implements vscode.CodeLensProvider {
98
_onDidChangeCodeLenses: vscode.EventEmitter<void> = new vscode.EventEmitter<void>();
109
_codeLenses: vscode.CodeLens[] = [];
11-
_codeLensesInfo: DocCodeLensesInfo;
10+
_codeLensesInfo: ResultCodeLensInfo[];
11+
_connectionController: ConnectionController;
1212

1313
readonly onDidChangeCodeLenses: vscode.Event<void> = this
1414
._onDidChangeCodeLenses.event;
1515

16-
constructor() {
16+
constructor(connectionController: ConnectionController) {
17+
this._connectionController = connectionController;
1718
this._codeLensesInfo = [];
1819

1920
vscode.workspace.onDidChangeConfiguration(() => {
@@ -22,16 +23,15 @@ implements vscode.CodeLensProvider {
2223
}
2324

2425
updateCodeLensesPosition(playgroundResult: OutputItem): void {
25-
if (!playgroundResult) {
26+
if (!playgroundResult || !playgroundResult.content) {
2627
this._codeLensesInfo = [];
2728

2829
return;
2930
}
3031

31-
const content = playgroundResult.content;
32-
const namespace = playgroundResult.namespace;
33-
const type = playgroundResult.type;
34-
const codeLensesInfo: DocCodeLensesInfo = [];
32+
const { content, namespace, type } = playgroundResult;
33+
const connectionId = this._connectionController.getActiveConnectionId();
34+
const codeLensesInfo: ResultCodeLensInfo[] = [];
3535

3636
// Show code lenses only for the list of documents or a single document
3737
// that are returned by the find() method.
@@ -44,7 +44,12 @@ implements vscode.CodeLensProvider {
4444
// We need _id and namespace for code lenses
4545
// to be able to save the editable document.
4646
if (item !== null && item._id && namespace) {
47-
codeLensesInfo.push({ line, documentId: item._id, namespace });
47+
codeLensesInfo.push({
48+
line,
49+
documentId: item._id,
50+
namespace,
51+
connectionId
52+
});
4853
// To calculate the position of the next open curly bracket,
4954
// we stringify the object and use a regular expression
5055
// so we can count the number of lines.
@@ -54,7 +59,12 @@ implements vscode.CodeLensProvider {
5459
} else if (type === 'Document' && content._id && namespace) {
5560
// When the playground result is the single document,
5661
// show the single code lense after {.
57-
codeLensesInfo.push({ line: 1, documentId: content._id, namespace });
62+
codeLensesInfo.push({
63+
line: 1,
64+
documentId: content._id,
65+
namespace,
66+
connectionId
67+
});
5868
}
5969

6070
this._codeLensesInfo = codeLensesInfo;
@@ -71,16 +81,11 @@ implements vscode.CodeLensProvider {
7181
const command: {
7282
title: string;
7383
command: EXTENSION_COMMANDS;
74-
arguments: {
75-
documentId: EJSON.SerializableTypes;
76-
namespace: string;
77-
}[];
84+
arguments: ResultCodeLensInfo[];
7885
} = {
7986
title: 'Edit Document',
8087
command: EXTENSION_COMMANDS.MDB_OPEN_MONGODB_DOCUMENT_FROM_PLAYGROUND,
81-
arguments: [
82-
{ documentId: item.documentId, namespace: item.namespace }
83-
]
88+
arguments: [item]
8489
};
8590

8691
this._codeLenses.push(new vscode.CodeLens(range, command));

0 commit comments

Comments
 (0)