summaryrefslogtreecommitdiff
path: root/lua/99/extensions/fzf_lua.lua
blob: 71a17e58e561dca8dd4bef1e6eeb0a761adcf7cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
local pickers_util = require("99.extensions.pickers")

local M = {}

-- move the current value to the top of the list so fzf opens with it focused
--- @param list string[]
--- @param current string
--- @return string[]
local function promote_current(list, current)
  local out = { unpack(list) }
  for i, item in ipairs(out) do
    if item == current then
      table.remove(out, i)
      table.insert(out, 1, current)
      break
    end
  end
  return out
end

--- @param provider _99.Providers.BaseProvider?
function M.select_model(provider)
  local ok, fzf = pcall(require, "fzf-lua")
  if not ok then
    vim.notify(
      "99: fzf-lua is required for this extension",
      vim.log.levels.ERROR
    )
    return
  end

  pickers_util.get_models(provider, function(models, current)
    fzf.fzf_exec(promote_current(models, current), {
      prompt = "99: Select Model (current: " .. current .. ")> ",
      actions = {
        ["enter"] = function(selected)
          if not selected or #selected == 0 then
            return
          end
          pickers_util.on_model_selected(selected[1])
        end,
      },
    })
  end)
end

function M.select_provider()
  local ok, fzf = pcall(require, "fzf-lua")
  if not ok then
    vim.notify(
      "99: fzf-lua is required for this extension",
      vim.log.levels.ERROR
    )
    return
  end

  local info = pickers_util.get_providers()

  fzf.fzf_exec(promote_current(info.names, info.current), {
    prompt = "99: Select Provider (current: " .. info.current .. ")> ",
    actions = {
      ["enter"] = function(selected)
        if not selected or #selected == 0 then
          return
        end
        pickers_util.on_provider_selected(selected[1], info.lookup)
      end,
    },
  })
end

return M