From 9c4d6d4616946bdba5f23b267b812856cd2cf75c Mon Sep 17 00:00:00 2001 From: Kyren223 Date: Sat, 14 Dec 2024 16:23:42 +0200 Subject: Implemented w motion in viminput and fixed a bug with w/W when on the start of the line it'd jump to the next (even if a word in the current line exists) --- internal/client/ui/viminput/viminput.go | 59 ++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) (limited to 'internal/client/ui') diff --git a/internal/client/ui/viminput/viminput.go b/internal/client/ui/viminput/viminput.go index 058e15d..bd78cd6 100644 --- a/internal/client/ui/viminput/viminput.go +++ b/internal/client/ui/viminput/viminput.go @@ -794,7 +794,64 @@ func (m *Model) Motion(motion string) (line, col int) { } // Search next whitespace - i, ok := SearchCharFunc(line, col, 1, unicode.IsSpace) + i, ok := SearchCharFunc(line, col + 1, 1, unicode.IsSpace) + if !ok { + if remember { + return lnum, 0 + } + if isLastLine { + return lnum, len(line) - 1 + } + lnum++ + col = 0 + remember = true + continue + } + + // Search start of next word + i, ok = SearchCharFunc(line, i, 1, func(c rune) bool { + return !unicode.IsSpace(c) + }) + if !ok { + if isLastLine { + return lnum, len(line) - 1 + } + lnum++ + col = 0 + remember = true + continue + } + return lnum, i + } + case "w": + lnum := m.cursorLine + col := m.cursorColumn + remember := false + for { + line := m.lines[lnum] + isLastLine := lnum == len(m.lines)-1 + + // Skip if empty + if len(line) == 0 && !isLastLine { + // What happens if this is the current line + if lnum == m.cursorLine { + lnum++ + col = 0 + remember = true + continue + } + return lnum, 0 + } + + // Search previous non-matching char + if col < 0 || len(line) == 0 { + return lnum, 0 + } + isKeyword := IsKeyword(line[col]) + isWhitespace := unicode.IsSpace(line[col]) + i, ok := SearchCharFunc(line, col + 1, 1, func(c rune) bool { + return unicode.IsSpace(c) || isWhitespace || isKeyword != IsKeyword(c) + }) if !ok { if remember { return lnum, 0 -- cgit v1.3.1