Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Limit highlights to beginning or end of word #211

Open
userrand opened this issue Mar 19, 2024 · 4 comments
Open

[Feature Request] Limit highlights to beginning or end of word #211

userrand opened this issue Mar 19, 2024 · 4 comments

Comments

@userrand
Copy link

Thank you for this great plugin. It might be the best one I have tried for quick movements (after using easymotion, hop and flash).

To increase the likelihood that the first match is the one we would like the cursor to get at, one can consider reducing the number of matches by imposing that all matches should be at the begining ( or end) of the word.

@ggandor
Copy link
Owner

ggandor commented Mar 24, 2024

You're thinking about having separate mappings for these "modes", or simply using Leap with this restriction?

By the way, it's easy to try out. Just tweak the resulting pattern here in the source:

local pattern = search["prepare-pattern"](in1, _3fin2)

E.g.: local pattern = '\\<' .. search["prepare-pattern"](in1, _3fin2), and Leap will only find matches at the beginnings of words.

My experience is that it doesn't help much, on average it reduces the number of matches by ~50% or even less... and we lose the ability to jump anywhere.

@userrand
Copy link
Author

@ggandor Nice your suggestion worked thank you .

I am not sure I understood the \\< at the beginning of the pattern. Could one modify that to include other word boundaries like a point . for python or a directory separator / ? That is, for words to be considered sequences of text separated by whitespace, certain punctuation marks, or / ?

For the initial question, yes, I meant the option to replace the default behavior of leap to only match the beginning of words.

I am using the lazy plugin manager and I modified the file
.local/share/nvim/lazy/leap.nvim/lua/leap/main.lua , I am not sure if that is what I was supposed to do.

I will continue to try this code modification as I think I usually just need to jump to beginning of words most of the time.

Thank you again.

@ggandor
Copy link
Owner

ggandor commented Mar 27, 2024

I am not sure I understood the \< at the beginning of the pattern. Could one modify that to include other word boundaries like a point . for python or a directory separator / ? That is, for words to be considered sequences of text separated by whitespace, certain punctuation marks, or / ?

See :help pattern-atoms, :h \<, etc. What Vim sees as a "word" depends on the iskeyword option, it is usually set together with the filetype.

If you simply want to match beginnings of whitespace-separated words, there's no atom for that, like \<, but you can prefix the pattern with \\(\\^\\|\\s\\)\\zs. Without all the escaping, it's: (^|\s)\zs - meaning, before the match itself (:help \zs), there should be a whitespace character or the very beginning of line. We need to escape every special character, because Leap internally converts the pattern to "very nomagic" (:h \V), and on top of that, escape the backslashes themselves, because we're not writing this on the command line, but passing a Lua string to the search function - vim.fn.search -, and we want to send literal \s as part of the pattern).

Could one modify that to include other word boundaries like a point . for python or a directory separator /

Add those to the parenthesized OR-group, e.g., whitespace + dot: \\(\\^\\|\\s\\|\\.\\)\\zs ((^|\s|\.)\zs).

I am using the lazy plugin manager and I modified the file
.local/share/nvim/lazy/leap.nvim/lua/leap/main.lua , I am not sure if that is what I was supposed to do.

Yep. Of course, you should do this manually after every upgrade, if you've grown to like this hack...

@userrand
Copy link
Author

userrand commented Mar 28, 2024

@ggandor

Thank you very much for all of your help :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants