Skip to content

Commit 10fc1ef

Browse files
committed
Fix search_replace_preview line indexing and update test cases to 1-indexed
- Fix search_replace_preview to convert 1-indexed line numbers to 0-indexed array indices (line_num - 1), consistent with replace_in_file_internal - Update 4 test cases from line: 0 to line: 1 to match 1-indexed convention used by search results and the backend conversion logic
1 parent 20d0ece commit 10fc1ef

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src-tauri/src/search.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -797,11 +797,16 @@ pub async fn search_replace_preview(
797797
let mut preview_lines: Vec<ReplacePreviewLine> = Vec::new();
798798

799799
for (line_num, line_matches) in &matches_by_line {
800-
if (*line_num as usize) >= new_lines.len() {
800+
let line_idx = if *line_num > 0 {
801+
(*line_num - 1) as usize
802+
} else {
803+
0
804+
};
805+
if line_idx >= new_lines.len() {
801806
continue;
802807
}
803808

804-
let original = new_lines[*line_num as usize].clone();
809+
let original = new_lines[line_idx].clone();
805810
let mut line = original.clone();
806811

807812
for m in line_matches {
@@ -821,7 +826,7 @@ pub async fn search_replace_preview(
821826
}
822827
}
823828

824-
new_lines[*line_num as usize] = line.clone();
829+
new_lines[line_idx] = line.clone();
825830

826831
preview_lines.push(ReplacePreviewLine {
827832
line_number: *line_num,
@@ -1212,7 +1217,7 @@ mod tests {
12121217

12131218
let matches = vec![SearchMatch {
12141219
id: "1".to_string(),
1215-
line: 0,
1220+
line: 1,
12161221
column: 6,
12171222
length: 5,
12181223
line_text: "hello world".to_string(),
@@ -1239,7 +1244,7 @@ mod tests {
12391244

12401245
let matches = vec![SearchMatch {
12411246
id: "1".to_string(),
1242-
line: 0,
1247+
line: 1,
12431248
column: 0,
12441249
length: 9,
12451250
line_text: "remove_me here".to_string(),
@@ -1266,7 +1271,7 @@ mod tests {
12661271

12671272
let matches = vec![SearchMatch {
12681273
id: "1".to_string(),
1269-
line: 0,
1274+
line: 1,
12701275
column: 0,
12711276
length: 5,
12721277
line_text: "Hello World".to_string(),
@@ -1334,7 +1339,7 @@ mod tests {
13341339

13351340
let matches = vec![SearchMatch {
13361341
id: "1".to_string(),
1337-
line: 0,
1342+
line: 1,
13381343
column: 7,
13391344
length: 4,
13401345
line_text: "price: $100".to_string(),

0 commit comments

Comments
 (0)