Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control Indent Mapping Context #241

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion include/yaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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? */
Expand Down Expand Up @@ -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.
*
Expand Down
12 changes: 12 additions & 0 deletions src/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
9 changes: 8 additions & 1 deletion src/emitter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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; */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test suite is passing although this was commented out. That seems suspicious.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was a change that exists upstream that I'm trying to harmonize. If I leave that in, it breaks tests. I suspect it may be a reverted change from earlier and can be safely removed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this.


return 1;
}
Expand Down Expand Up @@ -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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above comment. This is basically a revert from a previous commit, but the test suite doesn't catch that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed this change up.


return 1;
}
Expand Down