1
0
Fork 0
neovim-config/init.lua

467 lines
11 KiB
Lua
Raw Normal View History

2021-06-13 16:20:42 -04:00
---- Aliases ----
local cmd = vim.cmd
local g = vim.g
local opt = vim.opt
---- General Config ----
2023-04-14 19:38:59 -04:00
opt.mouse = "a"
2023-04-10 19:45:51 -04:00
opt.tabstop = 2
opt.shiftwidth = 2
2021-06-13 16:20:42 -04:00
opt.number = true
opt.breakindent = true
opt.showmatch = true
opt.ignorecase = true
opt.smartcase = true
opt.joinspaces = false
opt.showmode = false
opt.hidden = true
opt.backup = false
opt.writebackup = false
opt.swapfile = false
opt.shortmess:append({ I = true })
-- whichwrap -- left/right keys can traverse up/down
2023-04-17 19:38:42 -04:00
opt.whichwrap:append("<,>,[,]")
2021-06-13 16:20:42 -04:00
opt.cmdheight = 2
opt.showcmd = true
2023-04-14 19:38:59 -04:00
g.mapleader = " "
2021-06-13 16:20:42 -04:00
---- Terminal ----
2023-03-27 16:09:35 -03:00
vim.api.nvim_create_autocmd({ "TermOpen" }, {
callback = function()
if vim.l then
vim.l.number = false
end
2023-03-27 16:09:35 -03:00
end,
})
2021-06-13 16:20:42 -04:00
---- Theming ----
opt.termguicolors = true
2023-04-14 19:38:59 -04:00
opt.background = "dark"
2021-06-13 16:20:42 -04:00
g.gruvbox_italic = true
2023-04-14 19:38:59 -04:00
cmd.colorscheme("gruvbox")
2021-06-13 16:20:42 -04:00
opt.listchars = {
2023-04-14 19:38:59 -04:00
eol = "",
tab = "▶▹",
nbsp = "",
extends = "",
trail = "",
}
2021-06-13 16:20:42 -04:00
-- vim-airline config ---
g.airline_powerline_fonts = true
2023-04-14 19:38:59 -04:00
g.airline_theme = "powerlineish"
2021-06-13 16:20:42 -04:00
---- Maps ----
2023-04-14 19:38:59 -04:00
vim.keymap.set("!", "<C-BS>", "<C-w>")
vim.keymap.set("v", ">", ">gv")
vim.keymap.set("v", "<", "<gv")
vim.keymap.set("n", "<C-L>", ":nohlsearch<CR>")
2021-06-13 16:20:42 -04:00
---- Treesitter ----
2023-04-14 19:38:59 -04:00
local ts = require("nvim-treesitter.configs")
ts.setup({
2022-06-08 20:16:56 -04:00
ensure_installed = {
2023-04-14 19:38:59 -04:00
"bash",
"c",
"cmake",
"comment",
"cpp",
"css",
"dockerfile",
"diff",
"dot",
"git_config",
"git_rebase",
"gitattributes",
"gitcommit",
"gitignore",
"go",
"html",
"http",
"ini",
"javascript",
"json",
"kdl",
"kotlin",
"latex",
"lua",
"make",
"markdown",
"markdown_inline",
"passwd",
"perl",
"php",
"python",
"regex",
"rust",
"scss",
"sql",
"terraform",
"toml",
"typescript",
"vim",
"yaml",
2022-06-08 20:16:56 -04:00
},
2023-04-14 19:38:59 -04:00
highlight = { enable = true, indent = true },
})
2023-03-27 01:12:15 -03:00
2023-03-27 01:03:04 -03:00
---- Filetypes ---
2023-06-25 21:54:28 -04:00
-- KDL --
2023-04-14 19:38:59 -04:00
vim.filetype.add({ extension = { kdl = "kdl" } })
2023-06-25 21:54:28 -04:00
-- YAML --
vim.api.nvim_create_autocmd({ "FileType" }, {
pattern = "yaml",
callback = function()
vim.opt_local.expandtab = true
vim.opt_local.foldmethod = "indent"
end,
})
2023-04-14 22:08:41 -04:00
---- formatter.nvim ----
local formatter = require("formatter")
formatter.setup({
filetype = {
lua = {
require("formatter.filetypes.lua").stylua,
},
},
})
2021-06-13 16:20:42 -04:00
---- LSP ----
2023-04-14 19:38:59 -04:00
local lsp = require("lspconfig")
2021-12-13 22:04:05 -03:00
-- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer
2022-10-07 13:13:21 -03:00
local on_lsp_attach = function(client, bufnr)
2023-04-14 19:59:46 -04:00
vim.keymap.set("n", "gD", vim.lsp.buf.declaration)
vim.keymap.set("n", "gd", vim.lsp.buf.definition)
vim.keymap.set("n", "K", vim.lsp.buf.hover)
vim.keymap.set("n", "gi", vim.lsp.buf.implementation)
vim.keymap.set("n", "<C-k>", vim.lsp.buf.signature_help)
vim.keymap.set("n", "<Leader>D", vim.lsp.buf.type_definition)
vim.keymap.set("n", "<Leader>rn", vim.lsp.buf.rename)
vim.keymap.set("n", "<Leader>ca", vim.lsp.buf.code_action)
vim.keymap.set("n", "gr", vim.lsp.buf.references)
vim.keymap.set("n", "<Leader>e", vim.diagnostic.open_float)
vim.keymap.set("n", "[d", vim.diagnostic.goto_prev)
vim.keymap.set("n", "]d", vim.diagnostic.goto_next)
vim.keymap.set("n", "<Leader>q", vim.diagnostic.setloclist)
vim.keymap.set("n", "<F3>", function()
2023-04-14 22:08:41 -04:00
vim.lsp.buf.format({ async = true })
2023-04-14 19:59:46 -04:00
end)
2021-12-13 22:04:05 -03:00
end
-- Use a loop to conveniently call 'setup' on multiple servers and
-- map buffer local keybindings when the language server attaches
2022-11-16 01:11:41 -03:00
local servers = {
2023-03-31 20:16:08 -03:00
clangd = {},
2023-04-12 20:35:21 -04:00
eslint = {},
2023-03-31 20:16:08 -03:00
gopls = {},
jsonls = {},
kotlin_language_server = {},
2023-04-18 01:46:22 -04:00
lua_ls = {
settings = {
Lua = {
runtime = { version = "LuaJIT" },
diagnostics = { globals = { "vim" } },
workspace = {
library = vim.api.nvim_get_runtime_file("", true),
},
telemetry = { enable = false },
},
},
},
2023-03-31 20:16:08 -03:00
marksman = {},
pylsp = {},
rust_analyzer = {},
texlab = {},
tflint = {},
tsserver = {},
2022-11-16 01:11:41 -03:00
}
2022-10-07 13:13:21 -03:00
-- Setup loop moved to the cmp section, since calling setup multiple times
-- overrides the previous calls.
2021-06-13 16:20:42 -04:00
2022-06-07 12:55:56 -04:00
-- Auto format on save
2023-03-27 16:09:35 -03:00
vim.api.nvim_create_autocmd({ "BufWritePre" }, {
callback = function()
2023-04-18 01:46:22 -04:00
local lsp_format = false
for _, client in pairs(vim.lsp.get_active_clients({ bufnr = 0 })) do
if client.server_capabilities.documentFormattingProvider then
lsp_format = true
break
end
end
if lsp_format then
vim.lsp.buf.format({ async = false })
2023-04-18 01:46:22 -04:00
else
cmd("Format")
end
end,
})
2022-06-07 12:55:56 -04:00
2021-06-13 16:20:42 -04:00
---- Plugins ----
2022-06-07 12:55:45 -04:00
-- Go
2023-04-14 19:38:59 -04:00
g.ale_go_golangci_lint_options = ""
2022-06-07 12:55:45 -04:00
g.ale_go_golangci_lint_package = 1
2023-03-27 16:09:35 -03:00
---- nvim-lint ----
2023-04-14 19:38:59 -04:00
local lint = require("lint")
local clippy_format = "%E%f:%l:%c: %\\d%#:%\\d%# %.%\\{-}"
2023-04-18 01:46:22 -04:00
.. "error:%.%\\{-} %m,%W%f:%l:%c: %\\d%#:%\\d%# %.%\\{-}"
.. "warning:%.%\\{-} %m,%C%f:%l %m,%-G,%-G"
.. "error: aborting %.%#,%-G"
.. "error: Could not compile %.%#,%E"
.. "error: %m,%Eerror[E%n]: %m,%-G"
.. "warning: the option `Z` is unstable %.%#,%W"
.. "warning: %m,%Inote: %m,%C %#--> %f:%l:%c"
2023-04-07 23:10:00 -04:00
2023-04-17 23:53:58 -04:00
-- clippy --
2023-04-07 23:10:00 -04:00
lint.linters.clippy = {
2023-04-14 19:38:59 -04:00
cmd = "cargo clippy",
2023-04-07 23:10:00 -04:00
stdin = false,
append_fname = true,
args = {},
2023-04-14 19:38:59 -04:00
stream = "stdout",
2023-04-07 23:10:00 -04:00
ignore_exitcode = false,
env = nil,
2023-04-14 19:38:59 -04:00
parser = require("lint.parser").from_errorformat(clippy_format),
2023-04-07 23:10:00 -04:00
}
2023-04-17 23:53:58 -04:00
-- sqlfluff --
2023-04-17 23:10:56 -04:00
lint.linters.sqlfluff.args = {
"lint",
"--format=json",
-- note: users will have to replace the --dialect argument accordingly
"--dialect=sqlite",
"-",
}
2023-04-17 23:53:58 -04:00
-- yamllint --
lint.linters.yamllint.args = {
args = { "--format=parsable", "-d relaxed" },
}
2023-04-18 01:46:22 -04:00
local linters = {
2023-04-14 19:38:59 -04:00
bash = { "shellcheck" },
2023-04-17 15:52:33 -04:00
dockerfile = { "hadolint" },
2023-04-14 19:38:59 -04:00
go = { "golangcilint" },
python = { "flake8" },
rust = { "clippy" },
2023-04-17 15:52:33 -04:00
sql = { "sqlfluff" },
2023-04-14 19:38:59 -04:00
yaml = { "yamllint" },
2023-03-27 16:09:35 -03:00
}
-- set linters --
lint.linters_by_ft = linters
-- set autocommands --
2023-04-07 23:10:00 -04:00
for ft, ft_linters in pairs(linters) do
local on_change = false
local on_write = false
2023-04-17 22:16:23 -04:00
for _, l in ipairs(ft_linters) do
if lint.linters[l].stdin then
2023-04-07 23:10:00 -04:00
on_change = true
else
on_write = true
end
end
if on_change then
2023-04-14 19:38:59 -04:00
vim.api.nvim_create_autocmd({ "FileType" }, {
pattern = ft,
callback = function()
vim.api.nvim_create_autocmd({ "TextChanged" }, {
callback = function()
lint.try_lint(nil, { ignore_errors = true })
end,
})
2023-04-17 22:16:23 -04:00
lint.try_lint(nil, { ignore_errors = true })
2023-04-14 19:38:59 -04:00
end,
})
2023-04-07 23:10:00 -04:00
end
if on_write then
2023-04-14 19:38:59 -04:00
vim.api.nvim_create_autocmd({ "FileType" }, {
pattern = ft,
callback = function()
vim.api.nvim_create_autocmd({ "BufWritePost" }, {
callback = function()
lint.try_lint(nil, { ignore_errors = true })
end,
})
end,
})
2023-04-07 23:10:00 -04:00
end
end
2023-04-12 20:35:03 -04:00
---- LuaSnip
2023-04-14 19:38:59 -04:00
local luasnip = require("luasnip")
2023-04-14 19:59:46 -04:00
require("luasnip.loaders.from_lua").lazy_load()
luasnip.config.set_config({
2023-04-14 19:38:59 -04:00
store_selection_keys = "<c-s>",
})
2023-04-14 19:38:59 -04:00
vim.keymap.set({ "i", "s" }, "<Tab>", function()
2023-04-13 22:15:48 -04:00
if luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
else
vim.api.nvim_feedkeys(
2023-04-14 19:38:59 -04:00
vim.api.nvim_replace_termcodes("<Tab>", true, false, true),
"n", -- noremap to avoid infinite recursion
true
)
2023-04-13 22:15:48 -04:00
end
2023-04-14 19:38:59 -04:00
end, { silent = true })
vim.keymap.set({ "i", "s" }, "<S-Tab>", function()
2023-04-13 22:15:48 -04:00
if luasnip.jumpable(-1) then
luasnip.jump(-1)
end
2023-04-14 19:38:59 -04:00
end, { silent = true })
2021-06-13 16:20:42 -04:00
---- completion-nvim
2023-04-14 19:38:59 -04:00
local cmp = require("cmp")
cmp.setup({
snippet = {
expand = function(args)
2023-04-14 19:59:46 -04:00
luasnip.lsp_expand(args.body)
end,
2021-06-13 16:20:42 -04:00
},
window = {
-- completion = cmp.config.window.bordered(),
-- documentation = cmp.config.window.bordered(),
},
mapping = cmp.mapping.preset.insert({
2023-04-14 19:38:59 -04:00
["<C-b>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete(),
["<C-e>"] = cmp.mapping.abort(),
2022-07-26 17:17:54 -04:00
-- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
2023-04-14 19:38:59 -04:00
["<CR>"] = cmp.mapping.confirm({ select = true }),
}),
sources = cmp.config.sources({
2023-04-14 19:38:59 -04:00
{ name = "nvim_lsp" },
{ name = "luasnip" },
{ name = "path" },
{ name = "buffer" },
}),
})
2023-03-27 12:59:11 -03:00
-- Setup lspconfig.
2023-01-12 15:38:52 -03:00
local capabilities = vim.lsp.protocol.make_client_capabilities()
2023-04-14 19:38:59 -04:00
lsp.util.default_config = vim.tbl_deep_extend("force", lsp.util.default_config, { capabilities = capabilities })
2023-03-31 20:16:08 -03:00
for server, add_to_config in pairs(servers) do
2023-04-18 01:50:35 -04:00
local config = {
2023-03-31 20:16:08 -03:00
on_attach = on_lsp_attach,
flags = {
debounce_text_changes = 150,
},
}
2023-04-14 19:38:59 -04:00
for k, v in pairs(add_to_config) do
2023-03-31 20:16:08 -03:00
config[k] = v
2023-03-28 02:07:12 -03:00
end
2023-03-31 20:16:08 -03:00
lsp[server].setup(config)
end
2023-01-17 23:35:05 -03:00
---- git-blame.nvim
g.gitblame_enabled = 0
2022-06-20 20:42:48 -04:00
---- gitsigns.nvim
2023-04-14 19:38:59 -04:00
require("gitsigns").setup({
2022-06-20 20:42:48 -04:00
on_attach = function(bufnr)
local gs = package.loaded.gitsigns
-- Navigation
2023-04-14 19:38:59 -04:00
vim.keymap.set("n", "]c", function()
if vim.wo.diff then
return "]c"
end
vim.schedule(function()
gs.next_hunk()
end)
return "<Ignore>"
end, { expr = true })
2022-06-20 20:42:48 -04:00
2023-04-14 19:38:59 -04:00
vim.keymap.set("n", "[c", function()
if vim.wo.diff then
return "[c"
end
vim.schedule(function()
gs.prev_hunk()
end)
return "<Ignore>"
end, { expr = true })
2022-06-20 20:42:48 -04:00
-- Actions
2023-04-14 19:38:59 -04:00
vim.keymap.set({ "n", "v" }, "<leader>hs", ":Gitsigns stage_hunk<CR>")
vim.keymap.set({ "n", "v" }, "<leader>hr", ":Gitsigns reset_hunk<CR>")
vim.keymap.set("n", "<leader>hS", gs.stage_buffer)
vim.keymap.set("n", "<leader>hu", gs.undo_stage_hunk)
vim.keymap.set("n", "<leader>hR", gs.reset_buffer)
vim.keymap.set("n", "<leader>hp", gs.preview_hunk)
vim.keymap.set("n", "<leader>hb", function()
gs.blame_line({ full = true })
end)
vim.keymap.set("n", "<leader>tb", gs.toggle_current_line_blame)
vim.keymap.set("n", "<leader>hd", gs.diffthis)
vim.keymap.set("n", "<leader>hD", function()
gs.diffthis("~")
end)
vim.keymap.set("n", "<leader>td", gs.toggle_deleted)
2022-06-20 20:42:48 -04:00
-- Text object
2023-04-14 19:38:59 -04:00
vim.keymap.set({ "o", "x" }, "ih", ":<C-U>Gitsigns select_hunk<CR>")
end,
})
2022-06-21 20:53:24 -04:00
---- Comment.nvim
2023-04-14 19:38:59 -04:00
require("Comment").setup()
2021-06-13 16:20:42 -04:00
---- vim-markdown
g.vim_markdown_no_default_key_mappings = 1
g.vim_markdown_folding_disabled = 1
g.vim_markdown_toml_frontmatter = 1
---- Telescope
2023-04-14 19:38:59 -04:00
local telescope = require("telescope")
telescope.setup({
2021-09-21 12:26:07 -03:00
defaults = {
2023-04-14 19:38:59 -04:00
file_ignore_patterns = { "%.avif", "%.jpg", "%.pdf", "%.png", "%.webp" },
},
pickers = {
find_files = {
hidden = true,
2023-04-14 19:38:59 -04:00
},
},
})
telescope.load_extension("fzy_native")
telescope.load_extension("luasnip")
local builtin = require("telescope.builtin")
vim.keymap.set("n", "<Leader>f", builtin.fd)
vim.keymap.set("n", "<Leader>b", builtin.buffers)
vim.keymap.set("n", "<Leader>/", builtin.current_buffer_fuzzy_find)
vim.keymap.set("n", "<Leader>g", builtin.live_grep)
vim.keymap.set("n", "<Leader>cg", builtin.grep_string)
vim.keymap.set("n", "<Leader>:", builtin.commands)
vim.keymap.set("n", "<Leader>s", telescope.extensions.luasnip.luasnip)
2023-03-05 01:21:04 -03:00
---- nvim-dap
2023-04-14 19:38:59 -04:00
local dap = require("dap")
vim.fn.sign_define("DapBreakpoint", { text = "🟥", texthl = "", linehl = "", numhl = "" })
vim.fn.sign_define("DapStopped", { text = "▶️", texthl = "", linehl = "", numhl = "" })
-- Set keymaps to control the debugge
2023-04-14 19:59:46 -04:00
vim.keymap.set("n", "<leader>dc", dap.continue)
vim.keymap.set("n", "<leader>do", dap.step_over)
vim.keymap.set("n", "<leader>di", dap.step_into)
vim.keymap.set("n", "<leader>dO", dap.step_out)
vim.keymap.set("n", "<leader>db", dap.toggle_breakpoint)
2023-04-14 19:38:59 -04:00
vim.keymap.set("n", "<leader>dB", function()
2023-04-14 19:59:46 -04:00
dap.set_breakpoint(vim.fn.input("Breakpoint condition: "))
2023-03-05 01:21:04 -03:00
end)
2023-04-14 19:38:59 -04:00
local dapui = require("dapui")
dap.listeners.after.event_initialized["dapui_config"] = function()
2023-03-05 17:15:12 -03:00
dapui.open()
end
2023-04-14 19:38:59 -04:00
dap.listeners.before.event_terminated["dapui_config"] = function()
2023-03-05 17:15:12 -03:00
dapui.close()
end
2023-04-14 19:38:59 -04:00
dap.listeners.before.event_exited["dapui_config"] = function()
2023-03-05 17:15:12 -03:00
dapui.close()
end
-- nvim-dap-go
2023-04-14 19:38:59 -04:00
require("dap-go").setup()
---- web-devicons
2023-04-14 19:38:59 -04:00
require("nvim-web-devicons").setup()
2023-06-22 22:42:45 -04:00
---- firenvim
2023-06-25 00:09:06 -04:00
if g.started_by_firenvim == true then
g.airline_powerline_fonts = false
vim.o.laststatus = 0
2023-06-22 22:42:45 -04:00
vim.cmd("packadd firenvim")
end