-
-
Notifications
You must be signed in to change notification settings - Fork 38
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
Feature/preserve comment only files #200
base: main
Are you sure you want to change the base?
Changes from all commits
73dd41e
ca4fe36
0cfd930
86cac79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,6 +224,13 @@ def patch_sequence_style(key_node: Node, value_node: Node) -> None: | |
if not sequence_node.value: | ||
return | ||
|
||
# if this key_node is the `yamlfix_document_fix_id` node, | ||
# the sequence_node is the top-level list, which has to be forced | ||
# into block-mode, so the key_node can be removed afterwards | ||
force_block_style = force_block_style or self._seq_is_in_top_level_node( | ||
key_node | ||
) | ||
|
||
# if this sequence contains non-scalar nodes (i.e. dicts, lists, etc.), | ||
# force block-style | ||
force_block_style = ( | ||
|
@@ -252,6 +259,9 @@ def patch_sequence_style(key_node: Node, value_node: Node) -> None: | |
|
||
self.patch_functions.append(patch_sequence_style) | ||
|
||
def _seq_is_in_top_level_node(self, key_node: Node) -> bool: | ||
return str(key_node.value) == f"yamlfix_{self.config.document_fix_id}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Following the question above, can't we just use |
||
|
||
@staticmethod | ||
def _seq_contains_non_scalar_nodes(seq_node: Node) -> bool: | ||
return any(not isinstance(node, ScalarNode) for node in seq_node.value) | ||
|
@@ -342,9 +352,11 @@ def fix(self, source_code: str) -> str: | |
log.debug("Running source code fixers...") | ||
|
||
fixers = [ | ||
self._fix_comment_only_files, | ||
self._fix_truthy_strings, | ||
self._fix_jinja_variables, | ||
self._ruamel_yaml_fixer, | ||
self._restore_comment_only_files, | ||
self._restore_truthy_strings, | ||
self._restore_jinja_variables, | ||
self._restore_double_exclamations, | ||
|
@@ -693,3 +705,33 @@ def _restore_jinja_variables(source_code: str) -> str: | |
fixed_source_lines.append(line) | ||
|
||
return "\n".join(fixed_source_lines) | ||
|
||
def _fix_comment_only_files(self, source_code: str) -> str: | ||
"""Add a mapping key with an id to the start of the document\ | ||
to preserve comments.""" | ||
fixed_source_lines = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now we're running I'd only run these functions if the config is set. |
||
yamlfix_document_id_line = f"yamlfix_{self.config.document_fix_id}:" | ||
|
||
# Add the document id line after each document start | ||
has_start_indicator = False | ||
for line in source_code.splitlines(): | ||
fixed_source_lines.append(line) | ||
if line.startswith("---"): | ||
has_start_indicator = True | ||
fixed_source_lines.append(yamlfix_document_id_line) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For performance reasons it would be nice if we could stop the iteration on the file source as soon as we've done the required changes. Same applies at the Maybe reformulate it so that once the condition is met it injects the required line and exits. Now that I see it, although this is a good idea, it may need a non-trivial refactor where the fixers tweak directly the parts of the code from If you don't feel like doing this now we can open an issue to track it for the future |
||
|
||
# if the document has no start indicator, the document id as the first line | ||
if not has_start_indicator: | ||
fixed_source_lines.insert(0, yamlfix_document_id_line) | ||
|
||
return "\n".join(fixed_source_lines) | ||
|
||
def _restore_comment_only_files(self, source_code: str) -> str: | ||
"""Remove the document start id from the document again.""" | ||
fixed_source_lines = [] | ||
|
||
for line in source_code.splitlines(): | ||
if self.config.document_fix_id not in line: | ||
fixed_source_lines.append(line) | ||
|
||
return "\n".join(fixed_source_lines) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think this is something a user would want to change? I don't feel that without knowing the internals of
yamlfix
they can even understand the paragraph.If you feel it's interesting to keep the configuration, I'd move it to the bottom of the document, otherwise I'd just hardcode the value