History

Merge pull request #118 from router-for-me/config

feat(config): use block style for YAML maps/lists

Chén Mù committed 1 year agov6.2.7

Commit 9072acc2 parents 4fdbdf7 0d8134a1 file · +28 −0Browse files

Unified · Split

internal/config/config.go+28−0
@@ -326,6 +326,7 @@ func SaveConfigPreserveComments(configFile string, cfg *Config) error { · 325 unmodified lines
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) { · 234 unmodified lines
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+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t