Skip to content

Commit

Permalink
fix: e motion (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
tris203 authored May 17, 2024
1 parent a9a1b1d commit 2feea7e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
21 changes: 13 additions & 8 deletions lua/precognition/horizontal_motions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ end

---@param str string
---@param cursorcol integer
---@param _linelen integer
---@param linelen integer
---@return Precognition.PlaceLoc
function M.end_of_word(str, cursorcol, _linelen)
local len = vim.fn.strcharlen(str)
if cursorcol >= len then
function M.end_of_word(str, cursorcol, linelen)
if cursorcol >= linelen then
return 0
end
local offset = cursorcol
Expand All @@ -69,20 +68,26 @@ function M.end_of_word(str, cursorcol, _linelen)
end

if c_class ~= 0 and next_char_class ~= 0 then
while utils.char_class(char) == c_class and offset <= len do
while utils.char_class(char) == c_class and offset <= linelen do
offset = offset + 1
char = vim.fn.strcharpart(str, offset - 1, 1)
end
end

if c_class == 0 or next_char_class == 0 then
local next_word_start = M.next_word_boundary(str, offset, 0)
local next_word_start = M.next_word_boundary(str, cursorcol, linelen)
if next_word_start then
rev_offset = M.end_of_word(str, next_word_start + 1, 0)
next_char_class = utils.char_class(vim.fn.strcharpart(str, (next_word_start - 1) + 1, 1))
--next word is single char
if next_char_class == 0 then
rev_offset = next_word_start
else
rev_offset = M.end_of_word(str, next_word_start, linelen)
end
end
end

if rev_offset ~= nil and rev_offset <= 0 then
if rev_offset and rev_offset <= 0 then
return 0
end

Expand Down
20 changes: 14 additions & 6 deletions tests/precognition/horizontal_motions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ describe("boundaries", function()
eq(3, hm.end_of_word("abc efg", 2, 7))
eq(7, hm.end_of_word("abc efg", 3, 7))

eq(7, hm.end_of_word("slighly more complex test", 1, 22))
eq(7, hm.end_of_word("slighly more complex test", 2, 22))
eq(12, hm.end_of_word("slighly more complex test", 10, 22))
eq(20, hm.end_of_word("slighly more complex test", 13, 22))
eq(20, hm.end_of_word("slighly more complex test", 15, 22))
eq(25, hm.end_of_word("slighly more complex test", 21, 22))
eq(7, hm.end_of_word("slighly more complex test", 1, 25))
eq(7, hm.end_of_word("slighly more complex test", 2, 25))
eq(12, hm.end_of_word("slighly more complex test", 10, 25))
eq(20, hm.end_of_word("slighly more complex test", 13, 25))
eq(20, hm.end_of_word("slighly more complex test", 15, 25))
eq(25, hm.end_of_word("slighly more complex test", 21, 25))

eq(14, hm.end_of_word(" myFunction(example, stuff)", 1, 30))
eq(14, hm.end_of_word(" myFunction(example, stuff)", 2, 30))
Expand Down Expand Up @@ -175,4 +175,12 @@ describe("edge case", function()
local str = "there is a space "
eq(0, hm.end_of_word(str, 16, #str))
end)

it("single character next word ends", function()
local str = "show_something = true,"
eq(14, hm.end_of_word(str, 1, #str))
eq(16, hm.end_of_word(str, 14, #str))
eq(16, hm.end_of_word(str, 15, #str))
eq(22, hm.end_of_word(str, 21, #str))
end)
end)

0 comments on commit 2feea7e

Please sign in to comment.