lsp_live_workspace_symbols rework, better API for live commands

main
bhagwan 3 years ago
parent f4e531b08a
commit c876f8315a

@ -296,22 +296,17 @@ M.fzf_files = function(opts)
end
M.set_fzf_interactive_cmd = function(opts)
M.fzf_files_interactive = function(opts)
opts = opts or config.normalize_opts(opts, config.globals.files)
if not opts then return end
-- cannot be nil
local query = opts._live_query or ''
-- fzf already adds single quotes around the placeholder when expanding
-- for skim we surround it with double quotes or single quote searches fail
local placeholder = utils._if(opts._is_skim, '"{}"', '{q}')
local uv = vim.loop
local raw_async_act = require("fzf.actions").raw_async_action(function(pipe, args)
local shell_cmd = opts._cb_live_cmd(args[1])
local shell_cmd = opts._reload_get_cmd(args[1])
local output_pipe = uv.new_pipe(false)
local error_pipe = uv.new_pipe(false)
local read_cb_count = 0
@ -370,7 +365,49 @@ M.fzf_files_interactive = function(opts)
error_pipe:read_start(read_cb)
end, placeholder)
local act_cmd = raw_async_act
return M.set_fzf_interactive(opts, raw_async_act, placeholder)
end
M.set_fzf_interactive_cb = function(opts)
if not opts then return end
-- fzf already adds single quotes around the placeholder when expanding
-- for skim we surround it with double quotes or single quote searches fail
local placeholder = utils._if(opts._is_skim, '"{}"', '{q}')
local uv = vim.loop
local raw_async_act = require("fzf.actions").raw_async_action(function(pipe, args)
local results = opts._reload_action(args[1])
local close_pipe = function()
if pipe and not uv.is_closing(pipe) then
uv.close(pipe)
pipe = nil
end
end
for _, entry in ipairs(results) do
uv.write(pipe, entry .. "\n", function(err)
if err then
close_pipe()
end
end)
end
close_pipe()
end, placeholder)
return M.set_fzf_interactive(opts, raw_async_act, placeholder)
end
M.set_fzf_interactive = function(opts, act_cmd, placeholder)
if not opts or not act_cmd or not placeholder then return end
-- cannot be nil
local query = opts.query or ''
if opts._is_skim then
-- do not run an empty string query unless the user requested
@ -411,8 +448,9 @@ M.fzf_files_interactive = function(opts)
opts.git_icons = false
opts.file_icons = false
opts = M.set_fzf_line_args(opts)
M.fzf_files(opts)
return opts
end
return M

@ -136,7 +136,7 @@ M.live_grep = function(opts)
opts.search = last_search.query
end
opts._live_query = opts.search or ''
opts.query = opts.search or ''
if opts.search and #opts.search>0 then
-- save the search query so the use can
-- call the same search again
@ -145,15 +145,15 @@ M.live_grep = function(opts)
last_search.query = opts.search
-- escape unless the user requested not to
if not (no_esc or opts.no_esc) then
opts._live_query = utils.rg_escape(opts.search)
opts.query = utils.rg_escape(opts.search)
end
end
-- search query in header line
opts = set_search_header(opts, 2)
opts._cb_live_cmd = function(query)
if query and #query>0 and not opts.do_not_save_last_search then
opts._reload_get_cmd = function(query)
if query and not opts.do_not_save_last_search then
last_search = {}
last_search.no_esc = true
last_search.query = query
@ -167,7 +167,9 @@ M.live_grep = function(opts)
return get_grep_cmd(opts, query, true)
end
core.fzf_files_interactive(opts)
opts = core.set_fzf_line_args(opts)
opts = core.set_fzf_interactive_cmd(opts)
core.fzf_files(opts)
end
M.live_grep_native = function(opts)

@ -506,98 +506,51 @@ M.workspace_diagnostics = function(opts)
return M.diagnostics(opts)
end
local live_workspace_symbols_sk = function(opts)
local last_search = {}
-- "'{}'" opens sk with an empty search_query showing all files
-- "{}" opens sk without executing an empty string query
-- the problem is the latter doesn't support escaped chars
-- TODO: how to open without a query with special char support
local sk_args = string.format("%s '{}'", opts._action)
M.live_workspace_symbols = function(opts)
opts = normalize_lsp_opts(opts, config.globals.lsp)
if not opts then return end
opts._fzf_cli_args = string.format("--cmd-prompt='%s' -i -c %s",
opts.prompt,
vim.fn.shellescape(sk_args))
-- exec empty query is the default here
if opts.exec_empty_query == nil then
opts.exec_empty_query = true
end
opts.git_icons = false
opts.file_icons = false
opts.fzf_fn = nil --function(_) end
opts = core.set_fzf_line_args(opts)
core.fzf_files(opts)
opts.search = nil
end
if not opts.query
and opts.continue_last_search ~= false
and opts.repeat_last_search ~= false then
opts.query = last_search.query
end
M.live_workspace_symbols = function(opts)
if opts.query and #opts.query>0 then
-- save the search query so the use can
-- call the same search again
last_search = {}
last_search.query = opts.search
end
opts = normalize_lsp_opts(opts, config.globals.lsp)
if not opts then return end
opts.lsp_params = {query = ''}
-- sent to the LSP server
opts.lsp_params = {query = opts.query or ''}
-- must get those here, otherwise we get the
-- fzf terminal buffer and window IDs
opts.bufnr = vim.api.nvim_get_current_buf()
opts.winid = vim.api.nvim_get_current_win()
local raw_act = raw_action(function(args)
opts._reload_action = function(query)
if query and not opts.do_not_save_last_search then
last_search = {}
last_search.query = query
end
opts.sync = true
opts.async = false
opts.lsp_params = {query = args[2] or ''}
opts.lsp_params = {query = query or ''}
opts = set_lsp_fzf_fn(opts)
return opts.fzf_fn
end)
local raw_async_act = raw_async_action(function(pipe, args)
coroutine.wrap(function ()
local co = coroutine.running()
local cb = function(item)
-- only close the pipe when nil is sent
if not item then
print("closing pipe")
uv.close(pipe)
coroutine.resume(co)
else
print(item)
uv.write(pipe, item, function(err)
assert(not err)
end)
end
end
opts.sync = false
opts.async = true
opts.lsp_params = {query = args[2] or ''}
opts = set_lsp_fzf_fn(opts)
opts.fzf_fn(cb)
-- wait for the request to be completed
coroutine.yield()
end)()
end)
-- TODO: async not working properly
-- use sync as default action
local act_lsp = raw_act
if opts.async or opts.sync == false then
act_lsp = raw_async_act
end
-- skim (rust version of fzf)
if opts._is_skim then
opts._action = act_lsp
return live_workspace_symbols_sk(opts)
end
-- use {q} as a placeholder for fzf
local reload_command = act_lsp .. " {q}"
opts._fzf_cli_args = string.format('--phony --query="" --bind=%s',
vim.fn.shellescape(string.format("change:reload:%s", reload_command)))
-- TODO:
-- this is not getting called past the initial command
-- until we fix that we cannot use icons as they interfere
-- with the extension parsing
opts.git_icons = false
opts.file_icons = false
opts.fzf_fn = {}
opts = core.set_fzf_interactive_cb(opts)
opts = core.set_fzf_line_args(opts)
core.fzf_files(opts)
opts.search = nil

Loading…
Cancel
Save