Skip to content

Commit 8a404ed

Browse files
VSCODE-97: Connect with code lenses (#154)
* feat: connect with code lenses * refactor: move the change connection method to the connection controller * feat: СonnectionQuickPicks list in the alphanumerical case insensitive order * test: disconnect to free resources
1 parent 970399c commit 8a404ed

11 files changed

+249
-94
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@
173173
"title": "Create MongoDB Playground"
174174
},
175175
{
176-
"command": "mdb.showActiveConnectionInPlayground",
177-
"title": "MongoDB: Show Active Connection In Playground"
176+
"command": "mdb.changeActiveConnection",
177+
"title": "MongoDB: Change Active Connection"
178178
},
179179
{
180180
"command": "mdb.runSelectedPlaygroundBlocks",
@@ -465,7 +465,7 @@
465465
"when": "false"
466466
},
467467
{
468-
"command": "mdb.showActiveConnectionInPlayground",
468+
"command": "mdb.changeActiveConnection",
469469
"when": "false"
470470
},
471471
{

src/connectionController.ts

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ export type SavedConnectionInformation = {
3737
// A loaded connection contains connection information.
3838
export type LoadedConnection = SavedConnection & SavedConnectionInformation;
3939

40+
export enum NewConnectionType {
41+
NEW_CONNECTION = 'NEW_CONNECTION',
42+
SAVED_CONNECTION = 'SAVED_CONNECTION'
43+
}
44+
4045
export default class ConnectionController {
4146
// This is a map of connection ids to their configurations.
4247
// These connections can be saved on the session (runtime),
@@ -586,13 +591,13 @@ export default class ConnectionController {
586591
const connectionNameToRemove:
587592
| string
588593
| undefined = await vscode.window.showQuickPick(
589-
connectionIds.map(
590-
(id, index) => `${index + 1}: ${this._connections[id].name}`
591-
),
592-
{
593-
placeHolder: 'Choose a connection to remove...'
594-
}
595-
);
594+
connectionIds.map(
595+
(id, index) => `${index + 1}: ${this._connections[id].name}`
596+
),
597+
{
598+
placeHolder: 'Choose a connection to remove...'
599+
}
600+
);
596601

597602
if (!connectionNameToRemove) {
598603
return Promise.resolve(false);
@@ -777,4 +782,63 @@ export default class ConnectionController {
777782
public setDisconnecting(disconnecting: boolean): void {
778783
this._disconnecting = disconnecting;
779784
}
785+
786+
public getСonnectionQuickPicks(): any[] {
787+
if (!this._connections) {
788+
return [
789+
{
790+
label: 'Add new connection',
791+
data: {
792+
type: NewConnectionType.NEW_CONNECTION
793+
}
794+
}
795+
];
796+
}
797+
798+
return [
799+
{
800+
label: 'Add new connection',
801+
data: {
802+
type: NewConnectionType.NEW_CONNECTION
803+
}
804+
},
805+
...Object.values(this._connections)
806+
.sort((connectionA: any, connectionB: any) =>
807+
(connectionA.name || '').localeCompare(connectionB.name || '')
808+
)
809+
.map((item: any) => ({
810+
label: item.name,
811+
data: {
812+
type: NewConnectionType.SAVED_CONNECTION,
813+
connectionId: item.id
814+
}
815+
}))
816+
];
817+
}
818+
819+
public changeActiveConnection(): Promise<boolean> {
820+
return new Promise(async (resolve) => {
821+
const selectedQuickPickItem = await vscode.window.showQuickPick(
822+
this.getСonnectionQuickPicks(),
823+
{
824+
placeHolder: 'Select new connection...'
825+
}
826+
);
827+
828+
if (!selectedQuickPickItem) {
829+
return resolve(true);
830+
}
831+
832+
if (
833+
selectedQuickPickItem.data.type === NewConnectionType.NEW_CONNECTION
834+
) {
835+
return this.connectWithURI();
836+
}
837+
838+
// Get the saved connection by id and return as the current connection.
839+
return this.connectWithConnectionId(
840+
selectedQuickPickItem.data.connectionId
841+
);
842+
});
843+
}
780844
}
Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode';
22

3-
export default class ActiveConnectionCodeLensProvider implements vscode.CodeLensProvider {
4-
private _codeLenses: vscode.CodeLens[] = [];
3+
export default class ActiveConnectionCodeLensProvider
4+
implements vscode.CodeLensProvider {
55
private _connectionController: any;
66
private _onDidChangeCodeLenses: vscode.EventEmitter<
77
void
@@ -22,27 +22,23 @@ export default class ActiveConnectionCodeLensProvider implements vscode.CodeLens
2222
}
2323

2424
public provideCodeLenses(): vscode.CodeLens[] {
25-
const activeConnection = this._connectionController.getActiveDataService();
26-
27-
if (!activeConnection) {
28-
return [];
25+
const codeLens = new vscode.CodeLens(new vscode.Range(0, 0, 0, 0));
26+
let message = '';
27+
28+
if (this._connectionController.isConnecting()) {
29+
message = 'Connecting...';
30+
} else if (this._connectionController.getActiveDataService()) {
31+
message = `Currently connected to ${this._connectionController.getActiveConnectionName()}. Click here to change connection.`;
32+
} else {
33+
message = 'Disconnected. Click here to connect.';
2934
}
3035

31-
this._codeLenses = [new vscode.CodeLens(new vscode.Range(0, 0, 0, 0))];
32-
33-
return this._codeLenses;
34-
}
35-
36-
public resolveCodeLens?(codeLens: vscode.CodeLens): vscode.CodeLens {
37-
const name = this._connectionController.getActiveConnectionName();
38-
const message = `Currently connected to ${name}`;
39-
4036
codeLens.command = {
4137
title: message,
42-
command: "mdb.showActiveConnectionInPlayground",
43-
arguments: [message]
38+
command: 'mdb.changeActiveConnection',
39+
arguments: []
4440
};
4541

46-
return codeLens;
42+
return [codeLens];
4743
}
4844
}

src/editors/playgroundController.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,6 @@ export default class PlaygroundController {
171171
});
172172
}
173173

174-
public showActiveConnectionInPlayground(message: string): Promise<boolean> {
175-
return new Promise((resolve) => {
176-
this._outputChannel.clear();
177-
this._outputChannel.append(message);
178-
this._outputChannel.show(true);
179-
180-
resolve(true);
181-
});
182-
}
183-
184174
public async evaluate(codeToEvaluate: string): Promise<any> {
185175
// Send a request to the language server to execute scripts from a playground.
186176
const result = await this._languageServerController.executeAll(

src/explorer/databaseTreeItem.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,9 @@ export default class DatabaseTreeItem extends vscode.TreeItem
106106
// Create new collection tree items, using previously cached items
107107
// where possible.
108108
collections
109-
.sort((collectionA: any, collectionB: any) => {
110-
if (collectionA.name < collectionB.name) {
111-
return -1;
112-
}
113-
if (collectionA.name > collectionB.name) {
114-
return 1;
115-
}
116-
return 0;
117-
})
109+
.sort((collectionA: any, collectionB: any) =>
110+
(collectionA.name || '').localeCompare(collectionB.name || '')
111+
)
118112
.forEach((collection: any) => {
119113
if (pastChildrenCache[collection.name]) {
120114
this._childrenCache[collection.name] = new CollectionTreeItem(

src/mdbExtensionController.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,8 @@ export default class MDBExtensionController implements vscode.Disposable {
126126
this.registerCommand('mdb.runPlayground', () =>
127127
this._playgroundController.runAllOrSelectedPlaygroundBlocks()
128128
);
129-
this.registerCommand(
130-
'mdb.showActiveConnectionInPlayground',
131-
(message: string) =>
132-
this._playgroundController.showActiveConnectionInPlayground(message)
129+
this.registerCommand('mdb.changeActiveConnection', () =>
130+
this._connectionController.changeActiveConnection()
133131
);
134132

135133
this.registerCommand('mdb.startStreamLanguageServerLogs', () =>
@@ -315,8 +313,9 @@ export default class MDBExtensionController implements vscode.Disposable {
315313
return false;
316314
}
317315

318-
const successfullyAddedCollection = await element
319-
.onAddCollectionClicked(this._context);
316+
const successfullyAddedCollection = await element.onAddCollectionClicked(
317+
this._context
318+
);
320319
if (successfullyAddedCollection) {
321320
vscode.window.showInformationMessage(
322321
'Collection successfully created.'
@@ -398,9 +397,7 @@ export default class MDBExtensionController implements vscode.Disposable {
398397
this.registerCommand(
399398
'mdb.copySchemaFieldName',
400399
async (fieldTreeItem: FieldTreeItem): Promise<boolean> => {
401-
await vscode.env.clipboard.writeText(
402-
fieldTreeItem.getFieldName()
403-
);
400+
await vscode.env.clipboard.writeText(fieldTreeItem.getFieldName());
404401
vscode.window.showInformationMessage('Copied to clipboard.');
405402

406403
return true;

src/test/suite/connectionController.test.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,4 +826,87 @@ suite('Connection Controller Test Suite', function () {
826826
assert(false);
827827
}
828828
});
829+
830+
test('СonnectionQuickPicks list is displayed in the alphanumerical case insensitive order', async () => {
831+
try {
832+
await vscode.workspace
833+
.getConfiguration('mdb.connectionSaving')
834+
.update(
835+
'defaultConnectionSavingLocation',
836+
DefaultSavingLocations.Workspace
837+
);
838+
await testConnectionController.addNewConnectionStringAndConnect(
839+
TEST_DATABASE_URI
840+
);
841+
await testConnectionController.addNewConnectionStringAndConnect(
842+
TEST_DATABASE_URI
843+
);
844+
await testConnectionController.disconnect();
845+
846+
testConnectionController.clearAllConnections();
847+
848+
await testConnectionController.loadSavedConnections();
849+
850+
let connections = testConnectionController._connections;
851+
let connectionIds = Object.keys(connections);
852+
853+
assert(
854+
connectionIds.length === 2,
855+
`Expected 2 connection configurations found ${connectionIds.length}`
856+
);
857+
assert(
858+
connections[connectionIds[0]].name === 'localhost:27018',
859+
`Expected the first connection name to be 'localhost:27018', found '${
860+
connections[connectionIds[0]].name
861+
}'.`
862+
);
863+
assert(
864+
connections[connectionIds[1]].name === 'localhost:27018',
865+
`Expected the second connection name to be 'localhost:27018', found '${
866+
connections[connectionIds[1]].name
867+
}'.`
868+
);
869+
870+
const mockInputBoxResolves = sinon.stub();
871+
872+
mockInputBoxResolves.onCall(0).resolves('Lynx');
873+
sinon.replace(vscode.window, 'showInputBox', mockInputBoxResolves);
874+
875+
const renameSuccess = await testConnectionController.renameConnection(
876+
connectionIds[0]
877+
);
878+
879+
assert(renameSuccess);
880+
881+
await testConnectionController.loadSavedConnections();
882+
883+
connections = testConnectionController._connections;
884+
885+
assert(
886+
connectionIds.length === 2,
887+
`Expected 2 connection configurations found ${connectionIds.length}`
888+
);
889+
890+
const connectionQuickPicks = testConnectionController.getСonnectionQuickPicks();
891+
892+
assert(
893+
connectionQuickPicks.length === 3,
894+
`Expected 3 connections found ${connectionIds.length} in connectionQuickPicks`
895+
);
896+
assert(
897+
connectionQuickPicks[0].label === 'Add new connection',
898+
`Expected the first quick pick label to be 'Add new connection', found '${connectionQuickPicks[0].name}'.`
899+
);
900+
assert(
901+
connectionQuickPicks[1].label === 'localhost:27018',
902+
`Expected the second quick pick label to be 'localhost:27018', found '${connectionQuickPicks[1].name}'.`
903+
);
904+
assert(
905+
connectionQuickPicks[2].label === 'Lynx',
906+
`Expected the third quick pick labele to be 'Lynx', found '${connectionQuickPicks[2].name}'.`
907+
);
908+
} catch (error) {
909+
assert(false);
910+
}
911+
});
829912
});

0 commit comments

Comments
 (0)