Skip to content

fix: searchcount exception on invalid search regex #2693

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

Merged
merged 2 commits into from
Mar 9, 2024

Conversation

xVermillionx
Copy link
Contributor

Hi there, I encountered an error message generated by vim.fn.searchcount in M.place_cursor_on_node function when having hijack_cursor option enabled.

I don't know if this fix is wanted or not, its just something that bothered me when using vim.fn.searchcount and I noticed it occurring when opening NvimTree.

Using pcall should fix the occurrence of the error message generated by vim.fn.searchcount as showing this error seems to be irrelevant to the M.place_cursor_on_node function:

E54: Unmatched (                                                                                                                                                    
Error detected while processing CursorMoved Autocommands for "NvimTree_*":                                                                                          
Error executing lua callback: Vim:E54: Unmatched (                                                                                                                  
stack traceback:                                                                                                                                                    
        [C]: in function 'searchcount'                                                                                                                              
        ...e/.local/share/nvim/lazy/nvim-tree.lua/lua/nvim-tree.lua:108: in function 'place_cursor_on_node'                                                         
        ...e/.local/share/nvim/lazy/nvim-tree.lua/lua/nvim-tree.lua:236: in function <...e/.local/share/nvim/lazy/nvim-tree.lua/lua/nvim-tree.lua:234>   

To reproduce I just search for ( in my neovim, then open nvim-tree.

  1. /\v(
  2. :NvimTreeOpen

@gegoune
Copy link
Collaborator

gegoune commented Mar 4, 2024

I don't get that error, only E54: Unmatched ( right after the search as one would expect.

@geril2207
Copy link
Collaborator

I can reproduce, this fix seems reasonable

@gegoune
Copy link
Collaborator

gegoune commented Mar 5, 2024

Doesn't wrapping in pcall just hide underlying issue? Perhaps more fundamental fix is needed instead?

@geril2207
Copy link
Collaborator

geril2207 commented Mar 5, 2024

Idea with searchcount was taken from lualine, and they also use searchcount with pcall wrap https://github.com/nvim-lualine/lualine.nvim/blob/master/lua/lualine/components/searchcount.lua#L22

Perhaps more fundamental fix is needed instead?

I think the problem here is error which interrupts the default behaviour, but the behaviour is correct(if we hide the error), isn't it? Are you about adding info log or something like this or more complex changes?

@alex-courtis
Copy link
Member

Idea with searchcount was taken from lualine, and they also use searchcount with pcall wrap https://github.com/nvim-lualine/lualine.nvim/blob/master/lua/lualine/components/searchcount.lua#L22

Looks like I made that change: nvim-lualine/lualine.nvim#1088

Doesn't wrapping in pcall just hide underlying issue? Perhaps more fundamental fix is needed instead?

I reckon this should be classed as a vim bug: the initial search throws the E54: Unmatched ( exception which is correct. Having vim.fn.searchcount throw an error instead of returning the "zero" dictionary goes against the documentation and adds no value.

Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works nicely for hijack_cursor:

  • valid, no match
  • valid, match
  • invalid

@alex-courtis alex-courtis changed the title fix: wrap searchcount in pcall to avoid error fix: searchcount exception on invalid search regex Mar 9, 2024
@alex-courtis alex-courtis merged commit 041dbd1 into nvim-tree:master Mar 9, 2024
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

Successfully merging this pull request may close these issues.

4 participants