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

Feature/preserve comment only files #200

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

marcules
Copy link
Contributor

@marcules marcules commented Dec 26, 2022

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

  • Add test cases to all the changes you introduce
  • Update the documentation for the changes

@coveralls
Copy link

coveralls commented Dec 26, 2022

Pull Request Test Coverage Report for Build 3784079844

Warning: 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

  • 17 of 17 (100.0%) changed or added relevant lines in 2 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.3%) to 99.543%

Files with Coverage Reduction New Missed Lines %
src/yamlfix/adapters.py 1 99.64%
Totals Coverage Status
Change from base Build 3740603077: 0.3%
Covered Lines: 436
Relevant Lines: 438

💛 - Coveralls

@marcules
Copy link
Contributor Author

@lyz-code @rsnodgrass this adds a mapping key to the document before it goes through ruyaml - meaning:

  1. if the document already has mapping keys on the top level, this line is treated as if it were a mapping with a "none" value
  2. if the document already has a sequences as the top level, this line is treated as if it were the name of the sequence

When it has other mappings yamlfix_document_fix_id has none value:

---
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 is the name of said 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.

@marcules
Copy link
Contributor Author

@lyz-code please wait on merging this until #199 is merged and this is rebased. I want to modify it a bit.

@rsnodgrass
Copy link

Thanks @marcules for the fix!!

@lyz-code
Copy link
Owner

lyz-code commented Dec 27, 2022

@marcules #199 is merged! Tell me when it's ready for review

@lyz-code
Copy link
Owner

@marcules pull the changes from main and run make install. I've updated the dependencies versions

@marcules marcules force-pushed the feature/preserve-comment-only-files branch from 3ab4d87 to c36d86e Compare December 29, 2022 16:13
@marcules marcules force-pushed the feature/preserve-comment-only-files branch from c36d86e to 86cac79 Compare January 1, 2023 19:10
@marcules
Copy link
Contributor Author

marcules commented Jan 1, 2023

@lyz-code you can review this and pull it in now if it looks good
I have rebased it onto main and also fixed the issue in regards to flakeheaven looking into folders other than src/ tests/

Copy link
Owner

@lyz-code lyz-code left a 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.

Copy link
Owner

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}"
Copy link
Owner

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 = []
Copy link
Owner

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)
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Preserve comments for a file with only comments
4 participants