-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e38dac0
commit 26062ea
Showing
19 changed files
with
326 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
use crate::{ | ||
buffer::{ | ||
highlight::{Highlight, HL_FIND_TEXT}, | ||
Buffer, | ||
}, | ||
core::text_position::TextPosition, | ||
}; | ||
|
||
impl Buffer { | ||
pub fn find(&mut self, text: &str) { | ||
self.clear_highlight(HL_FIND_TEXT); | ||
self.finds.clear(); | ||
|
||
for row in 0..self.get_line_count() { | ||
let line = self.get_line(row).clone(); | ||
if text.len() <= line.len() { | ||
let column_end = line.len() - text.len() + 1; | ||
for column in 0..column_end { | ||
if &line[column..(column + text.len())] == text { | ||
self.finds.push(TextPosition { | ||
row, | ||
start: column, | ||
end: column + text.len() - 1, | ||
}); | ||
self.highlights.push(Highlight { | ||
name: HL_FIND_TEXT, | ||
row, | ||
start: column, | ||
end: column + text.len() - 1, | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn clear_finds(&mut self) { | ||
self.finds.clear(); | ||
self.clear_highlight(HL_FIND_TEXT); | ||
} | ||
|
||
pub fn move_to_next_find(&mut self) { | ||
if self.finds.len() == 0 { | ||
return; | ||
} | ||
|
||
let mut pos: Option<TextPosition> = None; | ||
for find in self.finds.iter() { | ||
if find.row < self.cursor.y { | ||
continue; | ||
} else if find.row == self.cursor.y && find.start <= self.cursor.x { | ||
continue; | ||
} else { | ||
pos = Some(find.clone()); | ||
break; | ||
} | ||
} | ||
if let Some(p) = pos { | ||
self.move_cursor(p.row, p.start); | ||
} else { | ||
let p = self.finds.get(0).unwrap(); | ||
self.move_cursor(p.row, p.start); | ||
} | ||
} | ||
|
||
pub fn move_to_previous_find(&mut self) { | ||
if self.finds.len() == 0 { | ||
return; | ||
} | ||
|
||
let mut pos: Option<TextPosition> = None; | ||
for find in self.finds.iter().rev() { | ||
if self.cursor.y < find.row { | ||
continue; | ||
} else if find.row == self.cursor.y && self.cursor.x <= find.start { | ||
continue; | ||
} else { | ||
pos = Some(find.clone()); | ||
break; | ||
} | ||
} | ||
if let Some(p) = pos { | ||
self.move_cursor(p.row, p.start); | ||
} else { | ||
let p = self.finds.last().unwrap(); | ||
self.move_cursor(p.row, p.start); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{buffer::Buffer, core::rectangle::Rectangle}; | ||
|
||
#[test] | ||
fn test() { | ||
let mut buffer = Buffer::new(Rectangle { | ||
x: 0, | ||
y: 0, | ||
width: 10, | ||
height: 10, | ||
}); | ||
|
||
buffer.lines.clear(); | ||
buffer.lines.push(String::from("123 567 90")); | ||
buffer.lines.push(String::from("qwer yuiio")); | ||
buffer.lines.push(String::from("")); | ||
buffer.lines.push(String::from("1 3 56")); | ||
buffer.lines.push(String::from("")); | ||
|
||
buffer.find("56"); | ||
|
||
assert_eq!(2, buffer.finds.len()); | ||
assert_eq!(0, buffer.finds.get(0).unwrap().row); | ||
assert_eq!(4, buffer.finds.get(0).unwrap().start); | ||
assert_eq!(5, buffer.finds.get(0).unwrap().end); | ||
assert_eq!(3, buffer.finds.get(1).unwrap().row); | ||
assert_eq!(5, buffer.finds.get(1).unwrap().start); | ||
assert_eq!(6, buffer.finds.get(1).unwrap().end); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.