No look: Refactor to drop 0.6 support

This commit is contained in:
ViViDboarder 2024-05-29 13:16:28 -07:00
parent 4a042358f6
commit 02c5301379
9 changed files with 107 additions and 234 deletions

View File

@ -55,16 +55,9 @@ function _G.complete_space()
end
utils.keymap_set("i", "<C-Space>", "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
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" })
-- Pop spelling completion for word under cursor
utils.keymap_set("n", "<leader>s", "viw<esc>a<c-x>s", { desc = "Show spelling suggestions" })

View File

@ -61,16 +61,11 @@ end
-- Don't need the autocommand when dark-notify is installed
local utils = require("utils")
if not utils.is_plugin_loaded("dark-notify") then
-- 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

View File

@ -37,16 +37,11 @@ o.mouse = "a"
-- Autocomplete options
o.completeopt = "menuone,noinsert,noselect,preview"
-- TODO: remove check when dropping v0.6.0
if vim.fn.has("nvim-0.7.0") == 1 then
vim.api.nvim_create_autocmd({ "CompleteDone" }, {
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)

View File

@ -1,5 +1,5 @@
if vim.fn.has("nvim-0.6.0") ~= 1 then
print("ERROR: Requires nvim >= 0.6.0")
if vim.fn.has("nvim-0.7.0") ~= 1 then
print("ERROR: Requires nvim >= 0.7.0")
end
local o = vim.o
@ -26,4 +26,6 @@ vim.g.polyglot_disabled = { "go", "rust" }
-- Plugins
-- Packer auto installs and then lazy loads itself on PackerCommand and require the plugins module
-- This command should only really be needed to bootstrap a new system
vim.cmd([[command! PackerBootstrap lua require("plugins")]])
vim.api.nvim_create_user_command("PackerBootstrap", function()
require("plugins")
end, { desc = "Bootstrap Packer" })

View File

@ -190,16 +190,9 @@ use({
})
-- Custom status line
use({
"https://github.com/SmiteshP/nvim-gps",
requires = "https://github.com/nvim-treesitter/nvim-treesitter",
disable = vim.fn.has("nvim-0.7.0") == 1,
})
-- Replaces gps for 0.7+
use({
"https://github.com/SmiteshP/nvim-navic",
requires = "https://github.com/neovim/nvim-lspconfig",
disable = vim.fn.has("nvim-0.7.0") ~= 1,
})
use({
@ -207,7 +200,7 @@ use({
config = function()
require("plugins.lualine").config_lualine()
end,
after = vim.fn.has("nvim-0.7.0") == 1 and "nvim-navic" or "nvim-gps",
after = "nvim-navic",
})
-- On Mac, update colors when dark mode changes
@ -305,8 +298,6 @@ use({
"https://github.com/neovim/nvim-lspconfig",
"https://github.com/williamboman/mason-lspconfig.nvim",
},
-- Only supports >=0.7.0
disable = vim.fn.has("nvim-0.7.0") ~= 1,
})
-- Lua dev for vim
@ -318,7 +309,6 @@ use({
-- Rust analyzer
use({
"https://github.com/simrat39/rust-tools.nvim",
disable = vim.fn.has("nvim-0.7.0") ~= 1,
})
-- Better display of lsp diagnostics
@ -388,16 +378,6 @@ use({
require("utils").require_with_local("plugins.treesitter").setup()
end,
})
--[[ TODO: Enable this as an alterantive or fallback for LSPs
use({
"https://github.com/nvim-treesitter/nvim-treesitter-refactor",
requires = "https://github.com/nvim-treesitter/nvim-treesitter",
commit = utils.map_version_rule({
[">=0.7.0"] = utils.nil_val,
["<0.7.0"] = "75f5895",
}),
})
--]]
use({
"https://github.com/nvim-treesitter/nvim-treesitter-textobjects",
requires = "https://github.com/nvim-treesitter/nvim-treesitter",
@ -497,13 +477,17 @@ use("https://github.com/hsanson/vim-android")
use({
"https://github.com/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
au BufRead,BufNewFile go.mod,go.sum set filetype=gomod
augroup end
]])
local gid = vim.api.nvim_create_augroup("polyglot_fts", { clear = true })
vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
pattern = { "*/playbooks/*.yml", "*/playbooks/*.yaml" },
command = "set filetype=yaml.ansible",
group = gid,
})
vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
pattern = { "go.mod", "go.sum" },
command = "set filetype=gomod",
group = gid,
})
end,
})

View File

@ -48,7 +48,9 @@ function M.config_cmp()
-- Add a plug mapping to use in C-Space binding
local utils = require("utils")
utils.keymap_set("i", "<Plug>(cmp_complete)", "<cmd>lua require('cmp').complete()<CR>", { desc = "Autocomplete" })
utils.keymap_set("i", "<Plug>(cmp_complete)", function()
require("cmp").complete()
end, { desc = "Autocomplete" })
-- Maybe add obsidian and obsidian new. This is done here in case obsidian.nvim is loaded before cmp
utils.try_require("cmp_obsidian", function(cmp_obsidian)

View File

@ -15,33 +15,15 @@ require("utils").keymap_set({ "n", "x" }, "gs", "<plug>(GrepperOperator)", {
})
-- Override Todo command to use Grepper
-- 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
vim.api.nvim_create_user_command("Todo", ":Grepper -noprompt -query TODO", { desc = "Search for TODO tags" })
-- Make some shortands for various grep programs
-- TODO: Remove check when dropping 0.6
if vim.fn.has("nvim-0.7.0") == 1 then
if vim.fn.executable("rg") == 1 then
if vim.fn.executable("rg") == 1 then
vim.api.nvim_create_user_command("Rg", ":GrepperRg <args>", { nargs = "+", desc = "Ripgrep" })
end
if vim.fn.executable("ag") == 1 then
vim.api.nvim_create_user_command("Ag", ":GrepperAg <args>", { nargs = "+", desc = "Silversearcher" })
end
if vim.fn.executable("ack") == 1 then
vim.api.nvim_create_user_command("Ack", ":GrepperAck <args>", { nargs = "+", desc = "Ack search" })
end
else
if vim.fn.executable("rg") == 1 then
vim.cmd("command -nargs=+ Rg :GrepperRg <args>")
end
if vim.fn.executable("ag") == 1 then
vim.cmd("command -nargs=+ Ag :GrepperAg <args>")
end
if vim.fn.executable("ack") == 1 then
vim.cmd("command -nargs=+ Ack :GrepperAck <args>")
end
end
if vim.fn.executable("ag") == 1 then
vim.api.nvim_create_user_command("Ag", ":GrepperAg <args>", { nargs = "+", desc = "Silversearcher" })
end
if vim.fn.executable("ack") == 1 then
vim.api.nvim_create_user_command("Ack", ":GrepperAck <args>", { nargs = "+", desc = "Ack search" })
end

View File

@ -90,67 +90,38 @@ local function get_default_attach(override_capabilities)
end
-- Mappings
-- TODO: use functions instead of strings when dropping 0.6
local lsp_keymap = utils.curry_keymap("n", "<leader>l", { buffer = bufnr })
lsp_keymap("h", "<cmd>lua vim.lsp.buf.hover()<CR>", { desc = "Display hover" })
lsp_keymap("rn", "<cmd>lua vim.lsp.buf.rename()<CR>", { desc = "Refactor rename" })
lsp_keymap("e", "<cmd>lua vim.diagnostic.open_float()<CR>", { desc = "Open float dialog" })
lsp_keymap("D", "<cmd>lua vim.lsp.buf.declaration()<CR>", { desc = "Go to declaration" })
lsp_keymap("d", "<cmd>lua vim.lsp.buf.definition()<CR>", { desc = "Go to definition" })
lsp_keymap("t", "<cmd>lua vim.lsp.buf.type_definition()<CR>", { desc = "Go to type definition" })
lsp_keymap("i", "<cmd>lua vim.lsp.buf.implementation()<CR>", { desc = "Show implementations" })
lsp_keymap("s", "<cmd>lua vim.lsp.buf.signature_help()<CR>", { desc = "Show signature help" })
lsp_keymap("wa", "<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>", { desc = "Workspace: Add folder" })
lsp_keymap("wr", "<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>", { desc = "Workspace: Remove folder" })
lsp_keymap(
"wl",
"<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>",
{ desc = "Workspace: List folders" }
)
lsp_keymap("r", "<cmd>lua vim.lsp.buf.references()<CR>", { desc = "References" })
lsp_keymap("p", "<cmd>lua vim.diagnostic.goto_prev()<CR>", { desc = "Previous diagnostic" })
lsp_keymap("n", "<cmd>lua vim.diagnostic.goto_next()<CR>", { desc = "Next diagnostic" })
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" })
lsp_keymap("D", vim.lsp.buf.declaration, { desc = "Go to declaration" })
lsp_keymap("d", vim.lsp.buf.definition, { desc = "Go to definition" })
lsp_keymap("t", vim.lsp.buf.type_definition, { desc = "Go to type definition" })
lsp_keymap("i", vim.lsp.buf.implementation, { desc = "Show implementations" })
lsp_keymap("s", vim.lsp.buf.signature_help, { desc = "Show signature help" })
lsp_keymap("wa", vim.lsp.buf.add_workspace_folder, { desc = "Workspace: Add folder" })
lsp_keymap("wr", vim.lsp.buf.remove_workspace_folder, { desc = "Workspace: Remove folder" })
lsp_keymap("wl", function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, { desc = "Workspace: List folders" })
lsp_keymap("r", vim.lsp.buf.references, { desc = "References" })
lsp_keymap("p", vim.diagnostic.goto_prev, { desc = "Previous diagnostic" })
lsp_keymap("n", vim.diagnostic.goto_next, { desc = "Next diagnostic" })
if server_capabilities.codeActionProvider then
lsp_keymap("A", vim.lsp.buf.code_action, { desc = "Select code actions" })
lsp_keymap("A", vim.lsp.buf.code_action, { mode = "v", desc = "Select code actions" })
end
-- Set insert keymap for signature help
utils.keymap_set(
"i",
"<C-k>",
"<cmd>lua vim.lsp.buf.signature_help()<CR>",
{ buffer = bufnr, desc = "Show signature help" }
)
utils.keymap_set("i", "<C-k>", vim.lsp.buf.signature_help, { buffer = bufnr, desc = "Show signature help" })
-- Some top level aliases or remaps
utils.keymap_set("n", "K", "<cmd>lua vim.lsp.buf.hover()<CR>", { buffer = bufnr, desc = "Display hover" })
utils.keymap_set(
"n",
"gD",
"<Cmd>lua vim.lsp.buf.declaration()<CR>",
{ buffer = bufnr, desc = "Go to declaration" }
)
utils.keymap_set(
"n",
"gd",
"<Cmd>lua vim.lsp.buf.definition()<CR>",
{ buffer = bufnr, desc = "Go to definition" }
)
utils.keymap_set(
"n",
"<leader>rn",
"<cmd>lua vim.lsp.buf.rename()<CR>",
{ buffer = bufnr, desc = "Refactor rename" }
)
utils.keymap_set(
"n",
"[d",
"<cmd>lua vim.diagnostic.goto_prev()<CR>",
{ buffer = bufnr, desc = "Previous diagnostic" }
)
utils.keymap_set(
"n",
"]d",
"<cmd>lua vim.diagnostic.goto_next()<CR>",
{ buffer = bufnr, desc = "Next diagnostic" }
)
utils.keymap_set("n", "K", vim.lsp.buf.hover, { buffer = bufnr, desc = "Display hover" })
utils.keymap_set("n", "gD", vim.lsp.buf.declaration, { buffer = bufnr, desc = "Go to declaration" })
utils.keymap_set("n", "gd", vim.lsp.buf.definition, { buffer = bufnr, desc = "Go to definition" })
utils.keymap_set("n", "<leader>rn", vim.lsp.buf.rename, { buffer = bufnr, desc = "Refactor rename" })
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
--[[
@ -181,12 +152,12 @@ local function get_default_attach(override_capabilities)
-- Set some keybinds conditional on server capabilities
if vim.fn.has("nvim-0.8") == 1 then
lsp_keymap("f", "<cmd>lua vim.lsp.buf.format({async=true})<CR>", { desc = "Format code" })
lsp_keymap(
"f",
"<cmd>lua vim.lsp.buf.format({async=true})<CR>",
{ mode = "v", desc = "Format selected code" }
)
lsp_keymap("f", function()
vim.lsp.buf.format({ async = true })
end, { desc = "Format code" })
lsp_keymap("f", function()
vim.lsp.buf.format({ async = true })
end, { mode = "v", desc = "Format selected code" })
if server_capabilities.documentFormattingProvider then
vim.api.nvim_create_autocmd({ "BufWritePre" }, {
pattern = { "*.rs", "*.go", "*.sh", "*.lua" },
@ -199,59 +170,57 @@ local function get_default_attach(override_capabilities)
end
else
-- HACK: Support for <0.8
lsp_keymap("f", "<cmd>lua vim.lsp.buf.formatting()<CR>", { desc = "Format code" })
lsp_keymap(
"f",
"<cmd>lua vim.lsp.buf.range_formatting()<CR>",
{ mode = "v", desc = "Format selected code" }
)
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
vim.cmd([[
augroup lsp_format
autocmd!
autocmd BufWritePre lua vim.lsp.buf.formatting_sync(nil, 1000)
augroup END
]])
vim.api.nvim_create_autocmd({ "BufWritePre" }, {
pattern = { "*.rs", "*.go", "*.sh", "*.lua" },
callback = function()
vim.lsp.buf.formatting_sync(nil, 1000)
end,
group = vim.api.nvim_create_augroup("lsp_format", { clear = true }),
desc = "Auto format code on save",
})
end
end
-- Set autocommands conditional on server_capabilities
if server_capabilities.documentHighlightProvider then
vim.cmd([[
:highlight link LspReferenceRead MatchParen
:highlight link LspReferenceText MatchParen
:highlight link LspReferenceWrite MatchParen
augroup lsp_document_highlight
autocmd!
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
augroup END
]])
vim.api.nvim_set_hl(0, "LspReferenceRead", { link = "MatchParen" })
vim.api.nvim_set_hl(0, "LspReferenceText", { link = "MatchParen" })
vim.api.nvim_set_hl(0, "LspReferenceWrite", { link = "MatchParen" })
local hl_group = vim.api.nvim_create_augroup("lsp_document_highlight", { clear = true })
vim.api.nvim_create_autocmd(
{ "CursorHold" },
{ pattern = "<buffer>", callback = vim.lsp.buf.document_highlight, group = hl_group }
)
vim.api.nvim_create_autocmd(
{ "CursorMoved" },
{ pattern = "<buffer>", callback = vim.lsp.buf.clear_references, group = hl_group }
)
end
-- Some override some fuzzy finder bindings to use lsp sources
if utils.try_require("telescope") ~= nil then
utils.try_require("telescope.builtin", function(builtin)
-- Replace some Telescope bindings with LSP versions
local telescope_keymap = utils.curry_keymap("n", "<leader>f", { buffer = bufnr })
if server_capabilities.documentSymbolProvider then
telescope_keymap("t", "<cmd>Telescope lsp_document_symbols<CR>", { desc = "Find buffer tags" })
telescope_keymap("t", builtin.lsp_document_symbols, { desc = "Find buffer tags" })
end
if server_capabilities.workspaceSymbolProvider then
telescope_keymap("T", "<cmd>Telescope lsp_dynamic_workspace_symbols<CR>", { desc = "Find tags" })
telescope_keymap("T", builtin.lsp_dynamic_workspace_symbols, { desc = "Find tags" })
end
-- Replace some LSP bindings with Telescope ones
if server_capabilities.definitionProvider then
lsp_keymap("d", "<cmd>Telescope lsp_definitions<CR>", { desc = "Find definition" })
lsp_keymap("d", builtin.lsp_definitions, { desc = "Find definition" })
end
if server_capabilities.typeDefinitionProvider then
lsp_keymap("t", "<cmd>Telescope lsp_type_definition<CR>", { desc = "Find type definition" })
end
lsp_keymap("i", "<cmd>Telescope lsp_implementations<CR>", { desc = "Find implementations" })
lsp_keymap("r", "<cmd>Telescope lsp_references<CR>", { desc = "Find references" })
lsp_keymap("A", "<cmd>Telescope lsp_code_actions<CR>", { desc = "Select code actions" })
lsp_keymap("A", "<cmd>Telescope lsp_range_code_actions<CR>", { mode = "v", desc = "Select code actions" })
lsp_keymap("t", builtin.lsp_type_definition, { desc = "Find type definition" })
end
lsp_keymap("i", builtin.lsp_implementations, { desc = "Find implementations" })
lsp_keymap("r", builtin.lsp_references, { desc = "Find references" })
end)
-- Attach navic for statusline location
if server_capabilities.documentSymbolProvider then
@ -342,13 +311,7 @@ function M.config_lsp()
-- Auto setup mason installed servers
utils.try_require("mason-lspconfig", function(mason_lspconfig)
-- Get list of servers that are installed but not set up
local already_setup
if lsp_config["util"] and lsp_config.util["available_servers"] then
already_setup = lsp_config.util.available_servers()
else
-- HACK: For lspconfig versions lower than 0.1.4, which is required for nvim <0.7.0
already_setup = lsp_config.available_servers()
end
local already_setup = lsp_config.util.available_servers()
local needs_setup = vim.tbl_filter(function(server)
return not vim.tbl_contains(already_setup, server)
end, mason_lspconfig.get_installed_servers())

View File

@ -5,23 +5,6 @@ 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
cmds = { cmds }
end
vim.cmd("augroup " .. group)
if clear then
vim.cmd([[au!]])
end
for _, cmd in ipairs(cmds) do
vim.cmd("autocmd " .. cmd)
end
vim.cmd([[augroup END]])
end
-- Terminal escape a given string
function M.t(str)
return vim.api.nvim_replace_termcodes(str, true, true, true)
@ -195,38 +178,13 @@ 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
-- 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")
@ -236,7 +194,6 @@ 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