Skip to content
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

Fix when you can move, swap, and resize #25

Merged
merged 1 commit into from
Nov 28, 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
19 changes: 15 additions & 4 deletions lua/winmove/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ end
---@param mode winmove.Mode
---@return boolean
local function can_move_or_swap(win_id, mode)
local at_edge_horizontal = config.modes.move.at_edge.horizontal
local at_edge_horizontal = config.modes[mode].at_edge.horizontal

if at_edge_horizontal == winmove.AtEdge.MoveToTab then
if winutil.window_count() == 1 and vim.fn.tabpagenr("$") == 1 then
---@cast mode string
message.error(("Cannot %s window, only one window and tab"):format(mode:lower()))
return false
end
elseif at_edge_horizontal == winmove.AtEdge.Wrap then
else
if winutil.window_count() == 1 then
---@cast mode string
message.error(("Cannot %s window, only one window"):format(mode:lower()))
Expand Down Expand Up @@ -681,8 +681,15 @@ end
start_mode = function(mode)
local cur_win_id = api.nvim_get_current_win()

if not can_move_or_swap(cur_win_id, mode) then
return
if mode == winmove.Mode.Resize then
if winutil.window_count() == 1 then
message.error("Cannot resize window, only one window")
return
end
else
if not can_move_or_swap(cur_win_id, mode) then
return
end
end

if winmove.current_mode() == mode then
Expand Down Expand Up @@ -777,6 +784,10 @@ function winmove.resize_window(win_id, dir, count, anchor)
anchor = { anchor, resize.is_valid_anchor, "a valid anchor" },
})

if winutil.window_count() == 1 then
return
end

winutil.wincall(win_id, function()
resize.resize_window(win_id, dir, count, anchor)
end)
Expand Down
41 changes: 35 additions & 6 deletions tests/init_spec.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
local message = require("winmove.message")
local stub = require("luassert.stub")
local winmove = require("winmove")
local vader = require("winmove.util.vader")

local given = vader.given

describe("init", function()
it("starts and stops a mode", function()
-- Split a window so we can start move mode
vim.cmd.split()
given(function()
-- Split a window so we can start move mode
vim.cmd.split()

winmove.start_mode(winmove.Mode.Move)
assert.are.same(winmove.current_mode(), winmove.Mode.Move)
winmove.start_mode(winmove.Mode.Move)
assert.are.same(winmove.current_mode(), winmove.Mode.Move)

winmove.stop_mode()
assert.is_nil(winmove.current_mode())
winmove.stop_mode()
assert.is_nil(winmove.current_mode())
end)
end)

it("validates arguments of start_mode", function()
Expand Down Expand Up @@ -108,4 +113,28 @@ describe("init", function()
winmove.resize_window(1000, "h", 1, "top_right")
end, "anchor: expected a valid anchor, got top_right")
end)

it("does not start resize mode if only one window", function()
stub(message, "error")

winmove.start_mode(winmove.Mode.Resize)
assert.are.same(winmove.current_mode(), nil)

assert.stub(message.error).was.called_with("Cannot resize window, only one window")

---@diagnostic disable-next-line: undefined-field
message.error:revert()
end)

it("does not start resize mode if only one window", function()
stub(message, "error")

winmove.start_mode(winmove.Mode.Resize)
assert.are.same(winmove.current_mode(), nil)

assert.stub(message.error).was.called_with("Cannot resize window, only one window")

---@diagnostic disable-next-line: undefined-field
message.error:revert()
end)
end)
52 changes: 52 additions & 0 deletions tests/swap_mode_spec.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
local config = require("winmove.config")
local message = require("winmove.message")
local stub = require("luassert.stub")
local winmove = require("winmove")
local vader = require("winmove.util.vader")
local test_helpers = require("winmove.util.test_helpers")
Expand Down Expand Up @@ -149,4 +152,53 @@ describe("swap mode", function()
assert.are.same(vim.api.nvim_win_get_buf(target_win_id), bufnr1)
end)
end)

it("does not start swap mode if only one window", function()
config.configure({
modes = {
swap = {
at_edge = {
horizontal = "none",
},
},
},
})

stub(message, "error")

winmove.start_mode(winmove.Mode.Swap)
assert.are.same(winmove.current_mode(), nil)

assert.stub(message.error).was.called_with("Cannot swap window, only one window")

---@diagnostic disable-next-line: undefined-field
message.error:revert()
end)

it(
"does not start swap mode if only one window and tab and at-edge behaviour is MoveToTab",
function()
config.configure({
modes = {
swap = {
at_edge = {
horizontal = "move_to_tab",
},
},
},
})

stub(message, "error")

winmove.start_mode(winmove.Mode.Swap)
assert.are.same(winmove.current_mode(), nil)

assert
.stub(message.error).was
.called_with("Cannot swap window, only one window and tab")

---@diagnostic disable-next-line: undefined-field
message.error:revert()
end
)
end)