Skip to content

fix(uncomment): preserve indentation and flush buffer on uncommented lines#179

Merged
dadav merged 1 commit intodadav:mainfrom
damemi:main
Feb 6, 2026
Merged

fix(uncomment): preserve indentation and flush buffer on uncommented lines#179
dadav merged 1 commit intodadav:mainfrom
damemi:main

Conversation

@damemi
Copy link
Copy Markdown
Contributor

@damemi damemi commented Feb 6, 2026

Fixes #178

Fix two bugs in RemoveCommentsFromYaml that caused invalid YAML output when using the --uncomment flag with nested structures.

Bug 1: Indentation stripping
The commentYamlMapMatcher regex captured the entire prefix including
YAML indentation (e.g., " # " for a 4-space indented comment).
When stripping comments, line[codeIndention:] removed both the comment
marker AND the indentation, causing nested values to lose their
nesting level and appear at the wrong depth in the output.

Fix: Capture indentation separately from the comment marker and preserve the leading whitespace when stripping comments.

Bug 2: Uncommented lines incorrectly buffered
When inCode was true (processing a commented YAML block), any non-comment line was added to the buffer instead of the result. This caused uncommented values following commented values within the same structure to be incorrectly grouped with the commented block, producing malformed YAML.

Fix: When encountering an uncommented line while inCode is true, flush the buffer to result, reset inCode to false, and add the uncommented line directly to result. This allows commented and uncommented fields to appear in any order within a structure.

Before (broken):
struct:
# commentedField: value
uncommentedField: value # incorrectly buffered

After (fixed):
struct:
commentedField: value # uncommented correctly
uncommentedField: value # passed through correctly

(Full disclosure this was done with the help of AI, no offense taken if you don't want to accept this. It worked on my fork and so I thought I would at least offer it up. Thanks again for this project!)

…lines

Fix two bugs in RemoveCommentsFromYaml that caused invalid YAML output
when using the --uncomment flag with nested structures.

Bug 1: Indentation stripping
The commentYamlMapMatcher regex captured the entire prefix including
YAML indentation (e.g., "    # " for a 4-space indented comment).
When stripping comments, line[codeIndention:] removed both the comment
marker AND the indentation, causing nested values to lose their
nesting level and appear at the wrong depth in the output.

Fix: Capture indentation separately from the comment marker and
preserve the leading whitespace when stripping comments.

Bug 2: Uncommented lines incorrectly buffered
When inCode was true (processing a commented YAML block), any
non-comment line was added to the buffer instead of the result.
This caused uncommented values following commented values within
the same structure to be incorrectly grouped with the commented
block, producing malformed YAML.

Fix: When encountering an uncommented line while inCode is true,
flush the buffer to result, reset inCode to false, and add the
uncommented line directly to result. This allows commented and
uncommented fields to appear in any order within a structure.

Before (broken):
  struct:
    # commentedField: value
    uncommentedField: value  # incorrectly buffered

After (fixed):
  struct:
    commentedField: value    # uncommented correctly
    uncommentedField: value  # passed through correctly
@dadav dadav merged commit 3815087 into dadav:main Feb 6, 2026
1 check passed
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.

--uncomment fails in a block if commented fields come before uncommented fields

2 participants