Skip to content

Commit 46956cb

Browse files
committed
Improve diff ranges and context merging
1 parent 97fe9d3 commit 46956cb

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/core/context.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,24 @@ impl ContextFetcher {
4141
if full_path.exists() {
4242
let content = tokio::fs::read_to_string(&full_path).await?;
4343
let file_lines: Vec<&str> = content.lines().collect();
44+
let merged_ranges = merge_ranges(lines);
4445

45-
for (start, end) in lines {
46+
for (start, end) in merged_ranges {
47+
if file_lines.is_empty() {
48+
break;
49+
}
50+
let start = start.max(1);
51+
let end = end.max(start);
4652
let start_idx = start.saturating_sub(1);
47-
let end_idx = (*end).min(file_lines.len());
53+
let end_idx = end.min(file_lines.len());
4854

4955
if start_idx < file_lines.len() {
5056
let chunk_content = file_lines[start_idx..end_idx].join("\n");
5157
chunks.push(LLMContextChunk {
5258
file_path: file_path.clone(),
5359
content: chunk_content,
5460
context_type: ContextType::FileContent,
55-
line_range: Some((*start, *end)),
61+
line_range: Some((start, end)),
5662
});
5763
}
5864
}
@@ -164,3 +170,26 @@ impl ContextFetcher {
164170
Ok(chunks)
165171
}
166172
}
173+
174+
fn merge_ranges(lines: &[(usize, usize)]) -> Vec<(usize, usize)> {
175+
if lines.is_empty() {
176+
return Vec::new();
177+
}
178+
179+
let mut ranges = lines.to_vec();
180+
ranges.sort_by_key(|(start, _)| *start);
181+
182+
let mut merged: Vec<(usize, usize)> = Vec::new();
183+
for (start, end) in ranges {
184+
let end = end.max(start);
185+
if let Some(last) = merged.last_mut() {
186+
if start <= last.1.saturating_add(1) {
187+
last.1 = last.1.max(end);
188+
continue;
189+
}
190+
}
191+
merged.push((start, end));
192+
}
193+
194+
merged
195+
}

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ async fn review_command(
288288
&diff
289289
.hunks
290290
.iter()
291-
.map(|h| (h.new_start, h.new_start + h.new_lines))
291+
.map(|h| (h.new_start, h.new_start + h.new_lines.saturating_sub(1)))
292292
.collect::<Vec<_>>(),
293293
)
294294
.await?;
@@ -801,7 +801,7 @@ async fn review_diff_content_raw(
801801
&diff
802802
.hunks
803803
.iter()
804-
.map(|h| (h.new_start, h.new_start + h.new_lines))
804+
.map(|h| (h.new_start, h.new_start + h.new_lines.saturating_sub(1)))
805805
.collect::<Vec<_>>(),
806806
)
807807
.await?;
@@ -1232,7 +1232,7 @@ async fn smart_review_command(
12321232
&diff
12331233
.hunks
12341234
.iter()
1235-
.map(|h| (h.new_start, h.new_start + h.new_lines))
1235+
.map(|h| (h.new_start, h.new_start + h.new_lines.saturating_sub(1)))
12361236
.collect::<Vec<_>>(),
12371237
)
12381238
.await?;

0 commit comments

Comments
 (0)