diff --git a/README.md b/README.md index d4f9650..fdcf532 100644 --- a/README.md +++ b/README.md @@ -12,29 +12,13 @@ The main feature of this plugin, however, is the automatic closing of buffers. I with [packer.nvim](https://github.com/wbthomason/packer.nvim) ```lua -use { - 'axkirillov/hbac.nvim', - requires = { - -- these are optional, add them, if you want the telescope module - 'nvim-telescope/telescope.nvim', - 'nvim-lua/plenary.nvim', - 'nvim-tree/nvim-web-devicons' - } -} +use { 'axkirillov/hbac.nvim' } ``` with [lazy.nvim](https://github.com/folke/lazy.nvim) ```lua { 'axkirillov/hbac.nvim', - dependencies = { - -- these are optional, add them, if you want the telescope module - 'nvim-telescope/telescope.nvim', - 'nvim-lua/plenary.nvim', - 'nvim-tree/nvim-web-devicons' - }, - config = function () - require("hbac").setup() - end + config = true, } ``` @@ -74,12 +58,26 @@ hbac.close_unpinned() hbac.pin_all() hbac.unpin_all() hbac.toggle_autoclose() -hbac.telescope() ``` -## Telescope integration +## Telescope extension + +The plugin provides a [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim) extension to view and manage the pin states of buffers. This requires telescope and its dependency [plenary.nvim](https://github.com/nvim-lua/plenary.nvim). [nvim-web-devicons](https://github.com/nvim-tree/nvim-web-devicons) is also recommended. + +### Usage +To use the telescope picker, load the hbac telescope extension in your config file. +```lua +require('telescope').load_extension('hbac') +``` -The plugin provides a [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim) integration to view and manage the pin states of buffers. This requires telescope and its dependency [plenary.nvim](https://github.com/nvim-lua/plenary.nvim). [nvim-web-devicons](https://github.com/nvim-tree/nvim-web-devicons) is also recommended. +After loading the extension, you can execute the picker like so: +``` +:Telescope hbac buffers +``` +or if you prefer lua: +```lua +require('telescope').extensions.hbac.buffers() +``` The picker provides the following actions: @@ -145,7 +143,7 @@ telescope = { You can also pass options to the picker directly when calling its function. These options will deep-extend your setup table. ```lua -require("hbac").telescope({ +require("telescope").extensions.hbac.buffers({ file_ignore_patterns = { ".json" }, layout_strategy = "horizontal", -- etc. @@ -170,3 +168,4 @@ lualine_c = { } } ``` + diff --git a/lua/hbac/command/subcommands.lua b/lua/hbac/command/subcommands.lua index 37c3ae4..d5bdbab 100644 --- a/lua/hbac/command/subcommands.lua +++ b/lua/hbac/command/subcommands.lua @@ -28,14 +28,8 @@ M.toggle_autoclose = function() vim.notify("Autoclose " .. autoclose_state, vim.log.levels.INFO, notify_opts) end -M.telescope = function(opts) - local hbac_telescope = require("hbac.telescope") - 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) +M.telescope = function() + vim.notify('This command has been removed. Check the docs for a migration guide.', vim.log.levels.WARN, notify_opts) end return M diff --git a/lua/hbac/config.lua b/lua/hbac/config.lua index 33ee460..f00dd72 100644 --- a/lua/hbac/config.lua +++ b/lua/hbac/config.lua @@ -1,15 +1,5 @@ -local actions = require("hbac.telescope.actions") - local M = {} -local action_mappings = { - [""] = actions.close_unpinned, - [""] = actions.delete_buffer, - [""] = actions.pin_all, - [""] = actions.unpin_all, - [""] = actions.toggle_pin, -} - local defaults = { autoclose = true, threshold = 10, @@ -19,10 +9,8 @@ local defaults = { sort_mru = true, sort_lastused = true, selection_strategy = "row", - mappings = { - n = action_mappings, - i = action_mappings, - }, + use_default_mappings = true, + mappings = nil, pin_icons = { pinned = { "󰐃 ", hl = "DiagnosticOk" }, unpinned = { "󰤱 ", hl = "DiagnosticError" }, diff --git a/lua/hbac/telescope/init.lua b/lua/hbac/telescope/init.lua index 30d8f76..be7be7a 100644 --- a/lua/hbac/telescope/init.lua +++ b/lua/hbac/telescope/init.lua @@ -1,26 +1,20 @@ -local missing = "" -for _, dependency in ipairs({ "plenary", "telescope" }) do - if not pcall(require, dependency) then - missing = missing .. "\n- " .. dependency .. ".nvim" - end -end - -if missing ~= "" then - local msg = "Missing dependencies:" .. missing - vim.notify(msg, vim.log.levels.ERROR, { title = "Hbac" }) - return false -end - +local actions = require("hbac.telescope.actions") local pickers = require("telescope.pickers") local telescope_conf = require("telescope.config").values +local default_mappings = { + [""] = actions.close_unpinned, + [""] = actions.delete_buffer, + [""] = actions.pin_all, + [""] = actions.unpin_all, + [""] = actions.toggle_pin, +} + 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 mode, hbac_telescope_mappings in pairs(opts.mappings) do for key, action in pairs(hbac_telescope_mappings) do map(mode, key, action) end @@ -29,9 +23,17 @@ local attach_mappings = function(opts) end end +local parse_opts = function(opts) + local telescope_opts = require("hbac.config").values.telescope + opts.mappings = telescope_opts.use_default_mappings + and vim.tbl_deep_extend('force', { i = default_mappings, n = default_mappings }, telescope_opts.mappings or {}) + or (telescope_opts.mappings or {}) + return vim.tbl_deep_extend("force", telescope_opts, opts or {}) +end + M.pin_picker = function(opts) local make_finder = require("hbac.telescope.make_finder") - opts = opts or {} + opts = parse_opts(opts or {}) pickers.new(opts, { prompt_title = "Hbac Pin States", finder = make_finder.make_finder(opts), diff --git a/lua/telescope/_extensions/hbac.lua b/lua/telescope/_extensions/hbac.lua new file mode 100644 index 0000000..be9b6ae --- /dev/null +++ b/lua/telescope/_extensions/hbac.lua @@ -0,0 +1,7 @@ +local telescope = require('telescope') + +return telescope.register_extension({ + exports = { + buffers = require('hbac.telescope').pin_picker, + } +})