Skip to content

Commit

Permalink
feat: even less debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts committed Mar 22, 2024
1 parent e1399af commit 8ccab35
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
8 changes: 3 additions & 5 deletions lua/no-neck-pain/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function NoNeckPain.toggleScratchPad()
_G.NoNeckPain.config = C.options
end

A.debounce("publicAPI_toggleScratchPad", M.toggleScratchPad)
M.toggleScratchPad()
end

--- Sets the config `width` to the given `width` value and resizes the NoNeckPain windows.
Expand All @@ -45,9 +45,7 @@ function NoNeckPain.resize(width)
_G.NoNeckPain.config = vim.tbl_deep_extend("keep", { width = width }, _G.NoNeckPain.config)
end

A.debounce("publicAPI_resize", function(scope)
M.init(scope, false)
end)
M.init("publicAPI_resize", false)
end

--- Toggles the config `${side}.enabled` and re-inits the plugin.
Expand Down Expand Up @@ -99,7 +97,7 @@ function NoNeckPain.setup(opts)
end

_G.NoNeckPain.config = C.defaults(opts)
A.debounce("ColorScheme", M.init)
M.init("ColorScheme")
end)
end,
group = "NoNeckPainAutocmd",
Expand Down
31 changes: 14 additions & 17 deletions lua/no-neck-pain/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -187,30 +187,30 @@ function N.enable(scope)

vim.api.nvim_create_autocmd({ "WinEnter" }, {
callback = function(p)
A.debounce(string.format("%s:splits", p.event), function(debounceScope)
vim.schedule(function()
if not S.hasTabs(S) or E.skip(S.getTab(S)) then
return D.log(debounceScope, "skip split logic")
return D.log(p.event, "skip split logic")
end

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

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

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

local wins = S.getUnregisteredWins(S)

if #wins ~= 1 then
return D.log(
debounceScope,
p.event,
"skip split logic: no new or too many unregistered windows"
)
end
Expand All @@ -221,7 +221,7 @@ function N.enable(scope)
S.setSplit(S, { id = focusedWin, vertical = isVSplit })

if isVSplit then
N.init(debounceScope)
N.init(p.event)
end
end)
end,
Expand Down Expand Up @@ -330,27 +330,24 @@ function N.enable(scope)

vim.api.nvim_create_autocmd({ "WinEnter", "WinClosed" }, {
callback = function(p)
A.debounce(string.format("%s:integrations", p.event), function(debounceScope)
vim.schedule(function()
if not S.hasTabs(S) or not S.isActiveTabRegistered(S) or E.skip(S.getTab(S)) then
return D.log(debounceScope, "skip integrations logic")
return D.log(p.event, "skip integrations logic")
end

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

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

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

N.init(debounceScope, false, true)
N.init(p.event, false, true)
end)
end,
group = augroupName,
Expand Down
7 changes: 7 additions & 0 deletions lua/no-neck-pain/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,29 @@ function State:closeIntegration()

for name, opts in pairs(self.tabs[self.activeTab].wins.integrations) do
if opts.id ~= nil and opts.close ~= nil then
local scope = string.format("closeIntegration:%s", name)
-- if this integration doesn't belong to any side we don't have to
-- close it to redraw side buffers
local side = _G.NoNeckPain.config.integrations[name].position
if side ~= "left" and side ~= "right" then
D.log(scope, "skipped because not a side integration")

goto continue
end

-- first element in the current wins list means it's the far left one,
-- if the integration is already at this spot then we don't have to close anything
if side == "left" and wins[1] == self.tabs[self.activeTab].wins.main[side] then
D.log(scope, "skipped because already at the far left side")

goto continue
end

-- last element in the current wins list means it's the far right one,
-- if the integration is already at this spot then we don't have to close anything
if side == "right" and wins[#wins] == self.tabs[self.activeTab].wins.main[side] then
D.log(scope, "skipped because already at the far right side")

goto continue
end

Expand Down
2 changes: 1 addition & 1 deletion tests/test_integrations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ T["TSPlayground"]["keeps sides open"] = function()
fileTypePattern = "tsplayground",
id = 1004,
open = "TSPlaygroundToggle",
width = 198,
width = 346,
})

Helpers.expect.state(child, "tabs[1].wins.main", {
Expand Down

0 comments on commit 8ccab35

Please sign in to comment.