configs/nvim/init.lua
2024-11-18 21:49:33 +13:00

174 lines
4.3 KiB
Lua

vim.opt.number = true
vim.opt.relativenumber = true
vim.opt.expandtab = true
vim.opt.tabstop = 8
vim.opt.softtabstop = 0
vim.opt.shiftwidth = 4
vim.opt.smarttab = true
vim.opt.autoindent = true
vim.opt.mouse = "a"
vim.opt.undofile = true
vim.opt.ignorecase = true
vim.opt.smartcase = true
vim.cmd("colorscheme habamax")
local plugins = {
{
source = "neovim/nvim-lspconfig",
},
{
source = "hrsh7th/nvim-cmp",
depends = { "hrsh7th/cmp-nvim-lsp" }
},
{
source = "andweeb/presence.nvim"
}
}
local servers = {
clangd = { cmd = { "clangd", "-header-insertion=never" } },
gopls = {},
nim_langserver = {},
rust_analyzer = {},
cssls = {
cmd = { "vscode-css-language-server", "--stdio" }
},
html = {
cmd = { "vscode-html-language-server", "--stdio" }
},
jsonls = {
cmd = { "vscode-json-language-server", "--stdio" }
}
}
-- Install plugin manager
local mini_path = vim.fn.stdpath("data") .. "/site/pack/deps/start/mini.nvim"
if not vim.loop.fs_stat(mini_path) then
vim.fn.system({
"git", "clone", "--filter=blob:none",
"https://github.com/echasnovski/mini.nvim", mini_path
})
vim.cmd("packadd mini.nvim | helptags ALL")
end
require("mini.deps").setup({ path = { package = path_package } })
-- mini.completion is missing snippet support so waiting for that, once added can get rid of cmp
-- require("mini.completion").setup()
-- <C-_> is Ctrl+/
local comment_keybind = "<C-_>"
require("mini.comment").setup({
mappings = {
comment = comment_keybind,
comment_line = comment_keybind,
comment_visual = comment_keybind,
textobject = comment_keybind,
},
})
-- Install plugins
for plugin in pairs(plugins) do
MiniDeps.add(plugins[plugin])
end
-- Setup completion
local cmp = require("cmp")
cmp.setup({
snippet = {
expand = function(args)
vim.snippet.expand(args.body)
end,
},
mapping = cmp.mapping.preset.insert({
["<C-Space>"] = cmp.mapping.complete(),
["<CR>"] = cmp.mapping.confirm({ select = true })
}),
sources = cmp.config.sources({
{ name = "nvim_lsp" },
})
}, {
{ name = "buffer" },
});
-- Setup servers and their configs
local capabilities = require("cmp_nvim_lsp").default_capabilities()
local lspconfig = require("lspconfig")
for server in pairs(servers) do
local config = servers[server]
config.capabilities = config.capabilities or capabilities
lspconfig[server].setup(config)
end
-- Setup Discord rich presence
require("presence").setup({
neovim_image_text = "Neovim",
})
-- Binds to jump between snippets
vim.keymap.set({ "i", "s" }, "<Tab>", function()
if vim.snippet.active({ direction = 1 }) then
return "<Cmd>lua vim.snippet.jump(1)<CR>"
else
return "<Tab>"
end
end, { expr = true })
vim.keymap.set({ "i", "s" }, "<S-Tab>", function()
if vim.snippet.active({ direction = -1 }) then
return "<Cmd>lua vim.snippet.jump(-1)<CR>"
else
return "<S-Tab>"
end
end, { expr = true })
vim.keymap.set({ "n" }, "gd", function()
vim.lsp.buf.definition()
end)
vim.keymap.set({ "n" }, "<space>a", function()
vim.lsp.buf.code_action()
end)
vim.keymap.set({ "n" }, "<space>e", function()
vim.diagnostic.open_float()
end)
-- Session saving and restoring
local session_file = vim.fn.stdpath("data") .. "/sessions/" .. vim.fn.fnamemodify(vim.fn.getcwd(), ':t') .. "/session.vim"
vim.api.nvim_create_autocmd("VimLeavePre", {
pattern = "*",
callback = function()
vim.fn.mkdir(vim.fn.fnamemodify(session_file, ":h"), "p")
vim.cmd("mksession! " .. session_file)
vim.v.this_session = session_file
end,
})
vim.api.nvim_create_autocmd("VimEnter", {
nested = true,
once = true,
callback = function()
if vim.fn.filereadable(session_file) == 1 and #vim.fn.argv() == 0 then
vim.cmd("silent! %bwipeout!")
vim.cmd("silent! source " .. session_file)
vim.v.this_session = session_file
end
end
})
vim.api.nvim_create_autocmd("FileType", {
pattern = { "c", "lua", "md", "vim" },
callback = function()
vim.treesitter.start()
end
})
vim.api.nvim_create_autocmd("BufWritePre", {
callback = function()
vim.lsp.buf.format({ async = false })
end
})