Skip to content

Commit 0b3d43e

Browse files
committed
refactor: remove utils
1 parent be3aa53 commit 0b3d43e

File tree

9 files changed

+182
-188
lines changed

9 files changed

+182
-188
lines changed

lua/tmux/layout/init.lua

+33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local log = require("tmux.log")
22
local parse = require("tmux.layout.parse")
3+
local nvim = require("tmux.wrapper.nvim")
34
local tmux = require("tmux.wrapper.tmux")
45

56
local direction_checks = {
@@ -53,6 +54,17 @@ local function check_is_border(display, id, direction)
5354
return nil
5455
end
5556

57+
--return true if there is no other nvim window in the direction of @border
58+
local function is_only_window(border)
59+
if border == "h" or border == "l" then
60+
--check for other horizontal window
61+
return (nvim.winnr("1h") == nvim.winnr("1l"))
62+
else
63+
--check for other vertical window
64+
return (nvim.winnr("1j") == nvim.winnr("1k"))
65+
end
66+
end
67+
5668
local M = {}
5769

5870
function M.is_border(direction)
@@ -67,4 +79,25 @@ function M.is_border(direction)
6779
return result
6880
end
6981

82+
function M.is_tmux_target(border)
83+
if not tmux.is_tmux then
84+
return false
85+
end
86+
87+
return not M.is_border(border) or is_only_window(border)
88+
end
89+
90+
function M.has_tmux_target(direction, persist_zoom, cycle_navigation)
91+
if not tmux.is_tmux then
92+
return false
93+
end
94+
if tmux.is_zoomed() and persist_zoom then
95+
return false
96+
end
97+
if not M.is_border(direction) then
98+
return true
99+
end
100+
return cycle_navigation and not M.is_border(nvim.opposite_direction(direction))
101+
end
102+
70103
return M

lua/tmux/navigation/navigate.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
local layout = require("tmux.layout")
12
local log = require("tmux.log")
23
local nvim = require("tmux.wrapper.nvim")
34
local options = require("tmux.configuration.options")
45
local tmux = require("tmux.wrapper.tmux")
5-
local utils = require("tmux.utils")
66

77
local M = {}
88

@@ -11,11 +11,11 @@ function M.to(direction)
1111

1212
local is_nvim_border = nvim.is_nvim_border(direction)
1313
local has_tmux_target =
14-
utils.has_tmux_target(direction, options.navigation.persist_zoom, options.navigation.cycle_navigation)
14+
layout.has_tmux_target(direction, options.navigation.persist_zoom, options.navigation.cycle_navigation)
1515
if (nvim.is_nvim_float() or is_nvim_border) and has_tmux_target then
1616
tmux.change_pane(direction)
1717
elseif is_nvim_border and options.navigation.cycle_navigation then
18-
nvim.wincmd(utils.opposite_direction(direction), 999)
18+
nvim.wincmd(nvim.opposite_direction(direction), 999)
1919
elseif not is_nvim_border then
2020
nvim.wincmd(direction, vim.v.count)
2121
end

lua/tmux/resize.lua

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
local layout = require("tmux.layout")
12
local keymaps = require("tmux.keymaps")
23
local nvim = require("tmux.wrapper.nvim")
34
local options = require("tmux.configuration.options")
45
local tmux = require("tmux.wrapper.tmux")
5-
local utils = require("tmux.utils")
66

77
local M = {}
8+
89
function M.setup()
910
if options.resize.enable_default_keybindings then
1011
keymaps.register("n", {
@@ -19,7 +20,7 @@ end
1920
function M.to_left(step)
2021
step = step or options.resize.resize_step_x
2122
local is_border = nvim.is_nvim_border("l")
22-
if (nvim.is_nvim_float() or is_border) and utils.is_tmux_target("l") then
23+
if (nvim.is_nvim_float() or is_border) and layout.is_tmux_target("l") then
2324
tmux.resize("h", step)
2425
elseif is_border then
2526
nvim.resize("x", "+", step)
@@ -31,7 +32,7 @@ end
3132
function M.to_bottom(step)
3233
step = step or options.resize.resize_step_y
3334
local is_border = nvim.is_nvim_border("j")
34-
if (nvim.is_nvim_float() or is_border) and utils.is_tmux_target("j") then
35+
if (nvim.is_nvim_float() or is_border) and layout.is_tmux_target("j") then
3536
tmux.resize("j", step)
3637
elseif is_border then
3738
nvim.resize("y", "-", step)
@@ -43,7 +44,7 @@ end
4344
function M.to_top(step)
4445
step = step or options.resize.resize_step_y
4546
local is_border = nvim.is_nvim_border("j")
46-
if (nvim.is_nvim_float() or is_border) and utils.is_tmux_target("j") then
47+
if (nvim.is_nvim_float() or is_border) and layout.is_tmux_target("j") then
4748
tmux.resize("k", step)
4849
elseif is_border then
4950
nvim.resize("y", "+", step)
@@ -55,7 +56,7 @@ end
5556
function M.to_right(step)
5657
step = step or options.resize.resize_step_x
5758
local is_border = nvim.is_nvim_border("l")
58-
if (nvim.is_nvim_float() or is_border) and utils.is_tmux_target("l") then
59+
if (nvim.is_nvim_float() or is_border) and layout.is_tmux_target("l") then
5960
tmux.resize("l", step)
6061
elseif is_border then
6162
nvim.resize("x", "-", step)

lua/tmux/swap.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
local keymaps = require("tmux.keymaps")
2-
local nvim = require("tmux.wrapper.nvim")
32
local options = require("tmux.configuration.options")
3+
local layout = require("tmux.layout")
4+
local nvim = require("tmux.wrapper.nvim")
45
local tmux = require("tmux.wrapper.tmux")
5-
local utils = require("tmux.utils")
66

77
local M = {}
88

@@ -20,11 +20,11 @@ end
2020
function M.to(direction)
2121
local is_nvim_border = nvim.is_nvim_border(direction)
2222
local persist_zoom = true -- tmux swap-pane when zoomed causes error
23-
local has_tmux_target = utils.has_tmux_target(direction, persist_zoom, options.swap.cycle_navigation)
23+
local has_tmux_target = layout.has_tmux_target(direction, persist_zoom, options.swap.cycle_navigation)
2424
if (nvim.is_nvim_float() or is_nvim_border) and has_tmux_target then
2525
tmux.swap(direction)
2626
elseif is_nvim_border and options.swap.cycle_navigation then
27-
nvim.swap(utils.opposite_direction(direction), 999)
27+
nvim.swap(nvim.opposite_direction(direction), 999)
2828
elseif not is_nvim_border then
2929
nvim.swap(direction, vim.v.count)
3030
end

lua/tmux/utils.lua

-44
This file was deleted.

lua/tmux/wrapper/nvim.lua

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local opposite_directions = { h = "l", j = "k", k = "j", l = "h" }
2+
13
local M = {}
24

35
function M.is_nvim_border(border)
@@ -36,4 +38,8 @@ function M.winnr(direction)
3638
return vim.api.nvim_call_function("winnr", { direction })
3739
end
3840

41+
function M.opposite_direction(direction)
42+
return opposite_directions[direction]
43+
end
44+
3945
return M

spec/tmux/layout/init_spec.lua

+123
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,126 @@ describe("check layout", function()
161161
assert.is_true(result)
162162
end)
163163
end)
164+
165+
describe("layout.has_tmux_target", function()
166+
local layout
167+
local tmux
168+
169+
setup(function()
170+
require("spec.tmux.mocks.log_mock").setup()
171+
require("spec.tmux.mocks.tmux_mock").setup("3.2a")
172+
173+
layout = require("tmux.layout")
174+
tmux = require("tmux.wrapper.tmux")
175+
end)
176+
177+
it("check is_tmux false", function()
178+
tmux.is_tmux = false
179+
180+
local result = layout.has_tmux_target("h", false, true)
181+
assert.is_false(result)
182+
183+
result = layout.has_tmux_target("j", false, true)
184+
assert.is_false(result)
185+
186+
result = layout.has_tmux_target("k", false, true)
187+
assert.is_false(result)
188+
189+
result = layout.has_tmux_target("l", false, true)
190+
assert.is_false(result)
191+
end)
192+
193+
it("check is_zoomed true", function()
194+
tmux.is_tmux = true
195+
tmux.is_zoomed = function()
196+
return true
197+
end
198+
layout.is_border = function(_)
199+
return false
200+
end
201+
202+
local result = layout.has_tmux_target("h", true, true)
203+
assert.is_false(result)
204+
205+
result = layout.has_tmux_target("j", true, true)
206+
assert.is_false(result)
207+
208+
result = layout.has_tmux_target("k", true, true)
209+
assert.is_false(result)
210+
211+
result = layout.has_tmux_target("l", true, true)
212+
assert.is_false(result)
213+
214+
result = layout.has_tmux_target("h", false, true)
215+
assert.is_true(result)
216+
217+
result = layout.has_tmux_target("j", false, true)
218+
assert.is_true(result)
219+
220+
result = layout.has_tmux_target("k", false, true)
221+
assert.is_true(result)
222+
223+
result = layout.has_tmux_target("l", false, true)
224+
assert.is_true(result)
225+
end)
226+
227+
it("check is_border false", function()
228+
tmux.is_tmux = true
229+
tmux.is_zoomed = function()
230+
return false
231+
end
232+
layout.is_border = function(_)
233+
return false
234+
end
235+
236+
local result = layout.has_tmux_target("h", false, true)
237+
assert.is_true(result)
238+
239+
result = layout.has_tmux_target("j", false, true)
240+
assert.is_true(result)
241+
242+
result = layout.has_tmux_target("k", false, true)
243+
assert.is_true(result)
244+
245+
result = layout.has_tmux_target("l", false, true)
246+
assert.is_true(result)
247+
end)
248+
249+
it("check is_border true", function()
250+
tmux.is_tmux = true
251+
tmux.is_zoomed = function()
252+
return false
253+
end
254+
layout.is_border = function(_)
255+
return true
256+
end
257+
258+
local result = layout.has_tmux_target("h", false, false)
259+
assert.is_false(result)
260+
261+
result = layout.has_tmux_target("j", false, false)
262+
assert.is_false(result)
263+
264+
result = layout.has_tmux_target("k", false, false)
265+
assert.is_false(result)
266+
267+
result = layout.has_tmux_target("l", false, false)
268+
assert.is_false(result)
269+
end)
270+
271+
it("check cycle_navigation true", function()
272+
tmux.is_tmux = true
273+
tmux.is_zoomed = function()
274+
return false
275+
end
276+
layout.is_border = function(direction)
277+
return direction == "h"
278+
end
279+
280+
local result = layout.has_tmux_target("h", false, false)
281+
assert.is_false(result)
282+
283+
result = layout.has_tmux_target("h", false, true)
284+
assert.is_true(result)
285+
end)
286+
end)

0 commit comments

Comments
 (0)