From 3de399e3776463044383df2fd26029d9108aa5cb Mon Sep 17 00:00:00 2001 From: angelozerr Date: Fri, 15 Jan 2021 10:00:27 +0100 Subject: [PATCH] Sort cluster by name is the settings Signed-off-by: azerr --- README.md | 2 +- src/explorer/models/kafka.ts | 11 ++--------- src/settings/clusters.ts | 15 +++++++++++---- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 8b69f27..7f45839 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Work with Kafka directly in Visual Studio Code. Kafka clusters running version 0.11 or higher are supported. Features: -- Connect to multiple clusters +- Connect to multiple clusters (clusters are displayed in explorer sorted by name) - View brokers in cluster - View topics - View configs diff --git a/src/explorer/models/kafka.ts b/src/explorer/models/kafka.ts index 4d6cc45..1bc2b2e 100644 --- a/src/explorer/models/kafka.ts +++ b/src/explorer/models/kafka.ts @@ -1,5 +1,5 @@ import { Disposable, TreeItemCollapsibleState } from "vscode"; -import { ClientAccessor, Cluster } from "../../client"; +import { ClientAccessor } from "../../client"; import { ClusterSettings } from "../../settings"; import { ClusterItem, NoClusterItem } from "./cluster"; import { NodeBase } from "./nodeBase"; @@ -16,8 +16,7 @@ export class KafkaModel extends NodeBase implements Disposable { } public async computeChildren(): Promise { - const clusters = this.clusterSettings.getAll() - .sort(this.sortByNameAscending); + const clusters = this.clusterSettings.getAll(); if (clusters.length === 0) { return [new NoClusterItem(this)]; } @@ -26,12 +25,6 @@ export class KafkaModel extends NodeBase implements Disposable { }); } - private sortByNameAscending(a: Cluster, b: Cluster): -1 | 0 | 1 { - if (a.name.toLowerCase() < b.name.toLowerCase()) { return -1; } - if (a.name.toLowerCase() > b.name.toLowerCase()) { return 1; } - return 0; - } - public dispose(): void { this.children?.forEach(child => (child).dispose()); } diff --git a/src/settings/clusters.ts b/src/settings/clusters.ts index 81a7165..b4e6375 100644 --- a/src/settings/clusters.ts +++ b/src/settings/clusters.ts @@ -21,7 +21,7 @@ export interface ClusterSettings { onDidChangeSelected: vscode.Event; /** - * Gets the full cluster collection.. + * Gets the full cluster collection sorted by cluster name. */ getAll(): Cluster[]; @@ -78,7 +78,14 @@ class MementoClusterSettings implements ClusterSettings { getAll(): Cluster[] { const state = this.storage.get(this.clusterCollectionStorageKey, {}); - return Object.values(state); + return Object.values(state) + .sort(this.sortByNameAscending); + } + + private sortByNameAscending(a: Cluster, b: Cluster): -1 | 0 | 1 { + if (a.name.toLowerCase() < b.name.toLowerCase()) { return -1; } + if (a.name.toLowerCase() > b.name.toLowerCase()) { return 1; } + return 0; } get(id: string): Cluster | undefined { @@ -112,7 +119,7 @@ class MementoClusterSettings implements ClusterSettings { } private setSelectedClusterIfNeeded(): void { - if (this.selected !== undefined){ + if (this.selected !== undefined) { return; } const all = this.getAll(); @@ -122,4 +129,4 @@ class MementoClusterSettings implements ClusterSettings { } } -export const getClusterSettings = (): ClusterSettings => MementoClusterSettings.getInstance(); \ No newline at end of file +export const getClusterSettings = (): ClusterSettings => MementoClusterSettings.getInstance();