refactor and add 'cwd_only' option to all file providers

main
bhagwan 3 years ago
parent b25e5bef33
commit 3c661e552e

@ -360,8 +360,8 @@ require'fzf-lua'.setup {
lsp = {
prompt = ' ',
-- cwd = vim.loop.cwd(),
cwd_only = false, -- workspace diagnostics for cwd only?
async_or_timeout = true, -- set to timeout|false for blocking calls
cwd_only = false, -- LSP/diagnostics for cwd only?
async_or_timeout = true, -- timeout(ms) or false for blocking calls
file_icons = true,
git_icons = false,
lsp_icons = true,

@ -304,7 +304,6 @@ M.globals.lsp = {
["Information"] = { icon = "", color = "blue" }, -- info
["Hint"] = { icon = "", color = "magenta" }, -- hint
},
cwd_only = false,
}
M.globals.builtin = {
prompt = 'Builtin> ',

@ -114,6 +114,12 @@ end
M.make_entry_file = function(opts, x)
local icon
local prefix = ''
if opts.cwd_only and path.starts_with_separator(x) then
local cwd = opts.cwd or vim.loop.cwd()
if not path.is_relative(x, cwd) then
return nil
end
end
if opts.cwd and #opts.cwd > 0 then
x = path.relative(x, opts.cwd)
end
@ -174,6 +180,10 @@ M.fzf_files = function(opts)
coroutine.wrap(function ()
if opts.cwd_only and not opts.cwd then
opts.cwd = vim.loop.cwd()
end
if opts.git_icons then
opts.diff_files = get_diff_files()
opts.untracked_files = get_untracked_files()

@ -76,6 +76,11 @@ function M.relative(path, relative_to)
return p
end
function M.is_relative(path, relative_to)
local p = path:match("^" .. M.to_matching_str(M.add_trailing(relative_to)))
return p ~= nil
end
function M.add_trailing(path)
if path:sub(-1) == M.separator() then
return path

@ -53,7 +53,7 @@ M.buffers = function(opts)
if opts.ignore_current_buffer and b == vim.api.nvim_get_current_buf() then
return false
end
if opts.cwd_only and not string.find(vim.api.nvim_buf_get_name(b), vim.loop.cwd(), 1, true) then
if opts.cwd_only and not path.is_relative(vim.api.nvim_buf_get_name(b), vim.loop.cwd()) then
return false
end
return true

@ -32,9 +32,11 @@ local function location_handler(opts, cb, _, result)
for _, entry in ipairs(items) do
entry = core.make_entry_lcol(opts, entry)
entry = core.make_entry_file(opts, entry)
cb(entry, function(err)
if err then return end
end)
if entry then
cb(entry, function(err)
if err then return end
end)
end
end
end
@ -47,9 +49,11 @@ local function symbol_handler(opts, cb, _, result)
end
entry = core.make_entry_lcol(opts, entry)
entry = core.make_entry_file(opts, entry)
cb(entry, function(err)
if err then return end
end)
if entry then
cb(entry, function(err)
if err then return end
end)
end
end
end
@ -70,6 +74,7 @@ local function diagnostics_handler(opts, cb, _, entry)
local type = entry.type
entry = core.make_entry_lcol(opts, entry)
entry = core.make_entry_file(opts, entry)
if not entry then return end
if opts.lsp_icons and opts.cfg.icons[type] then
local severity = opts.cfg.icons[type]
local icon = severity.icon
@ -424,14 +429,12 @@ M.diagnostics = function(opts)
end
end
for bufnr, diags in pairs(buffer_diags) do
if not opts.cwd_only or (opts.cwd_only and string.find(vim.api.nvim_buf_get_name(bufnr), vim.loop.cwd(), 1, true) ) then
for _, diag in ipairs(diags) do
-- workspace diagnostics may include empty tables for unused bufnr
if not vim.tbl_isempty(diag) then
if filter_diag_severity(opts, diag.severity) then
diagnostics_handler(opts, cb, co,
preprocess_diag(diag, bufnr))
end
for _, diag in ipairs(diags) do
-- workspace diagnostics may include empty tables for unused bufnr
if not vim.tbl_isempty(diag) then
if filter_diag_severity(opts, diag.severity) then
diagnostics_handler(opts, cb, co,
preprocess_diag(diag, bufnr))
end
end
end

@ -36,24 +36,17 @@ M.oldfiles = function(opts)
end
end
if opts.cwd_only then
opts.cwd = vim.loop.cwd()
local cwd = opts.cwd
cwd = cwd:gsub([[\]],[[\\]])
results = vim.tbl_filter(function(file)
return vim.fn.matchstrpos(file, cwd)[2] ~= -1
end, results)
end
opts.fzf_fn = function (cb)
for _, x in ipairs(results) do
x = core.make_entry_file(opts, x)
cb(x, function(err)
if err then return end
if x then
cb(x, function(err)
if err then return end
-- close the pipe to fzf, this
-- removes the loading indicator in fzf
cb(nil, function() end)
end)
end)
end
end
utils.delayed_cb(cb)
end

@ -25,12 +25,14 @@ local quickfix_run = function(opts, cfg, locations)
opts.fzf_fn = function (cb)
for _, x in ipairs(results) do
x = core.make_entry_file(opts, x)
cb(x, function(err)
if err then return end
-- close the pipe to fzf, this
-- removes the loading indicator in fzf
cb(nil, function() end)
end)
if x then
cb(x, function(err)
if err then return end
-- close the pipe to fzf, this
-- removes the loading indicator in fzf
cb(nil, function() end)
end)
end
end
utils.delayed_cb(cb)
end

@ -50,6 +50,7 @@ function M.shell_error()
end
function M.is_git_repo()
-- can also use: "git rev-parse is-inside-work-tree"
vim.fn.system("git rev-parse --git-dir")
return M._if(M.shell_error(), false, true)
end

Loading…
Cancel
Save