Skip to content

refactor(tool): use structured udiff hunks for diff metadata#145

Merged
omarluq merged 2 commits into
mainfrom
chore/diffs
Jun 20, 2026
Merged

refactor(tool): use structured udiff hunks for diff metadata#145
omarluq merged 2 commits into
mainfrom
chore/diffs

Conversation

@omarluq

@omarluq omarluq commented Jun 20, 2026

Copy link
Copy Markdown
Owner

No description provided.

@coderabbitai

coderabbitai Bot commented Jun 20, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: be7db77e-5e2c-450d-9321-d28d22f56b9c

📥 Commits

Reviewing files that changed from the base of the PR and between cd3be74 and 09b83b2.

📒 Files selected for processing (1)
  • internal/terminal/tool_blocks.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal/terminal/tool_blocks.go

📝 Walkthrough

Summary by CodeRabbit

  • Refactor
    • Updated tool edit diff rendering to use standardized unified diffs with configurable context, producing cleaner combined output across multiple edits.
    • Improved diff generation internals to compute first-changed line numbers from unified-diff hunks rather than parsing diff text.
  • Tests
    • Expanded diff-related internal coverage, including verification of inserted-line reporting and first-changed line behavior.

Walkthrough

Two diff-rendering implementations are replaced with structured go-udiff API usage. tool_blocks.go drops manual -/+ prefix writing in favor of per-edit unified diffs via a new diffFromEditArgumentTexts helper. diff.go removes string-parsing helpers for hunk headers and rewrites firstChangedLineFromUnifiedDiff to iterate udiff.UnifiedDiff hunks and line kinds directly.

Changes

Unified diff refactor using go-udiff structured API

Layer / File(s) Summary
tool_blocks: replace manual prefix diff with udiff helper
internal/terminal/tool_blocks.go
Adds go-udiff import and toolArgumentDiffContext constant. Rewrites diffFromEditArguments to delegate per-edit diff generation to new diffFromEditArgumentTexts, which calls udiff.Lines + udiff.ToUnifiedDiff and returns a trimmed unified diff string. Removes old writePrefixedDiffLines logic.
diff.go: switch to udiff.ToUnifiedDiff and structured hunk iteration
internal/tool/diff.go, internal/tool/diff_internal_test.go
generateDiffString switches from udiff.ToUnified to udiff.ToUnifiedDiff; truncation operates on diff.String(). firstChangedLineFromUnifiedDiff is rewritten to iterate udiff.UnifiedDiff hunks and line kinds, removing all string-parsing helpers (parseUnifiedHunkStart, parseUnifiedRange, normalizeUnifiedLineNumber) and strconv/strings imports. Test removes unifiedHunkStartCase struct and adds an insertion-detection case.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • omarluq/librecode#96: Directly related — rewrites internal/tool/diff.go to use go-udiff, establishing the unified diff pipeline that this PR further refactors.

Poem

🐇 Hoppity hop through the diff lines I go,
No more parsing @@ headers — what a show!
Structured hunks now tell me where changes reside,
udiff.Insert and Delete are my trusty guide.
The old string-splitter's gone, hooray!
Clean unified diffs are here to stay. ✨

🚥 Pre-merge checks | ✅ 3 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Description check ❓ Inconclusive No description was provided by the author, making it impossible to assess relatedness to the changeset. Add a pull request description explaining the motivation, approach, and benefits of switching to structured udiff hunks.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main refactoring change: replacing custom diff parsing logic with structured udiff hunks for extracting diff metadata.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch chore/diffs

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

@codecov

codecov Bot commented Jun 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.75758% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.43%. Comparing base (651ae84) to head (09b83b2).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
internal/terminal/tool_blocks.go 68.42% 3 Missing and 3 partials ⚠️
internal/tool/diff.go 85.71% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #145      +/-   ##
==========================================
- Coverage   80.45%   80.43%   -0.03%     
==========================================
  Files         277      277              
  Lines       21925    21888      -37     
==========================================
- Hits        17640    17605      -35     
- Misses       3074     3075       +1     
+ Partials     1211     1208       -3     
Flag Coverage Δ
unittests 80.43% <75.75%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.

@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

🤖 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 `@internal/terminal/tool_blocks.go`:
- Around line 571-575: The builder.WriteByte and builder.WriteString calls in
this block are not checking their error return values, which violates the coding
guidelines requiring explicit error handling with errcheck enabled. Capture the
error return values from both the builder.WriteByte('\n') call on line 572 and
the builder.WriteString(diff) call on line 575, then add appropriate error
handling to check and respond to these potential write failures rather than
dropping the returned errors.
🪄 Autofix (Beta)

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: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3e34180b-1068-4cca-8a30-d24e91d116e5

📥 Commits

Reviewing files that changed from the base of the PR and between 651ae84 and cd3be74.

📒 Files selected for processing (3)
  • internal/terminal/tool_blocks.go
  • internal/tool/diff.go
  • internal/tool/diff_internal_test.go

Comment thread internal/terminal/tool_blocks.go Outdated
@omarluq omarluq merged commit bbf3bf2 into main Jun 20, 2026
11 of 13 checks passed
@omarluq omarluq deleted the chore/diffs branch June 20, 2026 07:09
@sonarqubecloud

Copy link
Copy Markdown

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