Skip to content

Commit 0e00b68

Browse files
committed
make sequence style configurable
1 parent af1850a commit 0e00b68

File tree

4 files changed

+194
-28
lines changed

4 files changed

+194
-28
lines changed

goyaml.v3/emitterc.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func yaml_emitter_append_tag_directive(emitter *yaml_emitter_t, value *yaml_tag_
226226
}
227227

228228
// Increase the indentation level.
229-
func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool) bool {
229+
func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool, compact_seq bool) bool {
230230
emitter.indents = append(emitter.indents, emitter.indent)
231231
if emitter.indent < 0 {
232232
if flow {
@@ -241,7 +241,10 @@ func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool
241241
emitter.indent += 2
242242
} else {
243243
// Everything else aligns to the chosen indentation.
244-
emitter.indent = emitter.best_indent*((emitter.indent+emitter.best_indent)/emitter.best_indent)
244+
emitter.indent = emitter.best_indent * ((emitter.indent + emitter.best_indent) / emitter.best_indent)
245+
}
246+
if compact_seq {
247+
emitter.indent = emitter.indent - 2
245248
}
246249
}
247250
return true
@@ -488,7 +491,7 @@ func yaml_emitter_emit_document_content(emitter *yaml_emitter_t, event *yaml_eve
488491
if !yaml_emitter_emit_node(emitter, event, true, false, false, false) {
489492
return false
490493
}
491-
if !yaml_emitter_process_line_comment(emitter) {
494+
if !yaml_emitter_process_line_comment(emitter, false) {
492495
return false
493496
}
494497
if !yaml_emitter_process_foot_comment(emitter) {
@@ -534,7 +537,7 @@ func yaml_emitter_emit_flow_sequence_item(emitter *yaml_emitter_t, event *yaml_e
534537
if !yaml_emitter_write_indicator(emitter, []byte{'['}, true, true, false) {
535538
return false
536539
}
537-
if !yaml_emitter_increase_indent(emitter, true, false) {
540+
if !yaml_emitter_increase_indent(emitter, true, false, false) {
538541
return false
539542
}
540543
emitter.flow_level++
@@ -557,7 +560,7 @@ func yaml_emitter_emit_flow_sequence_item(emitter *yaml_emitter_t, event *yaml_e
557560
if !yaml_emitter_write_indicator(emitter, []byte{']'}, false, false, false) {
558561
return false
559562
}
560-
if !yaml_emitter_process_line_comment(emitter) {
563+
if !yaml_emitter_process_line_comment(emitter, false) {
561564
return false
562565
}
563566
if !yaml_emitter_process_foot_comment(emitter) {
@@ -602,7 +605,7 @@ func yaml_emitter_emit_flow_sequence_item(emitter *yaml_emitter_t, event *yaml_e
602605
return false
603606
}
604607
}
605-
if !yaml_emitter_process_line_comment(emitter) {
608+
if !yaml_emitter_process_line_comment(emitter, false) {
606609
return false
607610
}
608611
if !yaml_emitter_process_foot_comment(emitter) {
@@ -617,7 +620,7 @@ func yaml_emitter_emit_flow_mapping_key(emitter *yaml_emitter_t, event *yaml_eve
617620
if !yaml_emitter_write_indicator(emitter, []byte{'{'}, true, true, false) {
618621
return false
619622
}
620-
if !yaml_emitter_increase_indent(emitter, true, false) {
623+
if !yaml_emitter_increase_indent(emitter, true, false, false) {
621624
return false
622625
}
623626
emitter.flow_level++
@@ -643,7 +646,7 @@ func yaml_emitter_emit_flow_mapping_key(emitter *yaml_emitter_t, event *yaml_eve
643646
if !yaml_emitter_write_indicator(emitter, []byte{'}'}, false, false, false) {
644647
return false
645648
}
646-
if !yaml_emitter_process_line_comment(emitter) {
649+
if !yaml_emitter_process_line_comment(emitter, false) {
647650
return false
648651
}
649652
if !yaml_emitter_process_foot_comment(emitter) {
@@ -716,7 +719,7 @@ func yaml_emitter_emit_flow_mapping_value(emitter *yaml_emitter_t, event *yaml_e
716719
return false
717720
}
718721
}
719-
if !yaml_emitter_process_line_comment(emitter) {
722+
if !yaml_emitter_process_line_comment(emitter, false) {
720723
return false
721724
}
722725
if !yaml_emitter_process_foot_comment(emitter) {
@@ -728,7 +731,9 @@ func yaml_emitter_emit_flow_mapping_value(emitter *yaml_emitter_t, event *yaml_e
728731
// Expect a block item node.
729732
func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
730733
if first {
731-
if !yaml_emitter_increase_indent(emitter, false, false) {
734+
seq := emitter.mapping_context && (emitter.column == 0 || !emitter.indention) &&
735+
emitter.compact_sequence_indent
736+
if !yaml_emitter_increase_indent(emitter, false, false, seq) {
732737
return false
733738
}
734739
}
@@ -752,7 +757,7 @@ func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_
752757
if !yaml_emitter_emit_node(emitter, event, false, true, false, false) {
753758
return false
754759
}
755-
if !yaml_emitter_process_line_comment(emitter) {
760+
if !yaml_emitter_process_line_comment(emitter, false) {
756761
return false
757762
}
758763
if !yaml_emitter_process_foot_comment(emitter) {
@@ -764,7 +769,7 @@ func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_
764769
// Expect a block key node.
765770
func yaml_emitter_emit_block_mapping_key(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
766771
if first {
767-
if !yaml_emitter_increase_indent(emitter, false, false) {
772+
if !yaml_emitter_increase_indent(emitter, false, false, false) {
768773
return false
769774
}
770775
}
@@ -828,7 +833,7 @@ func yaml_emitter_emit_block_mapping_value(emitter *yaml_emitter_t, event *yaml_
828833
} else if event.sequence_style() != yaml_FLOW_SEQUENCE_STYLE && (event.typ == yaml_MAPPING_START_EVENT || event.typ == yaml_SEQUENCE_START_EVENT) {
829834
// An indented block follows, so write the comment right now.
830835
emitter.line_comment, emitter.key_line_comment = emitter.key_line_comment, emitter.line_comment
831-
if !yaml_emitter_process_line_comment(emitter) {
836+
if !yaml_emitter_process_line_comment(emitter, false) {
832837
return false
833838
}
834839
emitter.line_comment, emitter.key_line_comment = emitter.key_line_comment, emitter.line_comment
@@ -838,7 +843,7 @@ func yaml_emitter_emit_block_mapping_value(emitter *yaml_emitter_t, event *yaml_
838843
if !yaml_emitter_emit_node(emitter, event, false, false, true, false) {
839844
return false
840845
}
841-
if !yaml_emitter_process_line_comment(emitter) {
846+
if !yaml_emitter_process_line_comment(emitter, false) {
842847
return false
843848
}
844849
if !yaml_emitter_process_foot_comment(emitter) {
@@ -896,7 +901,7 @@ func yaml_emitter_emit_scalar(emitter *yaml_emitter_t, event *yaml_event_t) bool
896901
if !yaml_emitter_process_tag(emitter) {
897902
return false
898903
}
899-
if !yaml_emitter_increase_indent(emitter, true, false) {
904+
if !yaml_emitter_increase_indent(emitter, true, false, false) {
900905
return false
901906
}
902907
if !yaml_emitter_process_scalar(emitter) {
@@ -1144,8 +1149,11 @@ func yaml_emitter_process_head_comment(emitter *yaml_emitter_t) bool {
11441149
}
11451150

11461151
// Write an line comment.
1147-
func yaml_emitter_process_line_comment(emitter *yaml_emitter_t) bool {
1152+
func yaml_emitter_process_line_comment(emitter *yaml_emitter_t, linebreak bool) bool {
11481153
if len(emitter.line_comment) == 0 {
1154+
if linebreak && !put_break(emitter) {
1155+
return false
1156+
}
11491157
return true
11501158
}
11511159
if !emitter.whitespace {
@@ -1894,7 +1902,7 @@ func yaml_emitter_write_literal_scalar(emitter *yaml_emitter_t, value []byte) bo
18941902
if !yaml_emitter_write_block_scalar_hints(emitter, value) {
18951903
return false
18961904
}
1897-
if !yaml_emitter_process_line_comment(emitter) {
1905+
if !yaml_emitter_process_line_comment(emitter, true) {
18981906
return false
18991907
}
19001908
//emitter.indention = true
@@ -1931,7 +1939,7 @@ func yaml_emitter_write_folded_scalar(emitter *yaml_emitter_t, value []byte) boo
19311939
if !yaml_emitter_write_block_scalar_hints(emitter, value) {
19321940
return false
19331941
}
1934-
if !yaml_emitter_process_line_comment(emitter) {
1942+
if !yaml_emitter_process_line_comment(emitter, true) {
19351943
return false
19361944
}
19371945

0 commit comments

Comments
 (0)