Add parse::format whitespace formatter to gix-config (#2594)#2636
Add parse::format whitespace formatter to gix-config (#2594)#2636Amey Pawar (ameyypawar) wants to merge 1 commit into
parse::format whitespace formatter to gix-config (#2594)#2636Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3af56a3f15
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "Codex (@codex) review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "Codex (@codex) address that feedback".
| while out.last() == Some(&b'\n') || out.last() == Some(&b'\r') { | ||
| out.pop(); |
There was a problem hiding this comment.
Preserve trailing blank lines when collapse is disabled
With the default max_consecutive_blank_lines: None, callers are told blank lines are left exactly as-is, but this loop strips every trailing \r/\n that was already emitted and then appends a single newline. For an input like [a]\nx = 1\n\n, normalization silently removes the final blank line even though blank-line collapsing was not requested; ensure_trailing_newline should add a newline only when one is missing, or otherwise honor the collapse option explicitly.
Useful? React with 👍 / 👎.
…deLabs#2594) Adds `gix_config::parse::format::normalize(input, &Options)`, which re-emits a single config file with sanitized whitespace without resolving includes. Values, comments and section headers are preserved verbatim; only insignificant whitespace, the `=` separator and newlines are rewritten. `Options` controls indentation (two spaces by default; tabs or none also available), spacing around `=`, the newline sequence (detect/LF/CRLF), the trailing newline, and optional blank-line collapsing. This is the library portion; the CLI will follow separately. Co-authored-by: Claude <ai-agent@example.invalid>
3af56a3 to
aaf3111
Compare
feat(gix-config): add a flat whitespace formatter (
parse::format)Implements the library half of #2594 — a config-file formatter that re-emits a single file
with normalized whitespace, without resolving
include/includeIf.What
A new module
gix_config::parse::formatwith:plus
Options,Indentation(Tab | Spaces(usize) | None) andNewline(Detect | Lf | CrLf).It works on the public
parse::Eventsstream, so it is flat by construction — parsing at thatlayer never resolves includes (only
Filedoes), which is exactly what Eir Nym (@eirnym) needed. Values,comments and section headers are re-emitted verbatim; only insignificant whitespace, the
=separator, and newlines are rewritten.
Defaults (from the discussion on #2594)
Nonefor no indentation, per 93578237)=Why this shape
parse::Eventsalready separates whitespace into its own events and round-trips losslessly viaEvent::write_to, so the formatter only substitutes the whitespace/separator/newline bytes andpasses everything else through unchanged. Line continuations (
\) carry their leading whitespaceinside the value chunk, so the whole
ValueNotDone … ValueDonespan is emitted verbatim and staysbyte-identical — covered by a dedicated test.
Scope
Library API only. The CLI is intentionally left out — Eir Nym (@eirnym) offered to build it on top once the
API lands.
Tests
Added
gix-config/tests/config/parse/format.rs(16 tests): default policy, semantic-equivalence(re-parse and compare section/key/value triples), line continuation untouched, trailing backslash at
EOF, implicit boolean keys (no injected
=), comments preserved, quoted subsection/value verbatim,newline detect + forced LF/CRLF, tab / 2-space / no-indent, blank-line preserve + opt-in collapse,
separator toggle, and idempotency.
Verified locally against
mainwith:All pass (94 unit + 224 integration + 32 doctests, 0 failures).
Files changed
gix-config/src/parse/format.rs(new)gix-config/src/parse/mod.rs(pub mod format;)gix-config/tests/config/parse/format.rs(new)gix-config/tests/config/parse/mod.rs(mod format;)Part of #2594 — this is the library API; the CLI will follow (per Eir Nym (@eirnym)'s offer). Intentionally
not using a
Closeskeyword so the issue stays open for the CLI work.Disclosure: developed with AI assistance, reviewed and tested by me.