Skip to content

chore: refactor extract utils#24

Merged
smnatale merged 4 commits intomainfrom
refactor/extract-utils
Apr 17, 2026
Merged

chore: refactor extract utils#24
smnatale merged 4 commits intomainfrom
refactor/extract-utils

Conversation

@smnatale
Copy link
Copy Markdown
Owner

@smnatale smnatale commented Apr 16, 2026

Description

Extract common utils

Screenshots/Images

Summary by CodeRabbit

  • Refactor
    • Consolidated common utilities (file I/O, JSON parsing, notifications, and text formatting) into a centralised module for improved maintainability and consistency across the plugin.
    • Streamlined notification messaging for clearer user feedback.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 16, 2026

Warning

Rate limit exceeded

@smnatale has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 49 minutes and 10 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 49 minutes and 10 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4b7b05c3-d882-4c4f-8535-55fe8a803338

📥 Commits

Reviewing files that changed from the base of the PR and between 11403ce and 53f2989.

📒 Files selected for processing (3)
  • lua/coderabbit/quickfix.lua
  • lua/coderabbit/review.lua
  • tests/coderabbit/utils_spec.lua
📝 Walkthrough

Walkthrough

A new utility module is introduced that provides centralised helpers for JSON decoding, file I/O, notifications, and text pluralisation. Six existing modules (health, history, parser, review, show, storage) are refactored to delegate to these utilities instead of using direct Vim and I/O APIs.

Changes

Cohort / File(s) Summary
Utility Module
lua/coderabbit/utils.lua
New module exporting functions for JSON decoding (json_decode), file I/O (read_file, write_file), notifications (notify), and text pluralisation (pluralize). All functions provide consistent error handling by returning nil or false on failure.
JSON Parsing Refactor
lua/coderabbit/health.lua, lua/coderabbit/parser.lua, lua/coderabbit/storage.lua
Replaced direct pcall(vim.json.decode, ...) calls with utils.json_decode(...). Additionally, storage.lua now uses utils.read_file() and utils.write_file() for file I/O instead of io.open().
Notification & Display Refactor
lua/coderabbit/history.lua, lua/coderabbit/review.lua, lua/coderabbit/show.lua
Replaced vim.notify(...) calls with utils.notify(...) and removed inline "CodeRabbit: " prefixes from messages. Inline pluralisation logic replaced with utils.pluralize(count, word) for consistent formatting.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • PR #3: Modifies notification and fidget messaging in lua/coderabbit/review.lua, which overlaps with this PR's refactoring of review status and completion messages through the new utility notify and pluralise functions.
  • PR #8: Introduces or modifies lua/coderabbit/show.lua functionality that this PR refactors to use the centralised utility notification and pluralisation helpers.
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 55.56% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'chore: refactor extract utils' accurately reflects the main objective of the pull request, which is extracting common utilities into a new utils module and refactoring existing files to use these utilities.

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


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 and usage tips.

@smnatale
Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 17, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (3)
lua/coderabbit/utils.lua (2)

54-56: Nit: pluralize only handles regular -s plurals.

Fine for current callers ("finding"), but callers passing words like "entry" or "match" would yield "entrys" / "matchs". Worth a short doc note or a future plural override parameter if usage expands.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lua/coderabbit/utils.lua` around lines 54 - 56, The pluralize helper
M.pluralize currently only appends "s" and mispluralizes irregular nouns; update
it to accept an optional override parameter (e.g., M.pluralize(n, word,
plural_override)) or a custom pluralizer function so callers can supply the
correct plural form (or a table/map of exceptions) when needed; document the new
signature in the function comment and update callers that might pass words like
"entry" or "match" to provide the correct plural_override or mapping.

31-39: Optional: surface file:write errors more faithfully.

io.open(path, "w") in Lua returns nil, err on failure, and file:write returns the file handle on success or nil, err on failure. The current implementation collapses both to a boolean and silently discards the error message, which can make write failures hard to diagnose (e.g. permission denied, disk full). Consider capturing and returning the error, or at least logging it via utils.notify at ERROR level.

♻️ Optional refactor
 function M.write_file(path, content)
-  local file = io.open(path, "w")
-  if not file then
-    return false
+  local file, open_err = io.open(path, "w")
+  if not file then
+    return false, open_err
   end
-  local ok = file:write(content)
+  local ok, write_err = file:write(content)
   file:close()
-  return ok ~= nil
+  return ok ~= nil, write_err
 end
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lua/coderabbit/utils.lua` around lines 31 - 39, The write_file implementation
(function M.write_file) currently discards error details from io.open and
file:write; update it to capture both open and write errors (io.open returns
nil, err and file:write returns nil, err) and either return them to the caller
or log them via utils.notify at ERROR level; specifically, when io.open fails
include the err in the failure return/log, and when file:write fails capture its
err (close the file if opened) and return/log that error instead of just
true/false so callers can diagnose permission/disk issues.
lua/coderabbit/storage.lua (1)

83-85: Consider surfacing write failures to the user.

M.save silently returns nil when utils.write_file fails (e.g. permission denied, disk full). Callers in review.lua invoke storage.save without checking the return value, so the user currently gets no feedback if a completed review can't be persisted. A utils.notify(..., vim.log.levels.ERROR) on failure would make this debuggable without adding much noise.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lua/coderabbit/storage.lua` around lines 83 - 85, M.save currently returns
nil on write failures without user feedback; update M.save to detect a failed
utils.write_file call, call utils.notify with an ERROR level
(vim.log.levels.ERROR) including contextual details (file path and any error
message returned by utils.write_file if available), and then return nil/false so
callers can react; reference the M.save function, utils.write_file and
utils.notify, and ensure review.lua callers of storage.save will surface the
notification to the user when persistence fails.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@lua/coderabbit/storage.lua`:
- Around line 83-85: M.save currently returns nil on write failures without user
feedback; update M.save to detect a failed utils.write_file call, call
utils.notify with an ERROR level (vim.log.levels.ERROR) including contextual
details (file path and any error message returned by utils.write_file if
available), and then return nil/false so callers can react; reference the M.save
function, utils.write_file and utils.notify, and ensure review.lua callers of
storage.save will surface the notification to the user when persistence fails.

In `@lua/coderabbit/utils.lua`:
- Around line 54-56: The pluralize helper M.pluralize currently only appends "s"
and mispluralizes irregular nouns; update it to accept an optional override
parameter (e.g., M.pluralize(n, word, plural_override)) or a custom pluralizer
function so callers can supply the correct plural form (or a table/map of
exceptions) when needed; document the new signature in the function comment and
update callers that might pass words like "entry" or "match" to provide the
correct plural_override or mapping.
- Around line 31-39: The write_file implementation (function M.write_file)
currently discards error details from io.open and file:write; update it to
capture both open and write errors (io.open returns nil, err and file:write
returns nil, err) and either return them to the caller or log them via
utils.notify at ERROR level; specifically, when io.open fails include the err in
the failure return/log, and when file:write fails capture its err (close the
file if opened) and return/log that error instead of just true/false so callers
can diagnose permission/disk issues.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4465e25e-978f-48b7-83b3-441a58a3c6f0

📥 Commits

Reviewing files that changed from the base of the PR and between 5b0be55 and 11403ce.

📒 Files selected for processing (7)
  • lua/coderabbit/health.lua
  • lua/coderabbit/history.lua
  • lua/coderabbit/parser.lua
  • lua/coderabbit/review.lua
  • lua/coderabbit/show.lua
  • lua/coderabbit/storage.lua
  • lua/coderabbit/utils.lua

@smnatale smnatale merged commit e81bd51 into main Apr 17, 2026
5 checks passed
@smnatale smnatale deleted the refactor/extract-utils branch April 17, 2026 13:03
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