Skip to content

Commit

Permalink
feat(scope): Add autocommands for keymap hooks.
Browse files Browse the repository at this point in the history
  • Loading branch information
DrKJeff16 committed Jun 1, 2024
1 parent fba0ea7 commit 003572e
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions lua/lazy_cfg/scope/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@

local User = require('user')
local Check = User.check
local maps_t = User.types.user.maps

local exists = Check.exists.module
local is_tbl = Check.value.is_tbl
local is_int = Check.value.is_int
local empty = Check.value.empty

if not exists('scope') then
return
end

local au_exec = vim.api.nvim_exec_autocmds
local augroup = vim.api.nvim_create_augroup
local au = vim.api.nvim_create_autocmd

local Scope = require('scope')

Expand All @@ -29,3 +35,86 @@ if exists('barbar') then
end

Scope.setup(opts)

local tab_hook = function()
local kmap = User.maps.kmap
local WK = User.maps.wk

local desc = kmap.desc
local nop = User.maps.nop

---@type fun(tabnr: integer?): fun()
local function tab_cmd(tabnr)
if not is_int(tabnr) or empty(tabnr) then
return function()
vim.cmd('ScopeMoveBuf')
end
end

return function()
vim.cmd('ScopeMoveBuf' .. tostring(tabnr))
end
end

local prefix = '<leader>b<C-t>'
local nop_opts = { noremap = false, nowait = false, silent = true }
local tab_count = #vim.api.nvim_list_tabpages()

---@type KeyMapDict
local Keys = {
[prefix .. 't'] = { tab_cmd(), desc('Prompt To Move Buf To Tab') },
}

if tab_count > 1 then
local i = 1

while i < 10 do
local i_str = tostring(i)

if i <= tab_count then
Keys[prefix .. i_str] = { tab_cmd(i), desc('Move Current Buffer To Tab ' .. i_str) }
else
nop(prefix .. i_str, nop_opts, 'n')
end

i = i + 1
end

if WK.available() then
WK.register({ [prefix] = { name = '+Move Buff To Tab' } }, { mode = 'n' })

WK.register(WK.convert_dict(Keys), { mode = 'n' })
else
for lhs, v in next, Keys do
v[2] = is_tbl(v[2]) and v[2] or {}

kmap.n(lhs, v[1], v[2])
end
end
else
--- TODO: Fix parsing for `'which_key_ignore'` in `maps.wk.register`
--- HACK: Use `which-key` directly due to unnacounted behaviour
if WK.available() then
require('which-key').register({ [prefix] = 'which_key_ignore' }, { mode = 'n' })
end

local i = 1

while i < 10 do
local i_str = tostring(i)

nop(prefix .. i_str, nop_opts, 'n')

i = i + 1
end
end
end

au({ 'TabNew', 'TabNewEntered' }, {
group = augroup('ScopeMapHook', { clear = false }),
callback = tab_hook,
})
au('TabClosed', {
group = augroup('ScopeMapHook', { clear = false }),
callback = tab_hook,
})

0 comments on commit 003572e

Please sign in to comment.