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 = { lsp = {
prompt = ' ', prompt = ' ',
-- cwd = vim.loop.cwd(), -- cwd = vim.loop.cwd(),
cwd_only = false, -- workspace diagnostics for cwd only? cwd_only = false, -- LSP/diagnostics for cwd only?
async_or_timeout = true, -- set to timeout|false for blocking calls async_or_timeout = true, -- timeout(ms) or false for blocking calls
file_icons = true, file_icons = true,
git_icons = false, git_icons = false,
lsp_icons = true, lsp_icons = true,

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

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

@ -76,6 +76,11 @@ function M.relative(path, relative_to)
return p return p
end 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) function M.add_trailing(path)
if path:sub(-1) == M.separator() then if path:sub(-1) == M.separator() then
return path return path

@ -53,7 +53,7 @@ M.buffers = function(opts)
if opts.ignore_current_buffer and b == vim.api.nvim_get_current_buf() then if opts.ignore_current_buffer and b == vim.api.nvim_get_current_buf() then
return false return false
end 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 return false
end end
return true return true

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

@ -36,24 +36,17 @@ M.oldfiles = function(opts)
end end
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) opts.fzf_fn = function (cb)
for _, x in ipairs(results) do for _, x in ipairs(results) do
x = core.make_entry_file(opts, x) x = core.make_entry_file(opts, x)
cb(x, function(err) if x then
if err then return end cb(x, function(err)
if err then return end
-- close the pipe to fzf, this -- close the pipe to fzf, this
-- removes the loading indicator in fzf -- removes the loading indicator in fzf
cb(nil, function() end) cb(nil, function() end)
end) end)
end
end end
utils.delayed_cb(cb) utils.delayed_cb(cb)
end end

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

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

Loading…
Cancel
Save