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

Handle Error when no tab is found (E5108) #24

Open
domi413 opened this issue Nov 10, 2024 · 8 comments · May be fixed by #25
Open

Handle Error when no tab is found (E5108) #24

domi413 opened this issue Nov 10, 2024 · 8 comments · May be fixed by #25

Comments

@domi413
Copy link

domi413 commented Nov 10, 2024

I encountered the following error when using the telescope-tabs extension for telescope.nvim:

E5108: Error executing lua: ...e-tabs/lua/telescope/_extensions/telescope-tabs/main.lua:135: attempt to index local 'selection' (a nil value)             
stack traceback:                                                             
        ...e-tabs/lua/telescope/_extensions/telescope-tabs/main.lua:135: in function 'run_replace_or_original'                                            
        ...re/nvim/lazy/telescope.nvim/lua/telescope/actions/mt.lua:65: in function 'key_func'                                                        
        ...hare/nvim/lazy/telescope.nvim/lua/telescope/mappings.lua:293: in function <...hare/nvim/lazy/telescope.nvim/lua/telescope/mappings.lua:292>

This error occurs when I search for a tab that doesn't exist (e.g., typing a string like dddd and pressing Enter).

Is there an option to catch the error, so it only prints a warning like "tab is not available"

I tried to implement the following approach but it don't know how i can catch the error. Any tips?

  if not selection then
    vim.notify("*tab* is not available", vim.log.levels.WARN, {title = "Telescope Tabs"})
    return
  end
@domi413 domi413 changed the title Handle Error in telescope-tabs When No Tab is Found (E5108) Handle Error in when no tab is found (E5108) Nov 10, 2024
@domi413 domi413 changed the title Handle Error in when no tab is found (E5108) Handle Error when no tab is found (E5108) Nov 10, 2024
@LukasPietzschmann
Copy link
Owner

LukasPietzschmann commented Nov 11, 2024

Thanks a lot for filing the issue! Your implementation looks good to me. If you want to submit a pull request, I'm happy to merge your changes. Or I can implement it myself if you prefer not to file a PR. Either way is fine for me :)

@domi413
Copy link
Author

domi413 commented Nov 11, 2024

I took a look at the code, and a simple fix could be applied as follows:

Line 134, main.lua:

if selection then
    vim.api.nvim_set_current_tabpage(selection.value[5])
end

While this prevents the error from occurring, it basically just does nothing when the selection is invalid. A better approach, in my opinion, would be to display an error message like *tab* not available and keep the Telescope preview open. Unfortunately, I haven’t been able to determine how to read the input string, so this was the best workaround I could devise.

@LukasPietzschmann LukasPietzschmann linked a pull request Nov 11, 2024 that will close this issue
@LukasPietzschmann
Copy link
Owner

Alright, I implemented a fix in #25. The only thing that I changed compared to your solution is that the picker does not get closed (that's what most telescope pickers do).

If that's fine for you, I would merge it.

@domi413
Copy link
Author

domi413 commented Nov 11, 2024

Your fix doesn't seem to work on my machine (?) (could also be an issue on my side...)

A fix that works better for me is this approach:

local selection = action_state.get_selected_entry()
if selection then
    actions.close(prompt_bufnr)
    vim.api.nvim_set_current_tabpage(selection.value[5])
else
    vim.notify('No matching tab found', vim.log.levels.WARN)
end

I still have the issue that the else case never gets called, so you can basically just leave the else out...
This fix would prevent the error-message but wouldn't return a message like no matching tab found

@LukasPietzschmann
Copy link
Owner

Your fix doesn't seem to work on my machine

That's strange, 'cause the control flow of our solutions is exactly the same. Do you get an error with mine, or what happens?

I quickly swapped in your code snippet and I could trigger the else branch.

@domi413
Copy link
Author

domi413 commented Nov 11, 2024

https://github.com/domi413/telescope-tabs

I made a fork of your repository. Maybe you can include my fork and try if it works on your machine

@LukasPietzschmann
Copy link
Owner

Yes, your fork works perfectly fine on my machine. If I hit enter after searching for a nonexistent tab, vim.notify gets triggered.

@domi413
Copy link
Author

domi413 commented Nov 11, 2024

This took quite a while... So I'm always using insert-mode inside Telescope and the message only gets printed (at least in my case) when I'm in normal mode

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 a pull request may close this issue.

2 participants