From 0e446b86d10198b93d3203bc536af1e5aba1802b Mon Sep 17 00:00:00 2001 From: ViViDboarder Date: Fri, 9 Jun 2023 12:10:55 -0700 Subject: [PATCH] Now using v0.7+ API functions in many places This preserves backwards compatibility still, but that will be removed at some point when dropping .6. At that point, another refactor can simplify many of the functions used. --- neovim/lua/_bindings.lua | 80 +++++++++++---------- neovim/lua/init.lua | 10 --- neovim/lua/plugins.lua | 28 +++----- neovim/lua/plugins/completion.lua | 4 +- neovim/lua/plugins/grepper.lua | 45 ++++++++---- neovim/lua/plugins/lsp.lua | 102 +++++++++++++++----------- neovim/lua/plugins/null-ls/init.lua | 2 +- neovim/lua/plugins/telescope.lua | 64 ++++++++++++----- neovim/lua/utils.lua | 106 ++++++++++++++++------------ 9 files changed, 256 insertions(+), 185 deletions(-) diff --git a/neovim/lua/_bindings.lua b/neovim/lua/_bindings.lua index 2905d0c..b371610 100644 --- a/neovim/lua/_bindings.lua +++ b/neovim/lua/_bindings.lua @@ -1,48 +1,43 @@ local utils = require("utils") --- TODO: Use which-key for mappings -local map = vim.api.nvim_set_keymap - -local opt_silent = { silent = true } -local opt_default = { silent = true, noremap = true } -map("n", "", ":set wrap!", opt_silent) -map("n", "lw", ":set wrap!", opt_silent) -map("n", "", ":set invnumber", opt_silent) -map("n", "ln", ":set invnumber", opt_silent) -map("n", "/", ":set hlsearch! hlsearch?", opt_silent) -map("n", "cs", ":nohlsearch", opt_silent) -map("n", "ws", ":set list!", opt_silent) +utils.keymap_set("n", "", ":set wrap!", { desc = "Toggle line wrapping" }) +utils.keymap_set("n", "lw", ":set wrap!", { desc = "Toggle line wrapping" }) +utils.keymap_set("n", "", ":set invnumber", { desc = "Toggle line numbers" }) +utils.keymap_set("n", "ln", ":set invnumber", { desc = "Toggle line numbers" }) +utils.keymap_set("n", "/", ":set hlsearch! hlsearch?", { desc = "Toggle search highlighting" }) +utils.keymap_set("n", "cs", ":nohlsearch", { desc = "Clear search highlighting" }) +utils.keymap_set("n", "ws", ":set list!", { desc = "Toggle whitespace characters" }) -- Save and quit typos -map("c", "WQ", "wq", opt_silent) -map("c", "Wq", "wq", opt_silent) -map("c", "W", "w", opt_silent) -map("c", "Q", "q", opt_silent) -map("c", "Q!", "q!", opt_silent) -map("c", "Qa", "qa", opt_silent) -map("c", "Qa!", "qa!", opt_silent) -map("c", "QA", "qa", opt_silent) -map("c", "QA!", "qa!", opt_silent) -map("c", "w;", "w", opt_default) -map("c", "W;", "w", opt_default) -map("c", "q;", "q", opt_default) -map("c", "Q;", "q", opt_default) +utils.keymap_set("c", "WQ", "wq", { desc = "Write and quit" }) +utils.keymap_set("c", "Wq", "wq", { desc = "Write and quit" }) +utils.keymap_set("c", "W", "w", { desc = "Write" }) +utils.keymap_set("c", "Q", "q", { desc = "Quit" }) +utils.keymap_set("c", "Q!", "q!", { desc = "Force quit" }) +utils.keymap_set("c", "Qa", "qa", { desc = "Quit all" }) +utils.keymap_set("c", "Qa!", "qa!", { desc = "Force quit all" }) +utils.keymap_set("c", "QA", "qa", { desc = "Quit all" }) +utils.keymap_set("c", "QA!", "qa!", { desc = "Force quit all" }) +utils.keymap_set("c", "w;", "w", { desc = "Write" }) +utils.keymap_set("c", "W;", "w", { desc = "Write" }) +utils.keymap_set("c", "q;", "q", { desc = "Quit" }) +utils.keymap_set("c", "Q;", "q", { desc = "Quit" }) -- Paste over -map("v", "pp", "p", opt_default) -map("v", "po", '"_dP', opt_default) +utils.keymap_set("v", "pp", "p", { desc = "Paste" }) +utils.keymap_set("v", "po", '"_dP', { desc = "Paste over and keep clipboard" }) -- Buffer nav -map("n", "gb", ":bnext", { desc = "Next buffer" }) -map("n", "gB", ":bprevious", { desc = "Previous buffer" }) +utils.keymap_set("n", "gb", ":bnext", { desc = "Next buffer" }) +utils.keymap_set("n", "gB", ":bprevious", { desc = "Previous buffer" }) -- Easy redo -map("n", "U", ":redo", opt_default) +utils.keymap_set("n", "U", ":redo", { desc = "Redo" }) -- Make escape easier -map("i", "jk", "", opt_default) -map("i", "``", "", opt_default) -map("v", "``", "", opt_default) +utils.keymap_set("i", "jk", "", { desc = "Escape insert" }) +utils.keymap_set("i", "``", "", { desc = "Escape insert" }) +utils.keymap_set("v", "``", "", { desc = "Escape visual" }) -- C-Space completion function _G.complete_space() @@ -58,12 +53,21 @@ function _G.complete_space() return utils.t("") end end -map("i", "", "v:lua.complete_space()", { expr = true }) +utils.keymap_set("i", "", "v:lua.complete_space()", { expr = true }) + +-- 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" }) + vim.api.nvim_create_user_command("Spell", "setlocal spell! spelllang=en_us", { desc = "Toggle spelling" }) +else + vim.cmd("command! TagsUpdate !ctags -R .") + vim.cmd("command! Todo grep TODO") + vim.cmd("command Spell setlocal spell! spelllang=en_us") +end --- Easily toggle spelling -vim.cmd("command Spell setlocal spell! spelllang=en_us") -- Pop spelling completion for word under cursor -map("n", "s", "viwas", {}) +utils.keymap_set("n", "s", "viwas", { desc = "Show spelling suggestions" }) -- Build on F5 -map("n", "", ":make", {}) +utils.keymap_set("n", "", ":make", { desc = "Run make" }) diff --git a/neovim/lua/init.lua b/neovim/lua/init.lua index b66b5c3..5cbb876 100644 --- a/neovim/lua/init.lua +++ b/neovim/lua/init.lua @@ -5,16 +5,6 @@ require("_settings") require("_bindings") require("_colors") --- Create commands --- 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 o.grepprg = "rg --vimgrep --no-heading --color=never" diff --git a/neovim/lua/plugins.lua b/neovim/lua/plugins.lua index b114d10..aa5b596 100644 --- a/neovim/lua/plugins.lua +++ b/neovim/lua/plugins.lua @@ -131,9 +131,8 @@ 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 }) + require("utils").keymap_set("n", "//", ":TComment", { desc = "Toggle comment" }) + require("utils").keymap_set("v", "//", ":TCommentBlock", { desc = "Toggle comment" }) end, }) @@ -141,10 +140,7 @@ use({ use({ "FooSoft/vim-argwrap", config = function() - -- TODO: use which-key? - vim.api.nvim_set_keymap("n", "a", "ArgWrap", { - silent = true, - noremap = true, + require("utils").keymap_set("n", "a", "ArgWrap", { desc = "Wrap or unwrap arguments", }) end, @@ -154,13 +150,11 @@ use({ 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) - vim.api.nvim_set_keymap("n", "gs", "Git", opts) - vim.api.nvim_set_keymap("n", "gw", "Git write", opts) + require("utils").keymap_set("n", "gb", "Git blame", { desc = "Git blame" }) + require("utils").keymap_set("n", "gc", "Git commit", { desc = "Git commit" }) + require("utils").keymap_set("n", "gd", "Git diff", { desc = "Git diff" }) + require("utils").keymap_set("n", "gs", "Git", { desc = "Git status" }) + require("utils").keymap_set("n", "gw", "Git write", { desc = "Git write" }) end, }) @@ -168,9 +162,8 @@ 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 }) + require("utils").keymap_set("n", "", ":call ToggleQuickfixList()", { desc = "Toggle quickfix" }) + require("utils").keymap_set("n", "", ":call ToggleLocationList()", { desc = "Toggle location list" }) end, }) @@ -406,6 +399,7 @@ use("hsanson/vim-android") use({ "sheerun/vim-polyglot", config = function() + -- TODO: Replace with api calls when dropping 0.6 vim.cmd([[ augroup polyglot_fts au BufRead,BufNewFile */playbooks/*.yml,*/playbooks/*.yaml set filetype=yaml.ansible diff --git a/neovim/lua/plugins/completion.lua b/neovim/lua/plugins/completion.lua index be18116..10166c4 100644 --- a/neovim/lua/plugins/completion.lua +++ b/neovim/lua/plugins/completion.lua @@ -45,11 +45,11 @@ function M.config_cmp() }) -- Add a plug mapping to use in C-Space binding - vim.api.nvim_set_keymap( + require("utils").keymap_set( "i", "(cmp_complete)", "lua require('cmp').complete()", - { silent = true, noremap = true } + { desc = "Autocomplete" } ) end diff --git a/neovim/lua/plugins/grepper.lua b/neovim/lua/plugins/grepper.lua index e8a28aa..e4953b1 100644 --- a/neovim/lua/plugins/grepper.lua +++ b/neovim/lua/plugins/grepper.lua @@ -8,21 +8,40 @@ vim.g.grepper = { dir = "repo,cwd", } -local map = vim.api.nvim_set_keymap -local opt_silent = { silent = true } -map("n", "gs", "(GrepperOperator)", opt_silent) -map("x", "gs", "(GrepperOperator)", opt_silent) +require("utils").keymap_set({ "n", "x" }, "gs", "(GrepperOperator)", { + silent = true, + noremap = false, + desc = "Grepper", +}) -- Override Todo command to use Grepper -vim.cmd("command! Todo :Grepper -noprompt -query TODO") +-- TODO: Remove check when dropping 0.6 +if vim.fn.has("nvim-0.7.0") == 1 then + vim.api.nvim_create_user_command("Todo", ":Grepper -noprompt -query TODO", { desc = "Search for TODO tags" }) +else + vim.cmd("command! Todo :Grepper -noprompt -query TODO") +end -- Make some shortands for various grep programs -if vim.fn.executable("rg") == 1 then - vim.cmd("command -nargs=+ Rg :GrepperRg ") -end -if vim.fn.executable("ag") == 1 then - vim.cmd("command -nargs=+ Ag :GrepperAg ") -end -if vim.fn.executable("ack") == 1 then - vim.cmd("command -nargs=+ Ack :GrepperAck ") +-- TODO: Remove check when dropping 0.6 +if vim.fn.has("nvim-0.7.0") == 1 then + if vim.fn.executable("rg") == 1 then + vim.api.nvim_create_user_command("Rg", ":GrepperRg ", { nargs = "+", desc = "Ripgrep" }) + end + if vim.fn.executable("ag") == 1 then + vim.api.nvim_create_user_command("Ag", ":GrepperAg ", { nargs = "+", desc = "Silversearcher" }) + end + if vim.fn.executable("ack") == 1 then + vim.api.nvim_create_user_command("Ack", ":GrepperAck ", { nargs = "+", desc = "Ack search" }) + end +else + if vim.fn.executable("rg") == 1 then + vim.cmd("command -nargs=+ Rg :GrepperRg ") + end + if vim.fn.executable("ag") == 1 then + vim.cmd("command -nargs=+ Ag :GrepperAg ") + end + if vim.fn.executable("ack") == 1 then + vim.cmd("command -nargs=+ Ack :GrepperAck ") + end end diff --git a/neovim/lua/plugins/lsp.lua b/neovim/lua/plugins/lsp.lua index 51b70e6..f593480 100644 --- a/neovim/lua/plugins/lsp.lua +++ b/neovim/lua/plugins/lsp.lua @@ -73,10 +73,6 @@ local function get_default_attach(override_capabilities) server_capabilities = vim.tbl_extend("force", server_capabilities, override_capabilities or {}) end - local function buf_set_keymap(...) - vim.api.nvim_buf_set_keymap(bufnr, ...) - end - local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end @@ -94,27 +90,37 @@ local function get_default_attach(override_capabilities) end -- Mappings - local opts = { noremap = true, silent = true } - local lsp_keymap = utils.keymap_group("n", "l", opts, bufnr) - lsp_keymap("h", "lua vim.lsp.buf.hover()") - lsp_keymap("rn", "lua vim.lsp.buf.rename()") - lsp_keymap("e", "lua vim.diagnostic.open_float()") - lsp_keymap("D", "lua vim.lsp.buf.declaration()") - lsp_keymap("d", "lua vim.lsp.buf.definition()") - lsp_keymap("t", "lua vim.lsp.buf.type_definition()") - lsp_keymap("i", "lua vim.lsp.buf.implementation()") - lsp_keymap("s", "lua vim.lsp.buf.signature_help()") - lsp_keymap("wa", "lua vim.lsp.buf.add_workspace_folder()", opts) - lsp_keymap("wr", "lua vim.lsp.buf.remove_workspace_folder()", opts) - lsp_keymap("wl", "lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))", opts) - lsp_keymap("r", "lua vim.lsp.buf.references()", opts) - lsp_keymap("p", "lua vim.lsp.diagnostic.goto_prev()", opts) - lsp_keymap("n", "lua vim.lsp.diagnostic.goto_next()", opts) + -- TODO: use functions instead of strings when dropping 0.6 + local lsp_keymap = utils.curry_keymap("n", "l", { buffer = bufnr }) + lsp_keymap("h", "lua vim.lsp.buf.hover()", { desc = "Display hover" }) + lsp_keymap("rn", "lua vim.lsp.buf.rename()", { desc = "Refactor rename" }) + lsp_keymap("e", "lua vim.diagnostic.open_float()", { desc = "Open float dialog" }) + lsp_keymap("D", "lua vim.lsp.buf.declaration()", { desc = "Go to declaration" }) + lsp_keymap("d", "lua vim.lsp.buf.definition()", { desc = "Go to definition" }) + lsp_keymap("t", "lua vim.lsp.buf.type_definition()", { desc = "Go to type definition" }) + lsp_keymap("i", "lua vim.lsp.buf.implementation()", { desc = "Show implementations" }) + lsp_keymap("s", "lua vim.lsp.buf.signature_help()", { desc = "Show signature help" }) + lsp_keymap("wa", "lua vim.lsp.buf.add_workspace_folder()", { desc = "Workspace: Add folder" }) + lsp_keymap("wr", "lua vim.lsp.buf.remove_workspace_folder()", { desc = "Workspace: Remove folder" }) + lsp_keymap( + "wl", + "lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))", + { desc = "Workspace: List folders" } + ) + lsp_keymap("r", "lua vim.lsp.buf.references()", { desc = "References" }) + lsp_keymap("p", "lua vim.lsp.diagnostic.goto_prev()", { desc = "Diagnostics: Go to previous" }) + lsp_keymap("n", "lua vim.lsp.diagnostic.goto_next()", { desc = "Diagnostics: Go to next" }) -- Set insert keymap for signature help - buf_set_keymap("i", "", "lua vim.lsp.buf.signature_help()", opts) + utils.keymap_set( + "i", + "", + "lua vim.lsp.buf.signature_help()", + { buffer = bufnr, desc = "Show signature help" } + ) -- Older keymaps + --[[ buf_set_keymap("n", "gD", "lua vim.lsp.buf.declaration()", opts) buf_set_keymap("n", "gd", "lua vim.lsp.buf.definition()", opts) buf_set_keymap("n", "K", "lua vim.lsp.buf.hover()", opts) @@ -129,14 +135,17 @@ local function get_default_attach(override_capabilities) buf_set_keymap("n", "[d", "lua vim.lsp.diagnostic.goto_prev()", opts) buf_set_keymap("n", "]d", "lua vim.lsp.diagnostic.goto_next()", opts) buf_set_keymap("n", "q", "lua vim.lsp.diagnostic.set_loclist()", opts) + --]] -- Open diagnostic on hold if vim["diagnostic"] ~= nil then + -- TODO: When dropping 0.6, use lua aucommand api vim.cmd([[autocmd CursorHold * lua vim.diagnostic.open_float(nil, {focus=false, scope="cursor"})]]) end -- Use IncRename if available if utils.try_require("inc_rename") ~= nil then + -- TODO: Should I be using this for calling lua functions from keymaps? vim.keymap.set("n", "rn", function() return ":IncRename " .. vim.fn.expand("") end, { expr = true, buffer = true, desc = "Rename current symbol" }) @@ -144,25 +153,35 @@ local function get_default_attach(override_capabilities) -- Set some keybinds conditional on server capabilities if vim.fn.has("nvim-0.8") == 1 then - buf_set_keymap("n", "lf", "lua vim.lsp.buf.format({async=true})", opts) - buf_set_keymap("v", "lf", "lua vim.lsp.buf.format({async=true})", opts) + lsp_keymap("f", "lua vim.lsp.buf.format({async=true})", { desc = "Format code" }) + lsp_keymap( + "f", + "lua vim.lsp.buf.format({async=true})", + { mode = "v", desc = "Format selected code" } + ) if server_capabilities.documentFormattingProvider then - vim.cmd([[ - augroup lsp_format - autocmd! - autocmd BufWritePre *.rs,*.go,*.sh,*.lua lua vim.lsp.buf.format({async=false, timeout_ms=1000}) - augroup END - ]]) + vim.api.nvim_create_autocmd({ "BufWritePre" }, { + pattern = { "*.rs", "*.go", "*.sh", "*.lua" }, + callback = function() + vim.lsp.buf.format({ async = false, timeout_ms = 1000 }) + end, + group = vim.api.nvim_create_augroup("lsp_format", { clear = true }), + desc = "Auto format code on save", + }) end else -- HACK: Support for <0.8 - buf_set_keymap("n", "lf", "lua vim.lsp.buf.formatting()", opts) - buf_set_keymap("n", "lfr", "lua vim.lsp.buf.range_formatting()", opts) + lsp_keymap("f", "lua vim.lsp.buf.formatting()", { desc = "Format code" }) + lsp_keymap( + "f", + "lua vim.lsp.buf.range_formatting()", + { mode = "v", desc = "Format selected code" } + ) if server_capabilities.documentFormattingProvider then vim.cmd([[ augroup lsp_format autocmd! - autocmd BufWritePre *.rs,*.go,*.sh,*.lua lua vim.lsp.buf.formatting_sync(nil, 1000) + autocmd BufWritePre lua vim.lsp.buf.formatting_sync(nil, 1000) augroup END ]]) end @@ -185,26 +204,25 @@ local function get_default_attach(override_capabilities) -- Some override some fuzzy finder bindings to use lsp sources if utils.try_require("telescope") ~= nil then -- Replace some Telescope bindings with LSP versions + local telescope_keymap = utils.curry_keymap("n", "f", { buffer = bufnr }) if server_capabilities.documentSymbolProvider then - buf_set_keymap("n", "t", "Telescope lsp_document_symbols", opts) + telescope_keymap("t", "Telescope lsp_document_symbols", { desc = "Find buffer tags" }) end if server_capabilities.workspaceSymbolProvider then - buf_set_keymap("n", "ft", "Telescope lsp_dynamic_workspace_symbols", opts) + telescope_keymap("T", "Telescope lsp_dynamic_workspace_symbols", { desc = "Find tags" }) end -- Replace some LSP bindings with Telescope ones if server_capabilities.definitionProvider then - lsp_keymap("d", "Telescope lsp_definitions") + lsp_keymap("d", "Telescope lsp_definitions", { desc = "Find definition" }) end if server_capabilities.typeDefinitionProvider then - lsp_keymap("t", "Telescope lsp_type_definition()") + lsp_keymap("t", "Telescope lsp_type_definition", { desc = "Find type definition" }) end - lsp_keymap("i", "Telescope lsp_implementations") - lsp_keymap("r", "Telescope lsp_references") - lsp_keymap("A", "Telescope lsp_code_actions") - - buf_set_keymap("n", "ca", "Telescope lsp_code_actions", opts) - buf_set_keymap("v", "lA", "Telescope lsp_range_code_actions", opts) + lsp_keymap("i", "Telescope lsp_implementations", { desc = "Find implementations" }) + lsp_keymap("r", "Telescope lsp_references", { desc = "Find references" }) + lsp_keymap("A", "Telescope lsp_code_actions", { desc = "Select code actions" }) + lsp_keymap("A", "Telescope lsp_range_code_actions", { mode = "v", desc = "Select code actions" }) end -- Attach navic for statusline location diff --git a/neovim/lua/plugins/null-ls/init.lua b/neovim/lua/plugins/null-ls/init.lua index 35cd82d..c8b65c7 100644 --- a/neovim/lua/plugins/null-ls/init.lua +++ b/neovim/lua/plugins/null-ls/init.lua @@ -30,7 +30,7 @@ local function disable_formatter_filetypes_for_existing_servers(sources, preserv sources = vim.tbl_map(function(builtin) if builtin.method == NULL_LS_FORMATTING - or (type(builtin.method) == "table" and utils.list_contains(builtin.method, NULL_LS_FORMATTING)) + or (type(builtin.method) == "table" and vim.tbl_contains(builtin.method, NULL_LS_FORMATTING)) then return builtin.with({ disabled_filetypes = server_filetypes }) end diff --git a/neovim/lua/plugins/telescope.lua b/neovim/lua/plugins/telescope.lua index da6a073..ce9d011 100644 --- a/neovim/lua/plugins/telescope.lua +++ b/neovim/lua/plugins/telescope.lua @@ -20,25 +20,53 @@ local function config_telescope() layout_strategy = "flex", }, }) - local opts = { silent = true, noremap = true } - vim.api.nvim_set_keymap("n", "", "lua require('telescope.builtin').find_files()", opts) - vim.api.nvim_set_keymap("n", "ff", "lua require('telescope.builtin').find_files()", opts) - vim.api.nvim_set_keymap("n", "fl", "lua require('telescope.builtin').resume()", opts) - vim.api.nvim_set_keymap("n", "fh", "lua require('telescope.builtin').help_tags()", opts) - vim.api.nvim_set_keymap("n", "b", "lua require('telescope.builtin').buffers()", opts) - vim.api.nvim_set_keymap("n", "fb", "lua require('telescope.builtin').buffers()", opts) - vim.api.nvim_set_keymap("n", "t", "lua require('telescope.builtin').current_buffer_tags()", opts) - vim.api.nvim_set_keymap("n", "ft", "lua require('telescope.builtin').tags()", opts) - vim.api.nvim_set_keymap("n", "fg", "lua require('telescope.builtin').live_grep()", opts) - vim.api.nvim_set_keymap("n", "*", "lua require('telescope.builtin').grep_string()", opts) + local utils = require("utils") + utils.try_require("which-key", function(wk) + wk.register({ + [""] = { "lua require('telescope.builtin').find_files()", "Find files" }, + }) + wk.register({ + b = { "lua require('telescope.builtin').buffers()", "Find buffers" }, + t = { "lua require('telescope.builtin').current_buffer_tags()", "Find buffer tags" }, + ["*"] = { "lua require('telescope.builtin').grep_string()", "Find buffers" }, + f = { + b = { "lua require('telescope.builtin').buffers()", "Find buffers" }, + f = { "lua require('telescope.builtin').find_files()", "Find file" }, + g = { "lua require('telescope.builtin').live_grep()", "Live grep" }, + h = { "lua require('telescope.builtin').help_tags()", "Find help tags" }, + l = { "lua require('telescope.builtin').resume()", "Resume finding" }, + t = { "lua require('telescope.builtin').current_buffer_tags()", "Find buffer tags" }, + T = { "lua require('telescope.builtin').tags()", "Find tags" }, + }, + }, { + prefix = "", + }) + end, function() + local opts = { silent = true, noremap = true } + vim.api.nvim_set_keymap("n", "", "lua require('telescope.builtin').find_files()", opts) + vim.api.nvim_set_keymap("n", "ff", "lua require('telescope.builtin').find_files()", opts) + vim.api.nvim_set_keymap("n", "fl", "lua require('telescope.builtin').resume()", opts) + vim.api.nvim_set_keymap("n", "fh", "lua require('telescope.builtin').help_tags()", opts) + vim.api.nvim_set_keymap("n", "b", "lua require('telescope.builtin').buffers()", opts) + vim.api.nvim_set_keymap("n", "fb", "lua require('telescope.builtin').buffers()", opts) + vim.api.nvim_set_keymap( + "n", + "t", + "lua require('telescope.builtin').current_buffer_tags()", + opts + ) + vim.api.nvim_set_keymap("n", "ft", "lua require('telescope.builtin').tags()", opts) + vim.api.nvim_set_keymap("n", "fg", "lua require('telescope.builtin').live_grep()", opts) + vim.api.nvim_set_keymap("n", "*", "lua require('telescope.builtin').grep_string()", opts) - -- Better spelling replacement for word under cursor - vim.api.nvim_set_keymap( - "n", - "s", - "lua require('telescope.builtin').spell_suggest(require('telescope.themes').get_cursor())", - opts - ) + -- Better spelling replacement for word under cursor + vim.api.nvim_set_keymap( + "n", + "s", + "lua require('telescope.builtin').spell_suggest(require('telescope.themes').get_cursor())", + opts + ) + end) load_extensions() end diff --git a/neovim/lua/utils.lua b/neovim/lua/utils.lua index 42b0c25..75dab32 100644 --- a/neovim/lua/utils.lua +++ b/neovim/lua/utils.lua @@ -1,33 +1,12 @@ -- luacheck: globals packer_plugins local M = {} --- Key mapping -function M.map(mode, key, result, opts) - 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 -- Create an autocmd +-- TODO: Remove this and use nvim_create_autocmd and nvim_create_augroup when dropping .6 function M.autocmd(group, cmds, clear) clear = clear == nil and false or clear if type(cmds) == "string" then @@ -133,16 +112,6 @@ function M.cmp_versions(a, b) return 0 end --- Checks if a list contains a value -function M.list_contains(list, value) - for _, v in pairs(list) do - if v == value then - return true - end - end - return false -end - -- Materializes an iterator into a list function M.materialize_list(list, iterator) if iterator == nil then @@ -156,9 +125,10 @@ function M.materialize_list(list, iterator) return list end +-- Special not actually nil, but to be treated as nil value in some cases M.nil_val = {} --- Maps a set of version rules to a value eg. [">0.5.0"] = "has 0.5.0" +-- Maps a set of version rules to a value eg. [">=0.5.0"] = "has 0.5.0" -- If more than one rule matches, the one with the greatest version number is used function M.map_version_rule(rules) local v = vim.version() @@ -174,15 +144,15 @@ function M.map_version_rule(rules) local matches = function(cmp, major, minor, patch) local c = M.cmp_versions(current_version, { major, minor, patch }) if c == 1 then - if M.list_contains({ ">", ">=" }, cmp) then + if vim.tbl_contains({ ">", ">=" }, cmp) then return true end elseif c == 0 then - if M.list_contains({ "==", ">=", "<=" }, cmp) then + if vim.tbl_contains({ "==", ">=", "<=" }, cmp) then return true end elseif c == -1 then - if M.list_contains({ "<", "<=" }, cmp) then + if vim.tbl_contains({ "<", "<=" }, cmp) then return true end end @@ -215,17 +185,65 @@ function M.swapped_map(v, func) return func end --- Returns a function used to create keymaps with consistent prefixes -function M.keymap_group(mode, prefix, opts, bufnr) - local map_fn = vim.api.nvim_set_keymap - if bufnr ~= nil then - map_fn = function(...) - return vim.api.nvim_buf_set_keymap(bufnr, ...) +-- Pop from table +function M.tbl_pop(t, key) + local v = t[key] + t[key] = nil + return v +end + +-- Calls keymap_set with preferred defaults +function M.keymap_set(mode, lhs, rhs, opts) + opts = vim.tbl_extend("keep", opts, { noremap = true, silent = true }) + -- TODO: Remove this check when dropping 0.6 support + if vim.fn.has("nvim-0.7") == 1 then + vim.keymap.set(mode, lhs, rhs, opts) + else + -- Desc is not supported in 0.6 + opts["desc"] = nil + if type(mode) ~= "string" then + for _, m in pairs(mode) do + M.keymap_set(m, lhs, rhs, opts) + end + return + end + -- Buffer requires a different function + local buffer = M.tbl_pop(opts, "buffer") + if buffer == nil then + vim.api.nvim_set_keymap(mode, lhs, rhs, opts) + else + vim.api.nvim_buf_set_keymap(buffer, mode, lhs, rhs, opts) end end +end - return function(keys, rhs, new_opts) - map_fn(mode, prefix .. keys, rhs, new_opts or opts) +-- Returns a curried function for passing data into vim.keymap.set +function M.curry_keymap(mode, prefix, default_opts) + default_opts = vim.tbl_extend("keep", default_opts or {}, { noremap = true, silent = true }) + + -- TODO: Remove when dropping 0.6 + if vim.fn.has("nvim-0.7") ~= 1 then + -- NOTE: This is incompatible with a lua function on rhs and a bool buffer instead of number + return M.keymap_group(mode, prefix, default_opts) + end + + return function(lhs, rhs, opts) + opts = vim.tbl_extend("keep", opts or {}, default_opts) + local opt_mode = M.tbl_pop(opts, "mode") + vim.keymap.set(opt_mode or mode, prefix .. lhs, rhs, opts) + end +end + +-- Returns a function used to create keymaps with consistent prefixes +function M.keymap_group(mode, prefix, default_opts) + -- TODO: Remove when dropping 0.6 since curry_keymap exists + return function(lhs, rhs, opts) + opts = opts or default_opts + if opts ~= nil and default_opts ~= nil and opts ~= default_opts then + opts = vim.tbl_extend("keep", opts, default_opts) + end + local opt_mode = M.tbl_pop(opts, "mode") + M.keymap_set(opt_mode or mode, prefix .. lhs, rhs, opts) end end