chrisgrieser / nvim-spider

Use the w, e, b motions like a spider. Move by subwords and skip insignificant punctuation.
MIT License
663 stars 13 forks source link

Patterns with $ can match before the cursor position #43

Closed vanaigr closed 6 months ago

vanaigr commented 7 months ago

When calculating next match position for patterns that contain $, the result is not checked against the current position. This can lead to cursor moving backwards in some cases:

local s = require("spider")
s.setup { skipInsignificantPunctuation = true }
vim.keymap.set('n', 'w', function() s.motion('w') end)

-- ,,,,,,,,
--   ^ with the cursor here, press w

I originally encountered this issue while trying to add '$' pattern so that the motions stop at the end of the line, and noticed that it didn't work for half of the lines. The punctuationAtEnd = "%f[^%s]%p+$" pattern was matching closing parenthesis at the cursor position, and onTheSamePos check skipped the current match.

It also looks like the braces in one of the README examples should be different https://github.com/chrisgrieser/nvim-spider/blob/828444de406bc7df3b30c8e000ce6f54f0754499/README.md#L188-L194

require("spider").motion("w", {
    customPatterns = {
        patterns = {
            ("%x"):rep(6) .. "+",
        },
        overrideDefault = false,
    }
})
vanaigr commented 7 months ago

Since now the position returned from getNextPosition() is guaranteed to be greater than than current position, I also removed the onTheSamePos check

chrisgrieser commented 6 months ago

It also looks like the braces in one of the README examples should be different

Thanks, fixed.

chrisgrieser commented 6 months ago

Hmmm, I remember onTheSamePos being a fix for some edge cases. But I think your solution might be a bit cleaner. Thanks.

vanaigr commented 6 months ago

Hmmm, I remember onTheSamePos being a fix for some edge cases.

Yes, it was added in 23fa126 as a fix for, I believe, #11.

The situation in the issue is the same as in my example, with punctuationAtEnd matching before/at the cursor position. Since the pattern always matches the position of the first comma, if the cursor is at the first comma, the movement would've kept it in place.