Skip to content

Commit 6bae578

Browse files
committed
Fix theme editor bugs: hex colors and comments order
- Display colors in #RRGGBB hex format instead of [r, g, b] - Add hex parsing support for color input - Move field descriptions before their fields (instead of after) - Update prompt labels and error messages for hex format
1 parent 749ae61 commit 6bae578

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

plugins/theme_editor.ts

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,34 @@ function parseColorToRgb(value: ColorValue): RGB | null {
337337
}
338338

339339
/**
340-
* Format a color value for display
340+
* Convert RGB to hex string
341+
*/
342+
function rgbToHex(r: number, g: number, b: number): string {
343+
const toHex = (n: number) => n.toString(16).padStart(2, '0').toUpperCase();
344+
return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
345+
}
346+
347+
/**
348+
* Parse hex string to RGB
349+
*/
350+
function hexToRgb(hex: string): RGB | null {
351+
const match = hex.match(/^#?([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/);
352+
if (match) {
353+
return [
354+
parseInt(match[1], 16),
355+
parseInt(match[2], 16),
356+
parseInt(match[3], 16),
357+
];
358+
}
359+
return null;
360+
}
361+
362+
/**
363+
* Format a color value for display (as hex)
341364
*/
342365
function formatColorValue(value: ColorValue): string {
343366
if (Array.isArray(value)) {
344-
return `[${value[0]}, ${value[1]}, ${value[2]}]`;
367+
return rgbToHex(value[0], value[1], value[2]);
345368
}
346369
return String(value);
347370
}
@@ -550,6 +573,12 @@ function buildDisplayEntries(): TextPropertyEntry[] {
550573
properties: { type: "description", path: field.path },
551574
});
552575
} else {
576+
// Field description (before the field)
577+
entries.push({
578+
text: `${indent} // ${field.def.description}\n`,
579+
properties: { type: "description", path: field.path },
580+
});
581+
553582
// Color field with swatch
554583
const colorStr = formatColorValue(field.value);
555584

@@ -562,12 +591,6 @@ function buildDisplayEntries(): TextPropertyEntry[] {
562591
colorValue: field.value,
563592
},
564593
});
565-
566-
// Field description
567-
entries.push({
568-
text: `${indent} // ${field.def.description}\n`,
569-
properties: { type: "description", path: field.path },
570-
});
571594
}
572595

573596
entries.push({
@@ -736,7 +759,7 @@ function getFieldAtCursor(): ThemeField | null {
736759
function editColorField(field: ThemeField): void {
737760
const currentValue = formatColorValue(field.value);
738761

739-
editor.startPrompt(`${field.def.displayName} (RGB [r,g,b] or named): `, `theme-color-${field.path}`);
762+
editor.startPrompt(`${field.def.displayName} (#RRGGBB or named): `, `theme-color-${field.path}`);
740763

741764
// Build suggestions with named colors and current value
742765
const suggestions: PromptSuggestion[] = [
@@ -771,6 +794,12 @@ function parseColorInput(input: string): ColorValue | null {
771794
return input;
772795
}
773796

797+
// Try to parse as hex color #RRGGBB
798+
const hexResult = hexToRgb(input);
799+
if (hexResult) {
800+
return hexResult;
801+
}
802+
774803
// Try to parse as RGB array [r, g, b]
775804
const rgbMatch = input.match(/^\[?\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\]?$/);
776805
if (rgbMatch) {
@@ -809,7 +838,7 @@ globalThis.onThemeColorPromptConfirmed = function(args: {
809838
updateDisplay();
810839
editor.setStatus(`Updated ${path}`);
811840
} else {
812-
editor.setStatus("Invalid color format. Use [r,g,b] or named color.");
841+
editor.setStatus("Invalid color format. Use #RRGGBB, [r,g,b], or named color.");
813842
}
814843

815844
return true;
@@ -990,7 +1019,9 @@ async function setThemeAsDefault(themeName: string): Promise<void> {
9901019
const content = JSON.stringify(config, null, 2);
9911020
await editor.writeFile(configPath, content);
9921021

993-
editor.setStatus(`Default theme set to "${themeName}". Restart editor to apply.`);
1022+
// Note: The plugin API doesn't currently support applying themes at runtime.
1023+
// Users need to restart or use the Select Theme command (Ctrl+P -> "Select Theme")
1024+
editor.setStatus(`Default theme set to "${themeName}". Use 'Select Theme' command or restart to apply.`);
9941025
} catch (e) {
9951026
editor.setStatus(`Failed to update config: ${e}`);
9961027
}

0 commit comments

Comments
 (0)