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

--write: Avoid line-wrapping YAML flow objects #2022

Merged
merged 2 commits into from
Mar 19, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/ansiblelint/yaml_utils.py
@@ -1,4 +1,5 @@
"""Utility helpers to simplify working with yaml-based data."""
# pylint: disable=too-many-lines
ssbarnea marked this conversation as resolved.
Show resolved Hide resolved
import functools
import logging
import os
Expand All @@ -20,7 +21,7 @@
)

import ruamel.yaml.events
from ruamel.yaml.comments import CommentedMap, CommentedSeq
from ruamel.yaml.comments import CommentedMap, CommentedSeq, Format
from ruamel.yaml.constructor import RoundTripConstructor
from ruamel.yaml.emitter import Emitter, ScalarAnalysis

Expand Down Expand Up @@ -886,13 +887,58 @@ def loads(self, stream: str) -> Any:
def dumps(self, data: Any) -> str:
"""Dump YAML document to string (including its preamble_comment)."""
preamble_comment: Optional[str] = getattr(data, "preamble_comment", None)
self._prevent_wrapping_flow_style(data)
with StringIO() as stream:
if preamble_comment:
stream.write(preamble_comment)
self.dump(data, stream)
text = stream.getvalue()
return self._post_process_yaml(text)

def _prevent_wrapping_flow_style(self, data: Any) -> None:
if not isinstance(data, (CommentedMap, CommentedSeq)):
return
for key, value, parent_path in nested_items_path(data):
if not isinstance(value, (CommentedMap, CommentedSeq)):
continue
fa: Format = value.fa # pylint: disable=invalid-name
if fa.flow_style():
predicted_indent = self._predict_indent_length(parent_path, key)
predicted_width = len(str(value))
ssbarnea marked this conversation as resolved.
Show resolved Hide resolved
if predicted_indent + predicted_width > self.width:
# this flow-style map will probably get line-wrapped,
# so, switch it to block style to avoid the line wrap.
fa.set_block_style()

def _predict_indent_length(
self, parent_path: List[Union[str, int]], key: Any
) -> int:
indent = 0

# each parent_key type tells us what the indent is for the next level.
for parent_key in parent_path:
if isinstance(parent_key, int) and indent == 0:
# root level is a sequence
indent += self.sequence_dash_offset
elif isinstance(parent_key, int):
# next level is a sequence
indent += cast(int, self.sequence_indent)
elif isinstance(parent_key, str):
# next level is a map
indent += cast(int, self.map_indent)

if isinstance(key, int) and indent == 0:
# flow map is an item in a root-level sequence
indent += self.sequence_dash_offset
elif isinstance(key, int) and indent > 0:
# flow map is in a sequence
indent += cast(int, self.sequence_indent)
elif isinstance(key, str):
# flow map is in a map
indent += len(key + ": ")

return indent

# ruamel.yaml only preserves empty (no whitespace) blank lines
# (ie "/n/n" becomes "/n/n" but "/n /n" becomes "/n").
# So, we need to identify whitespace-only lines to drop spaces before reading.
Expand Down