Improvements for enter and cursor positioning

This commit is contained in:
Jeremy Soller 2022-10-21 11:51:04 -06:00
parent 784215e490
commit 88b78e059c
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE

View file

@ -432,8 +432,14 @@ impl<'a> TextBuffer<'a> {
self.shape_until_cursor(CursorScroll::Bottom); self.shape_until_cursor(CursorScroll::Bottom);
}, },
TextAction::Enter => { TextAction::Enter => {
let new_line = {
let line = &mut self.lines[self.cursor.line.get()];
line.reset();
line.text.split_off(self.cursor.index)
};
let next_line = self.cursor.line.get() + 1; let next_line = self.cursor.line.get() + 1;
self.lines.insert(next_line, TextBufferLine::new(String::new())); self.lines.insert(next_line, TextBufferLine::new(new_line));
self.cursor.line = TextLineIndex::new(next_line); self.cursor.line = TextLineIndex::new(next_line);
self.cursor.index = 0; self.cursor.index = 0;
@ -630,15 +636,24 @@ impl<'a> TextBuffer<'a> {
} }
let cursor_glyph_opt = |cursor: &TextCursor| -> Option<usize> { let cursor_glyph_opt = |cursor: &TextCursor| -> Option<usize> {
let mut glyph_i_opt = None; if cursor.line.get() == line_i {
for (glyph_i, glyph) in layout_line.glyphs.iter().enumerate() { for (glyph_i, glyph) in layout_line.glyphs.iter().enumerate() {
if cursor.index == glyph.start { if cursor.index == glyph.start {
glyph_i_opt = Some(glyph_i); return Some(glyph_i);
} else if cursor.index == glyph.end {
glyph_i_opt = Some(glyph_i + 1);
} }
} }
glyph_i_opt match layout_line.glyphs.last() {
Some(glyph) => {
if cursor.index == glyph.end {
return Some(layout_line.glyphs.len());
}
},
None => {
return Some(0);
}
}
}
None
}; };
// Highlight selection (TODO: HIGHLIGHT COLOR!) // Highlight selection (TODO: HIGHLIGHT COLOR!)
@ -736,7 +751,6 @@ impl<'a> TextBuffer<'a> {
} }
// Draw cursor // Draw cursor
if self.cursor.line.get() == line_i {
if let Some(cursor_glyph) = cursor_glyph_opt(&self.cursor) { if let Some(cursor_glyph) = cursor_glyph_opt(&self.cursor) {
let x = match layout_line.glyphs.get(cursor_glyph) { let x = match layout_line.glyphs.get(cursor_glyph) {
Some(glyph) => { Some(glyph) => {
@ -773,7 +787,6 @@ impl<'a> TextBuffer<'a> {
color, color,
); );
} }
}
layout_line.draw(color, |x, y, color| { layout_line.draw(color, |x, y, color| {
f(x, line_y + y, 1, 1, color); f(x, line_y + y, 1, 1, color);