Big neovim config refactor + adjustments

This commit is contained in:
2024-04-16 01:29:18 +02:00
parent 8031f3fe6a
commit 5574cc866a
42 changed files with 1001 additions and 875 deletions

View File

@@ -1,49 +1,9 @@
--[[
-- Basic vim config stuff
require("keymaps")
require("options")
require("autocmds")
=====================================================================
==================== READ THIS BEFORE CONTINUING ====================
=====================================================================
Kickstart.nvim is *not* a distribution.
Kickstart.nvim is a template for your own configuration.
The goal is that you can read every line of code, top-to-bottom, and understand
what your configuration is doing.
Once you've done that, you should start exploring, configuring and tinkering to
explore Neovim!
If you don't know anything about Lua, I recommend taking some time to read through
a guide. One possible example:
- https://learnxinyminutes.com/docs/lua/
And then you can explore or search through `:help lua-guide`
Kickstart Guide:
I have left several `:help X` comments throughout the init.lua
You should run that command and read that help section for more information.
In addition, I have some `NOTE:` items throughout the file.
These are for you, the reader to help understand what is happening. Feel free to delete
them once you know what you're doing, but they should serve as a guide for when you
are first encountering a few different constructs in your nvim config.
I hope you enjoy your Neovim journey,
- TJ
P.S. You can delete this when you're done too. It's your config now :)
--]]
-- Set <space> as the leader key
-- See `:help mapleader`
-- NOTE: Must happen before plugins are required (otherwise wrong leader will be used)
vim.g.mapleader = " "
vim.g.maplocalleader = " "
-- Install package manager
-- https://github.com/folke/lazy.nvim
-- `:help lazy.nvim.txt` for more info
-- Install lazy package manager
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
vim.fn.system({
@@ -57,220 +17,8 @@ if not vim.loop.fs_stat(lazypath) then
end
vim.opt.rtp:prepend(lazypath)
local symbols = require("constant.symbols")
local border = "rounded"
-- Helper function for staging selection
local function gitsigns_visual_op(op)
return function()
return require("gitsigns")[op]({ vim.fn.line("."), vim.fn.line("v") })
end
end
-- NOTE: Here is where you install your plugins.
-- You can configure plugins using the `config` key.
--
-- You can also configure plugins after the setup call,
-- as they will be available in your neovim runtime.
require("lazy").setup({
-- NOTE: This is where your plugins related to LSP can be installed.
-- The configuration is done below. Search for lspconfig to find it below.
{
-- LSP Configuration & Plugins
"neovim/nvim-lspconfig",
dependencies = {
-- Automatically install LSPs to stdpath for neovim
"williamboman/mason.nvim",
"williamboman/mason-lspconfig.nvim",
-- Useful status updates for LSP
-- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})`
{ "j-hui/fidget.nvim", opts = {} },
-- Additional lua configuration, makes nvim stuff amazing!
"folke/neodev.nvim",
-- Add document color
"mrshmllow/document-color.nvim",
"b0o/schemastore.nvim",
-- Rename with immediate visual feedback
{
"smjonas/inc-rename.nvim",
config = function()
require("inc_rename").setup({
preview_empty_name = true,
})
end,
},
},
},
{
"ray-x/lsp_signature.nvim",
dependencies = {
"neovim/nvim-lspconfig",
},
opts = {
hint_enable = false,
handler_opts = {
border = border,
},
},
},
{
-- Autocompletion
"hrsh7th/nvim-cmp",
dependencies = {
"hrsh7th/cmp-nvim-lsp",
"L3MON4D3/LuaSnip",
"saadparwaiz1/cmp_luasnip",
"hrsh7th/cmp-path",
"onsails/lspkind-nvim",
},
},
-- Useful plugin to show you pending keybinds.
{
"folke/which-key.nvim",
opts = {},
init = function()
require("which-key").register({
["<leader>g"] = { name = "[G]it", _ = "which_key_ignore" },
["<leader>b"] = { name = "[B]buffer", _ = "which_key_ignore" },
["<leader>s"] = { name = "[S]earch", _ = "which_key_ignore" },
})
end,
},
{
-- Adds git related signs to the gutter, as well as utilities for managing changes
"lewis6991/gitsigns.nvim",
init = function()
vim.keymap.set("n", "<leader>gs", require("gitsigns.actions").stage_hunk, { desc = "[G]it [S]tage hunk" })
vim.keymap.set(
"n",
"<leader>gS",
require("gitsigns.actions").undo_stage_hunk,
{ desc = "[G]it undo [S]tage hunk" }
)
vim.keymap.set(
"n",
"<leader>gd",
require("gitsigns.actions").preview_hunk_inline,
{ desc = "[G]it [D]iff hunk" }
)
vim.keymap.set("n", "<leader>gr", require("gitsigns.actions").reset_hunk, { desc = "[G]it [R]eset hunk" })
vim.keymap.set("v", "<leader>gs", gitsigns_visual_op("stage_hunk"), { desc = "[G]it [S]tage selection" })
vim.keymap.set("v", "<leader>gr", gitsigns_visual_op("reset_hunk"), { desc = "[G]it [R]eset selection" })
vim.keymap.set("n", "]g", function()
require("gitsigns.actions").nav_hunk("next")
end, { desc = "Go to next hunk" })
vim.keymap.set("n", "[g", function()
require("gitsigns.actions").nav_hunk("prev")
end, { desc = "Go to previous hunk" })
end,
opts = {
-- See `:help gitsigns.txt`
signs = {
add = { text = "+" },
change = { text = "~" },
delete = { text = "_" },
topdelete = { text = "" },
changedelete = { text = "~" },
},
},
},
{
-- Set lualine as statusline
"nvim-lualine/lualine.nvim",
-- See `:help lualine.txt`
opts = {
options = {
icons_enabled = true,
theme = "gruvbox",
component_separators = { left = "", right = "" },
section_separators = { left = "", right = "" },
},
sections = {
lualine_b = {
"branch",
"diff",
{ "diagnostics", symbols = { error = "", warn = "", info = "", hint = "" } },
},
lualine_c = {
{
"filename",
path = 1,
symbols = {
modified = symbols.file.modified,
readonly = symbols.file.readonly,
},
},
},
lualine_x = {
"encoding",
{ "fileformat", icons_enabled = false },
"filetype",
},
},
inactive_sections = {
lualine_c = {
{ "filename", path = 1 },
},
},
},
},
{
-- Add indentation guides even on blank lines
"lukas-reineke/indent-blankline.nvim",
-- Enable `lukas-reineke/indent-blankline.nvim`
-- See `:help indent_blankline.txt`
main = "ibl",
opts = {
indent = {
char = "¦",
},
},
},
-- Fuzzy Finder (files, lsp, etc)
{
"nvim-telescope/telescope.nvim",
version = "*",
dependencies = { "nvim-lua/plenary.nvim", "nvim-telescope/telescope-ui-select.nvim" },
},
-- Fuzzy Finder Algorithm which requires local dependencies to be built.
-- Only load if `make` is available. Make sure you have the system
-- requirements installed.
{
"nvim-telescope/telescope-fzf-native.nvim",
-- NOTE: If you are having trouble with this installation,
-- refer to the README for telescope-fzf-native for more instructions.
build = "make",
cond = function()
return vim.fn.executable("make") == 1
end,
},
{
-- Highlight, edit, and navigate code
"nvim-treesitter/nvim-treesitter",
dependencies = {
"nvim-treesitter/nvim-treesitter-textobjects",
"nvim-treesitter/nvim-treesitter-context",
},
config = function()
pcall(require("nvim-treesitter.install").update({ with_sync = true }))
end,
},
require("themes.gruvbox"),
{ import = "themes" },
{ import = "plugins" },
}, {
install = {
@@ -278,386 +26,8 @@ require("lazy").setup({
"gruvbox",
},
},
})
require("options")
require("keymaps")
require("autocmds")
-- [[ Configure Telescope ]]
-- See `:help telescope` and `:help telescope.setup()`
require("telescope").setup({
pickers = {
find_files = {
hidden = true,
},
},
defaults = {
file_ignore_patterns = {
".git/",
},
mappings = {
n = {
["<c-d>"] = "delete_buffer",
["<Tab>"] = "move_selection_next",
["<S-Tab>"] = "move_selection_previous",
},
i = {
["<c-d>"] = "delete_buffer",
["<Tab>"] = "move_selection_next",
["<S-Tab>"] = "move_selection_previous",
},
},
},
extensions = {
["ui-select"] = {
require("telescope.themes").get_dropdown({}),
},
ui = {
border = require("symbols.window").border,
backdrop = 100,
},
})
-- Enable telescope fzf native, if installed
pcall(require("telescope").load_extension, "fzf")
pcall(require("telescope").load_extension, "ui-select")
-- See `:help telescope.builtin`
vim.keymap.set("n", "<leader>.", require("telescope.builtin").oldfiles, { desc = "[.] Find recently opened files" })
vim.keymap.set("n", "<leader><space>", require("telescope.builtin").buffers, { desc = "[ ] Find existing buffers" })
vim.keymap.set("n", "<leader>/", function()
require("telescope.builtin").current_buffer_fuzzy_find({
-- Show matches in the order they appear in the document
sorting_strategy = "ascending",
})
end, { desc = "[/] Fuzzily search in current buffer" })
vim.keymap.set("n", "<leader>s/", function()
require("telescope.builtin").live_grep({
grep_open_files = true,
prompt_title = "Live Grep in Open Files",
})
end, { desc = "[S]earch [/] in Open Files" })
vim.keymap.set("n", "<leader>sf", require("telescope.builtin").find_files, { desc = "[S]earch [F]iles" })
vim.keymap.set("n", "<leader>sh", require("telescope.builtin").help_tags, { desc = "[S]earch [H]elp" })
vim.keymap.set("n", "<leader>sw", function()
require("telescope.builtin").grep_string({
-- Show matches in the order they appear in the document
sorting_strategy = "ascending",
})
end, { desc = "[S]earch current [W]ord" })
vim.keymap.set("n", "<leader>sg", require("telescope.builtin").live_grep, { desc = "[S]earch by [G]rep" })
vim.keymap.set("n", "<leader>sd", require("telescope.builtin").diagnostics, { desc = "[S]earch [D]iagnostics" })
vim.keymap.set("n", "<leader>sr", require("telescope.builtin").resume, { desc = "[S]earch [R]esume" })
vim.keymap.set("n", "<leader>sk", require("telescope.builtin").keymaps, { desc = "[S]earch [K]eymaps" })
vim.keymap.set("n", "<leader>sn", function()
require("telescope.builtin").find_files({ cwd = vim.fn.stdpath("config") })
end, { desc = "[S]earch [N]eovim files" })
-- [[ Configure Treesitter ]]
-- See `:help nvim-treesitter`
require("nvim-treesitter.configs").setup({
-- Add languages to be installed here that you want installed for treesitter
ensure_installed = {
"c",
"cpp",
"go",
"lua",
"python",
"rust",
"tsx",
"typescript",
"vimdoc",
"vim",
"markdown",
"markdown_inline",
"bash",
"zig",
},
-- Autoinstall languages that are not installed. Defaults to false (but you can change for yourself!)
auto_install = true,
highlight = { enable = true },
-- Disabled because it caueses issues with nvim-autopair
indent = { enable = false, disable = { "python" } },
incremental_selection = {
enable = true,
keymaps = {
init_selection = "<c-space>",
node_incremental = "<c-space>",
scope_incremental = "<c-s>",
node_decremental = "<M-space>",
},
},
textobjects = {
select = {
enable = true,
lookahead = true, -- Automatically jump forward to textobj, similar to targets.vim
keymaps = {
-- You can use the capture groups defined in textobjects.scm
["aa"] = "@parameter.outer",
["ia"] = "@parameter.inner",
["af"] = "@function.outer",
["if"] = "@function.inner",
["ac"] = "@class.outer",
["ic"] = "@class.inner",
},
},
move = {
enable = true,
set_jumps = true, -- whether to set jumps in the jumplist
goto_next_start = {
["]m"] = "@function.outer",
["]]"] = "@class.outer",
},
goto_next_end = {
["]M"] = "@function.outer",
["]["] = "@class.outer",
},
goto_previous_start = {
["[m"] = "@function.outer",
["[["] = "@class.outer",
},
goto_previous_end = {
["[M"] = "@function.outer",
["[]"] = "@class.outer",
},
},
swap = {
enable = true,
swap_next = {
["<leader>a"] = "@parameter.inner",
},
swap_previous = {
["<leader>A"] = "@parameter.inner",
},
},
},
})
-- LSP settings.
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { border = border })
vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.hover, { border = border })
vim.diagnostic.config({
float = {
border = border,
},
})
-- Set the diagnostic symbols (Also used by Neo-tree)
local signs = {
Error = symbols.diagnostic.error,
Warn = symbols.diagnostic.warning,
Hint = symbols.diagnostic.hint,
Info = symbols.diagnostic.info,
}
for type, icon in pairs(signs) do
local hl = "DiagnosticSign" .. type
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl })
end
-- This function gets run when an LSP connects to a particular buffer.
local on_attach = function(client, bufnr)
-- NOTE: Remember that lua is a real programming language, and as such it is possible
-- to define small helper and utility functions so you don't have to repeat yourself
-- many times.
--
-- In this case, we create a function that lets us more easily define mappings specific
-- for LSP related items. It sets the mode, buffer and description for us each time.
local nmap = function(keys, func, desc)
if desc then
desc = "LSP: " .. desc
end
vim.keymap.set("n", keys, func, { buffer = bufnr, desc = desc })
end
vim.keymap.set("n", "<leader>rn", function()
return ":IncRename " .. vim.fn.expand("<cword>")
end, { expr = true, desc = "[R]e[n]ame" })
-- nmap('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction')
-- Should allow code actions in visual mode
vim.keymap.set(
{ "v", "n" },
"<leader>ca",
vim.lsp.buf.code_action,
{ buffer = bufnr, desc = "LSP: [C]ode [A]ction", remap = true }
)
-- nmap('<leader>ca', require('telescope.builtin')., '[C]ode [A]ction')
nmap("gd", vim.lsp.buf.definition, "[G]oto [D]efinition")
nmap("gr", require("telescope.builtin").lsp_references, "[G]oto [R]eferences")
nmap("gI", vim.lsp.buf.implementation, "[G]oto [I]mplementation")
nmap("<leader>D", vim.lsp.buf.type_definition, "Type [D]efinition")
nmap("<leader>ds", require("telescope.builtin").lsp_document_symbols, "[D]ocument [S]symbols")
nmap("<leader>ws", require("telescope.builtin").lsp_dynamic_workspace_symbols, "[W]orkspace [S]symbols")
-- See `:help K` for why this keymap
nmap("K", vim.lsp.buf.hover, "Hover Documentation")
nmap("<C-k>", vim.lsp.buf.signature_help, "Signature Documentation")
-- Lesser used LSP functionality
nmap("gD", vim.lsp.buf.declaration, "[G]oto [D]eclaration")
nmap("<leader>wa", vim.lsp.buf.add_workspace_folder, "[W]orkspace [A]dd Folder")
nmap("<leader>wr", vim.lsp.buf.remove_workspace_folder, "[W]orkspace [R]emove Folder")
nmap("<leader>wl", function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, "[W]orkspace [L]ist Folders")
-- -- Create a command `:Format` local to the LSP buffer
-- vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
-- vim.lsp.buf.format()
-- end, { desc = 'Format current buffer with LSP' })
--
-- -- Format on save
-- local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
-- if client.supports_method("textDocument/formatting") then
-- vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
-- vim.api.nvim_create_autocmd("BufWritePre", {
-- group = augroup,
-- buffer = bufnr,
-- callback = function()
-- vim.lsp.buf.format()
-- end,
-- })
-- end
--
-- -- Attach document colour support
-- if client.server_capabilities.colorProvider then
-- require("document-color").buf_attach(bufnr)
-- end
end
-- Enable the following language servers
-- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
--
-- Add any additional override configuration in the following tables. They will be passed to
-- the `settings` field of the server config. You must look up that documentation yourself.
local servers = {
clangd = {},
gopls = {},
pyright = {},
rust_analyzer = {
["rust-analyzer"] = {
check = {
command = "clippy",
},
},
},
tsserver = {},
tailwindcss = {},
-- cssls = {},
lua_ls = {
Lua = {
workspace = { checkThirdParty = false },
telemetry = { enable = false },
},
},
jsonls = {
json = {
schemas = require("schemastore").json.schemas(),
},
},
yamlls = {
yaml = {
schemas = require("schemastore").yaml.schemas(),
},
},
taplo = {},
cmake = {},
}
-- Setup neovim lua configuration
require("neodev").setup()
-- nvim-cmp supports additional completion capabilities, so broadcast that to servers
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require("cmp_nvim_lsp").default_capabilities(capabilities)
capabilities.textDocument.colorProvider = {
dynamicRegistration = true,
}
-- Setup mason so it can manage external tooling
require("mason").setup()
-- Ensure the servers above are installed
local mason_lspconfig = require("mason-lspconfig")
mason_lspconfig.setup({
ensure_installed = vim.tbl_keys(servers),
})
mason_lspconfig.setup_handlers({
function(server_name)
require("lspconfig")[server_name].setup({
capabilities = capabilities,
on_attach = on_attach,
settings = servers[server_name],
})
end,
})
-- nvim-cmp setup
local cmp = require("cmp")
local luasnip = require("luasnip")
local lspkind = require("lspkind")
luasnip.config.setup({})
cmp.setup({
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
-- Include the source of the cmp entry
formatting = {
format = lspkind.cmp_format({
mode = "text",
menu = {
nvim_lsp = "[LSP]",
luasnip = "[LuaSnip]",
path = "[Path]",
},
}),
},
mapping = cmp.mapping.preset.insert({
["<C-u>"] = cmp.mapping.scroll_docs(-4),
["<C-d>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete({}),
["<CR>"] = cmp.mapping.confirm({
behavior = cmp.ConfirmBehavior.Replace,
select = true,
}),
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s" }),
}),
sources = {
{ name = "luasnip" },
{ name = "nvim_lsp" },
{ name = "path" },
},
})
-- The line beneath this is called `modeline`. See `:help modeline`
-- vim: ts=2 sts=2 sw=2 et