From 4707adc1ec9c5019590f6070ce578f68ed3a085c Mon Sep 17 00:00:00 2001 From: bhagwan Date: Fri, 8 Jul 2022 16:01:04 -0700 Subject: [PATCH] feat(file_ignore_patterns): consult the wiki link below https://github.com/ibhagwan/fzf-lua/wiki#file-ignore-patterns --- lua/fzf-lua/config.lua | 13 +++++++++ lua/fzf-lua/core.lua | 1 + lua/fzf-lua/libuv.lua | 42 ++++++++++++++++++------------ lua/fzf-lua/make_entry.lua | 12 ++++++--- lua/fzf-lua/providers/git.lua | 2 ++ lua/fzf-lua/providers/oldfiles.lua | 2 ++ 6 files changed, 52 insertions(+), 20 deletions(-) diff --git a/lua/fzf-lua/config.lua b/lua/fzf-lua/config.lua index c1c7de5..2b0abe6 100644 --- a/lua/fzf-lua/config.lua +++ b/lua/fzf-lua/config.lua @@ -736,6 +736,19 @@ function M.normalize_opts(opts, defaults) opts[k] or {}, utils.tbl_deep_clone(M.globals[k]) or {}) end + -- Merge arrays from globals|defaults, can't use 'vim.tbl_xxx' + -- for these as they only work for maps, ie. '{ key = value }' + for _, k in ipairs({ 'file_ignore_patterns' }) do + for _, m in ipairs({ defaults, M.globals }) do + if m[k] then + for _, item in ipairs(m[k]) do + if not opts[k] then opts[k] = {} end + table.insert(opts[k], item) + end + end + end + end + -- these options are copied from globals unless specifically set -- also check if we need to override 'opts.prompt' from cli args -- if we don't override 'opts.prompt' 'FzfWin.save_query' will diff --git a/lua/fzf-lua/core.lua b/lua/fzf-lua/core.lua index 8544cce..6056b7b 100644 --- a/lua/fzf-lua/core.lua +++ b/lua/fzf-lua/core.lua @@ -426,6 +426,7 @@ M.mt_cmd_wrapper = function(opts) "color_icons", "path_shorten", "strip_cwd_prefix", + "file_ignore_patterns", "rg_glob", "__module__", } diff --git a/lua/fzf-lua/libuv.lua b/lua/fzf-lua/libuv.lua index f49f530..e78b345 100644 --- a/lua/fzf-lua/libuv.lua +++ b/lua/fzf-lua/libuv.lua @@ -42,13 +42,13 @@ local function find_last_newline(str) end end ---[[ local function find_next_newline(str, start_idx) +local function find_next_newline(str, start_idx) for i=start_idx or 1,#str do if string_byte(str, i) == 10 then return i end end -end ]] +end local function process_kill(pid, signal) if not pid or not tonumber(pid) then return false end @@ -93,7 +93,6 @@ M.spawn = function(opts, fn_transform, fn_done) local error_pipe = uv.new_pipe(false) local write_cb_count = 0 local prev_line_content = nil - -- local num_lines = 0 if opts.fn_transform then fn_transform = opts.fn_transform end @@ -149,33 +148,42 @@ M.spawn = function(opts, fn_transform, fn_done) end) end - local function process_lines(data) + -- This is the old function used, worked very well + -- but couldn't handle 'fn_transform' nil retval + -- which we need for 'file_ignore_patterns' + --[[ local function process_lines(data) -- assert(#data<=66560) -- 65K write_cb(data:gsub("[^\n]+", function(x) return fn_transform(x) end)) - end + end ]] - --[[ local function process_lines(data) + local function process_lines(data) + local lines = {} local start_idx = 1 repeat - num_lines = num_lines + 1 local nl_idx = find_next_newline(data, start_idx) - local line = data:sub(start_idx, nl_idx) + local line = data:sub(start_idx, nl_idx-1) + -- makes no sense to feed lines + -- bigger than 1024 bytes into fzf if #line > 1024 then - local msg = - ("long line detected, consider adding '--max-columns=512' to ripgrep options:\n %s") - :format(utils.strip_ansi_coloring(line):sub(1,60)) - vim.defer_fn(function() - utils.warn(msg) - end, 0) - line = line:sub(1,512) .. '\n' + line = line:sub(1,1024) + -- io.stderr:write(string.format("[Fzf-lua] long line detected (%db), " + -- .. "consider adding '--max-columns=512' to ripgrep options: %s\n", + -- #line, line:sub(1,256))) end - write_cb(fn_transform(line)) + line = fn_transform(line) + if line then table.insert(lines, line) end start_idx = nl_idx + 1 until start_idx >= #data - end --]] + -- testing shows better performance writing the entire + -- table at once as opposed to calling 'write_cb' for + -- every line after 'fn_transform' + if #lines > 0 then + write_cb(table.concat(lines, "\n").."\n") + end + end local read_cb = function(err, data) diff --git a/lua/fzf-lua/make_entry.lua b/lua/fzf-lua/make_entry.lua index 26a1ebc..7e33d35 100644 --- a/lua/fzf-lua/make_entry.lua +++ b/lua/fzf-lua/make_entry.lua @@ -303,9 +303,6 @@ M.file = function(x, opts) local ret = {} local icon, hl local file = utils.strip_ansi_coloring(string.match(x, '[^:]*')) - -- TODO: this can cause issues with files/grep/live_grep - -- process_lines gsub will replace the entry with nil - -- **low priority as we never use 'cwd_only' with files/grep if opts.cwd_only and path.starts_with_separator(file) then local cwd = opts.cwd or vim.loop.cwd() if not path.is_relative(file, cwd) then @@ -331,6 +328,15 @@ M.file = function(x, opts) if path.starts_with_separator(x) then x = path.HOME_to_tilde(x) end + -- only check for ignored patterns after './' was + -- stripped and path was transformed to relative + if opts.file_ignore_patterns then + for _, pattern in ipairs(opts.file_ignore_patterns) do + if #pattern>0 and x:match(pattern) then + return nil + end + end + end if opts.path_shorten then x = path.shorten(x, tonumber(opts.path_shorten)) end diff --git a/lua/fzf-lua/providers/git.lua b/lua/fzf-lua/providers/git.lua index 17108a9..f0efb69 100644 --- a/lua/fzf-lua/providers/git.lua +++ b/lua/fzf-lua/providers/git.lua @@ -68,6 +68,8 @@ M.status = function(opts) f1, f2 = f1:match("(.*)%s%->%s(.*)") end f1 = f1 and make_entry.file(f1, opts) + -- accomodate 'file_ignore_patterns' + if not f1 then return end f2 = f2 and make_entry.file(f2, opts) local staged = git_iconify(x:sub(1,1):gsub("?", " ")) local unstaged = git_iconify(x:sub(2,2)) diff --git a/lua/fzf-lua/providers/oldfiles.lua b/lua/fzf-lua/providers/oldfiles.lua index d09b4fa..c86f88c 100644 --- a/lua/fzf-lua/providers/oldfiles.lua +++ b/lua/fzf-lua/providers/oldfiles.lua @@ -66,6 +66,8 @@ M.oldfiles = function(opts) end + -- for 'file_ignore_patterns' to work on relative paths + opts.cwd = opts.cwd or vim.loop.cwd() opts = core.set_header(opts, opts.headers or {"cwd"}) return core.fzf_exec(contents, opts) end