Skip to content

Commit 496545a

Browse files
committed
Improve inline comments on map keys (go-yaml#656).
1 parent 749611f commit 496545a

File tree

3 files changed

+156
-24
lines changed

3 files changed

+156
-24
lines changed

emitterc.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -814,26 +814,24 @@ func yaml_emitter_emit_block_mapping_value(emitter *yaml_emitter_t, event *yaml_
814814
}
815815
}
816816
if len(emitter.key_line_comment) > 0 {
817-
// [Go] A line comment was previously provided for the key. Handle it before
818-
// the value so the inline comments are placed correctly.
819-
if yaml_emitter_silent_nil_event(emitter, event) && len(emitter.line_comment) == 0 {
820-
// Nothing other than the line comment will be written on the line.
821-
emitter.line_comment = emitter.key_line_comment
822-
emitter.key_line_comment = nil
823-
} else {
824-
// An actual value is coming, so emit the comment line.
817+
// [Go] Line comments are generally associated with the value, but when there's
818+
// no value on the same line as a mapping key they end up attached to the
819+
// key itself.
820+
if event.typ == yaml_SCALAR_EVENT {
821+
if len(emitter.line_comment) == 0 {
822+
// A scalar is coming and it has no line comments by itself yet,
823+
// so just let it handle the line comment as usual. If it has a
824+
// line comment, we can't have both so the one from the key is lost.
825+
emitter.line_comment = emitter.key_line_comment
826+
emitter.key_line_comment = nil
827+
}
828+
} else if event.sequence_style() != yaml_FLOW_SEQUENCE_STYLE && (event.typ == yaml_MAPPING_START_EVENT || event.typ == yaml_SEQUENCE_START_EVENT) {
829+
// An indented block follows, so write the comment right now.
825830
emitter.line_comment, emitter.key_line_comment = emitter.key_line_comment, emitter.line_comment
826831
if !yaml_emitter_process_line_comment(emitter) {
827832
return false
828833
}
829834
emitter.line_comment, emitter.key_line_comment = emitter.key_line_comment, emitter.line_comment
830-
// Indent in unless it's a block that will reindent anyway.
831-
if event.sequence_style() == yaml_FLOW_SEQUENCE_STYLE || (event.typ != yaml_MAPPING_START_EVENT && event.typ != yaml_SEQUENCE_START_EVENT) {
832-
emitter.indent = emitter.best_indent*((emitter.indent+emitter.best_indent)/emitter.best_indent)
833-
if !yaml_emitter_write_indent(emitter) {
834-
return false
835-
}
836-
}
837835
}
838836
}
839837
emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_KEY_STATE)
@@ -1896,7 +1894,7 @@ func yaml_emitter_write_literal_scalar(emitter *yaml_emitter_t, value []byte) bo
18961894
if !yaml_emitter_write_block_scalar_hints(emitter, value) {
18971895
return false
18981896
}
1899-
if !put_break(emitter) {
1897+
if !yaml_emitter_process_line_comment(emitter) {
19001898
return false
19011899
}
19021900
//emitter.indention = true
@@ -1933,10 +1931,10 @@ func yaml_emitter_write_folded_scalar(emitter *yaml_emitter_t, value []byte) boo
19331931
if !yaml_emitter_write_block_scalar_hints(emitter, value) {
19341932
return false
19351933
}
1936-
1937-
if !put_break(emitter) {
1934+
if !yaml_emitter_process_line_comment(emitter) {
19381935
return false
19391936
}
1937+
19401938
//emitter.indention = true
19411939
emitter.whitespace = true
19421940

node_test.go

Lines changed: 137 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ var nodeTests = []struct {
549549
}},
550550
},
551551
}, {
552-
"a:\n # HM\n - # HB1\n # HB2\n b: # IB\n c # IC\n",
552+
"[decode]a:\n # HM\n - # HB1\n # HB2\n b: # IB\n c # IC\n",
553553
yaml.Node{
554554
Kind: yaml.DocumentNode,
555555
Line: 1,
@@ -597,6 +597,142 @@ var nodeTests = []struct {
597597
}},
598598
}},
599599
},
600+
}, {
601+
// When encoding the value above, it loses b's inline comment.
602+
"[encode]a:\n # HM\n - # HB1\n # HB2\n b: c # IC\n",
603+
yaml.Node{
604+
Kind: yaml.DocumentNode,
605+
Line: 1,
606+
Column: 1,
607+
Content: []*yaml.Node{{
608+
Kind: yaml.MappingNode,
609+
Tag: "!!map",
610+
Line: 1,
611+
Column: 1,
612+
Content: []*yaml.Node{{
613+
Kind: yaml.ScalarNode,
614+
Style: 0x0,
615+
Tag: "!!str",
616+
Value: "a",
617+
Line: 1,
618+
Column: 1,
619+
}, {
620+
Kind: yaml.SequenceNode,
621+
Tag: "!!seq",
622+
Line: 3,
623+
Column: 3,
624+
Content: []*yaml.Node{{
625+
Kind: yaml.MappingNode,
626+
Tag: "!!map",
627+
HeadComment: "# HM",
628+
Line: 5,
629+
Column: 5,
630+
Content: []*yaml.Node{{
631+
Kind: yaml.ScalarNode,
632+
Tag: "!!str",
633+
Value: "b",
634+
HeadComment: "# HB1\n# HB2",
635+
LineComment: "# IB",
636+
Line: 5,
637+
Column: 5,
638+
}, {
639+
Kind: yaml.ScalarNode,
640+
Tag: "!!str",
641+
Value: "c",
642+
LineComment: "# IC",
643+
Line: 6,
644+
Column: 7,
645+
}},
646+
}},
647+
}},
648+
}},
649+
},
650+
}, {
651+
// Multiple cases of comment inlining next to mapping keys.
652+
"a: | # IA\n str\nb: >- # IB\n str\nc: # IC\n - str\nd: # ID\n str:\n",
653+
yaml.Node{
654+
Kind: yaml.DocumentNode,
655+
Line: 1,
656+
Column: 1,
657+
Content: []*yaml.Node{{
658+
Kind: yaml.MappingNode,
659+
Tag: "!!map",
660+
Line: 1,
661+
Column: 1,
662+
Content: []*yaml.Node{{
663+
Kind: yaml.ScalarNode,
664+
Tag: "!!str",
665+
Value: "a",
666+
Line: 1,
667+
Column: 1,
668+
}, {
669+
Kind: yaml.ScalarNode,
670+
Style: yaml.LiteralStyle,
671+
Tag: "!!str",
672+
Value: "str\n",
673+
LineComment: "# IA",
674+
Line: 1,
675+
Column: 4,
676+
}, {
677+
Kind: yaml.ScalarNode,
678+
Tag: "!!str",
679+
Value: "b",
680+
Line: 3,
681+
Column: 1,
682+
}, {
683+
Kind: yaml.ScalarNode,
684+
Style: yaml.FoldedStyle,
685+
Tag: "!!str",
686+
Value: "str",
687+
LineComment: "# IB",
688+
Line: 3,
689+
Column: 4,
690+
}, {
691+
Kind: yaml.ScalarNode,
692+
Tag: "!!str",
693+
Value: "c",
694+
LineComment: "# IC",
695+
Line: 5,
696+
Column: 1,
697+
}, {
698+
Kind: yaml.SequenceNode,
699+
Tag: "!!seq",
700+
Line: 6,
701+
Column: 3,
702+
Content: []*yaml.Node{{
703+
Kind: yaml.ScalarNode,
704+
Tag: "!!str",
705+
Value: "str",
706+
Line: 6,
707+
Column: 5,
708+
}},
709+
}, {
710+
Kind: yaml.ScalarNode,
711+
Tag: "!!str",
712+
Value: "d",
713+
LineComment: "# ID",
714+
Line: 7,
715+
Column: 1,
716+
}, {
717+
Kind: yaml.MappingNode,
718+
Tag: "!!map",
719+
Line: 8,
720+
Column: 3,
721+
Content: []*yaml.Node{{
722+
Kind: yaml.ScalarNode,
723+
Tag: "!!str",
724+
Value: "str",
725+
Line: 8,
726+
Column: 3,
727+
}, {
728+
Kind: yaml.ScalarNode,
729+
Tag: "!!null",
730+
Line: 8,
731+
Column: 7,
732+
}},
733+
}},
734+
}},
735+
},
600736
}, {
601737
// Indentless sequence.
602738
"[decode]a:\n# HM\n- # HB1\n # HB2\n b: # IB\n c # IC\n",
@@ -611,7 +747,6 @@ var nodeTests = []struct {
611747
Column: 1,
612748
Content: []*yaml.Node{{
613749
Kind: yaml.ScalarNode,
614-
Style: 0x0,
615750
Tag: "!!str",
616751
Value: "a",
617752
Line: 1,

scannerc.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,10 +2260,9 @@ func yaml_parser_scan_block_scalar(parser *yaml_parser_t, token *yaml_token_t, l
22602260
}
22612261
}
22622262
if parser.buffer[parser.buffer_pos] == '#' {
2263-
// TODO Test this and then re-enable it.
2264-
//if !yaml_parser_scan_line_comment(parser, start_mark) {
2265-
// return false
2266-
//}
2263+
if !yaml_parser_scan_line_comment(parser, start_mark) {
2264+
return false
2265+
}
22672266
for !is_breakz(parser.buffer, parser.buffer_pos) {
22682267
skip(parser)
22692268
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {

0 commit comments

Comments
 (0)