From b3f2d03674574910157f175bb714deba90001fb8 Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Sun, 6 Aug 2023 23:35:45 +0200 Subject: [PATCH] vim: better window resizing --- config/nvim/lua/user/keymap.lua | 11 ++++--- config/nvim/lua/user/util.lua | 58 ++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/config/nvim/lua/user/keymap.lua b/config/nvim/lua/user/keymap.lua index 63574a9..597abe4 100644 --- a/config/nvim/lua/user/keymap.lua +++ b/config/nvim/lua/user/keymap.lua @@ -1,4 +1,5 @@ -local partial = require('user.util').partial +local util = require('user.util') +local partial = util.partial local nmap = partial(vim.keymap.set, 'n') local imap = partial(vim.keymap.set, 'i') local vmap = partial(vim.keymap.set, 'v') @@ -33,10 +34,10 @@ nmap('', 'h') nmap('', 'l') -- window resizing -nmap('', 'resize +1') -nmap('', 'resize -1') -nmap('', 'vertical resize -1') -nmap('', 'vertical resize +1') +nmap('', util.win_resize_up(2), { desc = 'Resize window upward' }) +nmap('', util.win_resize_down(2), { desc = 'Resize window downward' }) +nmap('', util.win_resize_left(2), { desc = 'Resize window leftward' }) +nmap('', util.win_resize_right(2), { desc = 'Resize window rightward' }) -- easy tab navigation nmap('', 'tabnext', { silent = true }) diff --git a/config/nvim/lua/user/util.lua b/config/nvim/lua/user/util.lua index f5c79eb..0380435 100644 --- a/config/nvim/lua/user/util.lua +++ b/config/nvim/lua/user/util.lua @@ -37,6 +37,17 @@ M.partial = function(f, ...) end end +--- Delayed function execution. +---@param f function: function whose evaluation will be delayed. +---@param ... any: arguments to `f`. +---@return function: a new function that calls f with provided arguments. +M.thunk = function(f, ...) + local args = {...} + return function() + return f(unpack(args)) + end +end + -- Perform `func` (which can freely use register `reg`) and make sure `reg` -- is restored afterwards. M.with_saved_register = function(reg, func) @@ -73,8 +84,53 @@ M.goto_prev_diagnostic = function(opts) vim.diagnostic.goto_prev(vim.tbl_extend('keep', opts or {}, diag_opts)) end -M.use_local = function() +--- Whether the current window is the last in a given direction. +---@param direction string: one of 'h', 'j', 'k', or 'l' +local win_is_last = function(direction) + local current = vim.api.nvim_get_current_win() + vim.cmd('wincmd ' .. direction) + local next = vim.api.nvim_get_current_win() + + local is_last = current == next + if not is_last then vim.cmd('wincmd p') end + + return is_last end +--- Resize current window in a given direction. +---@param dir string: one of 'h', 'j', 'k', or 'l' +---@param size integer: how much to resize +local win_resize = function(dir, size) + if dir ~= 'h' and dir ~= 'j' and dir ~= 'k' and dir ~= 'l' then return end + + size = math.abs(size) + local is_height = dir == 'j' or dir == 'k' + local is_positive = dir == 'j' or dir == 'l' + + if win_is_last(is_height and 'j' or 'l') then + is_positive = not is_positive + end + + local delta = string.format('%s%d', is_positive and '+' or '-', size) + local prefix = is_height and '' or 'vertical ' + vim.cmd(prefix .. 'resize ' .. delta .. '') +end + +--- Resize current window upwards. +---@param size integer: how much to resize +M.win_resize_up = function(size) return M.thunk(win_resize, 'k', size) end + +--- Resize current window downwards. +---@param size integer: how much to resize +M.win_resize_down = function(size) return M.thunk(win_resize, 'j', size) end + +--- Resize current window leftwards. +---@param size integer: how much to resize +M.win_resize_left = function(size) return M.thunk(win_resize, 'h', size) end + +--- Resize current window rightwards. +---@param size integer: how much to resize +M.win_resize_right = function(size) return M.thunk(win_resize, 'l', size) end + return M