diff --git a/lua/yacp/init.lua b/lua/yacp/init.lua index 1adbe6f..a7d0cb3 100644 --- a/lua/yacp/init.lua +++ b/lua/yacp/init.lua @@ -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) @@ -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 diff --git a/lua/yacp/providers/native.lua b/lua/yacp/providers/native.lua new file mode 100644 index 0000000..510e436 --- /dev/null +++ b/lua/yacp/providers/native.lua @@ -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