Skip to content

Commit

Permalink
feat(telescope)!: create telescope extension for pin picker
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimjii authored and axkirillov committed Oct 29, 2023
1 parent 610a604 commit c1daf6f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 61 deletions.
43 changes: 21 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
```

Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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.
Expand All @@ -170,3 +168,4 @@ lualine_c = {
}
}
```

10 changes: 2 additions & 8 deletions lua/hbac/command/subcommands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 2 additions & 14 deletions lua/hbac/config.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
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,
Expand All @@ -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" },
Expand Down
36 changes: 19 additions & 17 deletions lua/hbac/telescope/init.lua
Original file line number Diff line number Diff line change
@@ -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 = {
["<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 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
Expand All @@ -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),
Expand Down
7 changes: 7 additions & 0 deletions lua/telescope/_extensions/hbac.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local telescope = require('telescope')

return telescope.register_extension({
exports = {
buffers = require('hbac.telescope').pin_picker,
}
})

0 comments on commit c1daf6f

Please sign in to comment.