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

Preserve Correct Indentation When Uncommenting Lines #873

Conversation

viktorianer
Copy link
Contributor

🌈

Problem

When using Thor's uncomment_lines method to programmatically uncomment lines in YAML files or similar, the method was found to inaccurately handle the preservation of indentation spaces before the comment hash (#). Particularly, when uncommenting lines in structured files where indentation is crucial (like YAML files), the original method removed not only the comment hash but also all horizontal whitespace between the hash and the beginning of the content. This behavior led to a disruption in the file's structure and could potentially cause errors when the file was processed or parsed afterward.

For example, in a .github/workflows/ci.yml file containing commented-out configuration for Redis:

# redis:
#   image: redis
#   ports:
#     - 6379:6379
#   options: --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5

Applying uncomment_lines with the previous implementation would result in incorrectly formatted YAML due to the removal of spaces before the content, immediately after the comment hash, disrupting the indentation structure crucial for YAML files.

Solution

The proposed change corrects the regex pattern used in the uncomment_lines method from ^(\s*)#[[:blank:]]*(.*#{flag}) to ^(\s*)#[[:blank:]]?(.*#{flag}). This adjustment ensures that:

  • The leading indentation before the comment hash is preserved.
  • Only the first space or tab (if any) right after the comment hash is removed, along with the hash itself.

This means that the indentation structure of the uncommented lines remains intact, preserving the file's integrity and adherence to YAML or other structured file formats' syntax requirements.

Impact

This change significantly improves the utility of the uncomment_lines method for Rails developers and others using Thor for automation scripts, particularly when working with YAML files for CI/CD pipelines, configurations, etc. It ensures that the uncommented lines blend seamlessly into the document's structure without introducing syntax errors or misinterpretations by parsers.

@rafaelfranca rafaelfranca merged commit 9524b48 into rails:main Feb 26, 2024
5 of 7 checks passed
@viktorianer viktorianer deleted the fix-uncomment-lines-space-which-existed-before-the-comment-hash branch February 26, 2024 20:37
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.

2 participants