Skip to content

Commit

Permalink
feat(api): add autocmd to skip entering side buffer (#321)
Browse files Browse the repository at this point in the history
## 📃 Summary

close #306

add a new `autocmds` option named `skipEnteringNoNeckPainBuffer` (went
super explicit to let the user know it only triggers on no-neck-pain
buffers) which, when `true`, skips entering a side buffer until it finds
a valid one in the list of opened windows.

The option defaults to `false`
  • Loading branch information
shortcuts committed Mar 23, 2024
1 parent 96afa97 commit 5539239
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 13 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ require("no-neck-pain").setup({
-- When `true`, reloads the plugin configuration after a colorscheme change.
--- @type boolean
reloadOnColorSchemeChange = false,
-- When `true`, entering one of no-neck-pain side buffer will automatically skip it and go to the next available buffer.
--- @type boolean
skipEnteringNoNeckPainBuffer = false,
},
-- Creates mappings for you to easily interact with the exposed commands.
--- @type table
Expand Down
3 changes: 3 additions & 0 deletions doc/no-neck-pain.txt
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ values:
-- When `true`, reloads the plugin configuration after a colorscheme change.
--- @type boolean
reloadOnColorSchemeChange = false,
-- When `true`, entering one of no-neck-pain side buffer will automatically skip it and go to the next available buffer.
--- @type boolean
skipEnteringNoNeckPainBuffer = false,
},
-- Creates mappings for you to easily interact with the exposed commands.
--- @type table
Expand Down
3 changes: 3 additions & 0 deletions lua/no-neck-pain/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ NoNeckPain.options = {
-- When `true`, reloads the plugin configuration after a colorscheme change.
--- @type boolean
reloadOnColorSchemeChange = false,
-- When `true`, entering one of no-neck-pain side buffer will automatically skip it and go to the next available buffer.
--- @type boolean
skipEnteringNoNeckPainBuffer = false,
},
-- Creates mappings for you to easily interact with the exposed commands.
--- @type table
Expand Down
65 changes: 52 additions & 13 deletions lua/no-neck-pain/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -188,31 +188,29 @@ function N.enable(scope)
vim.api.nvim_create_autocmd({ "WinEnter" }, {
callback = function(p)
vim.schedule(function()
p.event = string.format("%s:split", p.event)
if not S.hasTabs(S) or E.skip(S.getTab(S)) then
return D.log(p.event, "skip split logic")
return D.log(p.event, "skip")
end

if S.checkSides(S, "and", false) then
return D.log(p.event, "skip split logic: no side buffer")
return D.log(p.event, "no side buffer")
end

if S.isSideTheActiveWin(S, "curr") then
return D.log(p.event, "skip split logic: current win")
return D.log(p.event, "current win")
end

-- an integration isn't considered as a split
local isSupportedIntegration = S.isSupportedIntegration(S, p.event, nil)
if isSupportedIntegration then
return D.log(p.event, "skip split logic: integration")
return D.log(p.event, "on an integration")
end

local wins = S.getUnregisteredWins(S)

if #wins ~= 1 then
return D.log(
p.event,
"skip split logic: no new or too many unregistered windows"
)
return D.log( p.event, "no new or too many unregistered windows")
end

local focusedWin = wins[1]
Expand Down Expand Up @@ -331,29 +329,70 @@ function N.enable(scope)
vim.api.nvim_create_autocmd({ "WinEnter", "WinClosed" }, {
callback = function(p)
vim.schedule(function()
local s = string.format("%s:integration", p.event)
if not S.hasTabs(S) or not S.isActiveTabRegistered(S) or E.skip(S.getTab(S)) then
return D.log(p.event, "skip integrations logic")
return D.log(s, "skip")
end

if S.wantsSides(S) and S.checkSides(S, "and", false) then
return D.log(p.event, "skip integrations logic: no side buffer")
return D.log(s, "no side buffer")
end

if p.event == "WinClosed" and not S.hasIntegrations(S) then
return D.log(p.event, "skip integrations logic: no registered integration")
return D.log(s, "no registered integration")
end

if p.event == "WinEnter" and #S.getUnregisteredWins(S) == 0 then
return D.log(p.event, "skip integrations logic: no new windows")
return D.log(s, "no new windows")
end

N.init(p.event, false, true)
N.init(s, false, true)
end)
end,
group = augroupName,
desc = "Resize to apply on WinEnter/Closed of an integration",
})

if _G.NoNeckPain.config.autocmds.skipEnteringNoNeckPainBuffer then
vim.api.nvim_create_autocmd({ "WinEnter" }, {
callback = function(p)
vim.schedule(function()
p.event = string.format("%s:skipEnteringNoNeckPainBuffer", p.event)
if
not S.hasTabs(S)
or not S.isActiveTabRegistered(S)
or E.skip()
then
return D.log(p.event, "skip")
end

local currentWin = vim.api.nvim_get_current_win()

if
currentWin ~= S.getSideID(S, "left")
and currentWin ~= S.getSideID(S, "right")
then
return
end

local wins = vim.api.nvim_list_wins()

for i = 1, #wins do
if wins[i] ~= currentWin then
goto continue
end

vim.fn.win_gotoid(wins[i == #wins and 1 or i + 1])

::continue::
end
end)
end,
group = augroupName,
desc = "Entering a no-neck-pain side buffer skips to the next available buffer",
})
end

S.save(S)
end

Expand Down
3 changes: 3 additions & 0 deletions tests/test_API.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ T["setup"]["sets exposed methods and default options value"] = function()
enableOnVimEnter = false,
enableOnTabEnter = false,
reloadOnColorSchemeChange = false,
skipEnteringNoNeckPainBuffer = false,
})

Helpers.expect.config(child, "mappings", {
Expand Down Expand Up @@ -156,6 +157,7 @@ T["setup"]["overrides default values"] = function()
enableOnVimEnter = true,
enableOnTabEnter = true,
reloadOnColorSchemeChange = true,
skipEnteringNoNeckPainBuffer = true,
},
debug = true,
disableOnLastBuffer = true,
Expand All @@ -171,6 +173,7 @@ T["setup"]["overrides default values"] = function()
enableOnVimEnter = true,
enableOnTabEnter = true,
reloadOnColorSchemeChange = true,
skipEnteringNoNeckPainBuffer = true,
})
end

Expand Down
42 changes: 42 additions & 0 deletions tests/test_autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,46 @@ T["auto command"]["does not shift when opening/closing float window"] = function
Helpers.expect.buf_width(child, "tabs[1].wins.main.right", 15)
end

T["skipEnteringNoNeckPainBuffer"] = MiniTest.new_set()

T["skipEnteringNoNeckPainBuffer"]["goes to new valid buffer when entering side"] = function()
child.set_size(5, 200)
child.lua([[ require('no-neck-pain').setup({width=50, autocmds = { skipEnteringNoNeckPainBuffer = true }}) ]])
Helpers.toggle(child)

Helpers.expect.config(child, "autocmds.skipEnteringNoNeckPainBuffer", true)

Helpers.expect.equality(Helpers.winsInTab(child), { 1001, 1000, 1002 })
Helpers.expect.equality(child.api.nvim_get_current_win(), 1000)

child.fn.win_gotoid(1001)
Helpers.wait(child)
Helpers.expect.equality(child.api.nvim_get_current_win(), 1000)

child.fn.win_gotoid(1002)
Helpers.wait(child)
Helpers.expect.equality(child.api.nvim_get_current_win(), 1000)

child.cmd("split")

Helpers.expect.equality(Helpers.winsInTab(child), { 1001, 1003, 1000, 1002 })
Helpers.expect.equality(child.api.nvim_get_current_win(), 1003)

child.fn.win_gotoid(1000)
Helpers.wait(child)
Helpers.expect.equality(child.api.nvim_get_current_win(), 1000)

child.fn.win_gotoid(1003)
Helpers.wait(child)
Helpers.expect.equality(child.api.nvim_get_current_win(), 1003)

child.fn.win_gotoid(1001)
Helpers.wait(child)
Helpers.expect.equality(child.api.nvim_get_current_win(), 1003)

child.fn.win_gotoid(1002)
Helpers.wait(child)
Helpers.expect.equality(child.api.nvim_get_current_win(), 1003)
end

return T

0 comments on commit 5539239

Please sign in to comment.