Skip to content

Commit d6221d0

Browse files
committed
fix(fzf_live): with string shell commands
1 parent fd2339d commit d6221d0

29 files changed

Lines changed: 331 additions & 20 deletions

File tree

lua/fzf-lua/config.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,15 @@ function M.normalize_opts(opts, globals, __resume_key)
907907
and [[return require("fzf-lua.make_entry").preprocess]]
908908
or opts.fn_preprocess
909909
end
910+
-- Must have preprocess to load icon sets, relocate {argvz}, etc
911+
if opts.fn_preprocess == nil
912+
and (opts.file_icons
913+
or opts.git_icons
914+
or opts.formatter
915+
or opts.fn_transform_cmd)
916+
then
917+
opts.fn_preprocess = [[return require("fzf-lua.make_entry").preprocess]]
918+
end
910919

911920
if opts.line_query and not utils.has(opts, "fzf", { 0, 59 }) then
912921
utils.warn("'line_query' requires fzf >= 0.59, ignoring.")

lua/fzf-lua/core.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ M.fzf_live = function(contents, opts)
176176
-- convert "reload" actions to fzf's `reload` binds
177177
-- convert "exec_silent" actions to fzf's `execute-silent` binds
178178
shell.clear_protected()
179+
if type(contents) == "string" then
180+
-- Signal to stringify_mt we are relocating <query>
181+
-- Signal to preprocess we are looking to replace {argvz}
182+
-- Append query placeholder if not found in command
183+
opts.argv_expr = opts.multiprocess
184+
if not contents:match(M.fzf_query_placeholder) then
185+
contents = ("%s %s"):format(contents, M.fzf_query_placeholder)
186+
end
187+
end
179188
opts.fn_reload = shell.stringify(contents, opts, nil, true)
180189
local fzf_field_index = M.fzf_field_index(opts)
181190
local cmd = M.expand_query(opts.fn_reload, fzf_field_index)

lua/fzf-lua/libuv.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,11 @@ M.spawn_stdio = function(opts, fn_transform_str, fn_preprocess_str, fn_postproce
441441
end
442442
end
443443

444+
-- Requiring make_entry will create the pseudo `_G.FzfLua` global
445+
-- Must be called after global vars are created or devicons will
446+
-- err with "fzf-lua fatal: '_G._fzf_lua_server', '_G._devicons_path' both nil"
447+
pcall(require, "fzf-lua.make_entry")
448+
444449
local fn_transform = load_fn(fn_transform_str)
445450
local fn_preprocess = load_fn(fn_preprocess_str)
446451
local fn_postprocess = load_fn(fn_postprocess_str)

lua/fzf-lua/make_entry.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ if _G._fzf_lua_is_headless then
7171
if _config.globals.nbsp then utils.nbsp = _config.globals.nbsp end
7272

7373
config = _config
74+
75+
-- Compat global with known modules so we can use it in callbacks (fn_transform, etc)
76+
_G.FzfLua = {
77+
make_entry = M,
78+
config = config,
79+
path = path,
80+
utils = utils,
81+
libuv = libuv,
82+
devicons = devicons,
83+
}
7484
end
7585

7686
M.get_diff_files = function(opts)

lua/fzf-lua/providers/grep.lua

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,6 @@ M.live_grep = function(opts)
294294
opts.multiprocess = opts.multiprocess and 1
295295
end
296296

297-
-- signal to preprocess we are looking to replace {argvz}
298-
opts.argv_expr = opts.multiprocess
299-
300297
-- this will be replaced by the appropriate fzf
301298
-- FIELD INDEX EXPRESSION by 'fzf_exec'
302299
local cmd = get_grep_cmd(opts, core.fzf_query_placeholder, 2)

lua/fzf-lua/shell.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ M.stringify_mt = function(cmd, opts)
215215
-- NOTE: since we cannot guarantee the positional index
216216
-- of arguments (#291), we use the last argument instead
217217
opts.cmd = opts.cmd:gsub(FzfLua.core.fzf_query_placeholder, "{argvz}")
218+
-- NOTE: we add preprocess in config.normalize_opts but `opts.argv_expr`
219+
-- isn't yet set at that point
220+
if opts.fn_preprocess == nil then
221+
opts.fn_preprocess = [[return require("fzf-lua.make_entry").preprocess]]
222+
end
218223
end
219224
local spawn_cmd = libuv.wrap_spawn_stdio(
220225
serialize(filter_opts(opts)),

tests/api_spec.lua

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ T["api"]["fzf_exec"]["function"] = new_set({ parametrize = { { "sync" }, { "asyn
4949
end,
5050
})
5151

52+
T["api"]["fzf_exec"]["rg"] = new_set({ parametrize = { { true }, { false }, { 1 } } }, {
53+
function(multiprocess)
54+
helpers.FzfLua.fzf_exec(child,
55+
[['rg --files -g "!.git" --sort=path']],
56+
{
57+
__expect_lines = true,
58+
debug = 1,
59+
multiprocess = multiprocess,
60+
})
61+
end
62+
})
63+
64+
5265
T["api"]["fzf_live"] = new_set()
5366

5467
T["api"]["fzf_live"]["table"] = function()
@@ -123,4 +136,35 @@ T["api"]["fzf_live"]["function"] = new_set({ parametrize = { { "sync" }, { "asyn
123136
end,
124137
})
125138

139+
T["api"]["fzf_live"]["rg"] = new_set()
140+
141+
T["api"]["fzf_live"]["rg"]["error"] = new_set({ parametrize = { { true }, { false }, { 1 } } }, {
142+
function(multiprocess)
143+
helpers.FzfLua.fzf_live(child, [["rg --column --line-number --smart-case"]], {
144+
__expect_lines = true,
145+
-- multiprocess==1 should fallback to native (no [DEBUG] line)
146+
-- as no fn_transform or fn_preprocess are present
147+
multiprocess = multiprocess,
148+
debug = 1,
149+
query = [[\]],
150+
fzf_opts = { ["--wrap"] = true },
151+
})
152+
end
153+
})
154+
155+
T["api"]["fzf_live"]["rg"]["no error"] = new_set(
156+
{ parametrize = { { [[\]] }, { [[table of cont]] } } },
157+
{
158+
function(query)
159+
helpers.FzfLua.fzf_live(child,
160+
[["rg --column --line-number --smart-case --sort=path -g !tests/ -- <query> 2>/dev/null"]],
161+
{
162+
__expect_lines = true,
163+
debug = 1,
164+
query = query,
165+
fzf_opts = { ["--wrap"] = true },
166+
})
167+
end
168+
})
169+
126170
return T
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--|---------|---------|---------|---------|---------|---------|----
2+
01|
3+
02|~
4+
03|~ ╭─────────────────────────────────────────────────╮
5+
04|~ │> 118/118 │
6+
05|~ │──────────────────────────────────────────────── │
7+
06|~ │▌ LICENSE ││
8+
07|~ │ Makefile ││
9+
08|~ │ OPTIONS.md │
10+
09|~ │ README-Win.md │
11+
10|~ │ README.md │
12+
11|~ │ autoload/fzf_lua.vim │
13+
12|~ │ autoload/health/fzf_lua.vim │
14+
13|~ │ doc/fzf-lua-opts.txt │
15+
14|~ │ doc/fzf-lua.txt │
16+
15|~ │ lua/fzf-lua/_health.lua │
17+
16|~ │ lua/fzf-lua/actions.lua │
18+
17|~ │ lua/fzf-lua/class.lua │
19+
18|~ │ lua/fzf-lua/cmd.lua │
20+
19|~ │ lua/fzf-lua/cmp_src.lua │
21+
20|~ │ lua/fzf-lua/complete.lua │
22+
21|~ │ lua/fzf-lua/config.lua │
23+
22|~ ╰─────────────────────────────────────────────────╯
24+
23|~
25+
24|[No Name] 0,0-1 All
26+
25|
27+
26|
28+
27|
29+
28|-- TERMINAL -- 1,0-1 All
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--|---------|---------|---------|---------|---------|---------|----
2+
01|
3+
02|~
4+
03|~ ╭─────────────────────────────────────────────────╮
5+
04|~ │> 119/119 │
6+
05|~ │──────────────────────────────────────────────── │
7+
06|~ │▌ [DEBUG] [st] rg --files -g "!.git" --sort=path││
8+
07|~ │ LICENSE ││
9+
08|~ │ Makefile │
10+
09|~ │ OPTIONS.md │
11+
10|~ │ README-Win.md │
12+
11|~ │ README.md │
13+
12|~ │ autoload/fzf_lua.vim │
14+
13|~ │ autoload/health/fzf_lua.vim │
15+
14|~ │ doc/fzf-lua-opts.txt │
16+
15|~ │ doc/fzf-lua.txt │
17+
16|~ │ lua/fzf-lua/_health.lua │
18+
17|~ │ lua/fzf-lua/actions.lua │
19+
18|~ │ lua/fzf-lua/class.lua │
20+
19|~ │ lua/fzf-lua/cmd.lua │
21+
20|~ │ lua/fzf-lua/cmp_src.lua │
22+
21|~ │ lua/fzf-lua/complete.lua │
23+
22|~ ╰─────────────────────────────────────────────────╯
24+
23|~
25+
24|[No Name] 0,0-1 All
26+
25|
27+
26|
28+
27|
29+
28|-- TERMINAL -- 1,0-1 All
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--|---------|---------|---------|---------|---------|---------|----
2+
01|
3+
02|~
4+
03|~ ╭─────────────────────────────────────────────────╮
5+
04|~ │> 119/119 │
6+
05|~ │──────────────────────────────────────────────── │
7+
06|~ │▌ [DEBUG] [mt] rg --files -g "!.git" --sort=path││
8+
07|~ │ LICENSE ││
9+
08|~ │ Makefile │
10+
09|~ │ OPTIONS.md │
11+
10|~ │ README-Win.md │
12+
11|~ │ README.md │
13+
12|~ │ autoload/fzf_lua.vim │
14+
13|~ │ autoload/health/fzf_lua.vim │
15+
14|~ │ doc/fzf-lua-opts.txt │
16+
15|~ │ doc/fzf-lua.txt │
17+
16|~ │ lua/fzf-lua/_health.lua │
18+
17|~ │ lua/fzf-lua/actions.lua │
19+
18|~ │ lua/fzf-lua/class.lua │
20+
19|~ │ lua/fzf-lua/cmd.lua │
21+
20|~ │ lua/fzf-lua/cmp_src.lua │
22+
21|~ │ lua/fzf-lua/complete.lua │
23+
22|~ ╰─────────────────────────────────────────────────╯
24+
23|~
25+
24|[No Name] 0,0-1 All
26+
25|
27+
26|
28+
27|
29+
28|-- TERMINAL -- 1,0-1 All

0 commit comments

Comments
 (0)