From 95d5c17e69144922df1c6423089b0257895ec9fd Mon Sep 17 00:00:00 2001 From: MisanthropicBit Date: Thu, 28 Nov 2024 17:22:20 +0100 Subject: [PATCH] Fixes (#25) --- lua/winmove/init.lua | 19 +++++++++++---- tests/init_spec.lua | 41 ++++++++++++++++++++++++++----- tests/swap_mode_spec.lua | 52 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 10 deletions(-) diff --git a/lua/winmove/init.lua b/lua/winmove/init.lua index a4b50b8..3b12ef2 100644 --- a/lua/winmove/init.lua +++ b/lua/winmove/init.lua @@ -137,7 +137,7 @@ 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 @@ -145,7 +145,7 @@ local function can_move_or_swap(win_id, mode) 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())) @@ -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 @@ -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) diff --git a/tests/init_spec.lua b/tests/init_spec.lua index 09e778a..b2755bc 100644 --- a/tests/init_spec.lua +++ b/tests/init_spec.lua @@ -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() @@ -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) diff --git a/tests/swap_mode_spec.lua b/tests/swap_mode_spec.lua index 81a2410..e3310b1 100644 --- a/tests/swap_mode_spec.lua +++ b/tests/swap_mode_spec.lua @@ -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") @@ -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)