Skip to content

Normalize blank lines in bottom mode - #17

Merged
mstdokumaci merged 4 commits into
mainfrom
bottom-bugs
Aug 10, 2026
Merged

mstdokumaci merged 4 commits into
mainfrom
bottom-bugs

Conversation

@mstdokumaci

@mstdokumaci mstdokumaci commented Aug 10, 2026 •

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • Bug Fixes

    • Normalized consecutive blank lines to a single blank line.
    • Ensured processed files end with one trailing newline.
    • Preserved CRLF line endings during formatting.
    • Improved spacing around hoisted imports and trailing content.
    • Improved full-file change reporting when formatting modifies output.
    • Made bottom-section formatting consistent and idempotent.
  • Tests

    • Added coverage for normalization, import grouping, EOF cleanup, idempotence, whitespace separators, and CRLF handling.

@coderabbitai

coderabbitai Bot commented Aug 10, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: fe55b656-64ff-4634-a0f7-87dea96c213d

📥 Commits

Reviewing files that changed from the base of the PR and between d1b5a5b and 0ce0e07.

📒 Files selected for processing (2)
  • src/zsort.zig
  • src/zsort_test.zig
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/zsort_test.zig
  • src/zsort.zig

📝 Walkthrough

Walkthrough

Blank-line normalization is centralized for import blocks and complete output. Standard and bottom processing remove excessive and trailing blank lines, preserve CRLF endings, track normalization changes, and select full-file diffs when needed. Tests cover these behaviors and idempotence.

Changes

Blank-line normalization

Layer / File(s) Summary
Centralized output normalization
src/zsort.zig
collapseBlankLines normalizes sorted imports and complete rewritten output. Processing uses full_diff to select full-file diffs when changes extend beyond the import block.
Normalization behavior tests
src/zsort_test.zig
Tests cover blank-run collapsing, hoisted bands, trailing blank-line removal, idempotence, updated output expectations, and CRLF preservation.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the bottom-mode blank-line normalization change, although the implementation also normalizes non-bottom output.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bottom-bugs

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
src/zsort_test.zig (1)

421-434: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Correct bottom-mode changed expectations.

Bottom mode sets changed by comparing the source with normalized output.

  • src/zsort_test.zig#L421-L434: expect result.changed and compare against output without the final blank line.
  • src/zsort_test.zig#L624-L653: expect !result.changed because the source already equals the expected normalized output.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/zsort_test.zig` around lines 421 - 434, Update the bottom-mode test at
src/zsort_test.zig lines 421-434 to expect result.changed and compare
result.new_text with the normalized output that omits the final blank line. Also
update the test at src/zsort_test.zig lines 624-653 to expect !result.changed
because its source already matches the expected normalized output.
src/zsort.zig (1)

786-821: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Set changed from the normalized complete output.

collapseBlankLines can modify rest after Line 806 calculates changed. For example, a sorted import block followed by two blank lines before a body declaration returns normalized new_text but reports changed = false. Fix mode then skips the rewrite.

Compare source with collapsed after normalization. If complete-output normalization changed text outside the import block, make check mode use the full-text diff path at Line 1221. Add a standard-layout regression test for this case.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/zsort.zig` around lines 786 - 821, Update the changed calculation in the
block-processing flow to compare the original source with the normalized
collapsed output, so changes introduced by collapseBlankLines are detected.
Ensure check mode uses the full-text diff path when normalization changes text
outside the import block, and add a standard-layout regression test covering
excessive blank lines between sorted imports and a body declaration.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/zsort_test.zig`:
- Around line 421-434: Update the bottom-mode test at src/zsort_test.zig lines
421-434 to expect result.changed and compare result.new_text with the normalized
output that omits the final blank line. Also update the test at
src/zsort_test.zig lines 624-653 to expect !result.changed because its source
already matches the expected normalized output.

In `@src/zsort.zig`:
- Around line 786-821: Update the changed calculation in the block-processing
flow to compare the original source with the normalized collapsed output, so
changes introduced by collapseBlankLines are detected. Ensure check mode uses
the full-text diff path when normalization changes text outside the import
block, and add a standard-layout regression test covering excessive blank lines
between sorted imports and a body declaration.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 827af227-2a25-4de1-8cac-8f0b0232a3b2

📥 Commits

Reviewing files that changed from the base of the PR and between 985626c and 144e11e.

📒 Files selected for processing (2)
  • src/zsort.zig
  • src/zsort_test.zig

@mstdokumaci mstdokumaci changed the title Bottom Bugs Normalize blank lines in bottom mode Aug 10, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/zsort.zig (1)

789-807: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Detect whitespace-only separator lines before inserting junction_blank.

If rest starts with a blank line such as " \r\n", rest[0] is a space. This condition inserts another newline. collapseBlankLines then keeps the inserted line and removes the original first blank line. The output can lose the original blank line’s whitespace or line-ending representation.

Inspect the first line of rest with the same trim rule used by collapseBlankLines.

Suggested fix
-    const junction_blank = !has_trailing_comment and !endsWithBlankLine(new_imports) and rest.len > 0 and rest[0] != '\n' and rest[0] != '\r';
+    const rest_starts_blank = rest.len > 0 and
+        std.mem.trim(u8, rest[0..findLineEnd(rest, 0)], " \t\r\n").len == 0;
+    const junction_blank = !has_trailing_comment and !endsWithBlankLine(new_imports) and
+        rest.len > 0 and !rest_starts_blank;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/zsort.zig` around lines 789 - 807, Update the junction_blank calculation
near last_span_end so it inspects the first line of rest using the same
std.mem.trim whitespace rule as collapseBlankLines, rather than checking rest[0]
for newline characters. Treat whitespace-only first lines as existing separators
and preserve their original whitespace and line-ending representation.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/zsort.zig`:
- Around line 789-807: Update the junction_blank calculation near last_span_end
so it inspects the first line of rest using the same std.mem.trim whitespace
rule as collapseBlankLines, rather than checking rest[0] for newline characters.
Treat whitespace-only first lines as existing separators and preserve their
original whitespace and line-ending representation.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d89f6a66-1716-4a2e-abbc-50860aad61eb

📥 Commits

Reviewing files that changed from the base of the PR and between 144e11e and 8f6aa26.

📒 Files selected for processing (2)
  • src/zsort.zig
  • src/zsort_test.zig
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/zsort_test.zig

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/zsort.zig (1)

798-807: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Use the blank-line predicate at the import/body seam.

When rest starts with a whitespace-only line such as " \r\n", junction_blank adds another separator. collapseBlankLines keeps the inserted separator and discards the original line, which changes its whitespace or line-ending representation. Detect rest_starts_blank with std.mem.trim and apply the same predicate in endsWithBlankLine. Add a regression test with spaces or tabs in the separator.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/zsort.zig` around lines 798 - 807, Update the import/body seam logic
around junction_blank and endsWithBlankLine to detect rest_starts_blank using
std.mem.trim, then reuse that predicate when deciding whether to add the
separator. Preserve the original whitespace and line-ending representation when
rest begins with a whitespace-only line, and add a regression test covering
spaces or tabs in that separator.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/zsort.zig`:
- Around line 798-807: Update the import/body seam logic around junction_blank
and endsWithBlankLine to detect rest_starts_blank using std.mem.trim, then reuse
that predicate when deciding whether to add the separator. Preserve the original
whitespace and line-ending representation when rest begins with a
whitespace-only line, and add a regression test covering spaces or tabs in that
separator.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d96c0206-7757-4ee4-b0ad-9bc32ae15ecf

📥 Commits

Reviewing files that changed from the base of the PR and between 8f6aa26 and d1b5a5b.

📒 Files selected for processing (2)
  • src/zsort.zig
  • src/zsort_test.zig

@mstdokumaci
mstdokumaci merged commit 9b71147 into main Aug 10, 2026
11 checks passed
@mstdokumaci
mstdokumaci deleted the bottom-bugs branch August 10, 2026 22:16
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.

1 participant