Skip to content

Commit

Permalink
Improve inline comments on map keys (#656).
Browse files Browse the repository at this point in the history
  • Loading branch information
niemeyer committed Jan 7, 2021
1 parent 749611f commit 496545a
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 24 deletions.
34 changes: 16 additions & 18 deletions emitterc.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,26 +814,24 @@ func yaml_emitter_emit_block_mapping_value(emitter *yaml_emitter_t, event *yaml_
}
}
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)
Expand Down Expand Up @@ -1896,7 +1894,7 @@ func yaml_emitter_write_literal_scalar(emitter *yaml_emitter_t, value []byte) bo
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
Expand Down Expand Up @@ -1933,10 +1931,10 @@ func yaml_emitter_write_folded_scalar(emitter *yaml_emitter_t, value []byte) boo
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

Expand Down
139 changes: 137 additions & 2 deletions node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ var nodeTests = []struct {
}},
},
}, {
"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,
Expand Down Expand Up @@ -597,6 +597,142 @@ var nodeTests = []struct {
}},
}},
},
}, {
// 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",
Expand All @@ -611,7 +747,6 @@ var nodeTests = []struct {
Column: 1,
Content: []*yaml.Node{{
Kind: yaml.ScalarNode,
Style: 0x0,
Tag: "!!str",
Value: "a",
Line: 1,
Expand Down
7 changes: 3 additions & 4 deletions scannerc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2260,10 +2260,9 @@ func yaml_parser_scan_block_scalar(parser *yaml_parser_t, token *yaml_token_t, l
}
}
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) {
Expand Down

0 comments on commit 496545a

Please sign in to comment.