Skip to content

feat(integrations): skip redraw on every calls #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
steps:
- uses: actions/checkout@v4

- uses: JohnnyMorganz/stylua-action@v2
- uses: JohnnyMorganz/stylua-action@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
version: latest
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ $(addprefix test-, $(TESTFILES)): test-%:
-c "lua MiniTest.run_file('tests/test_$*.lua', { execute = { reporter = MiniTest.gen_reporter.stdout({ group_depth = 2 }) } })"
deps:
git clone --depth 1 https://github.com/nvim-lua/plenary.nvim deps/plenary
git clone --depth 1 https://github.com/nvim-neotest/nvim-nio deps/nvim-nio
git clone --depth 1 https://github.com/echasnovski/mini.nvim deps/mini.nvim
git clone --depth 1 https://github.com/nvim-treesitter/nvim-treesitter deps/nvim-treesitter
git clone --depth 1 https://github.com/nvim-treesitter/playground deps/playground
Expand Down
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
34 changes: 33 additions & 1 deletion lua/no-neck-pain/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,43 @@ end
---@return boolean: whether we closed something or not.
---@private
function State:closeIntegration()
local wins = vim.api.nvim_list_wins()
local hasClosedIntegration = false

for _, opts in pairs(self.tabs[self.activeTab].wins.integrations) do
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

D.log(string.format("closeIntegration:%s", name), "integration was opened")

vim.cmd(opts.close)
hasClosedIntegration = true
end
::continue::
end

return hasClosedIntegration
Expand All @@ -89,6 +119,8 @@ function State:reopenIntegration()
and opts.open ~= nil
and _G.NoNeckPain.config.integrations[name].reopen == true
then
D.log(string.format("reopenIntegration:%s", name), "integration was closed previously")

vim.cmd(opts.open)
end
end
Expand Down
1 change: 1 addition & 0 deletions scripts/init_with_neotest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ vim.cmd([[let &rtp.=','.getcwd()]])

vim.cmd("set rtp+=deps/mini.nvim")
vim.cmd("set rtp+=deps/plenary")
vim.cmd("set rtp+=deps/nvim-nio")
vim.cmd("set rtp+=deps/fixcursorhold")
vim.cmd("set rtp+=deps/neotest")

Expand Down
1 change: 1 addition & 0 deletions scripts/init_with_nvimdapui.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
vim.cmd([[let &rtp.=','.getcwd()]])

vim.cmd("set rtp+=deps/mini.nvim")
vim.cmd("set rtp+=deps/nvim-nio")
vim.cmd("set rtp+=deps/nvimdap")
vim.cmd("set rtp+=deps/nvimdapui")

Expand Down
6 changes: 2 additions & 4 deletions tests/test_commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ T["commands"]["NoNeckPainWidthUp increases the width by 5"] = function()
Helpers.expect.global(child, "_G.NoNeckPain.config.width", 150)
end

T["commands"]["NoNeckPainWidthUp increases the width by N when mappings.widthUp is configured"] = function(
)
T["commands"]["NoNeckPainWidthUp increases the width by N when mappings.widthUp is configured"] = function()
child.lua([[require('no-neck-pain').setup({
mappings = {
widthUp = {mapping = "<Leader>k-", value = 12},
Expand Down Expand Up @@ -127,8 +126,7 @@ T["commands"]["NoNeckPainWidthUp decreases the width by 5"] = function()
Helpers.expect.global(child, "_G.NoNeckPain.config.width", 50)
end

T["commands"]["NoNeckPainWidthUp decreases the width by N when mappings.widthDown is configured"] = function(
)
T["commands"]["NoNeckPainWidthUp decreases the width by N when mappings.widthDown is configured"] = function()
child.lua([[require('no-neck-pain').setup({
mappings = {
widthDown = {mapping = "<Leader>k-", value = 8},
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