From 889a841575bd1140b3f24614266cd988bd971655 Mon Sep 17 00:00:00 2001 From: Massimo Mund Date: Tue, 14 May 2024 08:51:13 +0200 Subject: [PATCH] Replaced IsNonAlphaNumeric() with IsNonWordChar() --- internal/action/actions.go | 4 ++-- internal/buffer/autocomplete.go | 8 ++++---- internal/util/util.go | 18 ++++++++++-------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/internal/action/actions.go b/internal/action/actions.go index 883f8208..621cb55b 100644 --- a/internal/action/actions.go +++ b/internal/action/actions.go @@ -745,8 +745,8 @@ func (h *BufPane) Autocomplete() bool { } r := h.Cursor.RuneUnder(h.Cursor.X) prev := h.Cursor.RuneUnder(h.Cursor.X - 1) - if !util.IsAutocomplete(prev) || !util.IsNonAlphaNumeric(r) { - // don't autocomplete if cursor is on alpha numeric character (middle of a word) + if !util.IsAutocomplete(prev) || util.IsWordChar(r) { + // don't autocomplete if cursor is within a word return false } diff --git a/internal/buffer/autocomplete.go b/internal/buffer/autocomplete.go index 7a8c5bee..8a1c3742 100644 --- a/internal/buffer/autocomplete.go +++ b/internal/buffer/autocomplete.go @@ -73,11 +73,11 @@ func (b *Buffer) GetWord() ([]byte, int) { return []byte{}, -1 } - if util.IsNonAlphaNumeric(b.RuneAt(c.Loc.Move(-1, b))) { + if util.IsNonWordChar(b.RuneAt(c.Loc.Move(-1, b))) { return []byte{}, c.X } - args := bytes.FieldsFunc(l, util.IsNonAlphaNumeric) + args := bytes.FieldsFunc(l, util.IsNonWordChar) input := args[len(args)-1] return input, c.X - util.CharacterCount(input) } @@ -166,7 +166,7 @@ func BufferComplete(b *Buffer) ([]string, []string) { var suggestions []string for i := c.Y; i >= 0; i-- { l := b.LineBytes(i) - words := bytes.FieldsFunc(l, util.IsNonAlphaNumeric) + words := bytes.FieldsFunc(l, util.IsNonWordChar) for _, w := range words { if bytes.HasPrefix(w, input) && util.CharacterCount(w) > inputLen { strw := string(w) @@ -179,7 +179,7 @@ func BufferComplete(b *Buffer) ([]string, []string) { } for i := c.Y + 1; i < b.LinesNum(); i++ { l := b.LineBytes(i) - words := bytes.FieldsFunc(l, util.IsNonAlphaNumeric) + words := bytes.FieldsFunc(l, util.IsNonWordChar) for _, w := range words { if bytes.HasPrefix(w, input) && util.CharacterCount(w) > inputLen { strw := string(w) diff --git a/internal/util/util.go b/internal/util/util.go index bebd949b..b7bebcaf 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -218,12 +218,19 @@ func FSize(f *os.File) int64 { return fi.Size() } -// IsWordChar returns whether or not the string is a 'word character' -// Word characters are defined as numbers, letters, or '_' +// IsWordChar returns whether or not a rune is a 'word character' +// Word characters are defined as numbers, letters or '_' func IsWordChar(r rune) bool { return unicode.IsLetter(r) || unicode.IsNumber(r) || r == '_' } +// IsNonWordChar returns whether or not a rune is not a 'word character' +// Non word characters are defined as all characters not being numbers, letters or '_' +// See IsWordChar() +func IsNonWordChar(r rune) bool { + return !IsWordChar(r) +} + // Spaces returns a string with n spaces func Spaces(n int) string { return strings.Repeat(" ", n) @@ -445,14 +452,9 @@ func Clamp(val, min, max int) int { return val } -// IsNonAlphaNumeric returns if the rune is not a number of letter or underscore. -func IsNonAlphaNumeric(c rune) bool { - return !unicode.IsLetter(c) && !unicode.IsNumber(c) && c != '_' -} - // IsAutocomplete returns whether a character should begin an autocompletion. func IsAutocomplete(c rune) bool { - return c == '.' || !IsNonAlphaNumeric(c) + return c == '.' || IsWordChar(c) } // ParseSpecial replaces escaped ts with '\t'.