File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -326,6 +326,7 @@ func SaveConfigPreserveComments(configFile string, cfg *Config) error {
326326
327327 // Merge generated into original in-place, preserving comments/order of existing nodes.
328328 mergeMappingPreserve (original .Content [0 ], generated .Content [0 ])
329+ normalizeCollectionNodeStyles (original .Content [0 ])
329330
330331 // Write back.
331332 f , err := os .Create (configFile )
@@ -566,3 +567,30 @@ func removeMapKey(mapNode *yaml.Node, key string) {
566567 }
567568 }
568569}
570+
571+ // normalizeCollectionNodeStyles forces YAML collections to use block notation, keeping
572+ // lists and maps readable. Empty sequences retain flow style ([]) so empty list markers
573+ // remain compact.
574+ func normalizeCollectionNodeStyles (node * yaml.Node ) {
575+ if node == nil {
576+ return
577+ }
578+ switch node .Kind {
579+ case yaml .MappingNode :
580+ node .Style = 0
581+ for i := range node .Content {
582+ normalizeCollectionNodeStyles (node .Content [i ])
583+ }
584+ case yaml .SequenceNode :
585+ if len (node .Content ) == 0 {
586+ node .Style = yaml .FlowStyle
587+ } else {
588+ node .Style = 0
589+ }
590+ for i := range node .Content {
591+ normalizeCollectionNodeStyles (node .Content [i ])
592+ }
593+ default :
594+ // Scalars keep their existing style to preserve quoting
595+ }
596+ }
You can’t perform that action at this time.
0 commit comments