folke / trouble.nvim

🚦 A pretty diagnostics, references, telescope results, quickfix and location list to help you solve all the trouble your code is causing.
Apache License 2.0
5.24k stars 178 forks source link

feature: on_list handler #342

Closed 9999years closed 2 months ago

9999years commented 9 months ago

Did you check the docs?

Is your feature request related to a problem? Please describe.

When I use Neovim LSP features like "go to definition" or "go to references", sometimes there's only one result that gets jumped to immediately, and sometimes there's a list of results, presented to the user as a qflist. I'd like to use Trouble instead of the qflist. Many of the Neovim LSP functions can take an optional on_list handler like so:

vim.lsp.buf.definition{on_list=on_list}

There's built-in modes in Trouble for some of these, but not vim.lsp.buf.declaration or vim.lsp.buf.implementation, and there's nothing in the documented API for opening Trouble with a given set of results.

Describe the solution you'd like

I'd like a function analogous to trouble.providers.telescope.open_with_trouble, named something like trouble.providers.lsp.on_list, which can be used as an on_list callback.

Ideally, if there's only one item in the list, that item will be opened directly, and if there's multiple items a Trouble window will be opened.

Describe alternatives you've considered

I could build this function myself if there was a way to populate a Trouble window from a list of results, but the only interface appears to be the trouble.open and trouble.toggle functions, both of which just take a mode string rather than a structured list of items.

I think it would be a good idea to have a trouble.open_items function or similar, even if I suspect an on_list callback is a useful enough feature that it's worth shipping on its own.

I can also build this feature myself (hackily), by first setting the qflist or loclist from the items in the on_list handler and then opening Trouble in the qflist or loclist modes. Unfortunately, this will open Trouble even if there's one (or worse, zero) results, so it leaves something to be desired. It also feels awkward that I have to use the loclist as an intermediate buffer rather than being able to convert into Trouble's desired representation myself.