Skip to content

Commit

Permalink
refactor: retire complex windows escaping logic
Browse files Browse the repository at this point in the history
refactor: libuv.shellescape (+added tests)
refactor: path module (+added tests)
refactor: fzf `--multi` option
refactor: normalize shell escaping of `fzf_opts`
refactor: multiprocess cmd args serialization
chore: lua_ls warnings, format luarc.json with prettier
feat(win): added support for listing files with `dir /s/b/a:-d`
fix: path shorten/lengthen
fix: git commits|bcommits
fix: git_diff previewer
fix: profiles, help_tags
fix: cwd header test
fix(make_entry): cwd_only check
fix(path_shorten): do not shorten drive spec
fix(path): `HOME_to_tilde` case insensitive on windows
fix(live_grep): with `exec_empty_query={true|false}`
fix(tags): ctags adjustments for windows
fix(complete): path/file windows adjustments
fix: unesacpe fzf's {q}
  • Loading branch information
ibhagwan committed Feb 11, 2024
1 parent e8777aa commit a038250
Show file tree
Hide file tree
Showing 38 changed files with 1,865 additions and 709 deletions.
25 changes: 7 additions & 18 deletions .luarc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,24 @@
"runtime.version": "LuaJIT",
"diagnostics": {
"enable": true,
"globals": [
"vim",
"describe",
"pending",
"it",
"before_each",
"after_each"
],
"globals": ["vim"],
"neededFileStatus": {
"codestyle-check": "Any"
},
"disable": [
"need-check-nil",
"missing-parameter",
"cast-local-type"
]
"disable": ["need-check-nil", "missing-parameter", "cast-local-type"]
},
"workspace": {
"library": [
"$VIMRUNTIME/lua",
"lua",
"${3rd}/luv/library"
"$VIMRUNTIME/lua",
"${3rd}/luv/library",
"$XDG_DATA_HOME/nvim/lazy/plenary.nvim/lua",
"$LOCALAPPDATA/nvim-data/lazy/plenary.nvim/lua"
],
"checkThirdParty": false,
"maxPreload": 2000,
"preloadFileSize": 1000,
"ignoreDir": [
"tests/"
]
"ignoreDir": ["tests/"]
},
"type": {
"weakNilCheck": true,
Expand Down
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -639,9 +639,9 @@ require'fzf-lua'.setup {
fzf_opts = {
-- options are sent as `<left>=<right>`
-- set to `false` to remove a flag
-- set to '' for a non-value flag
-- set to `true` for a no-value flag
-- for raw args use `fzf_args` instead
["--ansi"] = "",
["--ansi"] = true,
["--info"] = "inline",
["--height"] = "100%",
["--layout"] = "reverse",
Expand Down Expand Up @@ -878,7 +878,6 @@ require'fzf-lua'.setup {
.. " %(subject) %(color:blue)%(taggername)%(color:reset)' refs/tags",
preview = "git log --graph --color --pretty=format:'%C(yellow)%h%Creset "
.. "%Cgreen(%><(12)%cr%><|(12))%Creset %s %C(blue)<%an>%Creset' {1}",
fzf_opts = { ["--no-multi"] = "" },
actions = { ["default"] = actions.git_checkout },
},
stash = {
Expand All @@ -889,10 +888,6 @@ require'fzf-lua'.setup {
["default"] = actions.git_stash_apply,
["ctrl-x"] = { fn = actions.git_stash_drop, reload = true },
},
fzf_opts = {
["--no-multi"] = '',
['--delimiter'] = "'[:]'",
},
},
icons = {
["M"] = { icon = "M", color = "yellow" },
Expand Down Expand Up @@ -988,7 +983,7 @@ require'fzf-lua'.setup {
},
fzf_opts = {
-- hide tabnr
['--delimiter'] = "'[\\):]'",
["--delimiter"] = "[\\):]",
["--with-nth"] = '2..',
},
},
Expand All @@ -1001,7 +996,7 @@ require'fzf-lua'.setup {
fzf_opts = {
-- do not include bufnr in fuzzy matching
-- tiebreak by line no.
['--delimiter'] = "'[\\]:]'",
["--delimiter"] = "[\\]:]",
["--nth"] = '2..',
["--tiebreak"] = 'index',
["--tabstop"] = "1",
Expand All @@ -1021,7 +1016,7 @@ require'fzf-lua'.setup {
-- start = "cursor" -- start display from cursor?
fzf_opts = {
-- hide filename, tiebreak by line no.
["--delimiter"] = "'[:]'",
["--delimiter"] = "[:]",
["--with-nth"] = '2..',
["--tiebreak"] = 'index',
["--tabstop"] = "1",
Expand Down
17 changes: 6 additions & 11 deletions doc/fzf-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -732,9 +732,9 @@ open an issue and I'll be more than happy to help.**
fzf_opts = {
-- options are sent as `<left>=<right>`
-- set to `false` to remove a flag
-- set to '' for a non-value flag
-- set to `true` for a no-value flag
-- for raw args use `fzf_args` instead
["--ansi"] = "",
["--ansi"] = true,
["--info"] = "inline",
["--height"] = "100%",
["--layout"] = "reverse",
Expand Down Expand Up @@ -971,7 +971,6 @@ open an issue and I'll be more than happy to help.**
.. " %(subject) %(color:blue)%(taggername)%(color:reset)' refs/tags",
preview = "git log --graph --color --pretty=format:'%C(yellow)%h%Creset "
.. "%Cgreen(%><(12)%cr%><|(12))%Creset %s %C(blue)<%an>%Creset' {1}",
fzf_opts = { ["--no-multi"] = "" },
actions = { ["default"] = actions.git_checkout },
},
stash = {
Expand All @@ -982,10 +981,6 @@ open an issue and I'll be more than happy to help.**
["default"] = actions.git_stash_apply,
["ctrl-x"] = { fn = actions.git_stash_drop, reload = true },
},
fzf_opts = {
["--no-multi"] = '',
['--delimiter'] = "'[:]'",
},
},
icons = {
["M"] = { icon = "M", color = "yellow" },
Expand Down Expand Up @@ -1081,7 +1076,7 @@ open an issue and I'll be more than happy to help.**
},
fzf_opts = {
-- hide tabnr
['--delimiter'] = "'[\\):]'",
["--delimiter"] = "[\\):]",
["--with-nth"] = '2..',
},
},
Expand All @@ -1094,7 +1089,7 @@ open an issue and I'll be more than happy to help.**
fzf_opts = {
-- do not include bufnr in fuzzy matching
-- tiebreak by line no.
['--delimiter'] = "'[\\]:]'",
["--delimiter"] = "[\\]:]",
["--nth"] = '2..',
["--tiebreak"] = 'index',
["--tabstop"] = "1",
Expand All @@ -1114,7 +1109,7 @@ open an issue and I'll be more than happy to help.**
-- start = "cursor" -- start display from cursor?
fzf_opts = {
-- hide filename, tiebreak by line no.
["--delimiter"] = "'[:]'",
["--delimiter"] = "[:]",
["--with-nth"] = '2..',
["--tiebreak"] = 'index',
["--tabstop"] = "1",
Expand Down Expand Up @@ -1441,4 +1436,4 @@ I missed your name feel free to contact me and I'll add it below:
as baseline for the builtin previewer and his must have plugin nvim-bqf
<https://github.com/kevinhwang91/nvim-bqf>

vim:tw=78:ts=8:ft=help:norl:
vim:tw=78:ts=8:ft=help:norl:
17 changes: 9 additions & 8 deletions lua/fzf-lua/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ M.expect = function(actions)
end
end
if #keys > 0 then
return string.format("--expect=%s", table.concat(keys, ","))
return table.concat(keys, ",")
end
return nil
end
Expand Down Expand Up @@ -104,7 +104,7 @@ M.vimcmd_file = function(vimcmd, selected, opts)
if entry.path == "<none>" then goto continue end
entry.ctag = opts._ctag and path.entry_to_ctag(selected[i])
local fullpath = entry.path or entry.uri and entry.uri:match("^%a+://(.*)")
if not path.starts_with_separator(fullpath) then
if not path.is_absolute(fullpath) then
fullpath = path.join({ opts.cwd or opts._cwd or vim.loop.cwd(), fullpath })
end
if vimcmd == "e"
Expand All @@ -128,7 +128,7 @@ M.vimcmd_file = function(vimcmd, selected, opts)
if vimcmd ~= "e" or curbuf ~= fullpath then
if entry.path then
-- do not run ':<cmd> <file>' for uri entries (#341)
local relpath = path.relative(entry.path, vim.loop.cwd())
local relpath = path.relative_to(entry.path, vim.loop.cwd())
if vim.o.autochdir then
-- force full paths when `autochdir=true` (#882)
relpath = fullpath
Expand Down Expand Up @@ -245,7 +245,7 @@ M.file_switch = function(selected, opts)
local bufnr = nil
local entry = path.entry_to_file(selected[1])
local fullpath = entry.path
if not path.starts_with_separator(fullpath) then
if not path.is_absolute(fullpath) then
fullpath = path.join({ opts.cwd or vim.loop.cwd(), fullpath })
end
for _, b in ipairs(vim.api.nvim_list_bufs()) do
Expand Down Expand Up @@ -629,7 +629,7 @@ end
local git_exec = function(selected, opts, cmd, silent)
local success
for _, e in ipairs(selected) do
local file = path.relative(path.entry_to_file(e, opts).path, opts.cwd)
local file = path.relative_to(path.entry_to_file(e, opts).path, opts.cwd)
local _cmd = vim.deepcopy(cmd)
table.insert(_cmd, file)
local output, rc = utils.io_systemlist(_cmd)
Expand Down Expand Up @@ -709,7 +709,7 @@ M.git_buf_edit = function(selected, opts)
local git_root = path.git_root(opts, true)
local win = vim.api.nvim_get_current_win()
local buffer_filetype = vim.bo.filetype
local file = path.relative(vim.fn.expand("%:p"), git_root)
local file = path.relative_to(vim.fn.expand("%:p"), git_root)
local commit_hash = match_commit_hash(selected[1], opts)
table.insert(cmd, commit_hash .. ":" .. file)
local git_file_contents = utils.io_systemlist(cmd)
Expand Down Expand Up @@ -819,8 +819,9 @@ end
---@param selected string[]
---@param opts table
M.apply_profile = function(selected, opts)
local fname = utils.__IS_WINDOWS and selected[1]:match("%u?:?[^:]+") or selected[1]:match("[^:]+")
local profile = selected[1]:match(":([^%s]+)")
local entry = path.entry_to_file(selected[1])
local fname = entry.path
local profile = entry.stripped:sub(#fname + 2):match("[^%s]+")
local ok = utils.load_profile(fname, profile, opts.silent)
if ok then
vim.cmd(string.format([[lua require("fzf-lua").setup({"%s"})]], profile))
Expand Down
23 changes: 12 additions & 11 deletions lua/fzf-lua/complete.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local core = require "fzf-lua.core"
local path = require "fzf-lua.path"
local utils = require "fzf-lua.utils"
local config = require "fzf-lua.config"

local M = {}
Expand All @@ -23,14 +24,14 @@ local function find_toplevel_cwd(maybe_cwd, postfix, orig_cwd)
if vim.fn.isdirectory(vim.fn.expand(maybe_cwd)) == 1 then
local disp_cwd, cwd = maybe_cwd, vim.fn.expand(maybe_cwd)
-- returned cwd must be full path
if cwd:sub(1, 1) == "." and cwd:sub(2, 2) == path.SEPARATOR then
if path.has_cwd_prefix(cwd) then
cwd = vim.loop.cwd() .. (#cwd > 1 and cwd:sub(2) or "")
-- inject "./" only if original path started with it
-- otherwise ignore the "." retval from fnamemodify
if #orig_cwd > 0 and orig_cwd:sub(1, 1) ~= "." then
disp_cwd = nil
end
elseif not path.starts_with_separator(cwd) then
elseif not path.is_absolute(cwd) then
cwd = path.join({ vim.loop.cwd(), cwd })
end
return disp_cwd, cwd, postfix
Expand Down Expand Up @@ -60,15 +61,13 @@ local set_cmp_opts_path = function(opts)
if not opts.prompt then
opts.prompt = "."
end
if not path.ends_with_separator(opts.prompt) then
opts.prompt = opts.prompt .. path.SEPARATOR
end
opts.prompt = path.add_trailing(opts.prompt)
-- completion function rebuilds the line with the full path
opts.complete = function(selected, o, l, _)
-- query fuzzy matching is empty
if #selected == 0 then return end
local replace_at = col - #before
local relpath = path.relative(path.entry_to_file(selected[1], o).path, opts.cwd)
local relpath = path.relative_to(path.entry_to_file(selected[1], o).path, opts.cwd)
local before_path = replace_at > 1 and l:sub(1, replace_at - 1) or ""
local rest_of_line = #l >= (col + #after) and l:sub(col + #after) or ""
local resolved_path = opts._cwd and path.join({ opts._cwd, relpath }) or relpath
Expand All @@ -87,8 +86,8 @@ M.path = function(opts)
return "fdfind"
elseif vim.fn.executable("fd") == 1 then
return "fd"
elseif vim.fn.executable("rg") == 1 then
return "rg --files"
elseif utils.__IS_WINDOWS then
return "dir /s/b"
else
return [[find ! -path '.' ! -path '*/\.git/*' -printf '%P\n']]
end
Expand All @@ -103,12 +102,14 @@ M.file = function(opts)
if not opts then return end
opts.cmp_is_file = true
opts.cmd = opts.cmd or (function()
if vim.fn.executable("rg") == 1 then
return "rg --files"
elseif vim.fn.executable("fdfind") == 1 then
if vim.fn.executable("fdfind") == 1 then
return "fdfind --type f --exclude .git"
elseif vim.fn.executable("fd") == 1 then
return "fd --type f --exclude .git"
elseif vim.fn.executable("rg") == 1 then
return "rg --files"
elseif utils.__IS_WINDOWS then
return "dir /s/b"
else
return [[find -type f ! -path '*/\.git/*' -printf '%P\n']]
end
Expand Down
22 changes: 16 additions & 6 deletions lua/fzf-lua/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ if utils.__HAS_DEVICONS then

-- get the devicons module path
M._devicons_path = M._has_devicons and M._devicons and M._devicons.setup
and debug.getinfo(M._devicons.setup, "S").source:gsub("^@", "")
if utils.__IS_WINDOWS then M._devicons_path = vim.fs.normalize(M._devicons_path) end
and path.normalize(debug.getinfo(M._devicons.setup, "S").source:gsub("^@", ""))
end

M._diricon_escseq = function()
Expand Down Expand Up @@ -275,6 +274,13 @@ function M.normalize_opts(opts, globals, __resume_key)
type(M.globals[k]) == "table" and utils.tbl_deep_clone(M.globals[k]) or {})
end

-- backward compat: no-value flags should be set to `true`, in the past these
-- would be set to an empty string which would now translate into a shell escaped
-- string as we automatically shell escape all fzf_opts
for k, v in pairs(opts.fzf_opts) do
if v == "" then opts.fzf_opts[k] = true end
end

-- prioritize fzf-tmux split pane flags over the
-- popup flag `-p` from fzf-lua defaults (#865)
if type(opts.fzf_tmux_opts) == "table" then
Expand Down Expand Up @@ -426,11 +432,15 @@ function M.normalize_opts(opts, globals, __resume_key)
utils.warn(("Unable to access '%s', removing 'cwd' option."):format(opts.cwd))
opts.cwd = nil
else
-- relative paths in cwd are inaccessible when using multiprocess
-- as the external process have no awareness of our current working
-- directory so we must convert to full path (#375)
if not path.starts_with_separator(opts.cwd) then
if not path.is_absolute(opts.cwd) then
-- relative paths in cwd are inaccessible when using multiprocess
-- as the external process have no awareness of our current working
-- directory so we must convert to full path (#375)
opts.cwd = path.join({ vim.loop.cwd(), opts.cwd })
elseif utils.__IS_WINDOWS and opts.cwd:sub(2) == ":" then
-- TODO: upstream bug? on Windoows: starting jobs with `cwd = C:` (without separator)
-- ignores the cwd argument and starts the job in the current working directory
opts.cwd = path.add_trailing(opts.cwd)
end
end
end
Expand Down
Loading

0 comments on commit a038250

Please sign in to comment.