Jelajahi Sumber

Emit pound prefix ("# ") in comments if missing.

Gustavo Niemeyer 7 tahun lalu
induk
melakukan
82dd14953a
2 mengubah file dengan 44 tambahan dan 5 penghapusan
  1. 9 5
      emitterc.go
  2. 35 0
      node_test.go

+ 9 - 5
emitterc.go

@@ -1900,21 +1900,25 @@ func yaml_emitter_write_folded_scalar(emitter *yaml_emitter_t, value []byte) boo
 func yaml_emitter_write_comment(emitter *yaml_emitter_t, comment []byte) bool {
 	// [Go] TODO Emit "# " when necessary.
 	breaks := false
+	pound := false
 	for i := 0; i < len(comment); {
-		if is_space(comment, i) {
-			if !write(emitter, comment, &i) {
-				return false
-			}
-		} else if is_break(comment, i) {
+		if is_break(comment, i) {
 			if !write_break(emitter, comment, &i) {
 				return false
 			}
 			//emitter.indention = true
 			breaks = true
+			pound = false
 		} else {
 			if breaks && !yaml_emitter_write_indent(emitter) {
 				return false
 			}
+			if !pound {
+				if comment[i] != '#' && (!put(emitter, '#') || !put(emitter, ' ')) {
+					return false
+				}
+				pound = true
+			}
 			if !write(emitter, comment, &i) {
 				return false
 			}

+ 35 - 0
node_test.go

@@ -626,6 +626,41 @@ var nodeTests = []struct {
 				FootComment: "# Four\n# Five",
 			}},
 		},
+	}, {
+		// Write out the pound character if missing from comments.
+		"[encode]# One\n# Two\ntrue # Three\n# Four\n# Five\n",
+		yaml.Node{
+			Kind:   yaml.DocumentNode,
+			Line:   3,
+			Column: 1,
+			Content: []*yaml.Node{{
+				Kind:        yaml.ScalarNode,
+				Value:       "true",
+				Tag:         "!!bool",
+				Line:        3,
+				Column:      1,
+				HeadComment: "One\nTwo\n",
+				LineComment: "Three\n",
+				FootComment: "Four\nFive\n",
+			}},
+		},
+	}, {
+		"[encode]#   One\n#   Two\ntrue #   Three\n#   Four\n#   Five\n",
+		yaml.Node{
+			Kind:   yaml.DocumentNode,
+			Line:   3,
+			Column: 1,
+			Content: []*yaml.Node{{
+				Kind:        yaml.ScalarNode,
+				Value:       "true",
+				Tag:         "!!bool",
+				Line:        3,
+				Column:      1,
+				HeadComment: "  One\n  Two",
+				LineComment: "  Three",
+				FootComment: "  Four\n  Five",
+			}},
+		},
 	}, {
 		"# DH1\n\n# DH2\n\n# H1\n# H2\ntrue # I\n# F1\n# F2\n\n# DF1\n\n# DF2\n",
 		yaml.Node{