Skip to content

Commit

Permalink
fix: 🐛 Several matches text in one line
Browse files Browse the repository at this point in the history
When using the text replacer, and there is several matches text in one line, only the first one will be replaced (all should be replaced).
  • Loading branch information
BHznJNs committed Sep 19, 2023
1 parent 95acf61 commit 016ed25
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
25 changes: 25 additions & 0 deletions src/editor/core/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ impl EditorLine {
return Ok(());
}

pub fn find_all(&self, pat: &str) -> Option<Vec<usize>> {
let mut text = self.content();
let mut pos_offset = 0;
let mut result_vec = vec![];
while let Some(pos) = text.find(pat) {
result_vec.push(pos + pos_offset);
pos_offset += pos + pat.len();
text = &text[(pos + pat.len())..];
}

if result_vec.is_empty() {
return None;
} else {
return Some(result_vec);
}
}

#[inline]
fn update_label_width(&mut self, new_width: usize) {
self.text_area.margin_left = new_width;
Expand Down Expand Up @@ -102,3 +119,11 @@ impl EditorLine {
self.text_area.len()
}
}

#[test]
fn editorline_find_all_test() {
let mut line = EditorLine::new(0);
line.push_str("abc abc abc");

assert_eq!(line.find_all("abc"), Some(vec![0, 5, 10]));
}
24 changes: 12 additions & 12 deletions src/editor/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,18 +608,18 @@ impl Editor {
}

fn search(&self, target: &str) -> Option<Vec<EditorCursorPos>> {
let result_pos_list: Vec<EditorCursorPos> = self
.lines
.iter()
.enumerate()
.filter_map(|(i, l)| match l.content().find(target) {
Some(pos) => Some(EditorCursorPos {
row: i + 1,
col: pos + 1,
}),
None => None,
})
.collect();
let mut result_pos_list = Vec::<EditorCursorPos>::new();
for (index, line) in self.lines.iter().enumerate() {
match line.find_all(target) {
Some(pos_list) => for pos in pos_list {
result_pos_list.push(EditorCursorPos {
row: index + 1,
col: pos + 1,
})
}
None => {}
}
}

if !result_pos_list.is_empty() {
return Some(result_pos_list);
Expand Down

0 comments on commit 016ed25

Please sign in to comment.