Skip to content

fix: use diff line index as 1-based line numbers#389

Merged
2bndy5 merged 4 commits into
mainfrom
diff-line-numbers
Jun 27, 2026
Merged

fix: use diff line index as 1-based line numbers#389
2bndy5 merged 4 commits into
mainfrom
diff-line-numbers

Conversation

@2bndy5

@2bndy5 2bndy5 commented Jun 27, 2026

Copy link
Copy Markdown
Collaborator

After doing some experiments, I found that the gix_imara_diff::Diff API uses 0-based line indexes instead of 1-based line numbers. Furthermore the gix_imara_diff::UnifiedDiff API uses 1-based line numbers when printing hunk headers (@@ -3,5 +3,6 @@). Thus, printing the unified diff into a patch file remains the same as it was already accurate.

So this patch increments the line indexes by 1 when they are be compared/normalized to ranges of 1-based line numbers.

I left the print_diff() function in the test module as a helper/proof when analyzing future regressions.

Summary by CodeRabbit

  • Bug Fixes
    • Improved how changed lines are detected and reported, especially for additions, removals, and mixed diffs.
    • Fixed line numbers shown in removal suggestions so they now match what users see in the file.
    • Better handles ranges when a removal covers an existing selected range, reducing missed or duplicated suggestions.

After doing some experiments, I found that the `Diff` API (from `gix-imara-diff` crate) uses 0-based line indexes instead of 1-based line numbers.
Furthermore the `UnifiedDiff` API (again from `gix-imara-diff` crate) uses 1-based line numbers when printing hunk headers (`@@ -3, 5 +3,6 @@`).
Thus, printing the unified diff into a patch file remains the same as it was already accurate.

So this patch gracefully increments the line indexes by 1 so they can be compared to ranges of 1-based line numbers.

I left the `print_diff()` function in the test module as helper/proof when analyzing future regressions.
@2bndy5 2bndy5 added the bug Something isn't working label Jun 27, 2026
@coderabbitai

coderabbitai Bot commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@2bndy5, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 42 minutes and 55 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: c1632731-9231-4ee2-8680-310e5baa1b6d

📥 Commits

Reviewing files that changed from the base of the PR and between 29f82f6 and f20e7c7.

📒 Files selected for processing (1)
  • cpp-linter/src/clang_tools/clang_format.rs
📝 Walkthrough

Walkthrough

The PR updates diff-to-line-range conversion to use 1-based inclusive bounds, adjusts overlap and pure-removal handling to match, and changes test and debug output to reflect the updated line numbering.

Changes

Diff range handling and output

Layer / File(s) Summary
Clang-format range conversion
cpp-linter/src/clang_tools/clang_format.rs
run_clang_format and three_way_diff convert diff hunks to 1-based inclusive ranges and change overlap and pure-removal handling to use the recalculated boundaries.
Clang-format tests and diff tracing
cpp-linter/src/clang_tools/clang_format.rs
The clang_format tests switch to make_patch, update expected ranges, and add a feature-gated print_diff helper that renders unified diff lines with ANSI styling.
File hunk matching and suggestions
cpp-linter/src/common_fs.rs
FileObj::is_hunk_in_diff and get_suggestions use 1-based inclusive line numbers, append_patch adjusts the patch header formatting call, and a pure-removal unit test checks the new suggestion text.
Colored patch output
cpp-linter/tests/comments.rs
The comments test harness prints patch content line by line and colors added and removed lines with ANSI styling.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 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 summarizes the main change: converting diff line indexes to 1-based line numbers.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@codecov

codecov Bot commented Jun 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.38%. Comparing base (4958912) to head (f20e7c7).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #389      +/-   ##
==========================================
+ Coverage   92.26%   92.38%   +0.12%     
==========================================
  Files          23       23              
  Lines        3750     3796      +46     
==========================================
+ Hits         3460     3507      +47     
+ Misses        290      289       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@2bndy5 2bndy5 marked this pull request as ready for review June 27, 2026 09:28
coderabbitai[bot]

This comment was marked as resolved.

@2bndy5 2bndy5 merged commit 339337a into main Jun 27, 2026
73 checks passed
@2bndy5 2bndy5 deleted the diff-line-numbers branch June 27, 2026 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant