From 832cce753173873ee4cc4949114dcdc3cf7c5f10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6ran=20Karl?= <3951388+JoeKar@users.noreply.github.com> Date: Thu, 8 Feb 2024 23:45:23 +0100 Subject: [PATCH] buffer: Improve cursor movement --- internal/buffer/cursor.go | 17 +++-------------- internal/buffer/line_array.go | 13 ++++--------- 2 files changed, 7 insertions(+), 23 deletions(-) diff --git a/internal/buffer/cursor.go b/internal/buffer/cursor.go index 7c229fd3..8511e7cb 100644 --- a/internal/buffer/cursor.go +++ b/internal/buffer/cursor.go @@ -601,24 +601,13 @@ func (c *Cursor) SubWordLeft() { // RuneUnder returns the rune under the given x position func (c *Cursor) RuneUnder(x int) rune { - line := c.buf.LineBytes(c.Y) - if len(line) == 0 || x >= util.CharacterCount(line) { + line := c.buf.LineCharacters(c.Y) + if len(line) == 0 || x >= len(line) { return '\n' } else if x < 0 { x = 0 } - i := 0 - for len(line) > 0 { - r, _, size := util.DecodeCharacter(line) - line = line[size:] - - if i == x { - return r - } - - i++ - } - return '\n' + return line[x].combc[0] } func (c *Cursor) StoreVisualX() { diff --git a/internal/buffer/line_array.go b/internal/buffer/line_array.go index 9630619c..3c296fff 100644 --- a/internal/buffer/line_array.go +++ b/internal/buffer/line_array.go @@ -371,18 +371,13 @@ func (la *LineArray) End() Loc { return Loc{len(la.lines[numlines-1].runes), numlines - 1} } -// Line returns line n as an array of runes -func (la *LineArray) Line(n int) []rune { +// LineCharacters returns line n as an array of characters +func (la *LineArray) LineCharacters(n int) []Character { if n >= len(la.lines) || n < 0 { - return []rune{} + return []Character{} } - var runes []rune - for _, r := range la.lines[n].runes { - runes = append(runes, r.combc[0:]...) - } - - return runes + return la.lines[n].runes } // LineBytes returns line n as an array of bytes