Skip to content

Commit

Permalink
fix: copy/paste
Browse files Browse the repository at this point in the history
  • Loading branch information
metiftikci committed Oct 28, 2023
1 parent dc89679 commit 9b7a11d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 6 deletions.
40 changes: 38 additions & 2 deletions src/buffer/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,49 @@ impl Buffer {
}

pub fn paste(&mut self) {
// TODO: Improve performance (add all text instead iterate chars)
if let Some(clipboard) = &self.clipboard {
let mut new_line = false;
for c in clipboard.text.clone().chars() {
match c {
'\n' => self.split_line_cursor(),
ch => self.insert_char_after(ch),
'\n' => {
self.split_line_after();
new_line = true;
}
ch => {
if new_line && self.cursor.x == 0 {
self.insert_char(ch);
self.move_left();
new_line = false;
} else {
self.insert_char_after(ch);
}
}
}
}
}
}
}

#[cfg(test)]
mod test {
use crate::{
buffer::{clipboard::Clipboard, Buffer},
core::{point::Point, size::Size},
};

#[test]
fn paste_test() {
let mut buffer = Buffer::new(Size::new(10, 10).to_rectangle());
buffer.lines.clear();
buffer.lines = vec![String::from("12345"), String::from("67890")];
buffer.cursor = Point::new(1, 2);
buffer.clipboard = Some(Clipboard {
is_line: false,
text: String::from("123\n456\n789"),
});
buffer.paste();
let expected = String::from("12345\n678123\n456\n78990");
assert_eq!(expected, buffer.get_content());
}
}
6 changes: 4 additions & 2 deletions src/buffer/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ impl Buffer {
start: reader.get_cursor_x(),
end: reader.get_cursor_x(),
};
while reader.get_cursor() <= to && !reader.is_line_last_x() {
while !reader.is_line_last_x() && reader.get_cursor() < to {
let _ = reader.next();
highlight.end += 1;
if reader.get_cursor() <= to {
highlight.end += 1;
}
}
list.push(highlight);
let _ = reader.next();
Expand Down
5 changes: 5 additions & 0 deletions src/buffer/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ pub fn get_default_visual_maps() -> ActionMap {
map.insert("k", buffer_action!(move_up));
map.insert("l", buffer_action!(move_right));

map.insert("0", buffer_action!(move_first_column));
map.insert("$", buffer_action!(move_last_column));
map.insert("g", buffer_action!(move_first_line));
map.insert("G", buffer_action!(move_last_line));

map.insert("w", buffer_action!(move_next_word));
map.insert("e", buffer_action!(move_next_word_end));
map.insert("b", buffer_action!(move_previous_word));
Expand Down
4 changes: 4 additions & 0 deletions src/buffer/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ impl Buffer {
self.split_line(self.cursor.y, self.cursor.x).unwrap();
}

pub fn split_line_after(&mut self) {
self.split_line(self.cursor.y, self.cursor.x + 1).unwrap();
}

pub fn join_lines(&mut self, row1: usize, row2: usize) -> Result<(), String> {
let line2 = self.get_line(row2)?.clone();
let line1 = self.get_line_mut(row1)?;
Expand Down
2 changes: 1 addition & 1 deletion src/buffer/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Buffer {
if let Some(ch) = reader.get_char() {
line.push(ch);
}
while !reader.is_line_last_x() && reader.get_cursor() <= to {
while !reader.is_line_last_x() && reader.get_cursor() < to {
if let Some(ch) = reader.next() {
line.push(ch);
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ impl<T: PartialOrd> PartialOrd for Point<T> {
if self.y < other.y {
Some(Ordering::Less)
} else if self.y == other.y && self.x < other.x {
Some(Ordering::Less)
} else if self == other {
Some(Ordering::Equal)
} else {
Some(Ordering::Greater)
Expand Down
4 changes: 3 additions & 1 deletion src/core/text_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl<'a> TextReader<'a> {

pub fn set_cursor(&mut self, cursor: Point<usize>) -> Result<(), String> {
if cursor.y < self.lines.len() {
if self.cursor.x < self.lines.get(cursor.y).unwrap().len() {
if cursor.x <= self.get_line_last_x(cursor.y) {
self.cursor = cursor;
return Ok(());
}
Expand Down Expand Up @@ -127,5 +127,7 @@ mod tests {
assert_eq!(None, reader.next());
assert_eq!(Some('e'), reader.previous());
assert_eq!(Some('d'), reader.previous());

assert_eq!(Ok(()), reader.set_cursor(Point::new(1, 0)));
}
}

0 comments on commit 9b7a11d

Please sign in to comment.