Skip to content

Commit

Permalink
fix #87 implement optional sorting in getServerNames
Browse files Browse the repository at this point in the history
  • Loading branch information
gjsjohnmurray committed Apr 30, 2021
1 parent 483cee0 commit 20d8593
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
21 changes: 15 additions & 6 deletions src/api/getServerNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import * as vscode from 'vscode';
import { ServerName } from '../extension';
import { serverDetail } from './getServerSummary';

export function getServerNames(scope?: vscode.ConfigurationScope): ServerName[] {
export function getServerNames(scope?: vscode.ConfigurationScope, sorted?: boolean): ServerName[] {
const allNames: ServerName[] = [];
let names: ServerName[] = [];
let defaultNames: ServerName[] = [];
const embeddedNames: ServerName[] = [];
const servers = vscode.workspace.getConfiguration('intersystems', scope).get('servers');

if (typeof servers === 'object' && servers) {
Expand All @@ -18,7 +19,7 @@ export function getServerNames(scope?: vscode.ConfigurationScope): ServerName[]
const inspectedDefault = vscode.workspace.getConfiguration('intersystems.servers', scope).inspect('/default');
const myDefault: string = notSet(inspectedDefault) ? '' : servers['/default'] || '';
if (myDefault.length > 0 && servers[myDefault]) {
names.push({
allNames.push({
name: myDefault,
description: `${servers[myDefault].description || ''} (default)`.trim(),
detail: serverDetail(servers[myDefault])
Expand All @@ -32,7 +33,7 @@ export function getServerNames(scope?: vscode.ConfigurationScope): ServerName[]

// Collect embedded (default~*) servers separately
if (notSet(inspected)) {
defaultNames.push({
embeddedNames.push({
name: key,
description: servers[key].description || '',
detail: serverDetail(servers[key])
Expand All @@ -48,9 +49,17 @@ export function getServerNames(scope?: vscode.ConfigurationScope): ServerName[]
}
}

// If requested, sort what we found
if (sorted) {
names = names.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
}

// Append them
allNames.push(...names);

// Append the embedded servers unless suppressed
if (!vscode.workspace.getConfiguration('intersystems.servers', scope).get('/hideEmbeddedEntries')) {
names.push(...defaultNames);
allNames.push(...embeddedNames);
}
return names;
return allNames;
}
5 changes: 3 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,9 @@ export function activate(context: vscode.ExtensionContext) {
async pickServer(scope?: vscode.ConfigurationScope, options: vscode.QuickPickOptions = {}): Promise<string | undefined> {
return await pickServer(scope, options);
},
getServerNames(scope?: vscode.ConfigurationScope): ServerName[] {
return getServerNames(scope);

getServerNames(scope?: vscode.ConfigurationScope, sorted?: boolean): ServerName[] {
return getServerNames(scope, sorted);
},

getServerSummary(name: string, scope?: vscode.ConfigurationScope): ServerName | undefined {
Expand Down
5 changes: 1 addition & 4 deletions src/ui/serverManagerView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,7 @@ export class SMTreeItem extends vscode.TreeItem {
function allServers(treeItem: SMTreeItem, params?: any): ServerTreeItem[] {
const children: ServerTreeItem[] = [];
const getAllServers = (sorted?: boolean): ServerTreeItem[] => {
let serverNames = getServerNames();
if (sorted) {
serverNames = serverNames.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
}
let serverNames = getServerNames(undefined, sorted);
return serverNames.map((serverName) => {
return new ServerTreeItem({ label: serverName.name, id:serverName.name, parent: treeItem }, serverName);
})
Expand Down

0 comments on commit 20d8593

Please sign in to comment.