summaryrefslogtreecommitdiff
path: root/lua/99/extensions/telescope.lua
blob: affb3da98fc0fab3021b13e47fefe41e3de0d217 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
local pickers_util = require("99.extensions.pickers")

local M = {}

--- @param list string[]
--- @param value string
--- @return number
local function index_of(list, value)
  for i, item in ipairs(list) do
    if item == value then
      return i
    end
  end
  return 1
end

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

  local finders = require("telescope.finders")
  local conf = require("telescope.config").values
  local actions = require("telescope.actions")
  local action_state = require("telescope.actions.state")

  pickers_util.get_models(provider, function(models, current)
    pickers
      .new({}, {
        prompt_title = "99: Select Model (current: " .. current .. ")",
        default_selection_index = index_of(models, current),
        finder = finders.new_table({ results = models }),
        sorter = conf.generic_sorter({}),
        attach_mappings = function(prompt_bufnr)
          actions.select_default:replace(function()
            actions.close(prompt_bufnr)
            local selection = action_state.get_selected_entry()
            if not selection then
              return
            end
            pickers_util.on_model_selected(selection[1])
          end)
          return true
        end,
      })
      :find()
  end)
end

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

  local finders = require("telescope.finders")
  local conf = require("telescope.config").values
  local actions = require("telescope.actions")
  local action_state = require("telescope.actions.state")

  local info = pickers_util.get_providers()

  pickers
    .new({}, {
      prompt_title = "99: Select Provider (current: " .. info.current .. ")",
      default_selection_index = index_of(info.names, info.current),
      finder = finders.new_table({ results = info.names }),
      sorter = conf.generic_sorter({}),
      attach_mappings = function(prompt_bufnr)
        actions.select_default:replace(function()
          actions.close(prompt_bufnr)
          local selection = action_state.get_selected_entry()
          if not selection then
            return
          end
          pickers_util.on_provider_selected(selection[1], info.lookup)
        end)
        return true
      end,
    })
    :find()
end

return M