Skip to content

Commit

Permalink
new line fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacperacy committed Jun 19, 2024
1 parent 2e23845 commit 4957601
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 15 deletions.
9 changes: 3 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 4 additions & 8 deletions src/gap_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()
}
Expand Down

0 comments on commit 4957601

Please sign in to comment.