Skip to content

Commit

Permalink
make sequence style configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
natasha41575 committed May 14, 2022
1 parent af1850a commit 0e00b68
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 28 deletions.
44 changes: 26 additions & 18 deletions goyaml.v3/emitterc.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func yaml_emitter_append_tag_directive(emitter *yaml_emitter_t, value *yaml_tag_
}

// Increase the indentation level.
func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool) bool {
func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool, compact_seq bool) bool {
emitter.indents = append(emitter.indents, emitter.indent)
if emitter.indent < 0 {
if flow {
Expand All @@ -241,7 +241,10 @@ func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool
emitter.indent += 2
} else {
// Everything else aligns to the chosen indentation.
emitter.indent = emitter.best_indent*((emitter.indent+emitter.best_indent)/emitter.best_indent)
emitter.indent = emitter.best_indent * ((emitter.indent + emitter.best_indent) / emitter.best_indent)
}
if compact_seq {
emitter.indent = emitter.indent - 2
}
}
return true
Expand Down Expand Up @@ -488,7 +491,7 @@ func yaml_emitter_emit_document_content(emitter *yaml_emitter_t, event *yaml_eve
if !yaml_emitter_emit_node(emitter, event, true, false, false, false) {
return false
}
if !yaml_emitter_process_line_comment(emitter) {
if !yaml_emitter_process_line_comment(emitter, false) {
return false
}
if !yaml_emitter_process_foot_comment(emitter) {
Expand Down Expand Up @@ -534,7 +537,7 @@ func yaml_emitter_emit_flow_sequence_item(emitter *yaml_emitter_t, event *yaml_e
if !yaml_emitter_write_indicator(emitter, []byte{'['}, true, true, false) {
return false
}
if !yaml_emitter_increase_indent(emitter, true, false) {
if !yaml_emitter_increase_indent(emitter, true, false, false) {
return false
}
emitter.flow_level++
Expand All @@ -557,7 +560,7 @@ func yaml_emitter_emit_flow_sequence_item(emitter *yaml_emitter_t, event *yaml_e
if !yaml_emitter_write_indicator(emitter, []byte{']'}, false, false, false) {
return false
}
if !yaml_emitter_process_line_comment(emitter) {
if !yaml_emitter_process_line_comment(emitter, false) {
return false
}
if !yaml_emitter_process_foot_comment(emitter) {
Expand Down Expand Up @@ -602,7 +605,7 @@ func yaml_emitter_emit_flow_sequence_item(emitter *yaml_emitter_t, event *yaml_e
return false
}
}
if !yaml_emitter_process_line_comment(emitter) {
if !yaml_emitter_process_line_comment(emitter, false) {
return false
}
if !yaml_emitter_process_foot_comment(emitter) {
Expand All @@ -617,7 +620,7 @@ func yaml_emitter_emit_flow_mapping_key(emitter *yaml_emitter_t, event *yaml_eve
if !yaml_emitter_write_indicator(emitter, []byte{'{'}, true, true, false) {
return false
}
if !yaml_emitter_increase_indent(emitter, true, false) {
if !yaml_emitter_increase_indent(emitter, true, false, false) {
return false
}
emitter.flow_level++
Expand All @@ -643,7 +646,7 @@ func yaml_emitter_emit_flow_mapping_key(emitter *yaml_emitter_t, event *yaml_eve
if !yaml_emitter_write_indicator(emitter, []byte{'}'}, false, false, false) {
return false
}
if !yaml_emitter_process_line_comment(emitter) {
if !yaml_emitter_process_line_comment(emitter, false) {
return false
}
if !yaml_emitter_process_foot_comment(emitter) {
Expand Down Expand Up @@ -716,7 +719,7 @@ func yaml_emitter_emit_flow_mapping_value(emitter *yaml_emitter_t, event *yaml_e
return false
}
}
if !yaml_emitter_process_line_comment(emitter) {
if !yaml_emitter_process_line_comment(emitter, false) {
return false
}
if !yaml_emitter_process_foot_comment(emitter) {
Expand All @@ -728,7 +731,9 @@ func yaml_emitter_emit_flow_mapping_value(emitter *yaml_emitter_t, event *yaml_e
// Expect a block item node.
func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
if first {
if !yaml_emitter_increase_indent(emitter, false, false) {
seq := emitter.mapping_context && (emitter.column == 0 || !emitter.indention) &&
emitter.compact_sequence_indent
if !yaml_emitter_increase_indent(emitter, false, false, seq) {
return false
}
}
Expand All @@ -752,7 +757,7 @@ func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_
if !yaml_emitter_emit_node(emitter, event, false, true, false, false) {
return false
}
if !yaml_emitter_process_line_comment(emitter) {
if !yaml_emitter_process_line_comment(emitter, false) {
return false
}
if !yaml_emitter_process_foot_comment(emitter) {
Expand All @@ -764,7 +769,7 @@ func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_
// Expect a block key node.
func yaml_emitter_emit_block_mapping_key(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
if first {
if !yaml_emitter_increase_indent(emitter, false, false) {
if !yaml_emitter_increase_indent(emitter, false, false, false) {
return false
}
}
Expand Down Expand Up @@ -828,7 +833,7 @@ func yaml_emitter_emit_block_mapping_value(emitter *yaml_emitter_t, event *yaml_
} 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) {
if !yaml_emitter_process_line_comment(emitter, false) {
return false
}
emitter.line_comment, emitter.key_line_comment = emitter.key_line_comment, emitter.line_comment
Expand All @@ -838,7 +843,7 @@ func yaml_emitter_emit_block_mapping_value(emitter *yaml_emitter_t, event *yaml_
if !yaml_emitter_emit_node(emitter, event, false, false, true, false) {
return false
}
if !yaml_emitter_process_line_comment(emitter) {
if !yaml_emitter_process_line_comment(emitter, false) {
return false
}
if !yaml_emitter_process_foot_comment(emitter) {
Expand Down Expand Up @@ -896,7 +901,7 @@ func yaml_emitter_emit_scalar(emitter *yaml_emitter_t, event *yaml_event_t) bool
if !yaml_emitter_process_tag(emitter) {
return false
}
if !yaml_emitter_increase_indent(emitter, true, false) {
if !yaml_emitter_increase_indent(emitter, true, false, false) {
return false
}
if !yaml_emitter_process_scalar(emitter) {
Expand Down Expand Up @@ -1144,8 +1149,11 @@ func yaml_emitter_process_head_comment(emitter *yaml_emitter_t) bool {
}

// Write an line comment.
func yaml_emitter_process_line_comment(emitter *yaml_emitter_t) bool {
func yaml_emitter_process_line_comment(emitter *yaml_emitter_t, linebreak bool) bool {
if len(emitter.line_comment) == 0 {
if linebreak && !put_break(emitter) {
return false
}
return true
}
if !emitter.whitespace {
Expand Down Expand Up @@ -1894,7 +1902,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 !yaml_emitter_process_line_comment(emitter) {
if !yaml_emitter_process_line_comment(emitter, true) {
return false
}
//emitter.indention = true
Expand Down Expand Up @@ -1931,7 +1939,7 @@ 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 !yaml_emitter_process_line_comment(emitter) {
if !yaml_emitter_process_line_comment(emitter, true) {
return false
}

Expand Down
Loading

0 comments on commit 0e00b68

Please sign in to comment.