Skip to content

Commit b64d280

Browse files
committed
chore(windows): adjust for fzf changes (>=v0.51)
1 parent d62c519 commit b64d280

File tree

6 files changed

+67
-31
lines changed

6 files changed

+67
-31
lines changed

lua/fzf-lua/config.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,11 @@ function M.normalize_opts(opts, globals, __resume_key)
466466
opts._is_skim = opts.fzf_bin:find("sk") ~= nil
467467

468468
-- enforce fzf minimum requirements
469+
vim.g.fzf_lua_fzf_version = nil
469470
if not opts._is_skim then
470471
local FZF_VERSION, rc, err = utils.fzf_version(opts)
471472
opts.__FZF_VERSION = FZF_VERSION
473+
vim.g.fzf_lua_fzf_version = FZF_VERSION
472474
if not opts.__FZF_VERSION then
473475
utils.err(string.format(
474476
"'fzf --version' failed with error %s: %s", rc, err))

lua/fzf-lua/core.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ M.mt_cmd_wrapper = function(opts)
699699
"file_ignore_patterns",
700700
"rg_glob",
701701
"_base64",
702+
utils.__IS_WINDOWS and "__FZF_VERSION" or nil,
702703
}
703704
-- caller requested rg with glob support
704705
if o.rg_glob then
@@ -1134,8 +1135,10 @@ M.setup_fzf_interactive_flags = function(command, fzf_field_expression, opts)
11341135
-- use `true` as $FZF_DEFAULT_COMMAND instead (#510)
11351136
opts.__fzf_init_cmd = utils.shell_nop()
11361137
if opts.exec_empty_query or (opts.query and #opts.query > 0) then
1138+
local q = not utils.__IS_WINDOWS and opts.query
1139+
or libuv.escape_fzf(opts.query, opts.__FZF_VERSION)
11371140
-- gsub doesn't like single % on rhs
1138-
local escaped_q = libuv.shellescape(libuv.escape_fzf(opts.query)):gsub("%%", "%%%%")
1141+
local escaped_q = libuv.shellescape(q):gsub("%%", "%%%%")
11391142
opts.__fzf_init_cmd = initial_command:gsub(fzf_field_expression, escaped_q)
11401143
end
11411144
if opts.__FZF_VERSION >= 0.25 then

lua/fzf-lua/libuv.lua

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -669,10 +669,12 @@ end
669669

670670
-- Windows fzf oddities, fzf's {q} will send escaped blackslahes,
671671
-- but only when the backslash prefixes another character which
672-
-- isn't a backslash
673-
M.unescape_fzf = function(s, is_win)
672+
-- isn't a backslash, test with:
673+
-- fzf --disabled --height 30% --preview-window up --preview "echo {q}"
674+
M.unescape_fzf = function(s, fzf_version, is_win)
674675
if is_win == nil then is_win = _is_win end
675676
if not is_win then return s end
677+
if tonumber(fzf_version) and tonumber(fzf_version) >= 0.52 then return s end
676678
local ret = s:gsub("\\+[^\\]", function(x)
677679
local bslash_num = #x:match([[\+]])
678680
return string.rep([[\]],
@@ -686,9 +688,10 @@ end
686688
-- doing weird extra escaping with {q}, we use this to simulate
687689
-- {q} being sent via the reload action as the initial command
688690
-- TODO: better solution for these stupid hacks (upstream issues?)
689-
M.escape_fzf = function(s, is_win)
691+
M.escape_fzf = function(s, fzf_version, is_win)
690692
if is_win == nil then is_win = _is_win end
691693
if not is_win then return s end
694+
if tonumber(fzf_version) and tonumber(fzf_version) >= 0.52 then return s end
692695
local ret = s:gsub("\\+[^\\]", function(x)
693696
local bslash_num = #x:match([[\+]])
694697
return string.rep([[\]], bslash_num * 2) .. x:sub(-1)

lua/fzf-lua/make_entry.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ M.get_diff_files = function(opts)
7676
local ok, status, err = pcall(utils.io_systemlist, path.git_cwd(cmd, opts))
7777
local seconds = os.time() - start
7878
if seconds >= 3 and opts.silent ~= true then
79-
local exec_str = string.format([[require"fzf-lua".utils.warn(]]
80-
.. [["`git status` took %d seconds, consider using `:FzfLua files git_icons=false` in this repository.")]]
79+
local exec_str = string.format([[require"fzf-lua".utils.warn(]] ..
80+
[["`git status` took %d seconds, consider using `:FzfLua files git_icons=false` in this repository.")]]
8181
, seconds)
8282
if not vim.g.fzf_lua_is_headless then
8383
loadstring(exec_str)()
@@ -173,7 +173,7 @@ M.preprocess = function(opts)
173173
io.stdout:write(("[DEBUGV]: raw_argv(%d) = %s\n"):format(idx, arg))
174174
end
175175
if utils.__IS_WINDOWS then
176-
arg = libuv.unescape_fzf(arg)
176+
arg = libuv.unescape_fzf(arg, opts.__FZF_VERSION)
177177
end
178178
if debug == "v" or debug == "verbose" then
179179
io.stdout:write(("[DEBUGV]: esc_argv(%d) = %s\n"):format(idx, libuv.shellescape(arg)))

lua/fzf-lua/shell.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ function M.raw_async_action(fn, fzf_field_expression, debug)
5555
local pipe = uv.new_pipe(false)
5656
local args = { ... }
5757
-- unescape double backslashes on windows
58-
if type(args[1]) == "table" then
59-
args[1] = vim.tbl_map(function(x) return libuv.unescape_fzf(x) end, args[1])
58+
if utils.__IS_WINDOWS and type(args[1]) == "table" then
59+
args[1] = vim.tbl_map(function(x)
60+
return libuv.unescape_fzf(x, vim.g.fzf_lua_fzf_version)
61+
end, args[1])
6062
end
6163
-- save selected item in main module's __INFO
6264
-- use loadstring to avoid circular require

tests/libuv_spec.lua

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,29 +90,55 @@ describe("Testing libuv module", function()
9090
assert.are.same(libuv.shellescape([[foo\\\^^^"]], 2), [[^"foo\\\^^^^^^\^"^"]])
9191
end)
9292

93-
it("escape {q} (win)", function()
94-
assert.are.same(libuv.escape_fzf([[]], true), [[]])
95-
assert.are.same(libuv.escape_fzf([[\]], true), [[\]])
96-
assert.are.same(libuv.escape_fzf([[\\]], true), [[\\]])
97-
assert.are.same(libuv.escape_fzf([[foo]], true), [[foo]])
98-
assert.are.same(libuv.escape_fzf([[\foo]], true), [[\\foo]])
99-
assert.are.same(libuv.escape_fzf([[\\foo]], true), [[\\\\foo]])
100-
assert.are.same(libuv.escape_fzf([[\\\foo]], true), [[\\\\\\foo]])
101-
assert.are.same(libuv.escape_fzf([[\\\\foo]], true), [[\\\\\\\\foo]])
102-
assert.are.same(libuv.escape_fzf([[foo\]], true), [[foo\]])
103-
assert.are.same(libuv.escape_fzf([[foo\\]], true), [[foo\\]])
93+
it("escape {q} (win, fzf v0.50)", function()
94+
assert.are.same(libuv.escape_fzf([[]], 0.50, true), [[]])
95+
assert.are.same(libuv.escape_fzf([[\]], 0.50, true), [[\]])
96+
assert.are.same(libuv.escape_fzf([[\\]], 0.50, true), [[\\]])
97+
assert.are.same(libuv.escape_fzf([[foo]], 0.50, true), [[foo]])
98+
assert.are.same(libuv.escape_fzf([[\foo]], 0.50, true), [[\\foo]])
99+
assert.are.same(libuv.escape_fzf([[\\foo]], 0.50, true), [[\\\\foo]])
100+
assert.are.same(libuv.escape_fzf([[\\\foo]], 0.50, true), [[\\\\\\foo]])
101+
assert.are.same(libuv.escape_fzf([[\\\\foo]], 0.50, true), [[\\\\\\\\foo]])
102+
assert.are.same(libuv.escape_fzf([[foo\]], 0.50, true), [[foo\]])
103+
assert.are.same(libuv.escape_fzf([[foo\\]], 0.50, true), [[foo\\]])
104104
end)
105105

106-
it("unescape {q} (win)", function()
107-
assert.are.same(libuv.unescape_fzf([[]], true), [[]])
108-
assert.are.same(libuv.unescape_fzf([[\]], true), [[\]])
109-
assert.are.same(libuv.unescape_fzf([[\\]], true), [[\\]])
110-
assert.are.same(libuv.unescape_fzf([[foo]], true), [[foo]])
111-
assert.are.same(libuv.unescape_fzf([[\foo]], true), [[\foo]])
112-
assert.are.same(libuv.unescape_fzf([[\\foo]], true), [[\foo]])
113-
assert.are.same(libuv.unescape_fzf([[\\\foo]], true), [[\foo]])
114-
assert.are.same(libuv.unescape_fzf([[\\\\foo]], true), [[\\foo]])
115-
assert.are.same(libuv.unescape_fzf([[foo\]], true), [[foo\]])
116-
assert.are.same(libuv.unescape_fzf([[foo\\]], true), [[foo\\]])
106+
it("unescape {q} (win, fzf v0.50)", function()
107+
assert.are.same(libuv.unescape_fzf([[]], 0.50, true), [[]])
108+
assert.are.same(libuv.unescape_fzf([[\]], 0.50, true), [[\]])
109+
assert.are.same(libuv.unescape_fzf([[\\]], 0.50, true), [[\\]])
110+
assert.are.same(libuv.unescape_fzf([[foo]], 0.50, true), [[foo]])
111+
assert.are.same(libuv.unescape_fzf([[\foo]], 0.50, true), [[\foo]])
112+
assert.are.same(libuv.unescape_fzf([[\\foo]], 0.50, true), [[\foo]])
113+
assert.are.same(libuv.unescape_fzf([[\\\foo]], 0.50, true), [[\foo]])
114+
assert.are.same(libuv.unescape_fzf([[\\\\foo]], 0.50, true), [[\\foo]])
115+
assert.are.same(libuv.unescape_fzf([[foo\]], 0.50, true), [[foo\]])
116+
assert.are.same(libuv.unescape_fzf([[foo\\]], 0.50, true), [[foo\\]])
117+
end)
118+
119+
it("escape {q} (win, fzf v0.52)", function()
120+
assert.are.same(libuv.escape_fzf([[]], 0.52, true), [[]])
121+
assert.are.same(libuv.escape_fzf([[\]], 0.52, true), [[\]])
122+
assert.are.same(libuv.escape_fzf([[\\]], 0.52, true), [[\\]])
123+
assert.are.same(libuv.escape_fzf([[foo]], 0.52, true), [[foo]])
124+
assert.are.same(libuv.escape_fzf([[\foo]], 0.52, true), [[\foo]])
125+
assert.are.same(libuv.escape_fzf([[\\foo]], 0.52, true), [[\\foo]])
126+
assert.are.same(libuv.escape_fzf([[\\\foo]], 0.52, true), [[\\\foo]])
127+
assert.are.same(libuv.escape_fzf([[\\\\foo]], 0.52, true), [[\\\\foo]])
128+
assert.are.same(libuv.escape_fzf([[foo\]], 0.52, true), [[foo\]])
129+
assert.are.same(libuv.escape_fzf([[foo\\]], 0.52, true), [[foo\\]])
130+
end)
131+
132+
it("unescape {q} (win, fzf v0.52)", function()
133+
assert.are.same(libuv.unescape_fzf([[]], 0.52, true), [[]])
134+
assert.are.same(libuv.unescape_fzf([[\]], 0.52, true), [[\]])
135+
assert.are.same(libuv.unescape_fzf([[\\]], 0.52, true), [[\\]])
136+
assert.are.same(libuv.unescape_fzf([[foo]], 0.52, true), [[foo]])
137+
assert.are.same(libuv.unescape_fzf([[\foo]], 0.52, true), [[\foo]])
138+
assert.are.same(libuv.unescape_fzf([[\\foo]], 0.52, true), [[\\foo]])
139+
assert.are.same(libuv.unescape_fzf([[\\\foo]], 0.52, true), [[\\\foo]])
140+
assert.are.same(libuv.unescape_fzf([[\\\\foo]], 0.52, true), [[\\\\foo]])
141+
assert.are.same(libuv.unescape_fzf([[foo\]], 0.52, true), [[foo\]])
142+
assert.are.same(libuv.unescape_fzf([[foo\\]], 0.52, true), [[foo\\]])
117143
end)
118144
end)

0 commit comments

Comments
 (0)