Skip to content

Commit

Permalink
Sort cluster by name is the settings
Browse files Browse the repository at this point in the history
Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr authored and fbricon committed Jan 18, 2021
1 parent a0257d0 commit 22692a5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 2 additions & 9 deletions src/explorer/models/kafka.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -16,8 +16,7 @@ export class KafkaModel extends NodeBase implements Disposable {
}

public async computeChildren(): Promise<NodeBase[]> {
const clusters = this.clusterSettings.getAll()
.sort(this.sortByNameAscending);
const clusters = this.clusterSettings.getAll();
if (clusters.length === 0) {
return [new NoClusterItem(this)];
}
Expand All @@ -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 => (<ClusterItem>child).dispose());
}
Expand Down
15 changes: 11 additions & 4 deletions src/settings/clusters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface ClusterSettings {
onDidChangeSelected: vscode.Event<SelectedClusterChangedEvent>;

/**
* Gets the full cluster collection..
* Gets the full cluster collection sorted by cluster name.
*/
getAll(): Cluster[];

Expand Down Expand Up @@ -78,7 +78,14 @@ class MementoClusterSettings implements ClusterSettings {

getAll(): Cluster[] {
const state = this.storage.get<ClusterStoreType>(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 {
Expand Down Expand Up @@ -112,7 +119,7 @@ class MementoClusterSettings implements ClusterSettings {
}

private setSelectedClusterIfNeeded(): void {
if (this.selected !== undefined){
if (this.selected !== undefined) {
return;
}
const all = this.getAll();
Expand All @@ -122,4 +129,4 @@ class MementoClusterSettings implements ClusterSettings {
}
}

export const getClusterSettings = (): ClusterSettings => MementoClusterSettings.getInstance();
export const getClusterSettings = (): ClusterSettings => MementoClusterSettings.getInstance();

0 comments on commit 22692a5

Please sign in to comment.