From 2710af916098c000b9dbdb69e6757265849644fe Mon Sep 17 00:00:00 2001 From: ViViDboarder Date: Thu, 30 May 2024 11:05:20 -0700 Subject: [PATCH] Lightly tested: Finish up refactor --- neovim/lua/_bindings.lua | 9 ++---- neovim/lua/plugins/copilotchat.lua | 2 +- neovim/lua/plugins/lsp.lua | 46 +++++++++++--------------- neovim/lua/plugins/lualine.lua | 24 ++------------ neovim/lua/plugins/telescope.lua | 52 +++++++++++------------------- neovim/lua/utils.lua | 20 +++++------- 6 files changed, 49 insertions(+), 104 deletions(-) diff --git a/neovim/lua/_bindings.lua b/neovim/lua/_bindings.lua index a8d1b34..36b5f64 100644 --- a/neovim/lua/_bindings.lua +++ b/neovim/lua/_bindings.lua @@ -40,20 +40,15 @@ utils.keymap_set("i", "``", "", { desc = "Escape insert" }) utils.keymap_set("v", "``", "", { desc = "Escape visual" }) -- C-Space completion -function _G.complete_space() +utils.keymap_set("i", "", function() if vim.fn.pumvisible() == 1 then return utils.t("") - elseif utils.is_plugin_loaded("completion-nvim") then - return utils.t("(completion_trigger)") - elseif utils.is_plugin_loaded("nvim-compe") then - return vim.fn["compe#complete"]() elseif utils.is_plugin_loaded("nvim-cmp") then return utils.t("(cmp_complete)") else return utils.t("") end -end -utils.keymap_set("i", "", "v:lua.complete_space()", { expr = true }) +end, { expr = true }) 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" }) diff --git a/neovim/lua/plugins/copilotchat.lua b/neovim/lua/plugins/copilotchat.lua index 118c917..6775e92 100644 --- a/neovim/lua/plugins/copilotchat.lua +++ b/neovim/lua/plugins/copilotchat.lua @@ -16,7 +16,7 @@ function M.setup() local utils = require("utils") if utils.try_require("telescope") ~= nil then - local cc_keymap = utils.curry_keymap("n", "cc") + local cc_keymap = utils.curry_keymap("n", "cc", { group_desc = "CopilotChat" }) cc_keymap("h", function() local actions = require("CopilotChat.actions") diff --git a/neovim/lua/plugins/lsp.lua b/neovim/lua/plugins/lsp.lua index 5073aa5..7457853 100644 --- a/neovim/lua/plugins/lsp.lua +++ b/neovim/lua/plugins/lsp.lua @@ -90,7 +90,7 @@ local function get_default_attach(override_capabilities) end -- Mappings - local lsp_keymap = utils.curry_keymap("n", "l", { buffer = bufnr }) + local lsp_keymap = utils.curry_keymap("n", "l", { buffer = bufnr, group_desc = "LSP" }) lsp_keymap("h", vim.lsp.buf.hover, { desc = "Display hover" }) lsp_keymap("rn", vim.lsp.buf.rename, { desc = "Refactor rename" }) lsp_keymap("e", vim.diagnostic.open_float, { desc = "Open float dialog" }) @@ -123,28 +123,18 @@ local function get_default_attach(override_capabilities) utils.keymap_set("n", "[d", vim.diagnostic.goto_prev, { buffer = bufnr, desc = "Previous diagnostic" }) utils.keymap_set("n", "]d", vim.diagnostic.goto_next, { buffer = bufnr, desc = "Next diagnostic" }) - -- Older keymaps - --[[ - buf_set_keymap("n", "gi", "lua vim.lsp.buf.implementation()", opts) - buf_set_keymap("n", "wa", "lua vim.lsp.buf.add_workspace_folder()", opts) - buf_set_keymap("n", "wr", "lua vim.lsp.buf.remove_workspace_folder()", opts) - buf_set_keymap("n", "wl", "lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))", opts) - buf_set_keymap("n", "D", "lua vim.lsp.buf.type_definition()", opts) - buf_set_keymap("n", "rn", "lua vim.lsp.buf.rename()", opts) - buf_set_keymap("n", "gr", "lua vim.lsp.buf.references()", opts) - buf_set_keymap("n", "e", "lua vim.diagnostic.open_float()", 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 + vim.api.nvim_create_autocmd({ "CursorHold" }, { + pattern = "", + callback = function() + vim.diagnostic.open_float(nil, { focus = false, scope = "cursor" }) + end, + group = vim.api.nvim_create_augroup("diagnostic_float", { clear = true }), + desc = "Open float dialog on hold", + }) -- 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" }) @@ -169,7 +159,7 @@ local function get_default_attach(override_capabilities) }) end else - -- HACK: Support for <0.8 + -- HACK: Support for <0.8 with older formatting lsp_keymap("f", vim.lsp.buf.formatting, { desc = "Format code" }) lsp_keymap("f", vim.lsp.buf.range_formatting, { mode = "v", desc = "Format selected code" }) if server_capabilities.documentFormattingProvider then @@ -201,25 +191,25 @@ local function get_default_attach(override_capabilities) end -- Some override some fuzzy finder bindings to use lsp sources - utils.try_require("telescope.builtin", function(builtin) + utils.try_require("telescope.builtin", function(telescope_builtin) -- Replace some Telescope bindings with LSP versions - local telescope_keymap = utils.curry_keymap("n", "f", { buffer = bufnr }) + local telescope_keymap = utils.curry_keymap("n", "f", { buffer = bufnr, group_desc = "Finder" }) if server_capabilities.documentSymbolProvider then - telescope_keymap("t", builtin.lsp_document_symbols, { desc = "Find buffer tags" }) + telescope_keymap("t", telescope_builtin.lsp_document_symbols, { desc = "Find buffer tags" }) end if server_capabilities.workspaceSymbolProvider then - telescope_keymap("T", builtin.lsp_dynamic_workspace_symbols, { desc = "Find tags" }) + telescope_keymap("T", telescope_builtin.lsp_dynamic_workspace_symbols, { desc = "Find tags" }) end -- Replace some LSP bindings with Telescope ones if server_capabilities.definitionProvider then - lsp_keymap("d", builtin.lsp_definitions, { desc = "Find definition" }) + lsp_keymap("d", telescope_builtin.lsp_definitions, { desc = "Find definition" }) end if server_capabilities.typeDefinitionProvider then - lsp_keymap("t", builtin.lsp_type_definition, { desc = "Find type definition" }) + lsp_keymap("t", telescope_builtin.lsp_type_definitions, { desc = "Find type definition" }) end - lsp_keymap("i", builtin.lsp_implementations, { desc = "Find implementations" }) - lsp_keymap("r", builtin.lsp_references, { desc = "Find references" }) + lsp_keymap("i", telescope_builtin.lsp_implementations, { desc = "Find implementations" }) + lsp_keymap("r", telescope_builtin.lsp_references, { desc = "Find references" }) end) -- Attach navic for statusline location diff --git a/neovim/lua/plugins/lualine.lua b/neovim/lua/plugins/lualine.lua index 38e96d9..8a18501 100644 --- a/neovim/lua/plugins/lualine.lua +++ b/neovim/lua/plugins/lualine.lua @@ -56,22 +56,8 @@ function M.config_lualine(theme_name) theme_name = "wombat" end - -- gps / navic + -- navic local code_loc = {} - - utils.try_require("nvim-gps", function(gps) - gps.setup({ - icons = { - ["class-name"] = "(c) ", - ["function-name"] = "(ƒ) ", - ["method-name"] = "(m) ", - ["container-name"] = "", - ["tag-name"] = "(t) ", - }, - }) - code_loc = { gps.get_location, cond = gps.is_available } - end) - utils.try_require("nvim-navic", function(navic) navic.setup({ icons = { @@ -89,12 +75,6 @@ function M.config_lualine(theme_name) code_loc = { "navic" } end) - local diagnostic_plugin = "nvim_diagnostic" - -- HACK: Support for <0.6 - if vim.fn.has("nvim-0.6.0") ~= 1 then - diagnostic_plugin = "nvim_lsp" - end - require("lualine").setup({ options = { theme = theme_name, @@ -116,7 +96,7 @@ function M.config_lualine(theme_name) lualine_x = { M.custom_ffenc, "filetype" }, lualine_y = { "progress", "location" }, lualine_z = { - { "diagnostics", sources = { diagnostic_plugin } }, + { "diagnostics", sources = { "nvim_diagnostic" } }, { M.mixed_indent, color = { bg = "#de4f1f" } }, { M.trailing_whitespace, color = { bg = "#de4f1f" } }, }, diff --git a/neovim/lua/plugins/telescope.lua b/neovim/lua/plugins/telescope.lua index edd2ab6..d1be74f 100644 --- a/neovim/lua/plugins/telescope.lua +++ b/neovim/lua/plugins/telescope.lua @@ -22,43 +22,27 @@ local function config_telescope() }) local utils = require("utils") - utils.keymap_set("n", "", "lua require('telescope.builtin').find_files()", { desc = "Find files" }) - utils.keymap_set("n", "b", "lua require('telescope.builtin').buffers()", { desc = "Find buffers" }) - utils.keymap_set( - "n", - "t", - "lua require('telescope.builtin').current_buffer_tags()", - { desc = "Find buffer tags" } - ) - utils.keymap_set( - "n", - "*", - "lua require('telescope.builtin').grep_string()", - { desc = "Find string under cursor" } - ) - -- Better spelling replacement for word under cursor - utils.keymap_set( - "n", - "s", - "lua require('telescope.builtin').spell_suggest(require('telescope.themes').get_cursor())", - { desc = "Spell check" } - ) + local telescope_builtin = require("telescope.builtin") - local finder_keymap = utils.curry_keymap("n", "f") - finder_keymap("b", "lua require('telescope.builtin').buffers()", { desc = "Find buffers" }) - finder_keymap("f", "lua require('telescope.builtin').find_files()", { desc = "Find file" }) - finder_keymap("g", "lua require('telescope.builtin').live_grep()", { desc = "Live grep" }) - finder_keymap("h", "lua require('telescope.builtin').help_tags()", { desc = "Find help tags" }) - finder_keymap("l", "lua require('telescope.builtin').resume()", { desc = "Resume finding" }) - finder_keymap("t", "lua require('telescope.builtin').current_buffer_tags()", { desc = "Find buffer tags" }) - finder_keymap("T", "lua require('telescope.builtin').tags()", { desc = "Find tags" }) + utils.keymap_set("n", "", telescope_builtin.find_files, { desc = "Find files" }) + utils.keymap_set("n", "b", telescope_builtin.buffers, { desc = "Find buffers" }) + utils.keymap_set("n", "t", telescope_builtin.current_buffer_tags, { desc = "Find buffer tags" }) + utils.keymap_set("n", "*", telescope_builtin.grep_string, { desc = "Find string under cursor" }) + utils.keymap_set("n", "s", function() + require("telescope.builtin").spell_suggest(require("telescope.themes").get_cursor()) + end, { desc = "Spell check" }) + + local finder_keymap = utils.curry_keymap("n", "f", { group_desc = "Finder" }) + finder_keymap("b", telescope_builtin.buffers, { desc = "Find buffers" }) + finder_keymap("f", telescope_builtin.find_files, { desc = "Find file" }) + finder_keymap("g", telescope_builtin.live_grep, { desc = "Live grep" }) + finder_keymap("h", telescope_builtin.help_tags, { desc = "Find help tags" }) + finder_keymap("l", telescope_builtin.resume, { desc = "Resume finding" }) + finder_keymap("t", telescope_builtin.current_buffer_tags, { desc = "Find buffer tags" }) + finder_keymap("T", telescope_builtin.tags, { desc = "Find tags" }) if utils.can_require("sg.telescope") then - finder_keymap( - "G", - "lua require('sg.telescope').fuzzy_search_results()", - { desc = "Search Sourcegraph" } - ) + finder_keymap("G", require("sg.telescope").fuzzy_search_results, { desc = "Search Sourcegraph" }) end load_extensions() diff --git a/neovim/lua/utils.lua b/neovim/lua/utils.lua index 10a7b45..5769462 100644 --- a/neovim/lua/utils.lua +++ b/neovim/lua/utils.lua @@ -184,6 +184,14 @@ end -- 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 }) + local group_desc = M.tbl_pop(default_opts, "group_desc") + if group_desc ~= nil then + M.try_require("which-key", function(wk) + wk.register({ + [prefix] = "+" .. group_desc, + }, default_opts) + end) + end return function(lhs, rhs, opts) opts = vim.tbl_extend("keep", opts or {}, default_opts) @@ -192,18 +200,6 @@ function M.curry_keymap(mode, prefix, default_opts) end end --- Returns a function used to create keymaps with consistent prefixes -function M.keymap_group(mode, prefix, default_opts) - 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 - -- Diagnostics signs M.diagnostic_signs = { Error = "🔥",