add sorting option for saved connections (#15229)

* add sort by name option for saved connections and groups
This commit is contained in:
Hai Cao
2021-04-28 11:41:09 -07:00
committed by GitHub
parent 13ab2bc487
commit 9bc1797e88
5 changed files with 128 additions and 6 deletions

View File

@@ -14,8 +14,14 @@ import * as nls from 'vs/nls';
import { ConfigurationTarget, IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { deepClone } from 'vs/base/common/objects';
const GROUPS_CONFIG_KEY = 'datasource.connectionGroups';
const CONNECTIONS_CONFIG_KEY = 'datasource.connections';
export const GROUPS_CONFIG_KEY = 'datasource.connectionGroups';
export const CONNECTIONS_CONFIG_KEY = 'datasource.connections';
export const CONNECTIONS_SORT_BY_CONFIG_KEY = 'datasource.connections.sortBy';
export const enum ConnectionsSortBy {
dateAdded = 'dateAdded',
displayName = 'displayName'
}
export interface ISaveGroupResult {
groups: IConnectionProfileGroup[];
@@ -49,6 +55,26 @@ export class ConnectionConfig {
}
allGroups = allGroups.concat(userValue);
}
const sortBy = this.configurationService.getValue<string>(CONNECTIONS_SORT_BY_CONFIG_KEY);
let sortFunc: (a: IConnectionProfileGroup, b: IConnectionProfileGroup) => number;
if (sortBy === ConnectionsSortBy.displayName) {
sortFunc = ((a, b) => {
if (a.name < b.name) {
return -1;
} else if (a.name > b.name) {
return 1;
} else {
return 0;
}
});
}
if (sortFunc) {
allGroups.sort(sortFunc);
}
return deepClone(allGroups).map(g => {
if (g.parentId === '' || !g.parentId) {
g.parentId = undefined;
@@ -214,6 +240,25 @@ export class ConnectionConfig {
return ConnectionProfile.createFromStoredProfile(p, this._capabilitiesService);
});
const sortBy = this.configurationService.getValue<string>(CONNECTIONS_SORT_BY_CONFIG_KEY);
let sortFunc: (a: ConnectionProfile, b: ConnectionProfile) => number;
if (sortBy === ConnectionsSortBy.displayName) {
sortFunc = ((a, b) => {
if (a.title < b.title) {
return -1;
} else if (a.title > b.title) {
return 1;
} else {
return 0;
}
});
}
if (sortFunc) {
connectionProfiles.sort(sortFunc);
}
return connectionProfiles;
}

View File

@@ -6,7 +6,7 @@
import * as assert from 'assert';
import * as azdata from 'azdata';
import { ProviderFeatures } from 'sql/platform/capabilities/common/capabilitiesService';
import { ConnectionConfig, ISaveGroupResult } from 'sql/platform/connection/common/connectionConfig';
import { ConnectionConfig, ISaveGroupResult, CONNECTIONS_SORT_BY_CONFIG_KEY, ConnectionsSortBy } from 'sql/platform/connection/common/connectionConfig';
import { ConnectionProfile } from 'sql/platform/connection/common/connectionProfile';
import { ConnectionProfileGroup, IConnectionProfileGroup } from 'sql/platform/connection/common/connectionProfileGroup';
import { IConnectionProfile, IConnectionProfileStore, ConnectionOptionSpecialType, ServiceOptionType } from 'sql/platform/connection/common/interfaces';
@@ -239,6 +239,33 @@ suite('ConnectionConfig', () => {
assert.ok(groupsAreEqual(allGroups, testGroups), 'the groups returned did not match expectation');
});
test('getAllGroups should return groups sorted alphabetically by display name given datasource.connections.sortBy is set to \'' + ConnectionsSortBy.displayName + '\'', () => {
let configurationService = new TestConfigurationService();
configurationService.updateValue('datasource.connectionGroups', deepClone(testGroups).slice(0, 3), ConfigurationTarget.USER);
configurationService.updateValue('datasource.connectionGroups', deepClone(testGroups).slice(2, testGroups.length), ConfigurationTarget.WORKSPACE);
configurationService.updateValue(CONNECTIONS_SORT_BY_CONFIG_KEY, ConnectionsSortBy.displayName, ConfigurationTarget.USER);
let config = new ConnectionConfig(configurationService, capabilitiesService.object);
let allGroups = config.getAllGroups();
assert.equal(allGroups.length, testGroups.length, 'did not meet the expected length');
assert.ok(groupsAreEqual(allGroups, testGroups), 'the groups returned did not match expectation');
assert.ok(allGroups.slice(1).every((item, i) => allGroups[i].name <= item.name), 'the groups are not sorted correctly');
});
test('getAllGroups should return groups sorted by date added given datasource.connections.sortBy is set to \'' + ConnectionsSortBy.dateAdded + '\'', () => {
let configurationService = new TestConfigurationService();
configurationService.updateValue('datasource.connectionGroups', deepClone(testGroups).slice(0, 3).reverse(), ConfigurationTarget.USER);
configurationService.updateValue('datasource.connectionGroups', deepClone(testGroups).slice(2, testGroups.length).reverse(), ConfigurationTarget.WORKSPACE);
configurationService.updateValue(CONNECTIONS_SORT_BY_CONFIG_KEY, ConnectionsSortBy.dateAdded, ConfigurationTarget.USER);
let config = new ConnectionConfig(configurationService, capabilitiesService.object);
let allGroups = config.getAllGroups();
let expectedGroups = deepClone(testGroups).slice(0, 3).reverse().concat(deepClone(testGroups).slice(3, testGroups.length).reverse());
assert.equal(allGroups.length, expectedGroups.length, 'The result groups length is invalid');
assert.ok(allGroups.every((item, i) => item.id === allGroups[i].id));
});
test('addConnection should add the new profile to user settings', async () => {
let newProfile: IConnectionProfile = {
serverName: 'new server',
@@ -384,6 +411,30 @@ suite('ConnectionConfig', () => {
});
});
test('getConnections should return connections sorted alphabetically by title given datasource.connections.sortBy is set to \'' + ConnectionsSortBy.displayName + '\'', () => {
let configurationService = new TestConfigurationService();
configurationService.updateValue('datasource.connections', deepClone(testConnections).slice(0, 2).reverse(), ConfigurationTarget.USER);
configurationService.updateValue('datasource.connections', deepClone(testConnections).slice(2, testConnections.length).reverse(), ConfigurationTarget.WORKSPACE);
configurationService.updateValue(CONNECTIONS_SORT_BY_CONFIG_KEY, ConnectionsSortBy.displayName, ConfigurationTarget.USER);
let config = new ConnectionConfig(configurationService, capabilitiesService.object);
let allConnections = config.getConnections(true);
assert.equal(allConnections.length, testConnections.length, 'The result connections length is invalid');
assert.ok(allConnections.slice(1).every((item, i) => allConnections[i].title <= item.title), 'The connections are not sorted correctly');
});
test('getConnections should return connections sorted by date added given datasource.connections.sortBy is set to \'' + ConnectionsSortBy.dateAdded + '\'', () => {
let configurationService = new TestConfigurationService();
configurationService.updateValue('datasource.connections', deepClone(testConnections).reverse(), ConfigurationTarget.USER);
configurationService.updateValue(CONNECTIONS_SORT_BY_CONFIG_KEY, ConnectionsSortBy.dateAdded, ConfigurationTarget.USER);
let config = new ConnectionConfig(configurationService, capabilitiesService.object);
let allConnections = config.getConnections(false);
let expectedConnections = deepClone(testConnections).reverse();
assert.equal(allConnections.length, expectedConnections.length, 'The result connections length is invalid');
assert.ok(allConnections.every((item, i) => item.id === expectedConnections[i].id));
});
test('saveGroup should save the new groups to tree and return the id of the last group name', () => {
let config = new ConnectionConfig(undefined!, undefined!);
let groups: IConnectionProfileGroup[] = deepClone(testGroups);