From 601547b83da75eb0364dc6edd53b3a569ddca848 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 19 Oct 2022 16:09:22 -0600 Subject: [PATCH] Improved backspace --- examples/editor-test/src/main.rs | 4 ++-- src/buffer.rs | 20 +++++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/examples/editor-test/src/main.rs b/examples/editor-test/src/main.rs index b20897f..c9acc57 100644 --- a/examples/editor-test/src/main.rs +++ b/examples/editor-test/src/main.rs @@ -137,6 +137,8 @@ fn main() { //TODO: support bidi for line in text.lines() { + log::debug!("Line {:?}", line); + for c in line.chars() { if c.is_control() { log::warn!("Ignoring control character {:?}", c); @@ -167,8 +169,6 @@ fn main() { buffer.action(TextAction::Insert(c)); } - log::debug!("Line '{}': {:?}", line, line); - // Test backspace of newline { let cursor = buffer.cursor(); diff --git a/src/buffer.rs b/src/buffer.rs index 393214e..55da9b3 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -473,21 +473,31 @@ impl<'a> TextBuffer<'a> { text_line.remove(glyph.start); self.reshape_line(line.line_i); } else if self.cursor.line > 0 { - self.cursor.glyph = self.layout_lines[self.cursor.line - 1].glyphs.len(); - { let line = &self.layout_lines[self.cursor.line]; - let old_line = self.text_lines.remove(line.line_i.get()); - self.text_lines[line.line_i.get() - 1].push_str(&old_line); + let prev_line = &self.layout_lines[self.cursor.line - 1]; + if prev_line.line_i.get() < line.line_i.get() { + let old_line = self.text_lines.remove(line.line_i.get()); + self.text_lines[prev_line.line_i.get()].push_str(&old_line); + } else { + match prev_line.glyphs.last() { + Some(glyph) => { + let text_line = &mut self.text_lines[line.line_i.get()]; + text_line.remove(glyph.end); + }, + None => (), // There should always be a last glyph + } + } // Reshape all lines after new line //TODO: improve performance - self.shape_lines.truncate(line.line_i.get() - 1); + self.shape_lines.truncate(prev_line.line_i.get()); self.relayout(); self.shape_until_scroll(); } self.cursor.line -= 1; + self.cursor.glyph = self.layout_lines[self.cursor.line].glyphs.len(); let lines = self.lines(); if (self.cursor.line as i32) < self.scroll