From 94fd46d2dd44879a64494a2393f78686b535f7b9 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 --- CHANGELOG.md | 1 + src/explorer/models/kafka.ts | 11 ++--------- src/settings/clusters.ts | 15 +++++++++++---- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ff112f..acac9b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ All notable changes to Kafka extension will be documented in this file. - Newly created topic or cluster is automatically selected in the Kafka Explorer. See [#61](https://github.com/jlandersen/vscode-kafka/issues/61). - Internal topics are now hidden by default. See [#29](https://github.com/jlandersen/vscode-kafka/issues/29) and [#74](https://github.com/jlandersen/vscode-kafka/pull/74). - Elements are now sorted alphabetically in the Kafka explorer. See [#63](https://github.com/jlandersen/vscode-kafka/issues/63). +- Clusters are now sorted in the cluster selection wizard. See [#83](https://github.com/jlandersen/vscode-kafka/issues/83). ## [0.10.0] - 2021-01-02 ### Added 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();