feat(readFile): add lines range option#23
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Warning Rate limit exceeded
To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the 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 configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughThis PR implements a new ChangesLine Range Reading Feature
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Review rate limit: 0/1 reviews remaining, refill in 11 minutes and 15 seconds.Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
__test__/read_file.spec.ts (1)
69-113: ⚡ Quick winAdd a CRLF fixture for the new
linesoption.All of the new cases use LF-only text, so they won't catch the carriage-return bug in
apply_line_range. A Windows-style fixture will lock down the cross-platform behavior here.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@__test__/read_file.spec.ts` around lines 69 - 113, Existing tests only use LF-only multilineFixture and won't detect CRLF handling in apply_line_range; add a Windows-style CRLF fixture (e.g., multilineFixtureCRLF) and a small test that uses readFile with lines option (use the same cases as one of the existing tests like "should read a line range" and "should ignore line range in Buffer mode") to assert that apply_line_range correctly handles '\r\n' line endings; reference the readFile call in the tests and the apply_line_range function so you add the CRLF fixture alongside multilineFixture and use it in one or two new test cases mirroring the LF tests to lock down cross-platform behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/read_file.rs`:
- Around line 95-110: The current apply_line_range uses contents.split('\n')
which leaves CR characters on Windows and creates an extra empty line for
trailing newlines; replace the splitter with contents.lines() to get
newline-aware, cross-platform 1-based semantics. In apply_line_range, collect
lines with contents.lines().collect::<Vec<_>>(), use lines.len() (usize) for
total_lines, compute from as (range.from.saturating_sub(1)) as usize, compute to
as (range.to.min(total_lines as u32)) as usize (or convert range.to to usize
after min), and return lines[from..to].join("\n") so ranges map correctly for
both Unix and CRLF inputs (function: apply_line_range; type: LineRange).
---
Nitpick comments:
In `@__test__/read_file.spec.ts`:
- Around line 69-113: Existing tests only use LF-only multilineFixture and won't
detect CRLF handling in apply_line_range; add a Windows-style CRLF fixture
(e.g., multilineFixtureCRLF) and a small test that uses readFile with lines
option (use the same cases as one of the existing tests like "should read a line
range" and "should ignore line range in Buffer mode") to assert that
apply_line_range correctly handles '\r\n' line endings; reference the readFile
call in the tests and the apply_line_range function so you add the CRLF fixture
alongside multilineFixture and use it in one or two new test cases mirroring the
LF tests to lock down cross-platform behavior.
🪄 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: defaults
Review profile: CHILL
Plan: Pro
Run ID: d6f3edf6-4c5b-4d91-8959-fa3960c0d2ce
📒 Files selected for processing (3)
__test__/read_file.spec.tsindex.d.tssrc/read_file.rs
Add support for reading specific line ranges via readFile API:
await readFile('./test.ts', { lines: { from: 110, to: 120 } })
Implementation uses BufReader for streaming line-by-line reading,
avoiding loading the entire file into memory. This makes it suitable
for large files (GB-scale) — memory usage is O(target lines) instead
of O(file size).
Features:
- 1-based inclusive line range selection
- Streaming via BufReader (64KB buffer) — O(1) memory overhead
- Graceful handling of out-of-bounds ranges (empty string)
- Auto-clamping when 'to' exceeds total lines
- Buffer mode (no encoding) ignores lines option
- TypeScript type definitions updated
Edge Cases Handled:
| Scenario | Behavior |
|----------|----------|
| from < 1 | Returns empty string |
| from > total lines | Returns empty string |
| to > total lines | Clamped to last line |
| from == to | Returns single line |
| Buffer mode | lines ignored, full buffer returned |
Closes issue #21
6afb469 to
b703ee7
Compare
Pre-existing oxlint warning fix.
Summary
Closes #21 — adds support for reading specific line ranges via the
readFileAPI.API
Implementation
LineRangestruct withfromandtofields (1-based, inclusive)ReadFileOptionswith optionallinesfieldapply_line_rangehelper handles slicing, clamping, and edge caseslinesoptionEdge Cases Handled
from < 1from > total linesto > total linesfrom == tolinesignored, full buffer returnedTests
5 new test cases, all passing:
Notes
Current implementation reads the full file then filters by line range. This is sufficient for most AI-coding scenarios. A streaming/seek-based optimization can be explored in a future PR for very large files.
Summary by CodeRabbit
linesparameter toreadFilefor reading specific line ranges from files. Configurefromandtoline numbers to extract only needed portions instead of reading entire file contents. Enables efficient processing of large files by limiting data transfers to requested line ranges.