From 3c661e552e8f525d0d5b9c1e8aa3f80812698763 Mon Sep 17 00:00:00 2001 From: bhagwan Date: Wed, 11 Aug 2021 15:26:19 -0700 Subject: [PATCH] refactor and add 'cwd_only' option to all file providers --- README.md | 4 ++-- lua/fzf-lua/config.lua | 1 - lua/fzf-lua/core.lua | 10 ++++++++++ lua/fzf-lua/path.lua | 5 +++++ lua/fzf-lua/providers/buffers.lua | 2 +- lua/fzf-lua/providers/lsp.lua | 31 ++++++++++++++++-------------- lua/fzf-lua/providers/oldfiles.lua | 17 +++++----------- lua/fzf-lua/providers/quickfix.lua | 14 ++++++++------ lua/fzf-lua/utils.lua | 1 + 9 files changed, 49 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 5bd1be7..17362f4 100644 --- a/README.md +++ b/README.md @@ -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, diff --git a/lua/fzf-lua/config.lua b/lua/fzf-lua/config.lua index 70ff69b..5b0780c 100644 --- a/lua/fzf-lua/config.lua +++ b/lua/fzf-lua/config.lua @@ -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> ', diff --git a/lua/fzf-lua/core.lua b/lua/fzf-lua/core.lua index c6300f5..0f54e82 100644 --- a/lua/fzf-lua/core.lua +++ b/lua/fzf-lua/core.lua @@ -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() diff --git a/lua/fzf-lua/path.lua b/lua/fzf-lua/path.lua index 226a5e6..78f33e8 100644 --- a/lua/fzf-lua/path.lua +++ b/lua/fzf-lua/path.lua @@ -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 diff --git a/lua/fzf-lua/providers/buffers.lua b/lua/fzf-lua/providers/buffers.lua index df39b5b..da592ab 100644 --- a/lua/fzf-lua/providers/buffers.lua +++ b/lua/fzf-lua/providers/buffers.lua @@ -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 diff --git a/lua/fzf-lua/providers/lsp.lua b/lua/fzf-lua/providers/lsp.lua index 500bc37..91fc1f0 100644 --- a/lua/fzf-lua/providers/lsp.lua +++ b/lua/fzf-lua/providers/lsp.lua @@ -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 diff --git a/lua/fzf-lua/providers/oldfiles.lua b/lua/fzf-lua/providers/oldfiles.lua index 68ee649..34fcd2c 100644 --- a/lua/fzf-lua/providers/oldfiles.lua +++ b/lua/fzf-lua/providers/oldfiles.lua @@ -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 diff --git a/lua/fzf-lua/providers/quickfix.lua b/lua/fzf-lua/providers/quickfix.lua index 0147cf2..e6e90af 100644 --- a/lua/fzf-lua/providers/quickfix.lua +++ b/lua/fzf-lua/providers/quickfix.lua @@ -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 diff --git a/lua/fzf-lua/utils.lua b/lua/fzf-lua/utils.lua index c1bfbfa..d1dfe88 100644 --- a/lua/fzf-lua/utils.lua +++ b/lua/fzf-lua/utils.lua @@ -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