From 0d147aa901b26fb6f8ea0e1db827f8762ab16bbb Mon Sep 17 00:00:00 2001 From: Zi-Onn <62586909+hzionn@users.noreply.github.com> Date: Fri, 4 Jul 2025 13:40:40 +0800 Subject: [PATCH] feat: add 'e' motion --- README.md | 1 + src/content.js | 2 ++ src/vim-motions.js | 27 +++++++++++++++++++++++++++ src/vim-state-machine.js | 1 + 4 files changed, 31 insertions(+) diff --git a/README.md b/README.md index fbaffc0..ff9de2d 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ - `w` - Jump forward to the start of the next word - `b` - Jump backward to the start of the previous word +- `e` - Jump to the end of the current word **Editing Commands:** diff --git a/src/content.js b/src/content.js index a479685..a9dc556 100644 --- a/src/content.js +++ b/src/content.js @@ -166,6 +166,7 @@ moveDown, moveWordForward, moveWordBackward, + moveWordEnd, deleteText, deleteWord, changeWord @@ -332,6 +333,7 @@ 'moveDown': () => moveDown(element), 'moveWordForward': () => moveWordForward(element), 'moveWordBackward': () => moveWordBackward(element), + 'moveWordEnd': () => moveWordEnd(element), 'deleteWord': () => deleteWord(element), 'changeWord': () => changeWord(element) }; diff --git a/src/vim-motions.js b/src/vim-motions.js index 857e9f0..d5870f5 100644 --- a/src/vim-motions.js +++ b/src/vim-motions.js @@ -247,6 +247,32 @@ return true; }; + const moveWordEnd = (element) => { + let pos = getCursorPosition(element); + const text = getText(element); + + if (pos >= text.length) return false; + + // Skip initial whitespace to mimic Vim's 'e' + while (pos < text.length && isWhitespace(text[pos])) pos++; + + if (pos >= text.length) { + setCursorPosition(element, text.length); + return true; + } + + const targetType = getCharType(text[pos]); + + if (targetType === 'word') { + while (pos < text.length - 1 && isWordChar(text[pos + 1])) pos++; + } else if (targetType === 'punctuation') { + while (pos < text.length - 1 && isPunctuation(text[pos + 1])) pos++; + } + + setCursorPosition(element, pos); + return true; + }; + // --- Text Modification Functions --- /** @@ -472,6 +498,7 @@ moveDown, moveWordForward, moveWordBackward, + moveWordEnd, deleteText, deleteWord, changeWord diff --git a/src/vim-state-machine.js b/src/vim-state-machine.js index 6d23383..77bffc5 100644 --- a/src/vim-state-machine.js +++ b/src/vim-state-machine.js @@ -39,6 +39,7 @@ 'l': 'moveRight', 'w': 'moveWordForward', 'b': 'moveWordBackward', + 'e': 'moveWordEnd', 'arrowleft': 'moveLeft', 'arrowdown': 'moveDown', 'arrowup': 'moveUp',