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.
This commit is contained in:
ViViDboarder 2023-06-09 12:10:55 -07:00
parent 72ba9591e7
commit 0e446b86d1
9 changed files with 256 additions and 185 deletions

View File

@ -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", "<C-L><C-L>", ":set wrap!<CR>", opt_silent)
map("n", "<leader>lw", ":set wrap!<CR>", opt_silent)
map("n", "<C-N><C-N>", ":set invnumber<CR>", opt_silent)
map("n", "<leader>ln", ":set invnumber<CR>", opt_silent)
map("n", "<leader>/", ":set hlsearch! hlsearch?<CR>", opt_silent)
map("n", "<leader>cs", ":nohlsearch<CR>", opt_silent)
map("n", "<leader>ws", ":set list!<CR>", opt_silent)
utils.keymap_set("n", "<C-L><C-L>", ":set wrap!<CR>", { desc = "Toggle line wrapping" })
utils.keymap_set("n", "<leader>lw", ":set wrap!<CR>", { desc = "Toggle line wrapping" })
utils.keymap_set("n", "<C-N><C-N>", ":set invnumber<CR>", { desc = "Toggle line numbers" })
utils.keymap_set("n", "<leader>ln", ":set invnumber<CR>", { desc = "Toggle line numbers" })
utils.keymap_set("n", "<leader>/", ":set hlsearch! hlsearch?<CR>", { desc = "Toggle search highlighting" })
utils.keymap_set("n", "<leader>cs", ":nohlsearch<CR>", { desc = "Clear search highlighting" })
utils.keymap_set("n", "<leader>ws", ":set list!<CR>", { desc = "Toggle whitespace characters" })
-- Save and quit typos
map("c", "WQ<CR>", "wq<CR>", opt_silent)
map("c", "Wq<CR>", "wq<CR>", opt_silent)
map("c", "W<CR>", "w<CR>", opt_silent)
map("c", "Q<CR>", "q<CR>", opt_silent)
map("c", "Q!<CR>", "q!<CR>", opt_silent)
map("c", "Qa<CR>", "qa<CR>", opt_silent)
map("c", "Qa!<CR>", "qa!<CR>", opt_silent)
map("c", "QA<CR>", "qa<CR>", opt_silent)
map("c", "QA!<CR>", "qa!<CR>", 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<CR>", "wq<CR>", { desc = "Write and quit" })
utils.keymap_set("c", "Wq<CR>", "wq<CR>", { desc = "Write and quit" })
utils.keymap_set("c", "W<CR>", "w<CR>", { desc = "Write" })
utils.keymap_set("c", "Q<CR>", "q<CR>", { desc = "Quit" })
utils.keymap_set("c", "Q!<CR>", "q!<CR>", { desc = "Force quit" })
utils.keymap_set("c", "Qa<CR>", "qa<CR>", { desc = "Quit all" })
utils.keymap_set("c", "Qa!<CR>", "qa!<CR>", { desc = "Force quit all" })
utils.keymap_set("c", "QA<CR>", "qa<CR>", { desc = "Quit all" })
utils.keymap_set("c", "QA!<CR>", "qa!<CR>", { 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<CR>", { desc = "Next buffer" })
map("n", "gB", ":bprevious<CR>", { desc = "Previous buffer" })
utils.keymap_set("n", "gb", ":bnext<CR>", { desc = "Next buffer" })
utils.keymap_set("n", "gB", ":bprevious<CR>", { desc = "Previous buffer" })
-- Easy redo
map("n", "U", ":redo<CR>", opt_default)
utils.keymap_set("n", "U", ":redo<CR>", { desc = "Redo" })
-- Make escape easier
map("i", "jk", "<esc>", opt_default)
map("i", "``", "<esc>", opt_default)
map("v", "``", "<esc>", opt_default)
utils.keymap_set("i", "jk", "<esc>", { desc = "Escape insert" })
utils.keymap_set("i", "``", "<esc>", { desc = "Escape insert" })
utils.keymap_set("v", "``", "<esc>", { desc = "Escape visual" })
-- C-Space completion
function _G.complete_space()
@ -58,12 +53,21 @@ function _G.complete_space()
return utils.t("<C-x><C-o>")
end
end
map("i", "<C-Space>", "v:lua.complete_space()", { expr = true })
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
-- Easily toggle spelling
vim.cmd("command Spell setlocal spell! spelllang=en_us")
-- Pop spelling completion for word under cursor
map("n", "<leader>s", "viw<esc>a<c-x>s", {})
utils.keymap_set("n", "<leader>s", "viw<esc>a<c-x>s", { desc = "Show spelling suggestions" })
-- Build on F5
map("n", "<F5>", ":make<CR>", {})
utils.keymap_set("n", "<F5>", ":make<CR>", { desc = "Run make" })

View File

@ -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"

View File

@ -131,9 +131,8 @@ use({
use({
"tomtom/tcomment_vim",
config = function()
-- TODO: use which-key?
vim.api.nvim_set_keymap("n", "//", ":TComment<CR>", { silent = true, noremap = true })
vim.api.nvim_set_keymap("v", "//", ":TCommentBlock<CR>", { silent = true, noremap = true })
require("utils").keymap_set("n", "//", ":TComment<CR>", { desc = "Toggle comment" })
require("utils").keymap_set("v", "//", ":TCommentBlock<CR>", { 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", "<Leader>a", "<cmd>ArgWrap<CR>", {
silent = true,
noremap = true,
require("utils").keymap_set("n", "<Leader>a", "<cmd>ArgWrap<CR>", {
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", "<cmd>Git blame<CR>", opts)
vim.api.nvim_set_keymap("n", "gc", "<cmd>Git commit<CR>", opts)
vim.api.nvim_set_keymap("n", "gd", "<cmd>Git diff<CR>", opts)
vim.api.nvim_set_keymap("n", "gs", "<cmd>Git<CR>", opts)
vim.api.nvim_set_keymap("n", "gw", "<cmd>Git write<CR>", opts)
require("utils").keymap_set("n", "gb", "<cmd>Git blame<CR>", { desc = "Git blame" })
require("utils").keymap_set("n", "gc", "<cmd>Git commit<CR>", { desc = "Git commit" })
require("utils").keymap_set("n", "gd", "<cmd>Git diff<CR>", { desc = "Git diff" })
require("utils").keymap_set("n", "gs", "<cmd>Git<CR>", { desc = "Git status" })
require("utils").keymap_set("n", "gw", "<cmd>Git write<CR>", { 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", "<F6>", ":call ToggleQuickfixList()<CR>", { silent = true, noremap = true })
vim.api.nvim_set_keymap("n", "<F7>", ":call ToggleLocationList()<CR>", { silent = true, noremap = true })
require("utils").keymap_set("n", "<F6>", ":call ToggleQuickfixList()<CR>", { desc = "Toggle quickfix" })
require("utils").keymap_set("n", "<F7>", ":call ToggleLocationList()<CR>", { 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

View File

@ -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",
"<Plug>(cmp_complete)",
"<cmd>lua require('cmp').complete()<CR>",
{ silent = true, noremap = true }
{ desc = "Autocomplete" }
)
end

View File

@ -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", "<plug>(GrepperOperator)", opt_silent)
map("x", "gs", "<plug>(GrepperOperator)", opt_silent)
require("utils").keymap_set({ "n", "x" }, "gs", "<plug>(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 <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>")
-- 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 <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

View File

@ -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", "<leader>l", opts, bufnr)
lsp_keymap("h", "<cmd>lua vim.lsp.buf.hover()<CR>")
lsp_keymap("rn", "<cmd>lua vim.lsp.buf.rename()<CR>")
lsp_keymap("e", "<cmd>lua vim.diagnostic.open_float()<CR>")
lsp_keymap("D", "<cmd>lua vim.lsp.buf.declaration()<CR>")
lsp_keymap("d", "<cmd>lua vim.lsp.buf.definition()<CR>")
lsp_keymap("t", "<cmd>lua vim.lsp.buf.type_definition()<CR>")
lsp_keymap("i", "<cmd>lua vim.lsp.buf.implementation()<CR>")
lsp_keymap("s", "<cmd>lua vim.lsp.buf.signature_help()<CR>")
lsp_keymap("wa", "<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>", opts)
lsp_keymap("wr", "<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>", opts)
lsp_keymap("wl", "<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>", opts)
lsp_keymap("r", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
lsp_keymap("p", "<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>", opts)
lsp_keymap("n", "<cmd>lua vim.lsp.diagnostic.goto_next()<CR>", opts)
-- 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.lsp.diagnostic.goto_prev()<CR>", { desc = "Diagnostics: Go to previous" })
lsp_keymap("n", "<cmd>lua vim.lsp.diagnostic.goto_next()<CR>", { desc = "Diagnostics: Go to next" })
-- Set insert keymap for signature help
buf_set_keymap("i", "<C-k>", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
utils.keymap_set(
"i",
"<C-k>",
"<cmd>lua vim.lsp.buf.signature_help()<CR>",
{ buffer = bufnr, desc = "Show signature help" }
)
-- Older keymaps
--[[
buf_set_keymap("n", "gD", "<Cmd>lua vim.lsp.buf.declaration()<CR>", opts)
buf_set_keymap("n", "gd", "<Cmd>lua vim.lsp.buf.definition()<CR>", opts)
buf_set_keymap("n", "K", "<Cmd>lua vim.lsp.buf.hover()<CR>", opts)
@ -129,14 +135,17 @@ local function get_default_attach(override_capabilities)
buf_set_keymap("n", "[d", "<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>", opts)
buf_set_keymap("n", "]d", "<cmd>lua vim.lsp.diagnostic.goto_next()<CR>", opts)
buf_set_keymap("n", "<leader>q", "<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>", 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", "<leader>rn", function()
return ":IncRename " .. vim.fn.expand("<cword>")
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", "<leader>lf", "<cmd>lua vim.lsp.buf.format({async=true})<CR>", opts)
buf_set_keymap("v", "<leader>lf", "<cmd>lua vim.lsp.buf.format({async=true})<CR>", opts)
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" }
)
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", "<leader>lf", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
buf_set_keymap("n", "<leader>lfr", "<cmd>lua vim.lsp.buf.range_formatting()<CR>", opts)
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" }
)
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", "<leader>f", { buffer = bufnr })
if server_capabilities.documentSymbolProvider then
buf_set_keymap("n", "<leader>t", "<cmd>Telescope lsp_document_symbols<CR>", opts)
telescope_keymap("t", "<cmd>Telescope lsp_document_symbols<CR>", { desc = "Find buffer tags" })
end
if server_capabilities.workspaceSymbolProvider then
buf_set_keymap("n", "<leader>ft", "<cmd>Telescope lsp_dynamic_workspace_symbols<CR>", opts)
telescope_keymap("T", "<cmd>Telescope lsp_dynamic_workspace_symbols<CR>", { desc = "Find tags" })
end
-- Replace some LSP bindings with Telescope ones
if server_capabilities.definitionProvider then
lsp_keymap("d", "<cmd>Telescope lsp_definitions<CR>")
lsp_keymap("d", "<cmd>Telescope lsp_definitions<CR>", { desc = "Find definition" })
end
if server_capabilities.typeDefinitionProvider then
lsp_keymap("t", "<cmd>Telescope lsp_type_definition()<CR>")
lsp_keymap("t", "<cmd>Telescope lsp_type_definition<CR>", { desc = "Find type definition" })
end
lsp_keymap("i", "<cmd>Telescope lsp_implementations<CR>")
lsp_keymap("r", "<cmd>Telescope lsp_references<CR>")
lsp_keymap("A", "<cmd>Telescope lsp_code_actions<CR>")
buf_set_keymap("n", "<leader>ca", "<cmd>Telescope lsp_code_actions<CR>", opts)
buf_set_keymap("v", "<leader>lA", "<cmd>Telescope lsp_range_code_actions<CR>", opts)
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" })
end
-- Attach navic for statusline location

View File

@ -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

View File

@ -20,25 +20,53 @@ local function config_telescope()
layout_strategy = "flex",
},
})
local opts = { silent = true, noremap = true }
vim.api.nvim_set_keymap("n", "<C-t>", "<cmd>lua require('telescope.builtin').find_files()<CR>", opts)
vim.api.nvim_set_keymap("n", "<leader>ff", "<cmd>lua require('telescope.builtin').find_files()<CR>", opts)
vim.api.nvim_set_keymap("n", "<leader>fl", "<cmd>lua require('telescope.builtin').resume()<CR>", opts)
vim.api.nvim_set_keymap("n", "<leader>fh", "<cmd>lua require('telescope.builtin').help_tags()<CR>", opts)
vim.api.nvim_set_keymap("n", "<leader>b", "<cmd>lua require('telescope.builtin').buffers()<CR>", opts)
vim.api.nvim_set_keymap("n", "<leader>fb", "<cmd>lua require('telescope.builtin').buffers()<CR>", opts)
vim.api.nvim_set_keymap("n", "<leader>t", "<cmd>lua require('telescope.builtin').current_buffer_tags()<CR>", opts)
vim.api.nvim_set_keymap("n", "<leader>ft", "<cmd>lua require('telescope.builtin').tags()<CR>", opts)
vim.api.nvim_set_keymap("n", "<leader>fg", "<cmd>lua require('telescope.builtin').live_grep()<CR>", opts)
vim.api.nvim_set_keymap("n", "<leader>*", "<cmd>lua require('telescope.builtin').grep_string()<CR>", opts)
local utils = require("utils")
utils.try_require("which-key", function(wk)
wk.register({
["<C-t>"] = { "<cmd>lua require('telescope.builtin').find_files()<CR>", "Find files" },
})
wk.register({
b = { "<cmd>lua require('telescope.builtin').buffers()<CR>", "Find buffers" },
t = { "<cmd>lua require('telescope.builtin').current_buffer_tags()<CR>", "Find buffer tags" },
["*"] = { "<cmd>lua require('telescope.builtin').grep_string()<CR>", "Find buffers" },
f = {
b = { "<cmd>lua require('telescope.builtin').buffers()<CR>", "Find buffers" },
f = { "<cmd>lua require('telescope.builtin').find_files()<CR>", "Find file" },
g = { "<cmd>lua require('telescope.builtin').live_grep()<CR>", "Live grep" },
h = { "<cmd>lua require('telescope.builtin').help_tags()<CR>", "Find help tags" },
l = { "<cmd>lua require('telescope.builtin').resume()<CR>", "Resume finding" },
t = { "<cmd>lua require('telescope.builtin').current_buffer_tags()<CR>", "Find buffer tags" },
T = { "<cmd>lua require('telescope.builtin').tags()<CR>", "Find tags" },
},
}, {
prefix = "<leader>",
})
end, function()
local opts = { silent = true, noremap = true }
vim.api.nvim_set_keymap("n", "<C-t>", "<cmd>lua require('telescope.builtin').find_files()<CR>", opts)
vim.api.nvim_set_keymap("n", "<leader>ff", "<cmd>lua require('telescope.builtin').find_files()<CR>", opts)
vim.api.nvim_set_keymap("n", "<leader>fl", "<cmd>lua require('telescope.builtin').resume()<CR>", opts)
vim.api.nvim_set_keymap("n", "<leader>fh", "<cmd>lua require('telescope.builtin').help_tags()<CR>", opts)
vim.api.nvim_set_keymap("n", "<leader>b", "<cmd>lua require('telescope.builtin').buffers()<CR>", opts)
vim.api.nvim_set_keymap("n", "<leader>fb", "<cmd>lua require('telescope.builtin').buffers()<CR>", opts)
vim.api.nvim_set_keymap(
"n",
"<leader>t",
"<cmd>lua require('telescope.builtin').current_buffer_tags()<CR>",
opts
)
vim.api.nvim_set_keymap("n", "<leader>ft", "<cmd>lua require('telescope.builtin').tags()<CR>", opts)
vim.api.nvim_set_keymap("n", "<leader>fg", "<cmd>lua require('telescope.builtin').live_grep()<CR>", opts)
vim.api.nvim_set_keymap("n", "<leader>*", "<cmd>lua require('telescope.builtin').grep_string()<CR>", opts)
-- Better spelling replacement for word under cursor
vim.api.nvim_set_keymap(
"n",
"<leader>s",
"<cmd>lua require('telescope.builtin').spell_suggest(require('telescope.themes').get_cursor())<CR>",
opts
)
-- Better spelling replacement for word under cursor
vim.api.nvim_set_keymap(
"n",
"<leader>s",
"<cmd>lua require('telescope.builtin').spell_suggest(require('telescope.themes').get_cursor())<CR>",
opts
)
end)
load_extensions()
end

View File

@ -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