|
1 | 1 | local config = require("tiny-code-action.config")
|
2 | 2 | local M = {}
|
3 | 3 |
|
| 4 | +--- Checks if a specific picker is available on the system |
| 5 | +--- @param picker_name string: Name of the picker to check |
| 6 | +--- @return boolean: True if picker is available |
| 7 | +local function is_picker_available(picker_name) |
| 8 | + if picker_name == "telescope" then |
| 9 | + return pcall(require, "telescope") |
| 10 | + elseif picker_name == "fzf-lua" then |
| 11 | + return pcall(require, "fzf-lua") |
| 12 | + elseif picker_name == "snacks" then |
| 13 | + return pcall(require, "snacks") |
| 14 | + elseif picker_name == "select" then |
| 15 | + return vim.ui and vim.ui.select ~= nil |
| 16 | + elseif picker_name == "buffer" then |
| 17 | + return true |
| 18 | + end |
| 19 | + return false |
| 20 | +end |
| 21 | + |
| 22 | +--- Autodetects the best available picker on the system |
| 23 | +--- @return string: Name of the detected picker |
| 24 | +function M.autodetect_picker() |
| 25 | + local picker_priority = { |
| 26 | + "telescope", |
| 27 | + "fzf-lua", |
| 28 | + "snacks", |
| 29 | + "select", |
| 30 | + "buffer", |
| 31 | + } |
| 32 | + |
| 33 | + for _, picker_name in ipairs(picker_priority) do |
| 34 | + if is_picker_available(picker_name) then |
| 35 | + return picker_name |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + return "buffer" |
| 40 | +end |
| 41 | + |
4 | 42 | -- Get a picker module by name, with fallbacks
|
5 | 43 | --- Retrieves a picker module by name, with fallbacks to defaults if unavailable.
|
6 | 44 | --- @param picker_name string: Name of the picker module
|
7 | 45 | --- @returntable|nil: Picker module or nil if not found
|
8 | 46 | function M.get_picker_module(picker_name)
|
9 | 47 | if not config.VALID_PICKERS[picker_name] then
|
10 | 48 | vim.notify(
|
11 |
| - "Invalid picker: " .. picker_name .. ". Using default 'telescope'.", |
| 49 | + "Invalid picker: " .. picker_name .. ". Using autodetected picker.", |
12 | 50 | vim.log.levels.WARN
|
13 | 51 | )
|
14 |
| - return M.get_picker_module("telescope") |
| 52 | + return M.get_picker_module(M.autodetect_picker()) |
15 | 53 | end
|
| 54 | + |
16 | 55 | local has_picker, picker_module = pcall(require, "tiny-code-action.pickers." .. picker_name)
|
17 | 56 | if has_picker then
|
18 | 57 | return picker_module
|
19 | 58 | end
|
20 |
| - if picker_name == "telescope" then |
| 59 | + |
| 60 | + local fallback_chain = { |
| 61 | + telescope = "fzf-lua", |
| 62 | + ["fzf-lua"] = "snacks", |
| 63 | + snacks = "select", |
| 64 | + select = "buffer", |
| 65 | + buffer = nil, |
| 66 | + } |
| 67 | + |
| 68 | + local fallback = fallback_chain[picker_name] |
| 69 | + if fallback and is_picker_available(fallback) then |
21 | 70 | vim.notify(
|
22 |
| - "Telescope picker is not available. Falling back to vim.ui.select.", |
| 71 | + picker_name:gsub("^%l", string.upper) |
| 72 | + .. " picker is not available. Falling back to " |
| 73 | + .. fallback |
| 74 | + .. ".", |
23 | 75 | vim.log.levels.WARN
|
24 | 76 | )
|
25 |
| - return M.get_picker_module("select") |
26 |
| - elseif picker_name == "snacks" then |
27 |
| - vim.notify("Snacks picker is not available. Falling back to telescope.", vim.log.levels.WARN) |
28 |
| - return M.get_picker_module("telescope") |
29 |
| - elseif picker_name == "select" then |
30 |
| - vim.notify("Select picker is not available. Falling back to buffer.", vim.log.levels.WARN) |
31 |
| - return M.get_picker_module("buffer") |
32 |
| - elseif picker_name == "fzf-lua" then |
33 |
| - vim.notify("Fzflua picker is not available. No picker could be loaded.", vim.log.levels.ERROR) |
34 |
| - return M.get_picker_module("select") |
35 |
| - elseif picker_name == "buffer" then |
36 |
| - vim.notify("Buffer picker is not available. No picker could be loaded.", vim.log.levels.ERROR) |
37 |
| - return nil |
| 77 | + return M.get_picker_module(fallback) |
| 78 | + elseif fallback then |
| 79 | + return M.get_picker_module(fallback) |
38 | 80 | else
|
39 |
| - vim.notify("Could not load any picker module. This should not happen.", vim.log.levels.ERROR) |
| 81 | + vim.notify("No picker could be loaded.", vim.log.levels.ERROR) |
40 | 82 | return nil
|
41 | 83 | end
|
42 | 84 | end
|
|
0 commit comments