Large rewrite, simplify keymapping, utilize as much keys from Lazy as

possible. Reduce manually setup plugins and utilize opts instead.
This commit is contained in:
Martin Larsson 2025-02-23 01:35:23 +00:00
parent 72eb62b9cb
commit 43020a4d12
17 changed files with 408 additions and 780 deletions

View file

@ -26,18 +26,10 @@ function M.setup()
vim.api.nvim_command("command! " .. cmd[1] .. " " .. cmd[2]) vim.api.nvim_command("command! " .. cmd[1] .. " " .. cmd[2])
end end
utils.add_keymaps({ utils.set_keymap_list({
n = { { "<leader>ff", function() M.format() end },
["<leader>ff"] = { { "<leader>fe", function() M.format_enable() end },
cmd = function() M.format(true) end { "<leader>fd", function() M.format_disable() end },
},
["<leader>fe"] = {
cmd = M.format_enable,
},
["<leader>fd"] = {
cmd = M.format_disable,
},
},
}) })
end end

View file

@ -1,151 +1,98 @@
local utils = require("utils")
local g = vim.g local g = vim.g
g.mapleader = " " g.mapleader = " "
g.maplocalleader = " " g.maplocalleader = " "
local move_up = { local move_up = { "v:count || mode(1)[0:1] == \"no\" ? \"k\" : \"gk\"", { expr = true } }
cmd = "v:count || mode(1)[0:1] == \"no\" ? \"k\" : \"gk\"", local move_down = { "v:count || mode(1)[0:1] == \"no\" ? \"j\" : \"gj\"", { expr = true } }
opts = {
expr = true
}
}
local move_down = { utils.foreach({
cmd = "v:count || mode(1)[0:1] == \"no\" ? \"j\" : \"gj\"", {
opts = { "n",
expr = true {
}
}
local utils = require("utils")
utils.add_keymaps({
n = {
-- Using lspsaga for hover doc -- Using lspsaga for hover doc
["K"] = { { "K", "<Nop>", },
cmd = "<Nop>",
},
-- Using lspsaga finder with gr which does references -- Using lspsaga finder with gr which does references
["grr"] = { { "grr", "<Nop>", },
cmd = "<Nop>", { "gra", "<Nop>", },
}, { "grn", "<Nop>", },
["gra"] = { { "gri", "<Nop>", },
cmd = "<Nop>",
},
["grn"] = {
cmd = "<Nop>",
},
["gri"] = {
cmd = "<Nop>",
},
-- Navigation -- Navigation
["<C-Left>"] = { { "<C-Left>", "<C-w>h", },
cmd = "<C-w>h", { "<C-Down>", "<C-w>j", },
}, { "<C-Up>", "<C-w>k", },
["<C-Down>"] = { { "<C-Right>", "<C-w>l", },
cmd = "<C-w>j", { "<C-h>", "<C-w>h", },
}, { "<C-j>", "<C-w>j", },
["<C-Up>"] = { { "<C-k>", "<C-w>k", },
cmd = "<C-w>k", { "<C-l>", "<C-w>l", },
},
["<C-Right>"] = {
cmd = "<C-w>l",
},
["<C-h>"] = {
cmd = "<C-w>h",
},
["<C-j>"] = {
cmd = "<C-w>j",
},
["<C-k>"] = {
cmd = "<C-w>k",
},
["<C-l>"] = {
cmd = "<C-w>l",
},
-- Window -- Window
["<C-q>"] = { { "<C-q>", "<C-w>q", },
cmd = "<C-w>q",
},
-- Disable current highlights -- Disable current highlights
["<Esc>"] = { { "<Esc>", "<cmd> noh <CR>", },
cmd = "<cmd> noh <CR>",
},
-- Copies the entire file -- Copies the entire file
["<C-c>"] = { { "<C-c>", ":%y+<CR>", { silent = true } },
cmd = ":%y+<CR>",
opts = {
silent = true
}
},
-- Allow moving the cursor through wrapped lines with <Up> and <Down> -- Allow moving the cursor through wrapped lines with <Up> and <Down>
-- http://www.reddit.com/r/vim/comments/2k4cbr/problem_with_gj_and_gk/ -- http://www.reddit.com/r/vim/comments/2k4cbr/problem_with_gj_and_gk/
-- empty mode is same as using <cmd> :map -- empty mode is same as using <cmd> :map
-- also don't use g[j|k] when in operator pending mode, so it doesn't alter d, y or c behaviour -- also don't use g[j|k] when in operator pending mode,
["<Up>"] = move_up, -- so it doesn't alter d, y or c behaviour
["<Down>"] = move_down, { "<Up>", move_up[1], move_up[2], },
["j"] = move_down, { "<Down>", move_down[1], move_down[2], },
["k"] = move_up, { "j", move_down[1], move_down[2], },
{ "k", move_up[1], move_up[2], },
-- Maps to remove -- Maps to remove
["<C-z>"] = { { "<C-z>", "<Nop>", },
cmd = "<Nop>",
},
-- Marks are less frequently used than leaping, also, less relevant with arrow and fzf navigation. -- Marks are less frequently used than leaping, also, less relevant with arrow and fzf navigation.
-- Prioritize regular m for leaping, and <leader>m for setting marks. -- Prioritize regular m for leaping, and <leader>m for setting marks.
["<leader>m"] = { { "<leader>m", "m", },
cmd = "m", { "[d", function()
},
["[d"] = {
cmd = function()
vim.diagnostic.jump({ count = -1, float = false }) vim.diagnostic.jump({ count = -1, float = false })
end end },
}, { "]d", function()
["]d"] = {
cmd = function()
vim.diagnostic.jump({ count = 1, float = false }) vim.diagnostic.jump({ count = 1, float = false })
end end },
}, }
},
i = {},
v = {
["<Up>"] = move_up,
["<Down>"] = move_down,
["j"] = move_down,
["k"] = move_up,
["<tab>"] = {
cmd = ">gv",
},
["<S-tab>"] = {
cmd = "<gv",
}, },
{
"v",
{
{ "<Up>", move_up[1], move_up[2], },
{ "<Down>", move_down[1], move_down[2], },
{ "j", move_down[1], move_down[2], },
{ "k", move_up[1], move_up[2], },
{ "<tab>", ">gv", },
{ "<S-tab>", "<gv", },
-- Marks are less frequently used than leaping, also, less relevant with arrow and fzf navigation. -- Marks are less frequently used than leaping, also, less relevant with arrow and fzf navigation.
-- Prioritize regular m for leaping, and <leader>m for setting marks. -- Prioritize regular m for leaping, and <leader>m for setting marks.
["<leader>m"] = { { "<leader>m", "m", },
cmd = "m",
}, },
}, },
x = { {
["<Up>"] = move_up, "x",
["<Down>"] = move_down, {
["j"] = move_down, { "<Up>", move_up[1], move_up[2], },
["k"] = move_up, { "<Down>", move_down[1], move_down[2], },
["p"] = { { "j", move_down[1], move_down[2], },
cmd = "p:let @+=@0<CR>:let @\"=@0<CR>", { "k", move_up[1], move_up[2], },
opts = { { "p", "p:let @+=@0<CR>:let @\"=@0<CR>", { silent = true }, },
silent = true }
}, },
{
"t",
{
{ "<C-q>", "<C-\\><C-N>", },
}
}, },
}, }, function(mode_mapping)
t = { local mode = mode_mapping[1]
["<C-q>"] = { local mappings = mode_mapping[2]
cmd = "<C-\\><C-N>", utils.set_keymap_list(mappings, mode)
}, end)
},
})

View file

@ -1,12 +1,14 @@
local utils = require("utils") local utils = require("utils")
-- https://clangd.llvm.org/extensions.html#switch-between-sourceheader -- https://clangd.llvm.org/extensions.html#switch-between-sourceheader
local function switch_source_header(bufnr) local function switch_source_header()
bufnr = utils.validate_bufnr(bufnr) local bufnr = utils.validate_bufnr(0)
local clangd_client = vim.lsp.get_clients({ bufnr = bufnr, name = "clangd" })[1] local clangd_client = vim.lsp.get_clients({ bufnr = bufnr, name = "clangd" })[1]
local params = { uri = vim.uri_from_bufnr(bufnr) }
if clangd_client then if clangd_client then
clangd_client.request("textDocument/switchSourceHeader", params, function(err, result) clangd_client.request(
"textDocument/switchSourceHeader",
{ uri = vim.uri_from_bufnr(bufnr) },
function(err, result)
if err then if err then
error(tostring(err)) error(tostring(err))
end end
@ -44,19 +46,9 @@ return {
"compile_flags.txt", "compile_flags.txt",
"configure.ac", "configure.ac",
}, },
on_attach = function(_, bufnr) on_attach = function()
local lsp_maps = { utils.set_keymap_list({
{ { "<leader>ko", switch_source_header, },
"<leader>ko", })
function() switch_source_header(0) end,
},
}
local keymaps = { n = {} }
for i, _ in ipairs(lsp_maps) do
local binding, cmd = unpack(lsp_maps[i])
keymaps.n[binding] = { cmd = cmd, opts = { buffer = bufnr } }
end
utils.add_keymaps(keymaps)
end, end,
} }

View file

@ -24,34 +24,9 @@ local function global_on_attach(client, bufnr)
}) })
end end
utils.add_keymaps({ utils.set_keymap_list({
n = { { "gd", function() vim.lsp.buf.definition({ reuse_win = true, }) end, { noremap = true, buffer = bufnr } },
{ "gD", function() vim.lsp.buf.declaration({ reuse_win = true, }) end, { noremap = true, buffer = bufnr } },
["gd"] = {
cmd = function()
vim.lsp.buf.definition({
reuse_win = true,
})
end,
opts = {
noremap = true,
silent = true,
buffer = bufnr
}
},
["gD"] = {
cmd = function()
vim.lsp.buf.declaration({
reuse_win = true,
})
end,
opts = {
noremap = true,
silent = true,
buffer = bufnr
}
},
}
}) })
end end

View file

@ -3,11 +3,8 @@ return {
dependencies = { dependencies = {
"nvim-lua/plenary.nvim", "nvim-lua/plenary.nvim",
"nvim-treesitter/nvim-treesitter", "nvim-treesitter/nvim-treesitter",
{ 'echasnovski/mini.diff', version = false },
{ "stevearc/dressing.nvim", opts = {} },
}, },
config = function() opts = {
require("codecompanion").setup({
strategies = { strategies = {
chat = { chat = {
adapter = "copilot", adapter = "copilot",
@ -30,26 +27,13 @@ return {
opts = { opts = {
log_level = "DEBUG", log_level = "DEBUG",
}, },
})
local keymaps = {
n = {
["<Leader>ci"] = { cmd = "<cmd>CodeCompanion<cr>" },
["<Leader>cc"] = { cmd = "<cmd>CodeCompanionChat toggle<cr>" },
["<Leader>cm"] = { cmd = "<cmd>CodeCompanion /commit<cr>" },
}, },
v = { keys = {
["<Leader>ci"] = { cmd = "<cmd>CodeCompanion<cr>" }, { "<Leader>ci", "<cmd>CodeCompanion<cr>" },
["ga"] = { cmd = "<cmd>CodeCompanionAdd<cr>" }, { "<Leader>cc", "<cmd>CodeCompanionChat toggle<cr>" },
["<Leader>ce"] = { cmd = "<cmd>CodeCompanion /explain<cr>" }, { "<Leader>ce", "<cmd>CodeCompanion /explain<cr>", mode = "v" },
["<Leader>cf"] = { cmd = "<cmd>CodeCompanion /fix<cr>" }, },
["<Leader>ct"] = { cmd = "<cmd>CodeCompanion /tests<cr>" }, init = function()
}
}
local utils = require("utils")
utils.add_opts_to_all_mappings(keymaps, { silent = true })
utils.add_keymaps(keymaps)
vim.cmd([[cab cc CodeCompanion]]) vim.cmd([[cab cc CodeCompanion]])
end end
} }

View file

@ -3,8 +3,7 @@ return {
build = "make", build = "make",
event = "VeryLazy", event = "VeryLazy",
lazy = true, lazy = true,
config = function() opts = {
require("codesnap").setup({
mac_window_bar = true, mac_window_bar = true,
title = "codesnap.nvim", title = "codesnap.nvim",
code_font_family = "JetBrainsMono Nerd Font", code_font_family = "JetBrainsMono Nerd Font",
@ -12,15 +11,8 @@ return {
has_breadcrumbs = true, has_breadcrumbs = true,
bg_theme = "grape", bg_theme = "grape",
watermark = "", watermark = "",
})
require("utils").add_keymaps({
v = {
["<leader>cs"] = {
cmd = ":CodeSnap<CR>",
opts = { silent = true },
}, },
} keys = {
}) { "<leader>cs", ":CodeSnap<CR>", mode = "v", silent = true, noremap = true },
end, },
} }

View file

@ -1,18 +1,18 @@
return { return {
"github/copilot.vim", "github/copilot.vim",
config = function() event = "InsertEnter",
init = function()
vim.g.copilot_no_tab_map = true vim.g.copilot_no_tab_map = true
require("utils").add_keymaps({ end,
i = { keys = {
["<Right>"] = { {
cmd = 'copilot#Accept("\\<Right>")', "<Right>",
opts = { 'copilot#Accept("\\<Right>")',
mode = "i",
expr = true, expr = true,
replace_keycodes = false, replace_keycodes = false,
silent = true, silent = true,
} desc = "Copilot Accept with <Right>",
} },
} },
})
end
} }

View file

@ -1,15 +1,18 @@
return { return {
"b0o/incline.nvim", "b0o/incline.nvim",
dependencies = { dependencies = {
"lewis6991/gitsigns.nvim" {
"lewis6991/gitsigns.nvim",
opts = {},
keys = {
{ "[g", function() require("gitsigns").nav_hunk("prev") end, },
{ "]g", function() require("gitsigns").nav_hunk("next") end, },
}
}
}, },
event = "VeryLazy", event = "VeryLazy",
lazy = true, lazy = true,
config = function() opts = {
local gitsigns = require("gitsigns")
gitsigns.setup({})
local devicons = require("nvim-web-devicons")
require("incline").setup({
window = { window = {
padding = 0, padding = 0,
}, },
@ -21,7 +24,7 @@ return {
if filename == "" then if filename == "" then
filename = "[No Name]" filename = "[No Name]"
end end
local ft_icon, ft_color = devicons.get_icon_color(filename) local ft_icon, ft_color = require("nvim-web-devicons").get_icon_color(filename)
local function get_git_diff() local function get_git_diff()
local icons = { removed = "", changed = "", added = "" } local icons = { removed = "", changed = "", added = "" }
@ -79,17 +82,5 @@ return {
{ " " } { " " }
} }
end, end,
require("utils").add_keymaps({
n = {
["[g"] = {
cmd = function() gitsigns.nav_hunk("prev") end
}, },
["]g"] = {
cmd = function() gitsigns.nav_hunk("next") end
}
}
})
})
end,
} }

View file

@ -1,3 +1,15 @@
local function leap_across_windows()
require("leap").leap({
target_windows = require("leap.user").get_focusable_windows()
})
end
local function leap_in_current_buffer()
require("leap").leap({
target_windows = { vim.api.nvim_get_current_win() }
})
end
local saved_hlsearch = false local saved_hlsearch = false
local saved_highlights = {} local saved_highlights = {}
local colors = require("ayu.colors") local colors = require("ayu.colors")
@ -19,7 +31,13 @@ return {
}, },
event = "VeryLazy", event = "VeryLazy",
lazy = true, lazy = true,
config = function() opts = {},
keys = {
{ "m", function() leap_across_windows() end, mode = "n" },
{ "m", function() leap_in_current_buffer() end, mode = "v" },
{ "m", function() leap_in_current_buffer() end, mode = "o" },
},
init = function()
local leap = require("leap") local leap = require("leap")
-- Disable auto jumping to the first match -- Disable auto jumping to the first match
@ -53,42 +71,5 @@ return {
for _, cmd in ipairs(autocmds) do for _, cmd in ipairs(autocmds) do
utils.create_user_event_cb(cmd.event_name, cmd.cb, leap_augroup_name) utils.create_user_event_cb(cmd.event_name, cmd.cb, leap_augroup_name)
end end
local leap_user = require("leap.user")
local function leap_across_windows()
leap.leap({
target_windows = leap_user.get_focusable_windows()
})
end
local function leap_in_current_buffer()
leap.leap({
target_windows = { vim.api.nvim_get_current_win() }
})
end
utils.add_keymaps({
n = {
["m"] = {
cmd = function()
leap_across_windows()
end,
},
},
v = {
["m"] = {
cmd = function()
leap_in_current_buffer()
end,
}
},
o = {
["m"] = {
cmd = function()
leap_in_current_buffer()
end,
}
}
})
end, end,
} }

View file

@ -7,9 +7,7 @@ return {
dependencies = { dependencies = {
"nvim-tree/nvim-web-devicons", "nvim-tree/nvim-web-devicons",
}, },
after = "nvim-lspconfig", opts = {
config = function()
require("lspsaga").setup({
request_timeout = 750, request_timeout = 750,
symbol_in_winbar = { symbol_in_winbar = {
enable = false, enable = false,
@ -31,35 +29,14 @@ return {
hover = { hover = {
jump_on_first_press = true jump_on_first_press = true
}, },
})
local keymaps = {
n = {
["K"] = {
cmd = ":Lspsaga hover_doc<CR>"
}, },
["<leader>rn"] = { keys = {
cmd = ":Lspsaga rename<CR>" { "K", ":Lspsaga hover_doc<CR>", silent = true, },
{ "<leader>rn", ":Lspsaga rename<CR>", silent = true, },
{ "gr", ":Lspsaga finder<CR>", silent = true, },
{ "<leader>lt", ":Lspsaga peek_type_definition<CR>", silent = true, },
{ "<leader>ld", ":Lspsaga peek_definition<CR>", silent = true, },
{ "<leader>ca", ":Lspsaga code_action<CR>", silent = true, },
{ "<leader>lc", ":Lspsaga incoming_calls<CR>", silent = true, },
}, },
["gr"] = {
cmd = ":Lspsaga finder<CR>"
},
["<leader>lt"] = {
cmd = ":Lspsaga peek_type_definition<CR>"
},
["<leader>ld"] = {
cmd = ":Lspsaga peek_definition<CR>"
},
["<leader>ca"] = {
cmd = ":Lspsaga code_action<CR>"
},
["<leader>lc"] = {
cmd = ":Lspsaga incoming_calls<CR>"
},
}
}
local utils = require("utils")
utils.add_opts_to_all_mappings(keymaps, { silent = true })
utils.add_keymaps(keymaps)
end,
} }

View file

@ -1,7 +1,5 @@
local wm = require("window_management")
local function resize_mode() local function resize_mode()
if wm.is_in_resizing_mode() then if require("window_management").is_in_resizing_mode() then
return "▲ Resizing ▼" return "▲ Resizing ▼"
else else
return "" return ""
@ -15,8 +13,7 @@ return {
}, },
event = "VeryLazy", event = "VeryLazy",
lazy = true, lazy = true,
config = function() opts = {
require("lualine").setup {
options = { options = {
theme = "ayu", theme = "ayu",
section_separators = { section_separators = {
@ -56,5 +53,4 @@ return {
}, },
tabline = {}, tabline = {},
} }
end
} }

View file

@ -14,16 +14,8 @@ return {
}, },
event = "VeryLazy", event = "VeryLazy",
lazy = true, lazy = true,
config = function() opts = {},
local neogit = require("neogit") keys = {
neogit.setup() { "<leader>g", function() require("neogit").open({ kind = "vsplit" }) end }
require("utils").add_keymaps({
n = {
["<leader>g"] = {
cmd = function() neogit.open({ kind = "vsplit" }) end,
}, },
}
})
end
} }

View file

@ -5,36 +5,9 @@ local are_stepping_keymaps_active = false
return { return {
"mfussenegger/nvim-dap", "mfussenegger/nvim-dap",
dependencies = { dependencies = {
{
"rcarriga/nvim-dap-ui", "rcarriga/nvim-dap-ui",
opts = {
-- Special adapters
"leoluz/nvim-dap-go",
"mfussenegger/nvim-dap-python",
{ "nvim-neotest/nvim-nio", lazy = true },
"LiadOz/nvim-dap-repl-highlights",
"theHamsta/nvim-dap-virtual-text",
"Weissle/persistent-breakpoints.nvim",
},
config = function()
local dap = require("dap")
local dapui = require("dapui")
-- Special adapters
require("dap-go").setup()
require("dap-python").setup("python3")
-- Special adapters
require("dap.ext.vscode").load_launchjs()
require("persistent-breakpoints").setup {
load_breakpoints_event = { "BufReadPost" }
}
require("nvim-dap-repl-highlights").setup()
require("nvim-dap-virtual-text").setup()
local virtual_text = require("nvim-dap-virtual-text/virtual_text")
local breakpoint_api = require("persistent-breakpoints.api")
dapui.setup({
controls = { controls = {
enabled = false, enabled = false,
}, },
@ -54,32 +27,35 @@ return {
size = 15 size = 15
} }
}, },
}) },
},
-- Special adapters
{ "leoluz/nvim-dap-go", opts = {} },
{ "mfussenegger/nvim-dap-python", },
{ "nvim-neotest/nvim-nio", lazy = true },
{ "LiadOz/nvim-dap-repl-highlights", opts = {} },
{ "theHamsta/nvim-dap-virtual-text", opts = {} },
{ "Weissle/persistent-breakpoints.nvim", opts = { load_breakpoints_event = { "BufReadPost" } } },
},
config = function()
local dap = require("dap")
require("dap-python").setup("python3")
require("dap.ext.vscode").load_launchjs()
local virtual_text = require("nvim-dap-virtual-text/virtual_text")
local breakpoint_api = require("persistent-breakpoints.api")
local stepping_keymaps = { local stepping_keymaps = {
n = { { "m", function() dap.step_out() end },
["m"] = { { "n", function() dap.step_over() end },
cmd = function() { "i", function() dap.step_into() end },
dap.step_out()
end
},
["n"] = {
cmd = function()
dap.step_over()
end
},
["i"] = {
cmd = function()
dap.step_into()
end
},
}
} }
local function enter_debug_mode() local function enter_debug_mode()
dapui.open() require("dapui").open()
if not are_stepping_keymaps_active then if not are_stepping_keymaps_active then
utils.add_temporary_keymaps(stepping_keymaps) utils.set_keymap_list(stepping_keymaps)
are_stepping_keymaps_active = true are_stepping_keymaps_active = true
end end
@ -87,9 +63,9 @@ return {
end end
local function exit_debug_mode() local function exit_debug_mode()
dapui.close() require("dapui").close()
if are_stepping_keymaps_active then if are_stepping_keymaps_active then
utils.remove_keymaps(stepping_keymaps) utils.del_keymap_list(stepping_keymaps)
are_stepping_keymaps_active = false are_stepping_keymaps_active = false
end end
@ -117,36 +93,17 @@ return {
exit_debug_mode() exit_debug_mode()
end end
utils.add_keymaps({ local function dap_stop()
n = {
["<leader>dr"] = {
cmd = function()
dap.continue()
end
},
["<leader>bt"] = {
cmd = function()
breakpoint_api.toggle_breakpoint()
end
},
["<leader>bc"] = {
cmd = function()
breakpoint_api.set_conditional_breakpoint()
end
},
["<leader>br"] = { -- breakpoint remove
cmd = function()
breakpoint_api.clear_all_breakpoints()
end
},
["<leader>ds"] = {
cmd = function()
dap.disconnect({ terminateDebuggee = true }) dap.disconnect({ terminateDebuggee = true })
dap.close() dap.close()
exit_debug_mode() exit_debug_mode()
end end
}, utils.set_keymap_list({
} { "<leader>dr", dap.continue },
{ "<leader>bt", breakpoint_api.toggle_breakpoint },
{ "<leader>bc", breakpoint_api.set_conditional_breakpoint },
{ "<leader>br", breakpoint_api.clear_all_breakpoints },
{ "<leader>ds", dap_stop },
}) })
end, end,
} }

View file

@ -30,29 +30,9 @@ return {
}) })
end end
utils.add_keymaps({ utils.set_keymap_list({
n = { { "gd", function() vim.lsp.buf.definition({ reuse_win = true, }) end, { noremap = true, buffer = bufnr } },
["gd"] = { { "gD", function() vim.lsp.buf.declaration({ reuse_win = true, }) end, { noremap = true, buffer = bufnr } },
cmd = function()
vim.lsp.buf.definition()
end,
opts = {
noremap = true,
silent = true,
buffer = bufnr,
}
},
["gD"] = {
cmd = function()
vim.lsp.buf.declaration()
end,
opts = {
noremap = true,
silent = true,
buffer = bufnr,
}
},
}
}) })
end, end,
} }

View file

@ -1,10 +1,8 @@
local utils = require("utils")
local wm = require("window_management")
local terminal_window = nil local terminal_window = nil
local terminal_bufnr = nil local terminal_bufnr = nil
local function open_terminal_window() local function open_terminal_window()
local utils = require("utils")
if terminal_bufnr and vim.api.nvim_buf_is_valid(terminal_bufnr) and utils.is_buf_buftype(terminal_bufnr, "terminal") then if terminal_bufnr and vim.api.nvim_buf_is_valid(terminal_bufnr) and utils.is_buf_buftype(terminal_bufnr, "terminal") then
vim.cmd("botright split") vim.cmd("botright split")
terminal_window = vim.api.nvim_get_current_win() terminal_window = vim.api.nvim_get_current_win()
@ -23,6 +21,7 @@ local function toggle_terminal()
return return
end end
local utils = require("utils")
open_terminal_window() open_terminal_window()
local term_height = vim.api.nvim_get_option("lines") local term_height = vim.api.nvim_get_option("lines")
@ -37,13 +36,7 @@ local function toggle_terminal()
vim.api.nvim_command("startinsert") vim.api.nvim_command("startinsert")
end end
utils.add_keymaps({ vim.keymap.set("n", "<leader>h", function()
n = {
["<leader>h"] = {
cmd = function()
toggle_terminal() toggle_terminal()
wm.autosize_windows() require("window_management").autosize_windows()
end, end)
}
}
})

View file

@ -1,10 +1,17 @@
local M = {} local M = {}
local overridden_default_keymaps = {} function M.set_keymap_list(keymap_list, mode)
mode = mode or "n"
M.foreach(keymap_list, function(mapping)
vim.keymap.set(mode, mapping[1], mapping[2], mapping[3] or {})
end)
end
local function is_single_keymap_table(map_table) function M.del_keymap_list(keymap_list, mode)
assert(map_table) mode = mode or "n"
return map_table.n or map_table.t or map_table.i or map_table.v or map_table.x or map_table.o M.foreach(keymap_list, function(mapping)
vim.keymap.del(mode, mapping[1])
end)
end end
function M.get_file_names_in_dir(dir, expr, strip_extension) function M.get_file_names_in_dir(dir, expr, strip_extension)
@ -75,94 +82,6 @@ function M.add_opts_to_all_mappings(mappings, opts)
end end
end end
local function pass_keymap_tbl_to_fn(maps, fn)
if is_single_keymap_table(maps) then
fn(maps)
else
for _, map_table in pairs(maps) do
fn(map_table)
end
end
end
local function get_keymaps(mode, buffer)
if buffer then
return vim.api.nvim_buf_get_keymap(buffer, mode)
end
return vim.api.nvim_get_keymap(mode)
end
function M.add_temporary_keymaps(maps)
assert(maps)
pass_keymap_tbl_to_fn(maps, function(map_table)
for mode, entries in pairs(map_table) do
-- We make an assumptino here which is that all the entries are buffers, or not buffers.
-- Meaning, we only check the first entry and trust that the rest are the same.
local result = get_keymaps(mode, (function()
for _, entry in pairs(entries) do
-- nil buffer is treated as a global keymap
return entry.buffer
end
end)())
for code, _ in pairs(entries) do
for _, map in ipairs(result) do
if map.lhs == code then
if not overridden_default_keymaps[mode] then
overridden_default_keymaps[mode] = {}
end
overridden_default_keymaps[mode][code] = {
cmd = map.callback or map.rhs,
opts = {
noremap = map.noremap == 1,
expr = map.expr == 1,
silent = map.silent == 1,
nowait = map.nowait == 1,
script = map.script == 1,
buffer = type(map.buffer) == "number" and map.buffer or nil,
},
}
end
end
end
end
end)
M.add_keymaps(maps)
end
function M.add_keymaps(maps)
assert(maps)
pass_keymap_tbl_to_fn(maps, function(map_table)
for mode, entries in pairs(map_table) do
for code, info in pairs(entries) do
vim.keymap.set(mode, code, info.cmd, info.opts)
end
end
end)
end
function M.remove_keymaps(maps)
assert(maps)
pass_keymap_tbl_to_fn(maps, function(map_table)
for mode, entries in pairs(map_table) do
local overriden_mode = overridden_default_keymaps[mode]
for code, _ in pairs(entries) do
vim.keymap.del(mode, code)
if overriden_mode and overriden_mode[code] then
vim.keymap.set(mode, code, overriden_mode[code].cmd, overriden_mode[code].opts)
end
end
end
end)
end
function M.is_buf_filetype(bufnr, filetype) function M.is_buf_filetype(bufnr, filetype)
return vim.api.nvim_buf_get_option(bufnr, "filetype") == filetype return vim.api.nvim_buf_get_option(bufnr, "filetype") == filetype
end end

View file

@ -189,75 +189,35 @@ end
function M.setup() function M.setup()
local resizing_mode_keymaps = { local resizing_mode_keymaps = {
n = { { "<Left>", function() resize_window(vim.api.nvim_get_current_win(), "h") end },
["<Left>"] = { { "<Down>", function() resize_window(vim.api.nvim_get_current_win(), "j") end },
cmd = function() resize_window(vim.api.nvim_get_current_win(), "h") end { "<Up>", function() resize_window(vim.api.nvim_get_current_win(), "k") end },
}, { "<Right>", function() resize_window(vim.api.nvim_get_current_win(), "l") end },
["<Down>"] = { { "<Esc>", function() exit_resizing_mode() end },
cmd = function() resize_window(vim.api.nvim_get_current_win(), "j") end { "<Enter>", function() exit_resizing_mode() end },
}, { "=", function() M.autosize_windows() end },
["<Up>"] = {
cmd = function() resize_window(vim.api.nvim_get_current_win(), "k") end
},
["<Right>"] = {
cmd = function() resize_window(vim.api.nvim_get_current_win(), "l") end
},
["<Esc>"] = {
cmd = function() exit_resizing_mode() end
},
["<Enter>"] = {
cmd = function() exit_resizing_mode() end
},
["="] = {
cmd = function() M.autosize_windows() end
},
}
} }
local enter_resizing_mode_keymaps = { local enter_resizing_mode_keymaps = {
n = { { "<C- >", function() enter_resizing_mode() end }
["<C- >"] = {
cmd = function() enter_resizing_mode() end
}
},
} }
local window_shifting_keymaps = { local window_shifting_keymaps = {
n = { { "<C-S-Left>", function() swap_window("h") end },
["<C-S-Left>"] = { { "<C-S-Down>", function() swap_window("j") end },
cmd = function() { "<C-S-Up>", function() swap_window("k") end },
swap_window("h") { "<C-S-Right>", function() swap_window("l") end },
end
},
["<C-S-Down>"] = {
cmd = function()
swap_window("j")
end
},
["<C-S-Up>"] = {
cmd = function()
swap_window("k")
end
},
["<C-S-Right>"] = {
cmd = function()
swap_window("l")
end
},
},
} }
utils.add_keymaps({ utils.set_keymap_list(window_shifting_keymaps)
window_shifting_keymaps, utils.set_keymap_list(enter_resizing_mode_keymaps)
enter_resizing_mode_keymaps
})
local function on_resize_mode_enter() local function on_resize_mode_enter()
utils.remove_keymaps(enter_resizing_mode_keymaps) M.del_keymap_list(enter_resizing_mode_keymaps)
utils.add_keymaps(resizing_mode_keymaps) M.set_keymap_list(resizing_mode_keymaps)
end end
local function on_resize_mode_exit() local function on_resize_mode_exit()
utils.remove_keymaps(resizing_mode_keymaps) M.del_keymap_list(resizing_mode_keymaps)
utils.add_keymaps(enter_resizing_mode_keymaps) M.set_keymap_list(enter_resizing_mode_keymaps)
end end
local window_management_augroup = "WindowManagementEvents" local window_management_augroup = "WindowManagementEvents"