Skip to content

Commit

Permalink
fix(fzf): support preview for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhwang91 committed Jan 4, 2022
1 parent f7842f8 commit 50dae9a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 38 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ So why not nvim-bqf?
- [fzf](https://github.com/junegunn/fzf) (optional, 0.24.0 later)
- [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter) (optional)

> Preview with fzf needs a pipe, Windows can't be supported. It must be stated that
> I'm not working under Windows.
### Installation

Install with [Packer.nvim](https://github.com/wbthomason/packer.nvim):
Expand Down
81 changes: 46 additions & 35 deletions lua/bqf/filter/fzf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ local phandler, qhandler, base, config, qfs
local utils = require('bqf.utils')
local log = require('bqf.log')

local action_for, extra_opts, can_preview, is_windows
local action_for, extra_opts, is_windows
local version
local headless

Expand Down Expand Up @@ -225,35 +225,44 @@ local function handler(qwinid, lines)
end
end

local function new_job(qwinid, tmpfile)
io.open(tmpfile, 'w'):close()
local stdout = uv.new_pipe(false)
local handle, pid
handle, pid = uv.spawn('tail', {args = {'-f', tmpfile}, stdio = {nil, stdout}}, function()
handle:close()
os.remove(tmpfile)
end)
stdout:read_start(function(err, data)
local function watch_file(qwinid, tmpfile)
local fd
if is_windows then
-- two processes can't write same file meanwhile in Windows :(
io.open(tmpfile, 'w'):close()
fd = assert(uv.fs_open(tmpfile, 'r', 438))
else
fd = assert(uv.fs_open(tmpfile, 'w+', 438))
end
local watch_ev = assert(uv.new_fs_event())
local function release()
-- watch_ev:stop and :close have the same effect
watch_ev:close(function(err)
assert(not err, err)
end)
uv.fs_close(fd, function(err)
assert(not err, err)
end)
end
watch_ev:start(tmpfile, {}, function(err, filename, events)
assert(not err, err)
if data then
if data ~= '' then
local tbl_data = vim.split(data, ',')
local idx
while #tbl_data > 0 and not idx do
idx = tonumber(table.remove(tbl_data))
end
local _ = filename
if events.change then
uv.fs_read(fd, 4 * 1024, -1, function(err2, data)
assert(not err2, err2)
local idx = is_windows and tonumber(data:match('%d+')) or tonumber(data)
if idx and idx > 0 then
vim.schedule(function()
set_qf_cursor(qwinid, idx)
phandler.open(qwinid, idx)
end)
end
end
end)
else
stdout:close()
release()
end
end)
return pid
return release
end

function M.headless_run(hl_ansi, padding_nr)
Expand All @@ -266,7 +275,7 @@ function M.headless_run(hl_ansi, padding_nr)
end
end

function M.prepare(qwinid, pid, size)
function M.pre_handle(qwinid, size)
local line_count = api.nvim_buf_line_count(0)
api.nvim_win_set_config(0, {
relative = 'win',
Expand All @@ -293,10 +302,12 @@ function M.prepare(qwinid, pid, size)
end, size > 1000 and 100 or 50)
end

if pid then
cmd('aug BqfFilterFzf')
cmd(('au BufWipeout <buffer> %s'):format(('lua vim.loop.kill(%d, 15)'):format(pid)))
cmd('aug END')
if M.post_handle then
cmd([[
aug BqfFilterFzf')
au BufWipeout <buffer> lua require('bqf.filter.fzf').post_handle()
aug END
]])
end
end

Expand Down Expand Up @@ -342,19 +353,20 @@ function M.run()
}
}

local pid
if can_preview then
if phandler.auto_enabled() then
local tmpfile = fn.tempname()
vim.list_extend(opts.options,
{'--preview-window', 0, '--preview', 'echo -n {1}, >> ' .. tmpfile})
pid = new_job(qwinid, tmpfile)
phandler.keep_preview()
if phandler.auto_enabled() then
local tmpfile = fn.tempname()
vim.list_extend(opts.options,
{'--preview-window', 0, '--preview', 'echo {1} >> ' .. tmpfile})
local release_cb = watch_file(qwinid, tmpfile)
M.post_handle = function()
release_cb()
M.post_handle = nil
end
phandler.keep_preview()
end

cmd(('au BqfFilterFzf FileType fzf ++once %s'):format(
([[lua require('bqf.filter.fzf').prepare(%d, %s, %d)]]):format(qwinid, tostring(pid), size)))
([[lua require('bqf.filter.fzf').pre_handle(%d, %d)]]):format(qwinid, size)))

fn.BqfFzfWrapper(opts)
end
Expand Down Expand Up @@ -382,7 +394,6 @@ local function init()
qhandler = require('bqf.qfwin.handler')
base = require('bqf.filter.base')
qfs = require('bqf.qfwin.session')
can_preview = fn.executable('tail') == 1 and fn.executable('echo') == 1
is_windows = utils.is_windows()

cmd([[
Expand Down

0 comments on commit 50dae9a

Please sign in to comment.