From eb0d02d644800d88b2f1cfaff6df70c7521e0de5 Mon Sep 17 00:00:00 2001 From: Shawn Garbett Date: Mon, 14 Feb 2022 10:59:44 -0600 Subject: [PATCH 1/4] Control Indent Mapping Context --- include/yaml.h | 13 ++++++++++++- src/api.c | 12 ++++++++++++ src/emitter.c | 9 ++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/include/yaml.h b/include/yaml.h index 89050e4f..66164021 100644 --- a/include/yaml.h +++ b/include/yaml.h @@ -1493,7 +1493,6 @@ typedef enum yaml_emitter_state_e { YAML_EMIT_DOCUMENT_CONTENT_STATE, /** Expect DOCUMENT-END. */ YAML_EMIT_DOCUMENT_END_STATE, - /** Expect the first item of a flow sequence. */ YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE, /** Expect an item of a flow sequence. */ @@ -1626,6 +1625,8 @@ typedef struct yaml_emitter_s { int canonical; /** The number of indentation spaces. */ int best_indent; + /** Whether or not to indent block sequences in mapping context. */ + int indent_mapping_sequence; /** The preferred width of the output lines. */ int best_width; /** Allow unescaped non-ASCII characters? */ @@ -1874,6 +1875,16 @@ yaml_emitter_set_canonical(yaml_emitter_t *emitter, int canonical); YAML_DECLARE(void) yaml_emitter_set_indent(yaml_emitter_t *emitter, int indent); +/* + * Set whether or not to indent block sequences in mapping context. + * + * @param[in,out] emitter An emitter object. + * @param[in] indent_mapping_sequence Boolean. + */ + +YAML_DECLARE(void) +yaml_emitter_set_indent_mapping_sequence(yaml_emitter_t *emitter, int indent_mapping_sequence); + /** * Set the preferred line width. @c -1 means unlimited. * diff --git a/src/api.c b/src/api.c index 16f88bd7..aa7272b9 100644 --- a/src/api.c +++ b/src/api.c @@ -540,6 +540,18 @@ yaml_emitter_set_indent(yaml_emitter_t *emitter, int indent) emitter->best_indent = (1 < indent && indent < 10) ? indent : 2; } +/* + * Set whether or not to indent block sequences in mapping context. + */ + +YAML_DECLARE(void) +yaml_emitter_set_indent_mapping_sequence(yaml_emitter_t *emitter, int indent_mapping_sequence) +{ + assert(emitter); /* Non-NULL emitter object expected. */ + + emitter->indent_mapping_sequence = indent_mapping_sequence; +} + /* * Set the preferred line width. */ diff --git a/src/emitter.c b/src/emitter.c index 609b28a4..8a57b6ac 100644 --- a/src/emitter.c +++ b/src/emitter.c @@ -886,7 +886,9 @@ yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter, if (first) { if (!yaml_emitter_increase_indent(emitter, 0, - (emitter->mapping_context && !emitter->indention))) + (emitter->mapping_context + && !emitter->indent_mapping_sequence + && !emitter->indention))) return 0; } @@ -1825,6 +1827,7 @@ yaml_emitter_write_indicator(yaml_emitter_t *emitter, emitter->whitespace = is_whitespace; emitter->indention = (emitter->indention && is_indention); +/* emitter->open_ended = 0; */ return 1; } @@ -1977,6 +1980,10 @@ yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter, emitter->whitespace = 0; emitter->indention = 0; + if (emitter->root_context) + { + emitter->open_ended = 1; + } return 1; } From adf52ff9eb76271e59717a851eba9c4a407533a7 Mon Sep 17 00:00:00 2001 From: Shawn Garbett Date: Mon, 14 Feb 2022 11:28:00 -0600 Subject: [PATCH 2/4] Removing accidently reverted commit --- src/emitter.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/emitter.c b/src/emitter.c index 8a57b6ac..e9183147 100644 --- a/src/emitter.c +++ b/src/emitter.c @@ -1980,10 +1980,6 @@ yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter, emitter->whitespace = 0; emitter->indention = 0; - if (emitter->root_context) - { - emitter->open_ended = 1; - } return 1; } From 26ec1dfd139ae37e05f4a5d73aa7e3aca809fab1 Mon Sep 17 00:00:00 2001 From: Shawn Garbett Date: Mon, 14 Feb 2022 11:33:49 -0600 Subject: [PATCH 3/4] Removing open_ended commented change --- src/emitter.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/emitter.c b/src/emitter.c index e9183147..ec06830e 100644 --- a/src/emitter.c +++ b/src/emitter.c @@ -1827,7 +1827,6 @@ yaml_emitter_write_indicator(yaml_emitter_t *emitter, emitter->whitespace = is_whitespace; emitter->indention = (emitter->indention && is_indention); -/* emitter->open_ended = 0; */ return 1; } From 0e28769ffcb2b0500edca203adafe6b5aade6d33 Mon Sep 17 00:00:00 2001 From: Shawn Garbett Date: Mon, 14 Feb 2022 11:38:18 -0600 Subject: [PATCH 4/4] Added assert readability --- src/api.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api.c b/src/api.c index aa7272b9..8fc71a3a 100644 --- a/src/api.c +++ b/src/api.c @@ -547,7 +547,7 @@ yaml_emitter_set_indent(yaml_emitter_t *emitter, int indent) YAML_DECLARE(void) yaml_emitter_set_indent_mapping_sequence(yaml_emitter_t *emitter, int indent_mapping_sequence) { - assert(emitter); /* Non-NULL emitter object expected. */ + assert(emitter && "Non-NULL emitter object expected."); emitter->indent_mapping_sequence = indent_mapping_sequence; }