Skip to content

Commit

Permalink
feat(telescope)!: restructure mapping config and accept mappings op…
Browse files Browse the repository at this point in the history
…tion

BREAKING CHANGE: The configuration for Hbac's telescope picker now follows the standard Telescope key mapping pattern of a key(string):function key/value pair. See `hbac.config` for changes

Most options that can be found in `h: telescope.builtin.buffers()` and in `h: telescope.setup()` that are relevant to a buffer picker are now accepted in the `telescope` configuration table, including `mappings`. The defaults can be overriden by the user's `setup()` call, and both the user and the default opts can be overriden by an `opts` table passed to the picker's function call, e.g. `hbac.telescope({sort_mru = false})`
  • Loading branch information
al-ce authored and axkirillov committed Oct 6, 2023
1 parent bf22ddf commit 610a604
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 98 deletions.
11 changes: 6 additions & 5 deletions lua/hbac/command/actions.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
local autocommands = require("hbac.autocommands")
local config = require("hbac.config")
local state = require("hbac.state")
local utils = require("hbac.utils")

local M = {}

M.close_unpinned = function()
local config = require("hbac.config")
local utils = require("hbac.utils")
local buflist = utils.get_listed_buffers()
for _, bufnr in ipairs(buflist) do
if utils.buf_autoclosable(bufnr) then
Expand All @@ -14,13 +13,14 @@ M.close_unpinned = function()
end
end

M.toggle_pin = function()
local bufnr = vim.api.nvim_get_current_buf()
M.toggle_pin = function(bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
local pinned_state = state.toggle_pin(bufnr) and "pinned" or "unpinned"
return bufnr, pinned_state
end

M.set_all = function(pin_value)
local utils = require("hbac.utils")
local buflist = utils.get_listed_buffers()
for _, bufnr in ipairs(buflist) do
state.pinned_buffers[bufnr] = pin_value
Expand All @@ -36,6 +36,7 @@ M.unpin_all = function()
end

M.toggle_autoclose = function()
local autocommands = require("hbac.autocommands")
state.autoclose_enabled = not state.autoclose_enabled
if state.autoclose_enabled then
autocommands.autoclose.setup()
Expand Down
2 changes: 2 additions & 0 deletions lua/hbac/command/subcommands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ M.telescope = function(opts)
if not hbac_telescope then
return
end
local telescope_opts = require("hbac.config").values.telescope
opts = vim.tbl_deep_extend("force", telescope_opts, opts or {})
hbac_telescope.pin_picker(opts)
end

Expand Down
33 changes: 16 additions & 17 deletions lua/hbac/config.lua
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
local actions = require("hbac.telescope.actions")

local M = {}

local action_mappings = {
["<M-c>"] = actions.close_unpinned,
["<M-x>"] = actions.delete_buffer,
["<M-a>"] = actions.pin_all,
["<M-u>"] = actions.unpin_all,
["<M-y>"] = actions.toggle_pin,
}

local defaults = {
autoclose = true,
threshold = 10,
close_buffers_with_windows = false,
close_command = function(bufnr)
vim.api.nvim_buf_delete(bufnr, {})
end,
close_command = function(bufnr) vim.api.nvim_buf_delete(bufnr, {}) end,
telescope = {
sort_mru = true,
sort_lastused = true,
selection_strategy = "row",
mappings = {
n = {
close_unpinned = "<M-c>",
delete_buffer = "<M-x>",
pin_all = "<M-a>",
unpin_all = "<M-u>",
toggle_selections = "<M-y>",
},
i = {
close_unpinned = "<M-c>",
delete_buffer = "<M-x>",
pin_all = "<M-a>",
unpin_all = "<M-u>",
toggle_selections = "<M-y>",
},
n = action_mappings,
i = action_mappings,
},
pin_icons = {
pinned = { "󰐃 ", hl = "DiagnosticOk" },
Expand Down
48 changes: 48 additions & 0 deletions lua/hbac/telescope/actions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
local action_state = require("telescope.actions.state")
local hbac_actions = require("hbac.command.actions")
local make_finder = require("hbac.telescope.make_finder")

local M = {}

local function execute_telescope_action(prompt_bufnr, action)
local finder, finder_opts = make_finder.make_finder, make_finder.finder_opts
local picker = action_state.get_current_picker(prompt_bufnr)
local multi_selection = picker:get_multi_selection()

if next(multi_selection) then
for _, entry in ipairs(multi_selection) do
action(entry.value)
end
else
local single_selection = action_state.get_selected_entry()
action(single_selection.value)
end

picker:refresh(finder(finder_opts), { reset_prompt = false })
end

local function hbac_toggle_pin(prompt_bufnr)
execute_telescope_action(prompt_bufnr, hbac_actions.toggle_pin)
end
local function hbac_pin_all(prompt_bufnr)
execute_telescope_action(prompt_bufnr, hbac_actions.pin_all)
end
local function hbac_unpin_all(prompt_bufnr)
execute_telescope_action(prompt_bufnr, hbac_actions.unpin_all)
end
local function hbac_close_unpinned(prompt_bufnr)
execute_telescope_action(prompt_bufnr, hbac_actions.close_unpinned)
end
local function hbac_delete_buffer(prompt_bufnr)
local close_command = require("hbac.config").values.close_command
execute_telescope_action(prompt_bufnr, close_command)
end


M.close_unpinned = hbac_close_unpinned
M.delete_buffer = hbac_delete_buffer
M.pin_all = hbac_pin_all
M.unpin_all = hbac_unpin_all
M.toggle_pin = hbac_toggle_pin

return M
65 changes: 0 additions & 65 deletions lua/hbac/telescope/attach_mappings.lua

This file was deleted.

32 changes: 21 additions & 11 deletions lua/hbac/telescope/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,30 @@ local telescope_conf = require("telescope.config").values

local M = {}

local attach_mappings = function(opts)
return function(_, map)
local telescope_mappings = require("hbac.config").values.telescope.mappings
telescope_mappings = vim.tbl_deep_extend("force", telescope_mappings, opts.mappings or {})
for mode, hbac_telescope_mappings in pairs(telescope_mappings) do
for key, action in pairs(hbac_telescope_mappings) do
map(mode, key, action)
end
end
return true
end
end

M.pin_picker = function(opts)
local attach_mappings = require("hbac.telescope.attach_mappings")
local make_finder = require("hbac.telescope.make_finder")
opts = opts or {}
pickers
.new(opts, {
prompt_title = "Hbac Pin States",
finder = make_finder.make_finder(opts),
sorter = telescope_conf.generic_sorter(opts),
attach_mappings = attach_mappings.attach_mappings,
previewer = telescope_conf.file_previewer(opts),
default_selection_index = make_finder.default_selection_idx,
})
:find()
pickers.new(opts, {
prompt_title = "Hbac Pin States",
finder = make_finder.make_finder(opts),
sorter = telescope_conf.generic_sorter(opts),
attach_mappings = attach_mappings(opts),
previewer = telescope_conf.file_previewer(opts),
default_selection_index = make_finder.default_selection_idx,
}):find()
end

return M

0 comments on commit 610a604

Please sign in to comment.