-
Notifications
You must be signed in to change notification settings - Fork 16
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
Implement display function to allow custom result rendering #47
base: main
Are you sure you want to change the base?
Conversation
@wbthomason I noticed same thing too (issue #1 in your above list) when adding a commit to this PR 564a9b7 Might need to check for nil in there somewhere? perhaps local icon = get_icon(filename, extension)
if icon ~= nil
return icon .. ' ' .. meta_result.result
else
return meta_result.result |
Fantastic, I can iterate on the issues you pointed out, or point you in the right direction when I get some time. I just landed #42 and so will be soon be ready for some fresh features to land. |
The attempted approach to caching results was broken because display is expected to return a string, not a meta-result. Thus, when display was applied to a string, it created but did not return a new meta-result table, only returning nil strings. This could be fixed pretty easily by changing the use of display to expect a meta-result, but I've opted to leave that as a possible future optimization.
I've fixed most of the issues with this PR, and have an updated, mostly working icon consumer example: local snap = require 'snap'
local icons = require 'nvim-web-devicons'
require('nvim-web-devicons').setup { default = true }
local fnamemodify = vim.fn.fnamemodify
local get_icon = icons.get_icon
local function add_icon(meta_result)
local filename = fnamemodify(meta_result.result, ':t:r')
local extension = fnamemodify(meta_result.result, ':e')
local icon = get_icon(filename, extension)
return icon .. ' ' .. meta_result.result
end
local function icons_consumer(producer)
return function(request)
for results in snap.consume(producer, request) do
if type(results) == 'table' then
if not vim.tbl_islist(results) then
coroutine.yield(results)
else
coroutine.yield(vim.tbl_map(function(result)
return snap.with_meta(result, 'display', add_icon)
end, results))
end
else
coroutine.yield(nil)
end
end
end
end
snap.register.map(
'n',
'<Leader>s',
snap.create(function()
return {
prompt = 'Files',
producer = icons_consumer(snap.get 'consumer.fzy'(snap.get 'producer.git.file')),
select = snap.get('select.file').select,
multiselect = snap.get('select.file').multiselect,
views = { snap.get 'preview.file' },
}
end)
) We could be more efficient by applying the consumer to the initial producer, since @camspiers, any chance you have time to take a look at the highlighting issue at some point? That's the last blocking problem for this PR - I know now where the highlight positions are calculated, but I'm not sure where the best place to update them for the displayed string is (I also don't know where the highlights get applied). |
I think will have some time tonight or tomorrow night to have a look, thanks so much for sticking with this, and apologies for not getting back to it as soon as I hoped. |
Not a problem! I absolutely understand not getting to open source things as quickly as one hopes to... (see: this PR, several PRs and issues on the |
@camspiers I might have some time to work on the last remaining issue for this PR (highlight offsets) in the next few days - any chance you could point me toward what I need to modify to shift the start/end of the match highlight ranges? |
Actually, nevermind - I figured it out! local snap = require 'snap'
local icons = require 'nvim-web-devicons'
require('nvim-web-devicons').setup { default = true }
local fnamemodify = vim.fn.fnamemodify
local get_icon = icons.get_icon
local function add_icon(meta_result)
local filename = fnamemodify(meta_result.result, ':t:r')
local extension = fnamemodify(meta_result.result, ':e')
local icon = get_icon(filename, extension)
return icon .. ' ' .. meta_result.result
end
local function icons_consumer(producer)
return function(request)
for results in snap.consume(producer, request) do
if type(results) == 'table' then
if not vim.tbl_islist(results) then
coroutine.yield(results)
else
coroutine.yield(vim.tbl_map(function(result)
return snap.with_metas(result, { display = add_icon, highlight_offset = 4 })
end, results))
end
else
coroutine.yield(nil)
end
end
end
end I think this PR is ready except for (1) docs and (2) deciding if we want to ship the icons consumer by default or just provide it as an example. |
Hmm, one thing I just found: if this icons consumer is applied to something with a lot of results (e.g. 10000 |
@camspiers Note that, despite that last comment, I think this PR is good to review (as you have time). |
Adds special handling for the
display
meta-field, which is an optional function returning a string from aMetaResult
, and which is used to render a result for display in the selection window.This PR is WIP. It mostly works, e.g. with the snippet:
However:
display
fieldicons_consumer
function in the above example is not quite right - sometimes thefzy
producer yields a table{continue = true}
, and yielding this again causes an error.@camspiers: if you have a chance to take a look at this, I'd appreciate guidance on how the
fzy
producer works to fix the second and third items above. If you have any suggestions for why the first item may be happening, that'd be great too, but I haven't dug into that issue yet.Closes #46.