From 72ba9591e75eac9c0097fe5b44aa675dc40baa62 Mon Sep 17 00:00:00 2001 From: ViViDboarder Date: Wed, 7 Jun 2023 16:26:46 -0700 Subject: [PATCH] Use new neovim api for augroup, autocommands, and user commands Also starts using WhichKey for more thigns, but in a very basic way. This part should probably be refactored and tested when devising a standard way to do mappings with descriptions. --- neovim/lua/_bindings.lua | 5 +++-- neovim/lua/_colors.lua | 11 ++++++++++- neovim/lua/_settings.lua | 13 ++++++++++--- neovim/lua/init.lua | 11 +++++++++-- neovim/lua/plugins.lua | 10 +++++++++- neovim/lua/utils.lua | 34 ++++++++++++++++++---------------- 6 files changed, 59 insertions(+), 25 deletions(-) diff --git a/neovim/lua/_bindings.lua b/neovim/lua/_bindings.lua index 11d405a..2905d0c 100644 --- a/neovim/lua/_bindings.lua +++ b/neovim/lua/_bindings.lua @@ -1,5 +1,6 @@ local utils = require("utils") +-- TODO: Use which-key for mappings local map = vim.api.nvim_set_keymap local opt_silent = { silent = true } @@ -32,8 +33,8 @@ map("v", "pp", "p", opt_default) map("v", "po", '"_dP', opt_default) -- Buffer nav -map("n", "gb", ":bnext", {}) -map("n", "gB", ":bprevious", {}) +map("n", "gb", ":bnext", { desc = "Next buffer" }) +map("n", "gB", ":bprevious", { desc = "Previous buffer" }) -- Easy redo map("n", "U", ":redo", opt_default) diff --git a/neovim/lua/_colors.lua b/neovim/lua/_colors.lua index e71863f..57436bf 100644 --- a/neovim/lua/_colors.lua +++ b/neovim/lua/_colors.lua @@ -61,7 +61,16 @@ end -- Don't need the autocommand when dark-notify is installed local utils = require("utils") if not utils.is_plugin_loaded("dark-notify") then - utils.autocmd("auto_colors", "FocusGained * call v:lua.update_colors()") + -- TODO: remove check when dropping v0.6.0 + if vim.fn.has("nvim-0.7.0") == 1 then + vim.api.nvim_create_autocmd({ "FocusGained" }, { + pattern = "*", + callback = _G.update_colors, + group = vim.api.nvim_create_augroup("auto_colors", { clear = true }), + }) + else + utils.autocmd("auto_colors", "FocusGained * call v:lua.update_colors()") + end end -- Initial setting of colors diff --git a/neovim/lua/_settings.lua b/neovim/lua/_settings.lua index 304172f..1f13acc 100644 --- a/neovim/lua/_settings.lua +++ b/neovim/lua/_settings.lua @@ -37,9 +37,16 @@ o.mouse = "a" -- Autocomplete options o.completeopt = "menuone,noinsert,noselect,preview" -utils.augroup("close_preview", function() - vim.cmd("autocmd! CompleteDone * if pumvisible() == 0 | pclose | endif") -end) +-- TODO: remove check when dropping v0.6.0 +if vim.fn.has("nvim-0.7.0") == 1 then + vim.api.nvim_create_autocmd({ "CompleteDone" }, { + pattern = "*", + command = "if pumvisible() == 0 | pclose | endif", + group = vim.api.nvim_create_augroup("close_preview", { clear = true }), + }) +else + utils.autocmd("close_preview", "CompleteDone * if pumvisible() == 0 | pclose | endif", true) +end local has = vim.fn.has g.is_mac = (has("mac") or has("macunix") or has("gui_macvim") or vim.fn.system("uname"):find("^darwin") ~= nil) diff --git a/neovim/lua/init.lua b/neovim/lua/init.lua index 9290dd0..b66b5c3 100644 --- a/neovim/lua/init.lua +++ b/neovim/lua/init.lua @@ -6,8 +6,14 @@ require("_bindings") require("_colors") -- Create commands -vim.cmd("command! TagsUpdate !ctags -R .") -vim.cmd("command! Todo grep TODO") +-- TODO: remove check when dropping v0.6.0 +if vim.fn.has("nvim-0.7.0") == 1 then + vim.api.nvim_create_user_command("TagsUpdate", "!ctags -R .", { desc = "Update ctags" }) + vim.api.nvim_create_user_command("Todo", "grep TODO", { desc = "Search for TODO tags" }) +else + vim.cmd("command! TagsUpdate !ctags -R .") + vim.cmd("command! Todo grep TODO") +end -- Use better grep programs if vim.fn.executable("rg") == 1 then @@ -20,6 +26,7 @@ elseif vim.fn.executable("ack") == 1 then end -- Disable polyglot for langauges I've added special support for +-- TODO: Can this be moved somewhere better? vim.g.polyglot_disabled = { "go", "rust" } -- Plugins diff --git a/neovim/lua/plugins.lua b/neovim/lua/plugins.lua index ac91ffa..b114d10 100644 --- a/neovim/lua/plugins.lua +++ b/neovim/lua/plugins.lua @@ -131,6 +131,7 @@ use({ use({ "tomtom/tcomment_vim", config = function() + -- TODO: use which-key? vim.api.nvim_set_keymap("n", "//", ":TComment", { silent = true, noremap = true }) vim.api.nvim_set_keymap("v", "//", ":TCommentBlock", { silent = true, noremap = true }) end, @@ -140,7 +141,12 @@ use({ use({ "FooSoft/vim-argwrap", config = function() - vim.api.nvim_set_keymap("n", "a", "ArgWrap", { silent = true, noremap = true }) + -- TODO: use which-key? + vim.api.nvim_set_keymap("n", "a", "ArgWrap", { + silent = true, + noremap = true, + desc = "Wrap or unwrap arguments", + }) end, }) @@ -149,6 +155,7 @@ use({ "tpope/vim-fugitive", config = function() local opts = { silent = true, noremap = true } + -- TODO: use which-key? vim.api.nvim_set_keymap("n", "gb", "Git blame", opts) vim.api.nvim_set_keymap("n", "gc", "Git commit", opts) vim.api.nvim_set_keymap("n", "gd", "Git diff", opts) @@ -161,6 +168,7 @@ use({ use({ "milkypostman/vim-togglelist", config = function() + -- TODO: use which-key? vim.api.nvim_set_keymap("n", "", ":call ToggleQuickfixList()", { silent = true, noremap = true }) vim.api.nvim_set_keymap("n", "", ":call ToggleLocationList()", { silent = true, noremap = true }) end, diff --git a/neovim/lua/utils.lua b/neovim/lua/utils.lua index 69ca566..42b0c25 100644 --- a/neovim/lua/utils.lua +++ b/neovim/lua/utils.lua @@ -1,30 +1,32 @@ -- luacheck: globals packer_plugins local M = {} --- Utils taken from https://github.com/zzzeyez/dots/blob/master/nvim/lua/utils.lua -- Key mapping function M.map(mode, key, result, opts) - vim.fn.nvim_set_keymap(mode, key, result, { - noremap = true, - silent = opts.silent or false, - expr = opts.expr or false, - script = opts.script or false, - }) -end - -function M.augroup(group, fn) - vim.api.nvim_command("augroup " .. group) - vim.api.nvim_command("autocmd!") - fn() - vim.api.nvim_command("augroup END") + M.try_require("which-key", function(wk) + local mappings = {} + mappings[key] = result + wk.register(mappings, { + mode = mode, + noremap = true, + silent = opts.silent or false, + expr = opts.expr or false, + script = opts.script or false, + }) + end, function() + vim.fn.nvim_set_keymap(mode, key, result, { + noremap = true, + silent = opts.silent or false, + expr = opts.expr or false, + script = opts.script or false, + }) + end) end function M.get_color(synID, what, mode) return vim.fn.synIDattr(vim.fn.synIDtrans(vim.fn.hlID(synID)), what, mode) end --- end zzzeyez utils - -- Create an autocmd function M.autocmd(group, cmds, clear) clear = clear == nil and false or clear