mirror of
https://github.com/harivansh-afk/forge.nvim.git
synced 2026-04-15 07:04:47 +00:00
refactor(picker): canonicalize backend names into single table
Problem: picker backend names were hard-coded as string literals in `picker/init.lua`, `health.lua`, and `init.lua` validation, making them easy to get out of sync. Solution: export `backends` and `detect_order` from `forge.picker` and reference them in health check and config validation instead of duplicating the name strings.
This commit is contained in:
parent
97698c2af1
commit
ac8db6851f
3 changed files with 13 additions and 17 deletions
|
|
@ -24,11 +24,9 @@ function M.check()
|
||||||
|
|
||||||
local picker_mod = require('forge.picker')
|
local picker_mod = require('forge.picker')
|
||||||
local backend = picker_mod.backend()
|
local backend = picker_mod.backend()
|
||||||
local picker_backends = { 'fzf-lua', 'telescope', 'snacks' }
|
|
||||||
local found_any = false
|
local found_any = false
|
||||||
for _, name in ipairs(picker_backends) do
|
for _, name in ipairs(picker_mod.detect_order) do
|
||||||
local mod = name == 'fzf-lua' and 'fzf-lua' or name
|
if pcall(require, name) then
|
||||||
if pcall(require, mod) then
|
|
||||||
local suffix = backend == name and ' (active)' or ''
|
local suffix = backend == name and ' (active)' or ''
|
||||||
vim.health.ok(name .. ' found' .. suffix)
|
vim.health.ok(name .. ' found' .. suffix)
|
||||||
found_any = true
|
found_any = true
|
||||||
|
|
|
||||||
|
|
@ -725,8 +725,9 @@ function M.config()
|
||||||
cfg.keys = false
|
cfg.keys = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local picker_backends = require('forge.picker').backends
|
||||||
vim.validate('forge.picker', cfg.picker, function(v)
|
vim.validate('forge.picker', cfg.picker, function(v)
|
||||||
return v == 'auto' or v == 'fzf-lua' or v == 'telescope' or v == 'snacks'
|
return v == 'auto' or picker_backends[v] ~= nil
|
||||||
end, "'auto', 'fzf-lua', 'telescope', or 'snacks'")
|
end, "'auto', 'fzf-lua', 'telescope', or 'snacks'")
|
||||||
vim.validate('forge.sources', cfg.sources, 'table')
|
vim.validate('forge.sources', cfg.sources, 'table')
|
||||||
vim.validate('forge.keys', cfg.keys, function(v)
|
vim.validate('forge.keys', cfg.keys, function(v)
|
||||||
|
|
|
||||||
|
|
@ -17,13 +17,14 @@ local M = {}
|
||||||
---@field actions forge.PickerActionDef[]
|
---@field actions forge.PickerActionDef[]
|
||||||
---@field picker_name string
|
---@field picker_name string
|
||||||
|
|
||||||
---@type table<string, string>
|
M.backends = {
|
||||||
local backends = {
|
|
||||||
['fzf-lua'] = 'forge.picker.fzf',
|
['fzf-lua'] = 'forge.picker.fzf',
|
||||||
telescope = 'forge.picker.telescope',
|
telescope = 'forge.picker.telescope',
|
||||||
snacks = 'forge.picker.snacks',
|
snacks = 'forge.picker.snacks',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
M.detect_order = { 'fzf-lua', 'snacks', 'telescope' }
|
||||||
|
|
||||||
---@return string
|
---@return string
|
||||||
local function detect()
|
local function detect()
|
||||||
local cfg = require('forge').config()
|
local cfg = require('forge').config()
|
||||||
|
|
@ -31,16 +32,12 @@ local function detect()
|
||||||
if name ~= 'auto' then
|
if name ~= 'auto' then
|
||||||
return name
|
return name
|
||||||
end
|
end
|
||||||
if pcall(require, 'fzf-lua') then
|
for _, backend in ipairs(M.detect_order) do
|
||||||
return 'fzf-lua'
|
if pcall(require, backend) then
|
||||||
|
return backend
|
||||||
end
|
end
|
||||||
if pcall(require, 'snacks') then
|
|
||||||
return 'snacks'
|
|
||||||
end
|
end
|
||||||
if pcall(require, 'telescope') then
|
return M.detect_order[1]
|
||||||
return 'telescope'
|
|
||||||
end
|
|
||||||
return 'fzf-lua'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param entry forge.PickerEntry
|
---@param entry forge.PickerEntry
|
||||||
|
|
@ -64,7 +61,7 @@ end
|
||||||
---@param opts forge.PickerOpts
|
---@param opts forge.PickerOpts
|
||||||
function M.pick(opts)
|
function M.pick(opts)
|
||||||
local name = detect()
|
local name = detect()
|
||||||
local mod_path = backends[name]
|
local mod_path = M.backends[name]
|
||||||
if not mod_path then
|
if not mod_path then
|
||||||
vim.notify('[forge]: unknown picker backend: ' .. name, vim.log.levels.ERROR)
|
vim.notify('[forge]: unknown picker backend: ' .. name, vim.log.levels.ERROR)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue