Improve inline comments on map keys (#656).
diff --git a/emitterc.go b/emitterc.go
index c29217e..0f47c9c 100644
--- a/emitterc.go
+++ b/emitterc.go
@@ -814,26 +814,24 @@
}
}
if len(emitter.key_line_comment) > 0 {
- // [Go] A line comment was previously provided for the key. Handle it before
- // the value so the inline comments are placed correctly.
- if yaml_emitter_silent_nil_event(emitter, event) && len(emitter.line_comment) == 0 {
- // Nothing other than the line comment will be written on the line.
- emitter.line_comment = emitter.key_line_comment
- emitter.key_line_comment = nil
- } else {
- // An actual value is coming, so emit the comment line.
+ // [Go] Line comments are generally associated with the value, but when there's
+ // no value on the same line as a mapping key they end up attached to the
+ // key itself.
+ if event.typ == yaml_SCALAR_EVENT {
+ if len(emitter.line_comment) == 0 {
+ // A scalar is coming and it has no line comments by itself yet,
+ // so just let it handle the line comment as usual. If it has a
+ // line comment, we can't have both so the one from the key is lost.
+ emitter.line_comment = emitter.key_line_comment
+ emitter.key_line_comment = nil
+ }
+ } else if event.sequence_style() != yaml_FLOW_SEQUENCE_STYLE && (event.typ == yaml_MAPPING_START_EVENT || event.typ == yaml_SEQUENCE_START_EVENT) {
+ // An indented block follows, so write the comment right now.
emitter.line_comment, emitter.key_line_comment = emitter.key_line_comment, emitter.line_comment
if !yaml_emitter_process_line_comment(emitter) {
return false
}
emitter.line_comment, emitter.key_line_comment = emitter.key_line_comment, emitter.line_comment
- // Indent in unless it's a block that will reindent anyway.
- if event.sequence_style() == yaml_FLOW_SEQUENCE_STYLE || (event.typ != yaml_MAPPING_START_EVENT && event.typ != yaml_SEQUENCE_START_EVENT) {
- emitter.indent = emitter.best_indent*((emitter.indent+emitter.best_indent)/emitter.best_indent)
- if !yaml_emitter_write_indent(emitter) {
- return false
- }
- }
}
}
emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_KEY_STATE)
@@ -1896,7 +1894,7 @@
if !yaml_emitter_write_block_scalar_hints(emitter, value) {
return false
}
- if !put_break(emitter) {
+ if !yaml_emitter_process_line_comment(emitter) {
return false
}
//emitter.indention = true
@@ -1933,10 +1931,10 @@
if !yaml_emitter_write_block_scalar_hints(emitter, value) {
return false
}
-
- if !put_break(emitter) {
+ if !yaml_emitter_process_line_comment(emitter) {
return false
}
+
//emitter.indention = true
emitter.whitespace = true
diff --git a/node_test.go b/node_test.go
index 147594b..b7d0c9a 100644
--- a/node_test.go
+++ b/node_test.go
@@ -549,7 +549,7 @@
}},
},
}, {
- "a:\n # HM\n - # HB1\n # HB2\n b: # IB\n c # IC\n",
+ "[decode]a:\n # HM\n - # HB1\n # HB2\n b: # IB\n c # IC\n",
yaml.Node{
Kind: yaml.DocumentNode,
Line: 1,
@@ -598,6 +598,142 @@
}},
},
}, {
+ // When encoding the value above, it loses b's inline comment.
+ "[encode]a:\n # HM\n - # HB1\n # HB2\n b: c # IC\n",
+ yaml.Node{
+ Kind: yaml.DocumentNode,
+ Line: 1,
+ Column: 1,
+ Content: []*yaml.Node{{
+ Kind: yaml.MappingNode,
+ Tag: "!!map",
+ Line: 1,
+ Column: 1,
+ Content: []*yaml.Node{{
+ Kind: yaml.ScalarNode,
+ Style: 0x0,
+ Tag: "!!str",
+ Value: "a",
+ Line: 1,
+ Column: 1,
+ }, {
+ Kind: yaml.SequenceNode,
+ Tag: "!!seq",
+ Line: 3,
+ Column: 3,
+ Content: []*yaml.Node{{
+ Kind: yaml.MappingNode,
+ Tag: "!!map",
+ HeadComment: "# HM",
+ Line: 5,
+ Column: 5,
+ Content: []*yaml.Node{{
+ Kind: yaml.ScalarNode,
+ Tag: "!!str",
+ Value: "b",
+ HeadComment: "# HB1\n# HB2",
+ LineComment: "# IB",
+ Line: 5,
+ Column: 5,
+ }, {
+ Kind: yaml.ScalarNode,
+ Tag: "!!str",
+ Value: "c",
+ LineComment: "# IC",
+ Line: 6,
+ Column: 7,
+ }},
+ }},
+ }},
+ }},
+ },
+ }, {
+ // Multiple cases of comment inlining next to mapping keys.
+ "a: | # IA\n str\nb: >- # IB\n str\nc: # IC\n - str\nd: # ID\n str:\n",
+ yaml.Node{
+ Kind: yaml.DocumentNode,
+ Line: 1,
+ Column: 1,
+ Content: []*yaml.Node{{
+ Kind: yaml.MappingNode,
+ Tag: "!!map",
+ Line: 1,
+ Column: 1,
+ Content: []*yaml.Node{{
+ Kind: yaml.ScalarNode,
+ Tag: "!!str",
+ Value: "a",
+ Line: 1,
+ Column: 1,
+ }, {
+ Kind: yaml.ScalarNode,
+ Style: yaml.LiteralStyle,
+ Tag: "!!str",
+ Value: "str\n",
+ LineComment: "# IA",
+ Line: 1,
+ Column: 4,
+ }, {
+ Kind: yaml.ScalarNode,
+ Tag: "!!str",
+ Value: "b",
+ Line: 3,
+ Column: 1,
+ }, {
+ Kind: yaml.ScalarNode,
+ Style: yaml.FoldedStyle,
+ Tag: "!!str",
+ Value: "str",
+ LineComment: "# IB",
+ Line: 3,
+ Column: 4,
+ }, {
+ Kind: yaml.ScalarNode,
+ Tag: "!!str",
+ Value: "c",
+ LineComment: "# IC",
+ Line: 5,
+ Column: 1,
+ }, {
+ Kind: yaml.SequenceNode,
+ Tag: "!!seq",
+ Line: 6,
+ Column: 3,
+ Content: []*yaml.Node{{
+ Kind: yaml.ScalarNode,
+ Tag: "!!str",
+ Value: "str",
+ Line: 6,
+ Column: 5,
+ }},
+ }, {
+ Kind: yaml.ScalarNode,
+ Tag: "!!str",
+ Value: "d",
+ LineComment: "# ID",
+ Line: 7,
+ Column: 1,
+ }, {
+ Kind: yaml.MappingNode,
+ Tag: "!!map",
+ Line: 8,
+ Column: 3,
+ Content: []*yaml.Node{{
+ Kind: yaml.ScalarNode,
+ Tag: "!!str",
+ Value: "str",
+ Line: 8,
+ Column: 3,
+ }, {
+ Kind: yaml.ScalarNode,
+ Tag: "!!null",
+ Line: 8,
+ Column: 7,
+ }},
+ }},
+ }},
+ },
+ }, {
// Indentless sequence.
"[decode]a:\n# HM\n- # HB1\n # HB2\n b: # IB\n c # IC\n",
yaml.Node{
@@ -611,7 +747,6 @@
Column: 1,
Content: []*yaml.Node{{
Kind: yaml.ScalarNode,
- Style: 0x0,
Tag: "!!str",
Value: "a",
Line: 1,
diff --git a/scannerc.go b/scannerc.go
index 40834de..ca00701 100644
--- a/scannerc.go
+++ b/scannerc.go
@@ -2260,10 +2260,9 @@
}
}
if parser.buffer[parser.buffer_pos] == '#' {
- // TODO Test this and then re-enable it.
- //if !yaml_parser_scan_line_comment(parser, start_mark) {
- // return false
- //}
+ if !yaml_parser_scan_line_comment(parser, start_mark) {
+ return false
+ }
for !is_breakz(parser.buffer, parser.buffer_pos) {
skip(parser)
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {