diff --git a/lua/winmove/config.lua b/lua/winmove/config.lua index c25f368..b523477 100644 --- a/lua/winmove/config.lua +++ b/lua/winmove/config.lua @@ -162,8 +162,6 @@ local mapping_descriptions = { }, }, resize = { - highlight = "Todo", - default_resize_count = 3, keymaps = { left = "Resize window left", down = "Resize window down", @@ -257,6 +255,9 @@ local vertical_validator = { local non_empty_string_validator = { is_non_empty_string, expected_non_empty_string } +local is_positive_non_zero_number_validator = + { is_positive_non_zero_number, "a positive, non-zero number" } + --- Validate a config ---@param _config winmove.Config ---@return boolean @@ -277,7 +278,7 @@ function config.validate(_config) highlight = "string", at_edge = { horizontal = horizontal_validator, - vertical = vertical_validator, + vertical = vertical_validator, }, keymaps = { left = non_empty_string_validator, @@ -298,28 +299,26 @@ function config.validate(_config) highlight = "string", at_edge = { horizontal = horizontal_validator, - vertical = vertical_validator, + vertical = vertical_validator, }, keymaps = { - left = non_empty_string_validator, - down = non_empty_string_validator, - up = non_empty_string_validator, - right = non_empty_string_validator, + left = non_empty_string_validator, + down = non_empty_string_validator, + up = non_empty_string_validator, + right = non_empty_string_validator, }, }, resize = { highlight = "string", - default_resize_count = { - is_positive_non_zero_number, - }, + default_resize_count = is_positive_non_zero_number_validator, keymaps = { - left = non_empty_string_validator, - down = non_empty_string_validator, - up = non_empty_string_validator, - right = non_empty_string_validator, - left_botright = non_empty_string_validator, - down_botright = non_empty_string_validator, - up_botright = non_empty_string_validator, + left = non_empty_string_validator, + down = non_empty_string_validator, + up = non_empty_string_validator, + right = non_empty_string_validator, + left_botright = non_empty_string_validator, + down_botright = non_empty_string_validator, + up_botright = non_empty_string_validator, right_botright = non_empty_string_validator, }, }, diff --git a/lua/winmove/init.lua b/lua/winmove/init.lua index 372c011..59c0804 100644 --- a/lua/winmove/init.lua +++ b/lua/winmove/init.lua @@ -283,7 +283,7 @@ function winmove.move_window_far(win_id, dir) winutil.wincall(win_id, function() vim.cmd("wincmd " .. dir:upper()) - end, dir) + end) end ---@param win_id integer @@ -338,17 +338,15 @@ function winmove.swap_window(win_id) end) end -local function toggle_mode() - local mode = winmove.current_mode() - local new_mode = mode == winmove.Mode.Move and winmove.Mode.Swap or winmove.Mode.Move - - stop_mode(mode) - start_mode(new_mode) -end +local next_mode = { + [winmove.Mode.Move] = winmove.Mode.Swap, + [winmove.Mode.Swap] = winmove.Mode.Resize, + [winmove.Mode.Resize] = winmove.Mode.Move, +} local function toggle_mode() local mode = winmove.current_mode() - local new_mode = mode == winmove.Mode.Move and winmove.Mode.Resize or winmove.Mode.Move + local new_mode = next_mode[mode] stop_mode(mode) start_mode(new_mode) @@ -773,13 +771,13 @@ end ---@param anchor winmove.ResizeAnchor? function winmove.resize_window(win_id, dir, count, anchor) vim.validate({ - win_id = win_id_validator(win_id), - dir = dir_validator(dir), - count = { count, is_nonnegative_number, "a non-negative number" }, + win_id = validators.win_id_validator(win_id), + dir = validators.dir_validator(dir), + count = { count, validators.is_nonnegative_number, "a non-negative number" }, anchor = { anchor, resize.is_valid_anchor, "a valid anchor" }, }) - winutil.win_id_context_call(win_id, function() + winutil.wincall(win_id, function() resize.resize_window(win_id, dir, count, anchor) end) end diff --git a/lua/winmove/layout.lua b/lua/winmove/layout.lua index cf6fc48..b6c6b0f 100644 --- a/lua/winmove/layout.lua +++ b/lua/winmove/layout.lua @@ -90,10 +90,11 @@ function layout.get_wraparound_neighbor(win_id, dir) end --- Apply a function to each neighbor in a direction +---@param win_id integer ---@param dir winmove.Direction ---@param func fun(win_id: integer): boolean, boolean -function layout.apply_to_neighbors(dir, func) - local neighbor_win_id = layout.get_neighbor(dir) +function layout.apply_to_neighbors(win_id, dir, func) + local neighbor_win_id = layout.get_neighbor(win_id, dir) local count, applied = 0, 0 while neighbor_win_id do diff --git a/lua/winmove/resize.lua b/lua/winmove/resize.lua index 70f47ec..75dd476 100644 --- a/lua/winmove/resize.lua +++ b/lua/winmove/resize.lua @@ -26,12 +26,13 @@ local neighbor_dir_table = { }, } +---@param win_id integer ---@param dir winmove.Direction ---@param get_dimension_func fun(win_id: integer): integer ---@param min_dimension integer ---@return boolean -local function can_resize(dir, get_dimension_func, min_dimension) - local neighbor_count, applied = layout.apply_to_neighbors(dir, function(neighbor_win_id) +local function can_resize(win_id, dir, get_dimension_func, min_dimension) + local neighbor_count, applied = layout.apply_to_neighbors(win_id, dir, function(neighbor_win_id) local dimension = get_dimension_func(neighbor_win_id) return true, dimension <= min_dimension @@ -47,17 +48,17 @@ local function can_resize(dir, get_dimension_func, min_dimension) end --- Adjust neighbors in the direction the current window is being resized +---@param win_id integer ---@param dir winmove.Direction ---@param get_dimension fun(win_id: integer): integer ---@param min_dimension integer ----@param count integer ---@param anchor winmove.ResizeAnchor -local function adjust_neighbors_in_direction(dir, get_dimension, min_dimension, count, anchor) - layout.apply_to_neighbors(dir, function(neighbor_win_id) +local function adjust_neighbors_in_direction(win_id, dir, get_dimension, min_dimension, anchor) + layout.apply_to_neighbors(win_id, dir, function(neighbor_win_id) local dimension = get_dimension(neighbor_win_id) if dimension <= min_dimension then - winutil.win_id_context_call( + winutil.wincall( neighbor_win_id, resize.resize_window, neighbor_win_id, @@ -107,12 +108,13 @@ function resize.resize_window(win_id, dir, count, anchor) if is_full_dimension(win_id) then return - elseif not can_resize(dir, get_dimension, min_dimension) then + elseif not can_resize(win_id, dir, get_dimension, min_dimension) then return end local _anchor = anchor or resize.anchor.TopLeft local top_left = _anchor == resize.anchor.TopLeft + local orig_win_id = win_id if not top_left then local neighbor_dir = neighbor_dir_table[horizontal][top_left] @@ -123,7 +125,7 @@ function resize.resize_window(win_id, dir, count, anchor) end resize_func(win_id, (dir == edges[2] and -1 or 1) * count) - adjust_neighbors_in_direction(dir, get_dimension, min_dimension, count, _anchor) + adjust_neighbors_in_direction(orig_win_id, dir, get_dimension, min_dimension, _anchor) end return resize diff --git a/lua/winmove/util/test_helpers.lua b/lua/winmove/util/test_helpers.lua index b0bf660..61dda82 100644 --- a/lua/winmove/util/test_helpers.lua +++ b/lua/winmove/util/test_helpers.lua @@ -8,6 +8,19 @@ if not has_luassert then error("Luassert library not found") end +---@param win_id integer +---@return integer +---@return integer +---@return integer +---@return integer +function test_helpers.get_win_pos_and_dimensions(win_id) + local pos = vim.api.nvim_win_get_position(win_id) + local width = vim.api.nvim_win_get_width(win_id) + local height = vim.api.nvim_win_get_height(win_id) + + return pos[1], pos[2], width, height +end + ---@param buffer integer ---@return table function test_helpers.get_buf_mapped_keymaps(buffer) diff --git a/lua/winmove/validators.lua b/lua/winmove/validators.lua index 796f920..6ef135a 100644 --- a/lua/winmove/validators.lua +++ b/lua/winmove/validators.lua @@ -2,19 +2,19 @@ local validators = {} ---@param value any ---@return boolean -local function is_nonnegative_number(value) - return type(value) == "number" and value >= 0 +local function is_valid_direction(value) + return value == "h" or value == "j" or value == "k" or value == "l" end ---@param value any ---@return boolean -local function is_valid_direction(value) - return value == "h" or value == "j" or value == "k" or value == "l" +function validators.is_nonnegative_number(value) + return type(value) == "number" and value >= 0 end ---@param value any function validators.win_id_validator(value) - return { value, is_nonnegative_number, "a non-negative number" } + return { value, validators.is_nonnegative_number, "a non-negative number" } end ---@param value any diff --git a/lua/winmove/winutil.lua b/lua/winmove/winutil.lua index 9637abc..74d1074 100644 --- a/lua/winmove/winutil.lua +++ b/lua/winmove/winutil.lua @@ -105,7 +105,6 @@ end ---@return integer function winutil.editor_height() local height = vim.o.lines - vim.o.cmdheight - local showtabline = vim.o.showtabline -- Subtract 1 if the tabline is visible diff --git a/tests/config_spec.lua b/tests/config_spec.lua index 026c4c3..d029a9c 100644 --- a/tests/config_spec.lua +++ b/tests/config_spec.lua @@ -48,13 +48,25 @@ describe("config", function() }, }, { - default_resize_count = false, + modes = { + resize = { + default_resize_count = false, + }, + }, }, { - default_resize_count = 0, + modes = { + resize = { + default_resize_count = 0, + }, + }, }, { - default_resize_count = -3, + modes = { + resize = { + default_resize_count = -3, + }, + }, }, { keymaps = { @@ -71,33 +83,20 @@ describe("config", function() }, }, { -<<<<<<< HEAD modes = { - swap = { + move = { keymaps = { left = "", }, -||||||| parent of 1c6907a (Revert "Remove resize mode for now (#16)") - keymaps = { - move = { - left = "", -======= - keymaps = { - resize = "no", - }, - }, - { - keymaps = { - move = { - left = "", ->>>>>>> 1c6907a (Revert "Remove resize mode for now (#16)") }, }, }, { - keymaps = { + modes = { resize = { - left_botright = true, + keymaps = { + left_botright = true, + }, }, }, }, @@ -115,43 +114,19 @@ describe("config", function() assert.is_false(ok) end + ---@diagnostic disable-next-line: undefined-field message.error:revert() end) it("throws no errors for a valid config", function() local ok = config.configure({ -<<<<<<< HEAD -||||||| parent of 1c6907a (Revert "Remove resize mode for now (#16)") - highlights = { - move = "Title", - }, - at_edge = { - horizontal = at_edge.Wrap, - vertical = false, - }, -======= - highlights = { - move = "Title", - resize = nil, - }, - at_edge = { - horizontal = at_edge.Wrap, - vertical = false, - }, - default_resize_count = 2, ->>>>>>> 1c6907a (Revert "Remove resize mode for now (#16)") keymaps = { help = "_", help_close = "z", quit = "i", -<<<<<<< HEAD toggle_mode = "", }, modes = { -||||||| parent of 1c6907a (Revert "Remove resize mode for now (#16)") -======= - toggle_mode = "", ->>>>>>> 1c6907a (Revert "Remove resize mode for now (#16)") move = { highlight = "Title", at_edge = { @@ -187,14 +162,18 @@ describe("config", function() }, }, resize = { - left = "", - down = "", - up = "", - right = "", - left_botright = "", - down_botright = "", - up_botright = "", - right_botright = "", + highlight = "Todo", + default_resize_count = 3, + keymaps = { + left = "", + down = "", + up = "", + right = "", + left_botright = "", + down_botright = "", + up_botright = "", + right_botright = "", + }, }, }, }) @@ -203,6 +182,7 @@ describe("config", function() end) it("throws no errors for empty user config", function() + ---@diagnostic disable-next-line: missing-fields assert.is_true(config.configure({})) end) diff --git a/tests/custom_highlights_spec.lua b/tests/custom_highlights_spec.lua index a339e5e..faf5bea 100644 --- a/tests/custom_highlights_spec.lua +++ b/tests/custom_highlights_spec.lua @@ -127,8 +127,10 @@ describe("custom highlights", function() vim.cmd(("hi link %s %s"):format("CustomWinmoveResizeMode", "Repeat")) config.configure({ - highlights = { - resize = "CustomWinmoveResizeMode", + modes = { + resize = { + highlight = "CustomWinmoveResizeMode", + }, }, }) diff --git a/tests/init_spec.lua b/tests/init_spec.lua index 71aae0f..09e778a 100644 --- a/tests/init_spec.lua +++ b/tests/init_spec.lua @@ -18,7 +18,7 @@ describe("init", function() assert.has_error(function() ---@diagnostic disable-next-line: param-type-mismatch winmove.start_mode("hello") - end, "mode: expected a valid mode (move, swap, resize), got hello") + end, "mode: expected a valid mode (move, resize, swap), got hello") end) it("fails to stop mode if no mode is currently active", function() diff --git a/tests/mode_mappings_spec.lua b/tests/mode_mappings_spec.lua index 764a926..bf95003 100644 --- a/tests/mode_mappings_spec.lua +++ b/tests/mode_mappings_spec.lua @@ -59,7 +59,7 @@ describe("mode mappings", function() local keymaps = test_helpers.get_buf_mapped_keymaps(vim.api.nvim_get_current_buf()) - for name, lhs in pairs(config.keymaps.resize) do + for name, lhs in pairs(config.modes.resize.keymaps) do compare_keymap("resize", name, keymaps[lhs] or keymaps[lhs:upper()]) end diff --git a/tests/resize_adjust_neighbors_spec.lua b/tests/resize_adjust_neighbors_spec.lua index a6bfe48..e320ff5 100644 --- a/tests/resize_adjust_neighbors_spec.lua +++ b/tests/resize_adjust_neighbors_spec.lua @@ -4,25 +4,13 @@ local test_helpers = require("winmove.util.test_helpers") local given = vader.given local make_layout = test_helpers.make_layout +local get_win_pos_and_dimensions = test_helpers.get_win_pos_and_dimensions describe("resize", function() describe("adjusts neighbors", function() local count = 3 assert:set_parameter("TableFormatLevel", 10) - ---@param win_id integer - ---@return integer - ---@return integer - ---@return integer - ---@return integer - local function get_win_pos_and_dimensions(win_id) - local pos = vim.api.nvim_win_get_position(win_id) - local width = vim.api.nvim_win_get_width(win_id) - local height = vim.api.nvim_win_get_height(win_id) - - return pos[1], pos[2], width, height - end - it("resizes towards non-sibling window", function() given(function() local layout = make_layout({ diff --git a/tests/resize_bottom_right_anchor_spec.lua b/tests/resize_bottom_right_anchor_spec.lua index ecd9bfa..fccb6ed 100644 --- a/tests/resize_bottom_right_anchor_spec.lua +++ b/tests/resize_bottom_right_anchor_spec.lua @@ -4,26 +4,12 @@ local test_helpers = require("winmove.util.test_helpers") local given = vader.given local make_layout = test_helpers.make_layout +local get_win_pos_and_dimensions = test_helpers.get_win_pos_and_dimensions describe("resize", function() local count = 3 assert:set_parameter("TableFormatLevel", 10) - -- TODO: Move to test_helpers - - ---@param win_id integer - ---@return integer - ---@return integer - ---@return integer - ---@return integer - local function get_win_pos_and_dimensions(win_id) - local pos = vim.api.nvim_win_get_position(win_id) - local width = vim.api.nvim_win_get_width(win_id) - local height = vim.api.nvim_win_get_height(win_id) - - return pos[1], pos[2], width, height - end - ---@param row_or_col "row" | "col" ---@return integer local function make_three_column_or_row_layout(row_or_col) diff --git a/tests/resize_top_left_anchor_spec.lua b/tests/resize_top_left_anchor_spec.lua index e76bb16..9e4da22 100644 --- a/tests/resize_top_left_anchor_spec.lua +++ b/tests/resize_top_left_anchor_spec.lua @@ -4,24 +4,12 @@ local test_helpers = require("winmove.util.test_helpers") local given = vader.given local make_layout = test_helpers.make_layout +local get_win_pos_and_dimensions = test_helpers.get_win_pos_and_dimensions describe("resize", function() local count = 3 assert:set_parameter("TableFormatLevel", 10) - ---@param win_id integer - ---@return integer - ---@return integer - ---@return integer - ---@return integer - local function get_win_pos_and_dimensions(win_id) - local pos = vim.api.nvim_win_get_position(win_id) - local width = vim.api.nvim_win_get_width(win_id) - local height = vim.api.nvim_win_get_height(win_id) - - return pos[1], pos[2], width, height - end - ---@param row_or_col "row" | "col" ---@return integer local function make_three_column_or_row_layout(row_or_col)