From bce6e94ba1c8e133032de21b33c84f8b6814214d Mon Sep 17 00:00:00 2001 From: MisanthropicBit Date: Sun, 4 Feb 2024 22:25:21 +0100 Subject: [PATCH] Test auto-quiting mode for events --- lua/winmove/init.lua | 12 ++++++++++++ tests/auto_quit_modes_spec.lua | 28 ++++++++++++++++++++++++++++ tests/no_win_events_spec.lua | 2 +- 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/auto_quit_modes_spec.lua diff --git a/lua/winmove/init.lua b/lua/winmove/init.lua index 7320468..78ef4c0 100644 --- a/lua/winmove/init.lua +++ b/lua/winmove/init.lua @@ -514,6 +514,18 @@ local function create_mode_autocmds(mode, source_win_id) desc = "Remove highlighting from any new window because the winhighlight option can leak into other windows", }) ) + + table.insert( + autocmds, + api.nvim_create_autocmd("InsertEnter", { + callback = function() + stop_mode(mode) + return true + end, + group = augroup, + desc = "Quits " .. mode .. " when entering insert mode", + }) + ) end ---@param mode winmove.Mode diff --git a/tests/auto_quit_modes_spec.lua b/tests/auto_quit_modes_spec.lua new file mode 100644 index 0000000..17f798b --- /dev/null +++ b/tests/auto_quit_modes_spec.lua @@ -0,0 +1,28 @@ +local winmove = require("winmove") +local vader = require("winmove.util.vader") + +local given = vader.given + +describe("auto-quit mode", function() + it("quits current mode when entering a new window", function() + given("", function() + vim.cmd.vnew() + winmove.start_mode(winmove.mode.Move) + assert.are.same(winmove.current_mode(), winmove.mode.Move) + + vim.cmd.new() + assert.are.same(winmove.current_mode(), nil) + end) + end) + + it("quits current mode when entering insert mode", function() + given("", function() + vim.cmd.vnew() + winmove.start_mode(winmove.mode.Move) + assert.are.same(winmove.current_mode(), winmove.mode.Move) + + vim.cmd.norm([[i]]) + assert.are.same(winmove.current_mode(), nil) + end) + end) +end) diff --git a/tests/no_win_events_spec.lua b/tests/no_win_events_spec.lua index 5cf5d33..cc6b334 100644 --- a/tests/no_win_events_spec.lua +++ b/tests/no_win_events_spec.lua @@ -25,7 +25,7 @@ describe("no window events", function() it("does not trigger any window events when moving", function() given("", function() - vim.cmd("belowright vnew") + vim.cmd.vnew() -- Set up autocmds *after* splitting for _, event in ipairs(events) do