Skip to content

Commit

Permalink
Add native provider and default to it
Browse files Browse the repository at this point in the history
  • Loading branch information
muchzill4 committed Feb 21, 2024
1 parent c37af2e commit 48a2073
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lua/yacp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local provider = nil
local providers = {
fzf = "yacp.providers.fzf",
telescope = "yacp.providers.telescope",
native = "yacp.providers.native",
}

local function is_valid_provider(name)
Expand All @@ -28,16 +29,18 @@ M.setup = function(opts)
opts = opts or {}

if opts.provider == nil then
notify.of_error "Missing provider setting"
return
elseif not is_valid_provider(opts.provider) then
opts.provider = "native"
end

if not is_valid_provider(opts.provider) then
notify.of_error(
"Invalid provider: "
.. opts.provider
.. ". Must be one of: 'fzf', 'telescope'."
.. ". Must be one of: 'native', 'fzf', 'telescope'."
)
return
end

provider = opts.provider

if opts.palette ~= nil then
Expand Down
46 changes: 46 additions & 0 deletions lua/yacp/providers/native.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
local M = {}

local notify = require "yacp.notify"
local exec = require "yacp.exec"
local palette = require "yacp.palette"

local function make_select_entries(p)
local entries = {}
for _, entry in ipairs(p) do
table.insert(entries, entry.name)
end
return entries
end

local function find_command(p, name)
for _, entry in ipairs(p) do
if entry.name == name then
return entry.cmd
end
end
return nil
end

function M.yacp(opts)
local p = palette.list()
if vim.tbl_isempty(palette) then
notify.of_error "Empty command palette"
return
end
local entries = make_select_entries(p)

local defaults = {
prompt = "",
}

opts = vim.tbl_deep_extend("keep", opts or {}, defaults)

vim.ui.select(entries, opts, function(choice)
local cmd = find_command(p, choice)
if cmd ~= nil then
exec.exec(cmd)
end
end)
end

return M

0 comments on commit 48a2073

Please sign in to comment.