nvim-telescope / telescope.nvim

Find, Filter, Preview, Pick. All lua, all the time.
MIT License
15.38k stars 824 forks source link

Support json_decode in entry_maker #523

Open psiska opened 3 years ago

psiska commented 3 years ago

I am trying to create plugin for querying local hoogle database (haskell hoogle). Source can be found at https://github.com/psiska/telescope-hoogle.nvim

Overview:

I've hit several obstacles. 1) entry_maker is supposed to create 1 entry for one line. However I get all entries in one line 2) I cannot decode json via vim.fn.json_decode in entry_maker since I receive E5560 error (cannot use vim function in callback)

Help needed: since I am new to lua and neovim, how should I solve point 2 ?

Solution:

Conni2461 commented 3 years ago

entry_maker gets called for each line. So for example for fd we call entry_maker with each file, which will generate a table with value, display, ordinal etc ... I am not sure how this should work if we first split the contents in the entry_maker because we kinda only can return one item. If i recall correctly we can't return a table of table/items but i need to recheck :)

For the vimL is not allowed in tight lua callback. You can fix that issue if you basically reimplement a json_decode in lua. So you can look at https://github.com/rxi/json.lua e.g. and just ship that file. Its MIT so it shouldn't be a problem.

You could also do something like this which could be way eaiser (similar to some of the git ones and symbols:

I think that should generally work but will block, depending how long hoogle needs to complete that query (i have no idea about that :laughing:)

Conni2461 commented 3 years ago

Okay you can't use finders.new_job because it will process each item/on_stdout line on its own. It was originally designed for ripgrep where each line is a new item. See https://github.com/nvim-telescope/telescope.nvim/blob/3a7fa41857394cd2d90d00891413c12fada039c3/lua/telescope/finders.lua#L63-L73

So you would need to write a new finder similar to JobFinder that allows you to process multiple lines or that JobFinder does the decoding and query entry_maker for each decoded item. I think something like this should be possible.

Feel free to ask more question is something isn't clear either here or in our telescope gitter room :)

psiska commented 3 years ago

Thanks for great pointers and assistance.

This was exactly what I was looking for. And sorry for spamming the issue tracker, I haven't noticed the gitter channel. If I hit problem again I will ask there.

psiska commented 3 years ago

Hello, I have prepared PR which provides utility to achieve the goal in this issue (create multiple results from single line).

How can I help to have PR-681 merged?

There is one linting failure - not from my change. What is expected here? Ignore it since it is not related to PR or fix it?