Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/extension/view-dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
<script type="module" crossorigin src="/assets/index-5D6F7MIX.js"></script>
<script type="module" crossorigin src="/assets/index-fFeT2p_H.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-DaHakWf7.css">
</head>
<body>
Expand Down
7 changes: 5 additions & 2 deletions src/internal/adapters.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as vscode from "vscode";
import { CONFIG_SECTION } from "../pkg/config-constants";
import { ButtonConfig, RefreshButtonConfig } from "../pkg/types";
import { ensureIdsInArray, ButtonConfigWithOptionalId } from "./utils/ensure-id";

const DEFAULT_REFRESH_CONFIG: RefreshButtonConfig = {
color: "#00BCD4",
enabled: true,
Expand Down Expand Up @@ -28,7 +30,7 @@ export type StatusBarCreator = (

export type QuickPickCreator = <T extends vscode.QuickPickItem>() => vscode.QuickPick<T>;

const getButtonsFromConfig = (config: vscode.WorkspaceConfiguration): ButtonConfig[] =>
const getButtonsFromConfig = (config: vscode.WorkspaceConfiguration): ButtonConfigWithOptionalId[] =>
config.get("buttons") || [];

const getRefreshConfigFromConfig = (config: vscode.WorkspaceConfiguration): RefreshButtonConfig =>
Expand All @@ -40,7 +42,8 @@ const isQuickCommandButtonsConfigChange = (event: vscode.ConfigurationChangeEven
export const createVSCodeConfigReader = (): ConfigReader => ({
getButtons: () => {
const config = vscode.workspace.getConfiguration(CONFIG_SECTION);
return getButtonsFromConfig(config);
const buttons = getButtonsFromConfig(config);
return ensureIdsInArray(buttons);
Comment thread
kubrickcode marked this conversation as resolved.
},
getRefreshConfig: () => {
const config = vscode.workspace.getConfiguration(CONFIG_SECTION);
Expand Down
20 changes: 20 additions & 0 deletions src/internal/utils/ensure-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { randomUUID } from "crypto";
Comment thread
kubrickcode marked this conversation as resolved.
import { ButtonConfig } from "../../pkg/types";

export type ButtonConfigWithOptionalId = Omit<ButtonConfig, "id" | "group"> & {
group?: ButtonConfigWithOptionalId[];
id?: string;
};

export const ensureId = (config: ButtonConfigWithOptionalId): ButtonConfig => {
const { group, id, ...restConfig } = config;

return {
...restConfig,
id: id ?? randomUUID(),
...(group !== undefined && { group: group.map(ensureId) }),
};
Comment thread
kubrickcode marked this conversation as resolved.
};

export const ensureIdsInArray = (configs: ButtonConfigWithOptionalId[]): ButtonConfig[] =>
configs.map((config) => ensureId(config));
1 change: 1 addition & 0 deletions src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export type ButtonConfig = {
command?: string;
executeAll?: boolean;
group?: ButtonConfig[];
id: string;
name: string;
shortcut?: string;
terminalName?: string;
Expand Down
56 changes: 54 additions & 2 deletions src/tests/adapters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("adapters", () => {
});

describe("getButtonsFromConfig", () => {
it("should return buttons from config", () => {
it("should return buttons from config with IDs added", () => {
const mockButtons = [
{ command: "test command", name: "Test Button" },
{ command: "another command", name: "Another Button" },
Expand All @@ -22,11 +22,63 @@ describe("adapters", () => {
const configReader = createVSCodeConfigReader();
const result = configReader.getButtons();

expect(result).toEqual(mockButtons);
expect(result).toHaveLength(2);
expect(result[0].name).toBe("Test Button");
expect(result[1].name).toBe("Another Button");
expect(result[0].id).toBeDefined();
expect(result[1].id).toBeDefined();
expect(result[0].id).not.toBe(result[1].id);
expect(vscode.workspace.getConfiguration).toHaveBeenCalledWith("quickCommandButtons");
expect(mockConfig.get).toHaveBeenCalledWith("buttons");
});

it("should preserve existing IDs in buttons", () => {
const existingId1 = "existing-id-1";
const existingId2 = "existing-id-2";
const mockButtons = [
{ command: "test command", name: "Test Button", id: existingId1 },
{ command: "another command", name: "Another Button", id: existingId2 },
];

const mockConfig = {
get: jest.fn((key: string) => (key === "buttons" ? mockButtons : undefined)),
};

(vscode.workspace.getConfiguration as jest.Mock).mockReturnValue(mockConfig);

const configReader = createVSCodeConfigReader();
const result = configReader.getButtons();

expect(result[0].id).toBe(existingId1);
expect(result[1].id).toBe(existingId2);
});

it("should add IDs to nested group items", () => {
const mockButtons = [
{
name: "Parent Group",
group: [
{ command: "child 1", name: "Child 1" },
{ command: "child 2", name: "Child 2" },
],
},
];

const mockConfig = {
get: jest.fn((key: string) => (key === "buttons" ? mockButtons : undefined)),
};

(vscode.workspace.getConfiguration as jest.Mock).mockReturnValue(mockConfig);

const configReader = createVSCodeConfigReader();
const result = configReader.getButtons();

expect(result[0].id).toBeDefined();
expect(result[0].group![0].id).toBeDefined();
expect(result[0].group![1].id).toBeDefined();
expect(result[0].group![0].id).not.toBe(result[0].group![1].id);
});

it("should return empty array when no buttons in config", () => {
const mockConfig = {
get: jest.fn(() => undefined),
Expand Down
Loading