-
-
Notifications
You must be signed in to change notification settings - Fork 37
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?
Feature/preserve comment only files #200
Conversation
Pull Request Test Coverage Report for Build 3818306019Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
@lyz-code @rsnodgrass this adds a mapping key to the document before it goes through ruyaml - meaning:
When it has other mappings ---
yamlfix_document_fix_id: # uuid
key: value
---
# equivalent to:
yamlfix_document_fix_id: none # uuid
key: value When it has a top level list, ---
yamlfix_document_fix_id: #uuid
- item
- item
---
# equivalent to:
yamlfix_document_fix_id: [item, item] # uuid When the document was previously only comments, ruyaml attaches them to the "last found node" (e.g. this line): ---
yamlfix_document_fix_id: #uuid
# other comments
# comments comments All previous tests are passing, and this should in theory work with any yaml file - but it is, admittedly, "clever" (not to say hacky 😅) - not exactly sure what happens with shorter files, that are converted to flow-style or something like that. |
Thanks @marcules for the fix!! |
@marcules pull the changes from |
3ab4d87
to
c36d86e
Compare
…ed in the Makefile for make lint
* if a yaml file contains comments only, preserve the comments (never harm the user!)
c36d86e
to
86cac79
Compare
@lyz-code you can review this and pull it in now if it looks good |
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.
Thanks again for your time, do you want to become a maintainer of the package? I can give you permissions, that way we can share the load of answering the users, taking care of the CI...
If you don't like the idea it's fine :)
Warning: You should not modify this value, if you're not sure you need to. | ||
|
||
This generates a UUID in hex-string-representation (no delimiters), which is used internally to generate a temporary top-level mapping node, where any lists or comments can be attached, so ruyaml is not removing comments, and comment-only documents can be formatted properly. | ||
|
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
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Following the question above, can't we just use yamlfix_top_node
?
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Now we're running _fix_comment_only_files
and _restore_comment_only_files
by default, which slows down the program as it needs to iterate through the source code 2 times.
I'd only run these functions if the config is set.
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 comment
The 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 _restore_comment_only_files
.
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 self.
instead of receiving the source as an argument and returning as a return value.
If you don't feel like doing this now we can open an issue to track it for the future
Fixes #183
Adds a temporary key to the document at the start and removes it again after ruyaml ran through, preventing it from removing the comments in a comment-only yaml file.
Checklist