feat(file_ignore_patterns): consult the wiki link below

https://github.com/ibhagwan/fzf-lua/wiki#file-ignore-patterns
main
bhagwan 2 years ago
parent ee19eda2e3
commit 4707adc1ec

@ -736,6 +736,19 @@ function M.normalize_opts(opts, defaults)
opts[k] or {}, utils.tbl_deep_clone(M.globals[k]) or {}) opts[k] or {}, utils.tbl_deep_clone(M.globals[k]) or {})
end 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 -- these options are copied from globals unless specifically set
-- also check if we need to override 'opts.prompt' from cli args -- also check if we need to override 'opts.prompt' from cli args
-- if we don't override 'opts.prompt' 'FzfWin.save_query' will -- if we don't override 'opts.prompt' 'FzfWin.save_query' will

@ -426,6 +426,7 @@ M.mt_cmd_wrapper = function(opts)
"color_icons", "color_icons",
"path_shorten", "path_shorten",
"strip_cwd_prefix", "strip_cwd_prefix",
"file_ignore_patterns",
"rg_glob", "rg_glob",
"__module__", "__module__",
} }

@ -42,13 +42,13 @@ local function find_last_newline(str)
end end
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 for i=start_idx or 1,#str do
if string_byte(str, i) == 10 then if string_byte(str, i) == 10 then
return i return i
end end
end end
end ]] end
local function process_kill(pid, signal) local function process_kill(pid, signal)
if not pid or not tonumber(pid) then return false end 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 error_pipe = uv.new_pipe(false)
local write_cb_count = 0 local write_cb_count = 0
local prev_line_content = nil local prev_line_content = nil
-- local num_lines = 0
if opts.fn_transform then fn_transform = opts.fn_transform end 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)
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 -- assert(#data<=66560) -- 65K
write_cb(data:gsub("[^\n]+", write_cb(data:gsub("[^\n]+",
function(x) function(x)
return fn_transform(x) return fn_transform(x)
end)) end))
end end ]]
--[[ local function process_lines(data) local function process_lines(data)
local lines = {}
local start_idx = 1 local start_idx = 1
repeat repeat
num_lines = num_lines + 1
local nl_idx = find_next_newline(data, start_idx) 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 if #line > 1024 then
local msg = line = line:sub(1,1024)
("long line detected, consider adding '--max-columns=512' to ripgrep options:\n %s") -- io.stderr:write(string.format("[Fzf-lua] long line detected (%db), "
:format(utils.strip_ansi_coloring(line):sub(1,60)) -- .. "consider adding '--max-columns=512' to ripgrep options: %s\n",
vim.defer_fn(function() -- #line, line:sub(1,256)))
utils.warn(msg)
end, 0)
line = line:sub(1,512) .. '\n'
end end
write_cb(fn_transform(line)) line = fn_transform(line)
if line then table.insert(lines, line) end
start_idx = nl_idx + 1 start_idx = nl_idx + 1
until start_idx >= #data 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) local read_cb = function(err, data)

@ -303,9 +303,6 @@ M.file = function(x, opts)
local ret = {} local ret = {}
local icon, hl local icon, hl
local file = utils.strip_ansi_coloring(string.match(x, '[^:]*')) 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 if opts.cwd_only and path.starts_with_separator(file) then
local cwd = opts.cwd or vim.loop.cwd() local cwd = opts.cwd or vim.loop.cwd()
if not path.is_relative(file, cwd) then if not path.is_relative(file, cwd) then
@ -331,6 +328,15 @@ M.file = function(x, opts)
if path.starts_with_separator(x) then if path.starts_with_separator(x) then
x = path.HOME_to_tilde(x) x = path.HOME_to_tilde(x)
end 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 if opts.path_shorten then
x = path.shorten(x, tonumber(opts.path_shorten)) x = path.shorten(x, tonumber(opts.path_shorten))
end end

@ -68,6 +68,8 @@ M.status = function(opts)
f1, f2 = f1:match("(.*)%s%->%s(.*)") f1, f2 = f1:match("(.*)%s%->%s(.*)")
end end
f1 = f1 and make_entry.file(f1, opts) 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) f2 = f2 and make_entry.file(f2, opts)
local staged = git_iconify(x:sub(1,1):gsub("?", " ")) local staged = git_iconify(x:sub(1,1):gsub("?", " "))
local unstaged = git_iconify(x:sub(2,2)) local unstaged = git_iconify(x:sub(2,2))

@ -66,6 +66,8 @@ M.oldfiles = function(opts)
end 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"}) opts = core.set_header(opts, opts.headers or {"cwd"})
return core.fzf_exec(contents, opts) return core.fzf_exec(contents, opts)
end end

Loading…
Cancel
Save