diff options
author | Petter Rodhelind <petter.rodhelind@gmail.com> | 2018-02-23 16:00:38 +0100 |
---|---|---|
committer | Petter Rodhelind <petter.rodhelind@gmail.com> | 2018-02-23 16:00:38 +0100 |
commit | 60e694dff66c92cdc18891e2e389097a740f6a27 (patch) | |
tree | 53277147734a3a658ecd43f185fe31b99c0776b7 | |
parent | 11ab21d930ffff4c091068d584ff087391e9cc66 (diff) | |
download | poe-60e694dff66c92cdc18891e2e389097a740f6a27.tar.gz poe-60e694dff66c92cdc18891e2e389097a740f6a27.tar.bz2 poe-60e694dff66c92cdc18891e2e389097a740f6a27.zip |
Refactor word offset scan and update Select().
-rw-r--r-- | text.go | 63 |
1 files changed, 44 insertions, 19 deletions
@@ -250,33 +250,58 @@ func (t *Text) ExpandDot(q, offset int) { } } -// Select expands current dot into something useful. Most likely a word, but if given pos is adjacent to a quote or bracket character, it tries to select into the matching pair. -func (t *Text) Select(pos int) { - start, end := pos, pos - c, _ := t.buf.ByteAt(start) +// Select expands current dot into something useful. +// +// If given pos is adjacent to a quote, parenthese, curly brace or bracket, it tries to select into the matching pair. +// +// If on newline, select the whole line. +// +// Otherwise, select word (longest alphanumeric sequence). +func (t *Text) Select(offset int) { + offset, _ = t.Seek(offset, io.SeekStart) + start, end := offset, offset // word - for unicode.IsLetter(rune(c)) || unicode.IsDigit(rune(c)) { - start-- - c, _ = t.buf.ByteAt(start) - } - if start < t.q0 { - start++ - } - c, _ = t.buf.ByteAt(end) - for unicode.IsLetter(rune(c)) || unicode.IsDigit(rune(c)) { - end++ - c, _ = t.buf.ByteAt(end) - } + start -= t.PrevWord(start) + end += t.NextWord(end) // return a single char selection if no word was found if start == end { - end++ + t.Seek(offset, io.SeekStart) + _, size, _ := t.ReadRune() + end += size } // Set dot - t.q0 = start - t.q1 = end + t.SetDot(start, end) +} + +func (t *Text) NextWord(offset int) (n int) { + offset, _ = t.Seek(offset, io.SeekStart) + + r, size, _ := t.ReadRune() + for unicode.IsLetter(r) || unicode.IsDigit(r) { + n += size + r, size, _ = t.ReadRune() + } + + return n +} + +func (t *Text) PrevWord(offset int) (n int) { + offset, _ = t.Seek(offset, io.SeekStart) + + r, size, _ := t.ReadRuneAt(offset) + for unicode.IsLetter(r) || unicode.IsDigit(r) { + r, size, _ = t.UnreadRune() + n += size + } + + if n > 0 { + n -= size // remove last iteration + } + + return n } // NextDelim returns number of bytes from given offset up until next delimiter. |