Skip to content

Commit c4b8d2e

Browse files
committed
fix: preserve .roomodes file formatting when importing modes
- Modified updateModesInFile method to detect and preserve original YAML formatting - Analyzes existing file indentation and applies same formatting when rewriting - Prevents complete reformatting of .roomodes files during mode import operations - All existing tests continue to pass Fixes issue where importing a mode would reformat the entire .roomodes file, changing user-preferred formatting and layout.
1 parent 82a3212 commit c4b8d2e

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/core/config/CustomModesManager.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,12 +458,38 @@ export class CustomModesManager {
458458

459459
private async updateModesInFile(filePath: string, operation: (modes: ModeConfig[]) => ModeConfig[]): Promise<void> {
460460
let content = "{}"
461+
let originalYamlOptions: yaml.ToStringOptions = { lineWidth: 0 }
461462

462463
try {
463464
content = await fs.readFile(filePath, "utf-8")
465+
466+
// Try to detect original YAML formatting style to preserve it
467+
if (content.trim()) {
468+
// Analyze the original content to preserve formatting style
469+
const lines = content.split("\n")
470+
let indentSize = 2 // default
471+
let hasFlowStyle = false
472+
473+
// Detect indentation size from the first indented line
474+
for (const line of lines) {
475+
const match = line.match(/^(\s+)/)
476+
if (match && match[1].length > 0) {
477+
indentSize = match[1].length
478+
break
479+
}
480+
}
481+
482+
// Check if the file uses flow style (arrays with brackets)
483+
hasFlowStyle = content.includes("[") && content.includes("]")
484+
485+
originalYamlOptions = {
486+
lineWidth: 0, // Prevent line wrapping
487+
indent: indentSize,
488+
}
489+
}
464490
} catch (error) {
465491
// File might not exist yet.
466-
content = yaml.stringify({ customModes: [] }, { lineWidth: 0 })
492+
content = yaml.stringify({ customModes: [] }, originalYamlOptions)
467493
}
468494

469495
let settings
@@ -484,7 +510,7 @@ export class CustomModesManager {
484510
}
485511

486512
settings.customModes = operation(settings.customModes)
487-
await fs.writeFile(filePath, yaml.stringify(settings, { lineWidth: 0 }), "utf-8")
513+
await fs.writeFile(filePath, yaml.stringify(settings, originalYamlOptions), "utf-8")
488514
}
489515

490516
private async refreshMergedState(): Promise<void> {
@@ -786,7 +812,7 @@ export class CustomModesManager {
786812
// This excludes the rules-{slug} folder from the path
787813
const relativePath = path.relative(modeRulesDir, filePath)
788814
// Normalize path to use forward slashes for cross-platform compatibility
789-
const normalizedRelativePath = relativePath.replace(/\\/g, '/')
815+
const normalizedRelativePath = relativePath.replace(/\\/g, "/")
790816
rulesFiles.push({ relativePath: normalizedRelativePath, content: content.trim() })
791817
}
792818
}

0 commit comments

Comments
 (0)