diff --git a/lua/dotvim/core/action.lua b/lua/dotvim/core/action.lua index 9812b2d3..b9f496f5 100644 --- a/lua/dotvim/core/action.lua +++ b/lua/dotvim/core/action.lua @@ -29,6 +29,13 @@ function Action:execute() end end +function Action:category_length() + if self.category == nil then + return 0 + end + return #self.category +end + ---@return LazyKeysSpec[] function Action:into_lazy_keys_spec() local keys = self.keys diff --git a/lua/dotvim/utils/tbl.lua b/lua/dotvim/utils/tbl.lua index 5861263e..67f558ef 100644 --- a/lua/dotvim/utils/tbl.lua +++ b/lua/dotvim/utils/tbl.lua @@ -119,4 +119,18 @@ function M.tbl_set(value, tbl, ...) return tbl end +---@generic T +---@generic U +---@param array U[] +---@param init T +---@param fn fun(T, U):T +---@return T +function M.reduce_left(array, init, fn) + local ret = init + for _, v in ipairs(array) do + ret = fn(ret, v) + end + return ret +end + return M diff --git a/lua/telescope/_extensions/command_palette.lua b/lua/telescope/_extensions/command_palette.lua index 06f6ff26..0b79e735 100644 --- a/lua/telescope/_extensions/command_palette.lua +++ b/lua/telescope/_extensions/command_palette.lua @@ -6,9 +6,8 @@ local finders = require("telescope.finders") local pickers = require("telescope.pickers") local entry_display = require("telescope.pickers.entry_display") ----@param opts table ---@return function -local function entry_maker(opts, category_width, title_width) +local function entry_maker(_, category_width, title_width) local displayer = entry_display.create { separator = " | ", items = { @@ -24,7 +23,7 @@ local function entry_maker(opts, category_width, title_width) } end - ---@param entry FunctionWithDescription + ---@param entry dotvim.core.action.Action return function(entry) local category = entry.category or "" entry.category = category @@ -37,15 +36,44 @@ local function entry_maker(opts, category_width, title_width) end end +---@type dotvim.core +local Core = require("dotvim.core") + +---@type dotvim.utils +local Utils = require("dotvim.utils") + local function open_command_palette(opts) - local functions, category_width, title_width = - require("ht.core.functions"):get_functions(vim.api.nvim_get_current_buf()) + local buffer = Core.vim.wrap_buffer(vim.api.nvim_get_current_buf()) + local buf_actions = Core.registry.buf_get_all_actions(buffer) + + local category_width = math.min( + Utils.tbl.reduce_left( + buf_actions, + 0, + ---@param acc number + ---@param action dotvim.core.action.Action + function(acc, action) + return math.max(acc, action:category_length()) + end + ), + 20 + ) + local title_width = Utils.tbl.reduce_left( + buf_actions, + 0, + ---@param acc number + ---@param action dotvim.core.action.Action + function(acc, action) + return math.max(acc, #action.title) + end + ) + pickers .new(opts, { prompt_title = "Command Palette", sorter = conf.generic_sorter(opts), finder = finders.new_table { - results = functions, + results = buf_actions, entry_maker = entry_maker(opts, category_width, title_width), }, previewer = false, @@ -54,9 +82,8 @@ local function open_command_palette(opts) local entry = action_state.get_selected_entry() actions.close(bufnr) if entry ~= nil then - ---@type FunctionWithDescription local value = entry.value - value.f() + value:execute() end end) return true @@ -65,6 +92,7 @@ local function open_command_palette(opts) :find() end +---@diagnostic disable-next-line: undefined-field return telescope.register_extension { exports = { command_palette = open_command_palette }, } diff --git a/lua/telescope/_extensions/my_find_files.lua b/lua/telescope/_extensions/my_find_files.lua deleted file mode 100644 index 853557b7..00000000 --- a/lua/telescope/_extensions/my_find_files.lua +++ /dev/null @@ -1,25 +0,0 @@ -local telescope = require("telescope") -local conf = require("telescope.config").values -local finders = require("telescope.finders") -local pickers = require("telescope.pickers") -local make_entry = require("telescope.make_entry") - -local find_files = function(opts) - local live_fd = finders.new_job(function(prompt) - return vim.tbl_flatten { "fd", "--type", "f", "--color", "never", prompt } - end, opts.entry_maker or make_entry.gen_from_file(opts)) - - pickers - .new(opts, { - prompt_title = "Find Files", - finder = live_fd, - previewer = conf.file_previewer(opts), - sorter = conf.file_sorter(opts), - }) - :find() -end - -return telescope.register_extension { - exports = { my_find_files = find_files }, -} - diff --git a/lua/telescope/_extensions/obsidian.lua b/lua/telescope/_extensions/obsidian.lua index 3a529b6d..d15c4502 100644 --- a/lua/telescope/_extensions/obsidian.lua +++ b/lua/telescope/_extensions/obsidian.lua @@ -3,6 +3,7 @@ local telescope = require("telescope") ---@type dotvim.extra.obsidian.telescope local Extra = require("dotvim.extra.obsidian.telescope") +---@diagnostic disable-next-line: undefined-field return telescope.register_extension { exports = { find_notes_by_alias = Extra.find_notes_alias,