Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
TwIStOy committed Mar 30, 2024
1 parent 804d8ab commit b41f281
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 33 deletions.
7 changes: 7 additions & 0 deletions lua/dotvim/core/action.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions lua/dotvim/utils/tbl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 36 additions & 8 deletions lua/telescope/_extensions/command_palette.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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 },
}
25 changes: 0 additions & 25 deletions lua/telescope/_extensions/my_find_files.lua

This file was deleted.

1 change: 1 addition & 0 deletions lua/telescope/_extensions/obsidian.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b41f281

Please sign in to comment.