Skip to content

feat: implement support for import at the bottom of the file - #16

Merged
mstdokumaci merged 2 commits into
mainfrom
bottom
Aug 10, 2026
Merged

mstdokumaci merged 2 commits into
mainfrom
bottom

Conversation

@mstdokumaci

@mstdokumaci mstdokumaci commented Aug 10, 2026 •

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • New Features

    • Added a --bottom option that moves the complete sorted import block to the end of a file.
    • Supported the option in both check and fix modes.
    • Preserved documentation, comments, spacing, and source newline style during relocation.
    • Added help text and command-line validation for the new option.
  • Documentation

    • Documented --bottom usage and behavior in the README and changelog.

@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: 022bf472-6c65-4527-aca1-c3927bc3e5c7

📥 Commits

Reviewing files that changed from the base of the PR and between 7d6d1fe and bb03a50.

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

📝 Walkthrough

Walkthrough

The PR adds a --bottom mode that moves the sorted import block to the file end. It preserves documentation, comments, spacing, and newline style. The option works with check and fix, with updated tests and documentation.

Changes

Bottom import layout

Layer / File(s) Summary
Layout primitives and import text
src/ast_scan.zig, src/zsort.zig
The implementation exposes findLineStart and adds helpers for attached comments, stray imports, source ordering, and bottom-mode import text.
Source reconstruction
src/zsort.zig
processSource separates the preamble, imports, and body, then appends the sorted import block with preserved comments, spacing, and newline style.
CLI and execution wiring
src/zsort.zig
Argument parsing, help output, file jobs, source processing, and check-mode diffs now support --bottom.
Validation and documentation
src/zsort_test.zig, README.md, CHANGELOG.md
Tests cover relocation, comments, stray imports, idempotence, CRLF handling, and parsing. Documentation describes usage and behavior.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CLI
  participant FileJob
  participant processSource
  participant File
  CLI->>FileJob: pass bottom=true
  FileJob->>processSource: process source with bottom mode
  processSource->>File: separate preamble, imports, and body
  processSource->>File: append sorted import block
  File-->>CLI: return rewritten source or full-file diff
Loading

Possibly related PRs

  • mstdokumaci/zsort#4: The implementation builds on its AST scanner and source-processing functionality.
  • mstdokumaci/zsort#5: It shares import-sorting and comment-preservation logic in the implementation and tests.
  • mstdokumaci/zsort#13: It contains related import reconstruction and stray-import handling changes.
🚥 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 clearly describes the main change: adding support for placing imports at the bottom of the file.
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

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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/zsort_test.zig (1)

574-585: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Assert import relocation in the CRLF test.

The test only checks that CRLF line endings remain valid. A regression that leaves imports at the top can pass this test. Assert the expected bottom-layout output.

Proposed test update
     try std.testing.expect(result.changed);
+    try std.testing.expectEqualStrings(
+        "pub fn main() !void {}\r\n\r\n" ++
+            "const a = `@import`(\"a.zig\");\r\n" ++
+            "const b = `@import`(\"b.zig\");\r\n",
+        result.new_text,
+    );
     var prev: u8 = 0;
🤖 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 574 - 585, Update the test named
“processSource: --bottom preserves CRLF line endings” to assert that
result.new_text matches the expected output with imports relocated to the
bottom, while retaining the existing CRLF validation. Use the source’s import
and blank-line structure to verify the complete bottom-layout result.
🤖 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.

Inline comments:
In `@src/zsort.zig`:
- Around line 233-246: Update the bottom-mode import emission to compute
attached comment starts with attachedCommentHeadStart for every import,
including top-region and stray imports, rather than relying on
Import.comment_start. Remove the first_start special-case block and preserve
contiguous attachment boundaries so repeated --bottom formatting remains
idempotent.

---

Nitpick comments:
In `@src/zsort_test.zig`:
- Around line 574-585: Update the test named “processSource: --bottom preserves
CRLF line endings” to assert that result.new_text matches the expected output
with imports relocated to the bottom, while retaining the existing CRLF
validation. Use the source’s import and blank-line structure to verify the
complete bottom-layout result.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2b6979cf-873f-40a5-8a4a-3e61fe540b06

📥 Commits

Reviewing files that changed from the base of the PR and between 1e2edd3 and 7d6d1fe.

📒 Files selected for processing (5)
  • CHANGELOG.md
  • README.md
  • src/ast_scan.zig
  • src/zsort.zig
  • src/zsort_test.zig

Comment thread src/zsort.zig
@mstdokumaci
mstdokumaci merged commit 985626c into main Aug 10, 2026
11 checks passed
@mstdokumaci
mstdokumaci deleted the bottom branch August 10, 2026 21:21
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