Skip to content

Commit 95f6bae

Browse files
committed
refactor(config): detect binary/version earlier
1 parent 6b5bb1c commit 95f6bae

6 files changed

Lines changed: 58 additions & 61 deletions

File tree

lua/fzf-lua/actions.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ M.expect = function(actions, opts)
5252
elseif k ~= "enter" then
5353
-- Skim does not support case sensitive alt-shift binds
5454
-- which are supported with fzf since version 0.25
55-
if not opts._is_skim or not k:match("^alt%-%u") then
55+
if not utils.has(opts, "sk") or not k:match("^alt%-%u") then
5656
table.insert(expect, k)
5757
end
5858
end

lua/fzf-lua/config.lua

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,58 @@ function M.normalize_opts(opts, globals, __resume_key) ---@diagnostic disable
227227
opts = M.resume_opts(opts)
228228
end
229229

230+
local executable = function(binary, fncerr, strerr)
231+
if binary and vim.fn.executable(binary) ~= 1 then
232+
fncerr(("'%s' is not a valid executable, %s"):format(binary, strerr))
233+
return false
234+
end
235+
return true
236+
end
237+
238+
-- Validate the fzf binary & version dependency
239+
opts.fzf_bin = opts.fzf_bin or M.globals.fzf_bin
240+
opts.fzf_bin = opts.fzf_bin and libuv.expand(opts.fzf_bin) or nil
241+
if not opts.fzf_bin or
242+
not executable(opts.fzf_bin, utils.warn, "fallback to 'fzf'.") then
243+
-- default|fallback to fzf
244+
opts.fzf_bin = "fzf"
245+
-- try fzf plugin if fzf is not installed globally
246+
if vim.fn.executable(opts.fzf_bin) ~= 1 then
247+
local ok, fzf_plug = pcall(vim.api.nvim_call_function, "fzf#exec", {})
248+
if ok and fzf_plug then
249+
opts.fzf_bin = fzf_plug
250+
end
251+
end
252+
if not executable(opts.fzf_bin, utils.error,
253+
"aborting. Please make sure 'fzf' is in installed.") then
254+
return nil
255+
end
256+
end
257+
258+
-- enforce fzf minimum requirements
259+
vim.g.fzf_lua_fzf_version = nil
260+
if opts.fzf_bin:match("sk$") then
261+
local SK_VERSION, rc, err = utils.sk_version(opts)
262+
opts.__SK_VERSION = SK_VERSION
263+
if not opts.__SK_VERSION then
264+
utils.error("'sk --version' failed with error %s: %s", rc, err)
265+
return nil
266+
end
267+
else
268+
local FZF_VERSION, rc, err = utils.fzf_version(opts)
269+
opts.__FZF_VERSION = FZF_VERSION
270+
vim.g.fzf_lua_fzf_version = FZF_VERSION
271+
if not opts.__FZF_VERSION then
272+
utils.error("'fzf --version' failed with error %s: %s", rc, err)
273+
return nil
274+
elseif not utils.has(opts, "fzf", { 0, 36 }) then
275+
utils.error("fzf version %s is lower than minimum (0.36), aborting.",
276+
utils.ver2str(opts.__FZF_VERSION))
277+
return nil
278+
end
279+
end
280+
281+
230282
local function convert_bool_opts()
231283
-- Enforce conversion of boolean options that are tables with `enabled`
232284
-- property, i.e. `winopts.treesitter = true` will be converted to
@@ -656,59 +708,6 @@ function M.normalize_opts(opts, globals, __resume_key) ---@diagnostic disable
656708
-- test for valid git_repo
657709
opts.git_icons = opts.git_icons and path.is_git_repo(opts, true)
658710

659-
local executable = function(binary, fncerr, strerr)
660-
if binary and vim.fn.executable(binary) ~= 1 then
661-
fncerr(("'%s' is not a valid executable, %s"):format(binary, strerr))
662-
return false
663-
end
664-
return true
665-
end
666-
667-
opts.fzf_bin = opts.fzf_bin or M.globals.fzf_bin
668-
opts.fzf_bin = opts.fzf_bin and libuv.expand(opts.fzf_bin) or nil
669-
if not opts.fzf_bin or
670-
not executable(opts.fzf_bin, utils.warn, "fallback to 'fzf'.") then
671-
-- default|fallback to fzf
672-
opts.fzf_bin = "fzf"
673-
-- try fzf plugin if fzf is not installed globally
674-
if vim.fn.executable(opts.fzf_bin) ~= 1 then
675-
local ok, fzf_plug = pcall(vim.api.nvim_call_function, "fzf#exec", {})
676-
if ok and fzf_plug then
677-
opts.fzf_bin = fzf_plug
678-
end
679-
end
680-
if not executable(opts.fzf_bin, utils.error,
681-
"aborting. Please make sure 'fzf' is in installed.") then
682-
return nil
683-
end
684-
end
685-
686-
-- are we using skim?
687-
opts._is_skim = opts.fzf_bin:match("sk$") ~= nil
688-
689-
-- enforce fzf minimum requirements
690-
vim.g.fzf_lua_fzf_version = nil
691-
if not opts._is_skim then
692-
local FZF_VERSION, rc, err = utils.fzf_version(opts)
693-
opts.__FZF_VERSION = FZF_VERSION
694-
vim.g.fzf_lua_fzf_version = FZF_VERSION
695-
if not opts.__FZF_VERSION then
696-
utils.error("'fzf --version' failed with error %s: %s", rc, err)
697-
return nil
698-
elseif not utils.has(opts, "fzf", { 0, 36 }) then
699-
utils.error("fzf version %s is lower than minimum (0.36), aborting.",
700-
utils.ver2str(opts.__FZF_VERSION))
701-
return nil
702-
end
703-
else
704-
local SK_VERSION, rc, err = utils.sk_version(opts)
705-
opts.__SK_VERSION = SK_VERSION
706-
if not opts.__SK_VERSION then
707-
utils.error("'sk --version' failed with error %s: %s", rc, err)
708-
return nil
709-
end
710-
end
711-
712711
if utils.has(opts, "fzf", { 0, 53 })
713712
-- `_multiline` is used to override `multiline` inherited from `defaults = {}`
714713
and opts.multiline and opts._multiline ~= false then

lua/fzf-lua/core.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ M.fzf = function(contents, opts)
414414
-- This was added by 'resume': when '--print-query' is specified
415415
-- we are guaranteed to have the query in the first line, save&remove it
416416
if selected and #selected > 0 then
417-
if not (opts._is_skim and opts.is_live) then
417+
if not (utils.has(opts, "sk") and opts.is_live) then
418418
-- reminder: this doesn't get called with 'live_grep' when using skim
419419
-- due to a bug where '--print-query --interactive' combo is broken:
420420
-- skim always prints an empty line where the typed query should be.
@@ -1103,7 +1103,7 @@ M.setup_fzf_live_flags = function(command, bind_start, opts)
11031103
reload_command = string.format("sleep %.2f; %s", opts.query_delay / 1000, reload_command)
11041104
end
11051105

1106-
if opts._is_skim then
1106+
if utils.has(opts, "sk") then
11071107
opts.prompt = opts.__prompt or opts.prompt or opts.fzf_opts["--prompt"]
11081108
if opts.prompt then
11091109
opts.fzf_opts["--prompt"] = opts.prompt:match("[^%*]+")
@@ -1146,7 +1146,7 @@ end
11461146
-- query placeholder for "live" queries
11471147
M.fzf_query_placeholder = "<query>"
11481148

1149-
---@param opts { field_index?: string, _is_skim?: boolean }
1149+
---@param opts { field_index?: string }
11501150
---@return string
11511151
M.fzf_field_index = function(opts)
11521152
-- fzf already adds single quotes around the placeholder when expanding.

lua/fzf-lua/providers/buffers.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ M.buffer_lines = function(opts)
375375
end)()
376376
end
377377

378-
opts = core.set_fzf_field_index(opts, "{3}", opts._is_skim and "{}" or "{..-2}")
378+
opts = core.set_fzf_field_index(opts, "{3}", utils.has(opts, "sk") and "{}" or "{..-2}")
379379
return core.fzf_exec(contents, opts)
380380
end
381381

lua/fzf-lua/providers/dap.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ M.breakpoints = function(opts)
123123
end)()
124124
end
125125

126-
opts = core.set_fzf_field_index(opts, "{3}", opts._is_skim and "{}" or "{..-2}")
126+
opts = core.set_fzf_field_index(opts, "{3}", utils.has(opts, "sk") and "{}" or "{..-2}")
127127
return core.fzf_exec(contents, opts)
128128
end
129129

lua/fzf-lua/types.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ local FzfLua = require("fzf-lua")
192192
---@field _actions? fun():fzf-lua.config.Actions?
193193
---@field __ACT_TO? function
194194
---@field _start? boolean
195-
---@field _is_skim? boolean
196195
---@field _treesitter? (fun(line: string):string?,string?,string?,string?)|boolean?
197196
---@field help_open_win? fun(buf: integer, enter: boolean, config: vim.api.keyset.win_config): integer
198197
---Auto close fzf-lua interface when a terminal is opened, set to `false` to keep the interface open.
@@ -223,7 +222,6 @@ local FzfLua = require("fzf-lua")
223222
---@field __resume_get? function
224223
---@field _contents? string
225224
---@field _is_fzf_tmux? boolean
226-
---@field _is_skim? boolean
227225
---@field __stringified? boolean
228226
---@field __stringify_cmd? boolean
229227
---@field __sigwinches? string[]

0 commit comments

Comments
 (0)