Skip to content

Commit

Permalink
chore: layout and side checking stuffs
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts committed Jul 3, 2024
1 parent 5a04153 commit 14c86a7
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 86 deletions.
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ $(addprefix test-, $(TESTFILES)): test-%:
-c "lua MiniTest.run_file('tests/test_$*.lua', { execute = { reporter = MiniTest.gen_reporter.stdout({ group_depth = 2 }) } })"
deps:
./scripts/clone_deps.sh 1 || true
# bc for mini.nvim before this date
./scripts/reset_deps_at_date.sh ./deps/mini.nvim

deps-lint:
luarocks install argparse --force
Expand Down
8 changes: 5 additions & 3 deletions lua/no-neck-pain/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ function N.toggleSide(scope, side)
return N.disable(scope)
end

S.scanLayout(S, scope)

N.init(scope)
end

Expand Down Expand Up @@ -223,15 +225,15 @@ function N.enable(scope)
end

if
(S.isSideRegistered(S, "left") and not S.isSideWinValid(S, "left"))
or (S.isSideRegistered(S, "right") and not S.isSideWinValid(S, "right"))
(S.isSideWinValid(S, "left") and not S.isSideWinValid(S, "left"))
or (S.isSideWinValid(S, "right") and not S.isSideWinValid(S, "right"))
then
D.log(p.event, "one of the NNP side has been closed")

return N.disable(p.event)
end

return N.init(p.event)
-- return N.init(p.event)
end)
end,
group = augroupName,
Expand Down
55 changes: 22 additions & 33 deletions lua/no-neck-pain/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ end

---Whether the side is enabled in the config or not.
---
---@param side "left"|"right": the side of the window.
---@param side "left"|"right"|"curr": the side of the window.
---@return boolean
---@private
function State:isSideEnabled(side)
Expand Down Expand Up @@ -247,21 +247,19 @@ function State:isActiveTabRegistered()
and vim.api.nvim_tabpage_is_valid(self.activeTab)
end

---Whether the side window is registered and enabled in the config or not.
---Whether the side window is registered and a valid window.
---
---@param side "left"|"right"|"curr": the side of the window.
---@return boolean
---@private
function State:isSideRegistered(side)
if not self.isActiveTabRegistered(self) then
function State:isSideWinValid(side)
if side ~= "curr" and not self.isSideEnabled(self, side) then
return false
end

if self.getSideID(self, side) == nil then
return false
end
local id = self.getSideID(self, side)

return _G.NoNeckPain.config.buffers[side].enabled
return id ~= nil and vim.api.nvim_win_is_valid(id)
end

---Whether the sides window are registered and enabled in the config or not.
Expand All @@ -272,23 +270,12 @@ end
---@private
function State:checkSides(condition, expected)
if condition == "or" then
return self.isSideRegistered(self, "left") == expected
or self.isSideRegistered(self, "right") == expected
return self.isSideWinValid(self, "left") == expected
or self.isSideWinValid(self, "right") == expected
end

return self.isSideRegistered(self, "left") == expected
and self.isSideRegistered(self, "right") == expected
end

---Whether the side window is registered and a valid window.
---
---@param side "left"|"right"|"curr": the side of the window.
---@return boolean
---@private
function State:isSideWinValid(side)
local id = self.getSideID(self, side)

return id ~= nil and vim.api.nvim_win_is_valid(id)
return self.isSideWinValid(self, "left") == expected
and self.isSideWinValid(self, "right") == expected
end

---Whether the side window is the currently active one or not.
Expand Down Expand Up @@ -474,15 +461,16 @@ end
---@private
function State:setLayoutWindows(scope, wins)
for _, win in ipairs(wins) do
if win[1] == "leaf" then
local supported, name, integration = self.isSupportedIntegration(self, scope, win[2])
local id = win[2]
if win[1] == "leaf" and not A.isRelativeWindow(id) then
local supported, name, integration = self.isSupportedIntegration(self, scope, id)
if supported and name and integration then
integration.width = vim.api.nvim_win_get_width(win[2]) * 2
integration.id = win[2]
integration.width = vim.api.nvim_win_get_width(id) * 2
integration.id = id

self.tabs[self.activeTab].wins.integrations[name] = integration
else
self.tabs[self.activeTab].wins.vsplits[win[2]] = true
self.tabs[self.activeTab].wins.vsplits[id] = true
end
end
end
Expand All @@ -499,10 +487,11 @@ end
---@private
function State:walkLayout(scope, parent, curr)
for _, group in ipairs(curr) do
if type(group) == "table" then
if type(group) == "table" and group[1] ~= "leaf" then
if parent == "row" then
self.setLayoutWindows(self, scope, group)
parent = nil
-- if we are on a `col`, we don't care about how many windows are in it,
-- because their width is grouped, so one is enough to keep track of
self.setLayoutWindows(self, scope, group[1] == "col" and { group[2][1] } or group)
end
self.walkLayout(self, scope, parent, group)
elseif group == "leaf" and parent == nil then
Expand All @@ -526,9 +515,9 @@ function State:scanLayout(scope)
self.walkLayout(self, scope, nil, vim.fn.winlayout(self.activeTab))
self.save(self)

local _, nbNBVsplits = self.getVSplits(self)
local vsplits, nbNBVsplits = self.getVSplits(self)

D.log(scope, "computed %d vsplits", nbVSplits)
D.log(scope, "computed vsplits: %s", vim.inspect(vsplits))

-- TODO: real table diff
return nbVSplits ~= nbNBVsplits
Expand Down
44 changes: 16 additions & 28 deletions lua/no-neck-pain/wins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ function W.createSideBuffers(skipIntegrations)
end

for _, side in pairs(Co.SIDES) do
if S.isSideRegistered(S, side) then
if S.isSideWinValid(S, side) then
local padding = wins[side].padding or W.getPadding(side)

if padding > _G.NoNeckPain.config.minSideBufferWidth then
Expand All @@ -180,21 +180,19 @@ function W.createSideBuffers(skipIntegrations)

-- if we still have side buffers open at this point, and we have vsplit opened,
-- there might be width issues so we the resize opened vsplits.
if (leftID or rightID) and nbVSplits > 0 then
-- local sWidth = wins.left.padding or wins.right.padding
-- local nbSide = leftID and rightID and 2 or 1
--
-- -- get the available usable width (screen size without side paddings)
-- sWidth = vim.api.nvim_list_uis()[1].width - sWidth * nbSide
-- sWidth = math.floor(sWidth / (nbVSplits - nbSide))
if (leftID or rightID) and nbVSplits > 1 then
local sWidth = wins.left.padding or wins.right.padding
local nbSide = leftID and rightID and 2 or 1

-- get the available usable width (screen size without side paddings)
sWidth = vim.api.nvim_list_uis()[1].width - sWidth * nbSide
sWidth = math.floor(sWidth / (nbVSplits - nbSide))

for vsplit, _ in pairs(vsplits) do
if vsplit ~= leftID and vsplit ~= rightID and vsplit ~= S.getSideID(S, "curr") then
resize(vsplit, _G.NoNeckPain.config.width, "vsplit")
resize(vsplit, sWidth, string.format("vsplit:%s", vsplit))
end
end

resize(S.getSideID(S, "curr"), _G.NoNeckPain.config.width, "curr")
end

-- closing integrations and reopening them means new window IDs
Expand All @@ -210,28 +208,18 @@ end
---@private
function W.getPadding(side)
local scope = string.format("W.getPadding:%s", side)
local uis = vim.api.nvim_list_uis()

if uis[1] == nil then
error("attempted to get the padding of a non-existing UI.")

return 0
end

local width = uis[1].width

-- if the available screen size is lower than the config width,
-- we don't have to create side buffers.
if _G.NoNeckPain.config.width >= width then
D.log(scope, "[%s] - ui %s - no space left to create side buffers", side, width)
if _G.NoNeckPain.config.width >= vim.o.columns then
D.log(scope, "[%s] - ui %s - no space left to create side buffers", side, vim.o.columns)

return 0
end

local _, columns = S.getVSplits(S)

for _, s in ipairs(Co.SIDES) do
if S.isSideEnabled(S, s) and columns > 1 then
if S.isSideWinValid(S, s) and columns > 1 then
columns = columns - 1
end
end
Expand All @@ -243,13 +231,13 @@ function W.getPadding(side)

-- if there's no space left according to the config width,
-- then we don't have to create side buffers.
if occupied >= width then
if occupied >= vim.o.columns then
D.log(scope, "%d occupied - no space left to create side", occupied)

return 0
end

D.log(scope, "%d/%d with vsplits, computing integrations", occupied, width)
D.log(scope, "%d/%d with vsplits, computing integrations", occupied, vim.o.columns)

-- now we need to determine how much we should substract from the remaining padding
-- if there's side integrations open.
Expand All @@ -267,9 +255,9 @@ function W.getPadding(side)
end
end

local final = math.floor((width - occupied) / 2)
local final = math.floor((vim.o.columns - occupied) / 2)

D.log(scope, "%d/%d with integrations - final %d", occupied, width, final)
D.log(scope, "%d/%d with integrations - final %d", occupied, vim.o.columns, final)

return final
end
Expand Down
5 changes: 0 additions & 5 deletions tests/test_autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ T["auto command"]["disabling clears VimEnter autocmd"] = function()
end

T["auto command"]["does not shift when opening/closing float window"] = function()
child.set_size(5, 200)
child.lua([[ require('no-neck-pain').setup({width=50}) ]])
Helpers.toggle(child)

Expand Down Expand Up @@ -86,7 +85,6 @@ 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 }}) ]]
)
Expand Down Expand Up @@ -128,7 +126,6 @@ T["skipEnteringNoNeckPainBuffer"]["goes to new valid buffer when entering side"]
end

T["skipEnteringNoNeckPainBuffer"]["does not register if scratchPad feature is enabled (global)"] = function()
child.set_size(5, 200)
child.lua(
[[ require('no-neck-pain').setup({width=50, buffers = { scratchPad = { enabled = true } }, autocmds = { skipEnteringNoNeckPainBuffer = true }}) ]]
)
Expand All @@ -147,7 +144,6 @@ T["skipEnteringNoNeckPainBuffer"]["does not register if scratchPad feature is en
end

T["skipEnteringNoNeckPainBuffer"]["does not register if scratchPad feature is enabled (left)"] = function()
child.set_size(5, 200)
child.lua(
[[ require('no-neck-pain').setup({width=50, buffers = { left = { scratchPad = { enabled = true } } }, autocmds = { skipEnteringNoNeckPainBuffer = true }}) ]]
)
Expand All @@ -166,7 +162,6 @@ T["skipEnteringNoNeckPainBuffer"]["does not register if scratchPad feature is en
end

T["skipEnteringNoNeckPainBuffer"]["does not register if scratchPad feature is enabled (right)"] = function()
child.set_size(5, 200)
child.lua(
[[ require('no-neck-pain').setup({width=50, buffers = { right = { scratchPad = { enabled = true } } }, autocmds = { skipEnteringNoNeckPainBuffer = true }}) ]]
)
Expand Down
7 changes: 0 additions & 7 deletions tests/test_integrations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ T["nvimdapui"]["keeps sides open"] = function()
end

child.restart({ "-u", "scripts/init_with_nvimdapui.lua" })
child.set_size(20, 100)

Helpers.toggle(child)

Expand Down Expand Up @@ -164,7 +163,6 @@ T["neotest"] = MiniTest.new_set()

T["neotest"]["keeps sides open"] = function()
child.restart({ "-u", "scripts/init_with_neotest.lua", "lua/no-neck-pain/main.lua" })
child.set_size(20, 100)

Helpers.toggle(child)

Expand Down Expand Up @@ -193,7 +191,6 @@ T["outline"] = MiniTest.new_set()

T["outline"]["keeps sides open"] = function()
child.restart({ "-u", "scripts/init_with_outline.lua", "lua/no-neck-pain/main.lua" })
child.set_size(20, 100)

Helpers.toggle(child)

Expand Down Expand Up @@ -266,7 +263,6 @@ T["neo-tree"] = MiniTest.new_set()

T["neo-tree"]["keeps sides open"] = function()
child.restart({ "-u", "scripts/init_with_neotree.lua", "foo" })
child.set_size(5, 300)

Helpers.toggle(child)

Expand Down Expand Up @@ -305,7 +301,6 @@ T["TSPlayground"] = MiniTest.new_set()

T["TSPlayground"]["keeps sides open"] = function()
child.restart({ "-u", "scripts/init_with_tsplayground.lua" })
child.set_size(5, 300)

Helpers.toggle(child)

Expand Down Expand Up @@ -354,7 +349,6 @@ end

T["TSPlayground"]["reduces `left` side if only active when integration is on `right`"] = function()
child.restart({ "-u", "scripts/init_with_tsplayground.lua" })
child.set_size(5, 300)

child.lua([[
require('no-neck-pain').setup({
Expand Down Expand Up @@ -426,7 +420,6 @@ T["aerial"]["keeps sides open"] = function()
end

child.restart({ "-u", "scripts/init_with_aerial.lua" })
child.set_size(5, 500)

Helpers.toggle(child)

Expand Down
4 changes: 0 additions & 4 deletions tests/test_options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ local T = MiniTest.new_set({
T["minSideBufferWidth"] = MiniTest.new_set()

T["minSideBufferWidth"]["closes side buffer respecting the given value"] = function()
child.set_size(500, 500)
child.lua([[ require('no-neck-pain').setup({width=50}) ]])
Helpers.toggle(child)

Expand All @@ -38,7 +37,6 @@ end
T["killAllBuffersOnDisable"] = MiniTest.new_set()

T["killAllBuffersOnDisable"]["closes every windows when disabling the plugin"] = function()
child.set_size(500, 500)
child.lua([[ require('no-neck-pain').setup({width=20,killAllBuffersOnDisable=true}) ]])
Helpers.toggle(child)

Expand All @@ -60,7 +58,6 @@ end
T["fallbackOnBufferDelete"] = MiniTest.new_set()

T["fallbackOnBufferDelete"]["invoking :bd keeps nnp enabled"] = function()
child.set_size(500, 500)
child.lua([[ require('no-neck-pain').setup({width=50,fallbackOnBufferDelete=true}) ]])

Helpers.expect.config(child, "fallbackOnBufferDelete", true)
Expand All @@ -76,7 +73,6 @@ T["fallbackOnBufferDelete"]["invoking :bd keeps nnp enabled"] = function()
end

T["fallbackOnBufferDelete"]["still allows nvim to quit"] = function()
child.set_size(500, 500)
child.lua([[ require('no-neck-pain').setup({width=50,fallbackOnBufferDelete=true}) ]])
Helpers.toggle(child)

Expand Down
4 changes: 0 additions & 4 deletions tests/test_tabs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ T["TabNewEntered"]["starts the plugin on new tab"] = function()
Helpers.expect.state(child, "activeTab", 2)

Helpers.expect.equality(Helpers.winsInTab(child), { 1004, 1003, 1005 })

child.stop()
end

T["TabNewEntered"]["does not re-enable if the user disables it"] = function()
Expand Down Expand Up @@ -143,8 +141,6 @@ T["TabNewEntered"]["does not re-enable if the user disables it"] = function()
Helpers.expect.state(child, "activeTab", 2)

Helpers.expect.equality(Helpers.winsInTab(child), { 1003 })

child.stop()
end

T["tabnew/tabclose"] = MiniTest.new_set()
Expand Down

0 comments on commit 14c86a7

Please sign in to comment.