From 49576015293bf91ba825dbbcc9c305d9d38f7ed1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Macio=C5=82ek?= Date: Wed, 19 Jun 2024 20:45:58 +0200 Subject: [PATCH] new line fix --- src/app.rs | 9 +++------ src/gap_buffer.rs | 12 ++++-------- src/ui.rs | 2 +- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/app.rs b/src/app.rs index ae0de22..39a7294 100644 --- a/src/app.rs +++ b/src/app.rs @@ -43,7 +43,7 @@ impl Default for App { fn default() -> Self { Self { running: true, - content: vec![], + content: vec![GapBuffer::new(80)], cursor_position: Position { x: 0, y: 0 }, cursor_offset: Position { x: 0, y: 0 }, opened_filename: String::new(), @@ -187,17 +187,14 @@ impl App { self.push_to_content(GapBuffer::new(GAP_BUFFER_DEFAULT_SIZE)); } - let current_line = &mut self.content[self.cursor_position.y]; + let current_line = &mut self.content[pos.y]; if current_line.len() > pos.x { let new_line = current_line.split_off(pos.x); self.insert_to_content(pos.y + 1, new_line); } else { - self.insert_to_content( - self.cursor_position.y + self.cursor_offset.y + 1, - GapBuffer::new(GAP_BUFFER_DEFAULT_SIZE), - ); + self.insert_to_content(pos.y + 1, GapBuffer::new(GAP_BUFFER_DEFAULT_SIZE)); } self.cursor_position.x = 0; diff --git a/src/gap_buffer.rs b/src/gap_buffer.rs index 2b9a522..9dfb8fe 100644 --- a/src/gap_buffer.rs +++ b/src/gap_buffer.rs @@ -29,17 +29,13 @@ impl GapBuffer { fn resize(&mut self) { let new_capacity = self.buffer.len() * 2; let mut new_buffer = vec![' '; new_capacity]; - let gap_size = self.gap_end - self.gap_start; - if self.gap_start > 0 { - new_buffer[..self.gap_start].copy_from_slice(&self.buffer[..self.gap_start]); - } + new_buffer[..self.gap_start].copy_from_slice(&self.buffer[..self.gap_start]); - if self.gap_end < self.buffer.len() { - new_buffer[new_capacity - gap_size..].copy_from_slice(&self.buffer[self.gap_end..]); - } + let new_gap_end = new_capacity - (self.buffer.len() - self.gap_end); + new_buffer[new_gap_end..].copy_from_slice(&self.buffer[self.gap_end..]); - self.gap_end = new_capacity - gap_size; + self.gap_end = new_gap_end; self.buffer = new_buffer; } diff --git a/src/ui.rs b/src/ui.rs index d9247a2..ab52488 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -36,7 +36,7 @@ pub fn render(app: &mut App, frame: &mut Frame) { } else { (1..=pos.y) .rev() - .chain(std::iter::once(pos.y)) + .chain(std::iter::once(pos.y + 1)) .chain(1..=app.content.len().saturating_sub(pos.y + 1)) .collect::>() }