Skip to content
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@
"title": "Create MongoDB Playground"
},
{
"command": "mdb.showActiveConnectionInPlayground",
"title": "MongoDB: Show Active Connection In Playground"
"command": "mdb.changeActiveConnection",
"title": "MongoDB: Change Active Connection"
},
{
"command": "mdb.runSelectedPlaygroundBlocks",
Expand Down Expand Up @@ -465,7 +465,7 @@
"when": "false"
},
{
"command": "mdb.showActiveConnectionInPlayground",
"command": "mdb.changeActiveConnection",
"when": "false"
},
{
Expand Down
78 changes: 71 additions & 7 deletions src/connectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export type SavedConnectionInformation = {
// A loaded connection contains connection information.
export type LoadedConnection = SavedConnection & SavedConnectionInformation;

export enum NewConnectionType {
NEW_CONNECTION = 'NEW_CONNECTION',
SAVED_CONNECTION = 'SAVED_CONNECTION'
}

export default class ConnectionController {
// This is a map of connection ids to their configurations.
// These connections can be saved on the session (runtime),
Expand Down Expand Up @@ -586,13 +591,13 @@ export default class ConnectionController {
const connectionNameToRemove:
| string
| undefined = await vscode.window.showQuickPick(
connectionIds.map(
(id, index) => `${index + 1}: ${this._connections[id].name}`
),
{
placeHolder: 'Choose a connection to remove...'
}
);
connectionIds.map(
(id, index) => `${index + 1}: ${this._connections[id].name}`
),
{
placeHolder: 'Choose a connection to remove...'
}
);

if (!connectionNameToRemove) {
return Promise.resolve(false);
Expand Down Expand Up @@ -777,4 +782,63 @@ export default class ConnectionController {
public setDisconnecting(disconnecting: boolean): void {
this._disconnecting = disconnecting;
}

public getСonnectionQuickPicks(): any[] {
if (!this._connections) {
return [
{
label: 'Add new connection',
data: {
type: NewConnectionType.NEW_CONNECTION
}
}
];
}

return [
{
label: 'Add new connection',
data: {
type: NewConnectionType.NEW_CONNECTION
}
},
...Object.values(this._connections)
.sort((connectionA: any, connectionB: any) =>
(connectionA.name || '').localeCompare(connectionB.name || '')
)
.map((item: any) => ({
label: item.name,
data: {
type: NewConnectionType.SAVED_CONNECTION,
connectionId: item.id
}
}))
];
}

public changeActiveConnection(): Promise<boolean> {
return new Promise(async (resolve) => {
const selectedQuickPickItem = await vscode.window.showQuickPick(
this.getСonnectionQuickPicks(),
{
placeHolder: 'Select new connection...'
}
);

if (!selectedQuickPickItem) {
return resolve(true);
}

if (
selectedQuickPickItem.data.type === NewConnectionType.NEW_CONNECTION
) {
return this.connectWithURI();
}

// Get the saved connection by id and return as the current connection.
return this.connectWithConnectionId(
selectedQuickPickItem.data.connectionId
);
});
}
}
32 changes: 14 additions & 18 deletions src/editors/activeConnectionCodeLensProvider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode';

export default class ActiveConnectionCodeLensProvider implements vscode.CodeLensProvider {
private _codeLenses: vscode.CodeLens[] = [];
export default class ActiveConnectionCodeLensProvider
implements vscode.CodeLensProvider {
private _connectionController: any;
private _onDidChangeCodeLenses: vscode.EventEmitter<
void
Expand All @@ -22,27 +22,23 @@ export default class ActiveConnectionCodeLensProvider implements vscode.CodeLens
}

public provideCodeLenses(): vscode.CodeLens[] {
const activeConnection = this._connectionController.getActiveDataService();

if (!activeConnection) {
return [];
const codeLens = new vscode.CodeLens(new vscode.Range(0, 0, 0, 0));
let message = '';

if (this._connectionController.isConnecting()) {
message = 'Connecting...';
} else if (this._connectionController.getActiveDataService()) {
message = `Currently connected to ${this._connectionController.getActiveConnectionName()}. Click here to change connection.`;
} else {
message = 'Disconnected. Click here to connect.';
}

this._codeLenses = [new vscode.CodeLens(new vscode.Range(0, 0, 0, 0))];

return this._codeLenses;
}

public resolveCodeLens?(codeLens: vscode.CodeLens): vscode.CodeLens {
const name = this._connectionController.getActiveConnectionName();
const message = `Currently connected to ${name}`;

codeLens.command = {
title: message,
command: "mdb.showActiveConnectionInPlayground",
arguments: [message]
command: 'mdb.changeActiveConnection',
arguments: []
};

return codeLens;
return [codeLens];
Copy link
Member

@Anemy Anemy Jul 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool! I didn't know we could skip the resolve code lens

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me too! But I wanted to have all data preparation in provideCodeLenses for testing purposes and it worked! :)

}
}
10 changes: 0 additions & 10 deletions src/editors/playgroundController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,6 @@ export default class PlaygroundController {
});
}

public showActiveConnectionInPlayground(message: string): Promise<boolean> {
return new Promise((resolve) => {
this._outputChannel.clear();
this._outputChannel.append(message);
this._outputChannel.show(true);

resolve(true);
});
}

public async evaluate(codeToEvaluate: string): Promise<any> {
// Send a request to the language server to execute scripts from a playground.
const result = await this._languageServerController.executeAll(
Expand Down
12 changes: 3 additions & 9 deletions src/explorer/databaseTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,9 @@ export default class DatabaseTreeItem extends vscode.TreeItem
// Create new collection tree items, using previously cached items
// where possible.
collections
.sort((collectionA: any, collectionB: any) => {
if (collectionA.name < collectionB.name) {
return -1;
}
if (collectionA.name > collectionB.name) {
return 1;
}
return 0;
})
.sort((collectionA: any, collectionB: any) =>
(collectionA.name || '').localeCompare(collectionB.name || '')
)
.forEach((collection: any) => {
if (pastChildrenCache[collection.name]) {
this._childrenCache[collection.name] = new CollectionTreeItem(
Expand Down
15 changes: 6 additions & 9 deletions src/mdbExtensionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,8 @@ export default class MDBExtensionController implements vscode.Disposable {
this.registerCommand('mdb.runPlayground', () =>
this._playgroundController.runAllOrSelectedPlaygroundBlocks()
);
this.registerCommand(
'mdb.showActiveConnectionInPlayground',
(message: string) =>
this._playgroundController.showActiveConnectionInPlayground(message)
this.registerCommand('mdb.changeActiveConnection', () =>
this._connectionController.changeActiveConnection()
);

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

const successfullyAddedCollection = await element
.onAddCollectionClicked(this._context);
const successfullyAddedCollection = await element.onAddCollectionClicked(
this._context
);
if (successfullyAddedCollection) {
vscode.window.showInformationMessage(
'Collection successfully created.'
Expand Down Expand Up @@ -398,9 +397,7 @@ export default class MDBExtensionController implements vscode.Disposable {
this.registerCommand(
'mdb.copySchemaFieldName',
async (fieldTreeItem: FieldTreeItem): Promise<boolean> => {
await vscode.env.clipboard.writeText(
fieldTreeItem.getFieldName()
);
await vscode.env.clipboard.writeText(fieldTreeItem.getFieldName());
vscode.window.showInformationMessage('Copied to clipboard.');

return true;
Expand Down
83 changes: 83 additions & 0 deletions src/test/suite/connectionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -826,4 +826,87 @@ suite('Connection Controller Test Suite', function () {
assert(false);
}
});

test('СonnectionQuickPicks list is displayed in the alphanumerical case insensitive order', async () => {
try {
await vscode.workspace
.getConfiguration('mdb.connectionSaving')
.update(
'defaultConnectionSavingLocation',
DefaultSavingLocations.Workspace
);
await testConnectionController.addNewConnectionStringAndConnect(
TEST_DATABASE_URI
);
await testConnectionController.addNewConnectionStringAndConnect(
TEST_DATABASE_URI
);
await testConnectionController.disconnect();

testConnectionController.clearAllConnections();

await testConnectionController.loadSavedConnections();

let connections = testConnectionController._connections;
let connectionIds = Object.keys(connections);

assert(
connectionIds.length === 2,
`Expected 2 connection configurations found ${connectionIds.length}`
);
assert(
connections[connectionIds[0]].name === 'localhost:27018',
`Expected the first connection name to be 'localhost:27018', found '${
connections[connectionIds[0]].name
}'.`
);
assert(
connections[connectionIds[1]].name === 'localhost:27018',
`Expected the second connection name to be 'localhost:27018', found '${
connections[connectionIds[1]].name
}'.`
);

const mockInputBoxResolves = sinon.stub();

mockInputBoxResolves.onCall(0).resolves('Lynx');
sinon.replace(vscode.window, 'showInputBox', mockInputBoxResolves);

const renameSuccess = await testConnectionController.renameConnection(
connectionIds[0]
);

assert(renameSuccess);

await testConnectionController.loadSavedConnections();

connections = testConnectionController._connections;

assert(
connectionIds.length === 2,
`Expected 2 connection configurations found ${connectionIds.length}`
);

const connectionQuickPicks = testConnectionController.getСonnectionQuickPicks();

assert(
connectionQuickPicks.length === 3,
`Expected 3 connections found ${connectionIds.length} in connectionQuickPicks`
);
assert(
connectionQuickPicks[0].label === 'Add new connection',
`Expected the first quick pick label to be 'Add new connection', found '${connectionQuickPicks[0].name}'.`
);
assert(
connectionQuickPicks[1].label === 'localhost:27018',
`Expected the second quick pick label to be 'localhost:27018', found '${connectionQuickPicks[1].name}'.`
);
assert(
connectionQuickPicks[2].label === 'Lynx',
`Expected the third quick pick labele to be 'Lynx', found '${connectionQuickPicks[2].name}'.`
);
} catch (error) {
assert(false);
}
});
});
Loading