Skip to content

Commit

Permalink
fix(horizontal_motions): handle quoted strings in word boundaries (#68)
Browse files Browse the repository at this point in the history
* fix(horizontal_motions): handle quoted strings in word boundaries

This commit improves the handling of quoted strings in word boundaries.
The `end_of_word` function now correctly identifies the end of a word
when the next word starts with punctuation or is a single character.
A new test case has been added to verify this behavior.

* fix(horizontal_motions): handle punctuation in end_of_word

This commit modifies the `end_of_word` function in `horizontal_motions.lua`
to handle cases where the next word starts with punctuation. This change
ensures that the function correctly identifies the end of a word in such
scenarios. Corresponding tests have been added in
`horizontal_motions_spec.lua` to verify this behavior.
  • Loading branch information
tris203 authored Jun 14, 2024
1 parent 9e6087f commit 7a76d43
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lua/precognition/horizontal_motions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,13 @@ function M.end_of_word(str, cursorcol, linelen, big_word)
if c_class == cc.whitespace or next_char_class == cc.whitespace then
local next_word_start = M.next_word_boundary(str, cursorcol, linelen, big_word)
if next_word_start then
c_class = utils.char_class(vim.fn.strcharpart(str, next_word_start - 1, 1), big_word)
next_char_class = utils.char_class(vim.fn.strcharpart(str, (next_word_start - 1) + 1, 1), big_word)
--next word is single char
if next_char_class == cc.whitespace then
--next word is single char
rev_offset = next_word_start
elseif c_class == cc.punctuation and next_char_class ~= cc.punctuation then
--next word starts with punctuation
rev_offset = next_word_start
else
rev_offset = M.end_of_word(str, next_word_start, linelen, big_word)
Expand Down
8 changes: 8 additions & 0 deletions tests/precognition/horizontal_motions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,12 @@ describe("edge case", function()
eq(3, hm.prev_word_boundary(str, 18, len, true))
eq(3, hm.prev_word_boundary(str, 5, len, false))
end)

it("quoted strings", function()
local str = 'this = "that"'
eq(8, hm.end_of_word(str, 6, #str, false))

str = 'b = "^", c = 2 },'
eq(8, hm.end_of_word(str, 3, #str, false))
end)
end)

0 comments on commit 7a76d43

Please sign in to comment.