Skip to content

Commit

Permalink
Fix/update code/tests after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
MisanthropicBit committed Nov 20, 2024
1 parent 3191ccc commit 58dd8db
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 146 deletions.
35 changes: 17 additions & 18 deletions lua/winmove/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ local mapping_descriptions = {
},
},
resize = {
highlight = "Todo",
default_resize_count = 3,
keymaps = {
left = "Resize window left",
down = "Resize window down",
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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,
},
},
Expand Down
24 changes: 11 additions & 13 deletions lua/winmove/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lua/winmove/layout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions lua/winmove/resize.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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]
Expand All @@ -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
13 changes: 13 additions & 0 deletions lua/winmove/util/test_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions lua/winmove/validators.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion lua/winmove/winutil.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
88 changes: 34 additions & 54 deletions tests/config_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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,
},
},
},
},
Expand All @@ -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 = "<c-t>",
},
modes = {
||||||| parent of 1c6907a (Revert "Remove resize mode for now (#16)")
=======
toggle_mode = "<c-t>",
>>>>>>> 1c6907a (Revert "Remove resize mode for now (#16)")
move = {
highlight = "Title",
at_edge = {
Expand Down Expand Up @@ -187,14 +162,18 @@ describe("config", function()
},
},
resize = {
left = "<Left>",
down = "<Down>",
up = "<Up>",
right = "<Right>",
left_botright = "<s-h>",
down_botright = "<s-j>",
up_botright = "<s-k>",
right_botright = "<s-l>",
highlight = "Todo",
default_resize_count = 3,
keymaps = {
left = "<Left>",
down = "<Down>",
up = "<Up>",
right = "<Right>",
left_botright = "<s-h>",
down_botright = "<s-j>",
up_botright = "<s-k>",
right_botright = "<s-l>",
},
},
},
})
Expand All @@ -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)

Expand Down
Loading

0 comments on commit 58dd8db

Please sign in to comment.