linuxbox/config/nvim/init.lua

801 lines
24 KiB
Lua

vim.g.mapleader = " "
vim.g.maplocalleader = " "
-- vim.g.loaded_netrw = 1
-- vim.g.loaded_netrwPlugin = 1
-- Set to true if you have a Nerd Font installed and selected in the terminal
vim.g.have_nerd_font = true
-- Set highlight on search
vim.o.hlsearch = false
-- Make line numbers default
vim.wo.number = true
-- Enable mouse mode
vim.o.mouse = "a"
-- Enable break indent
vim.o.breakindent = true
-- Save undo history
vim.o.undofile = true
-- Case insensitive searching UNLESS /C or capital in search
vim.o.ignorecase = true
vim.o.smartcase = true
-- Keep signcolumn on by default
vim.wo.signcolumn = "yes"
-- Set completeopt to have a better completion experience
-- vim.o.completeopt = "menuone,noselect,noinsert"
-- Use terminal colors
vim.o.termguicolors = true
-- Tabs
vim.o.shiftwidth = 4
vim.o.smarttab = true
vim.o.tabstop = 4
vim.o.expandtab = true
vim.o.softtabstop = 4
-- Preview substitutions live, as you type!
vim.o.inccommand = "split"
-- Show cursor line
vim.o.cursorline = true
vim.o.showmatch = true
-- Decrease update time
vim.o.updatetime = 250
-- Decrease mapped sequence wait time
vim.o.timeoutlen = 300
vim.o.pumheight = 10
vim.o.relativenumber = true
vim.o.scrolloff = 8
-- Don't show the mode, since it's already in the status line
vim.o.showmode = false
vim.o.showtabline = 0
vim.o.wrap = true
vim.o.foldmethod = "expr"
vim.o.foldexpr = "nvim_treesitter#foldexpr()"
vim.o.foldlevelstart = 99
vim.schedule(function()
vim.o.clipboard = "unnamedplus"
end)
vim.o.autoread = true
-------------------------------------------------------------------
-- Autocommands
-------------------------------------------------------------------
-- Autoset current working directory based on closest git repo
vim.api.nvim_create_autocmd("BufEnter", {
callback = function()
local git_dir = vim.fn.finddir(".git", ".;")
if git_dir ~= "" then
local git_root = vim.fn.fnamemodify(git_dir, ":h")
vim.cmd("lcd " .. git_root)
end
end,
})
-- show cursor line only in active window
local cursorLineGrp = vim.api.nvim_create_augroup("CursorLine", { clear = true })
vim.api.nvim_create_autocmd(
{ "InsertLeave", "WinEnter" },
{ pattern = "*", command = "set cursorline", group = cursorLineGrp }
)
vim.api.nvim_create_autocmd(
{ "InsertEnter", "WinLeave" },
{ pattern = "*", command = "set nocursorline", group = cursorLineGrp }
)
vim.api.nvim_create_autocmd(
{ "InsertLeave", "FocusGained" },
{ pattern = "*", command = "set cursorline", group = cursorLineGrp }
)
vim.api.nvim_create_autocmd(
{ "InsertEnter", "FocusLost" },
{ pattern = "*", command = "set nocursorline", group = cursorLineGrp }
)
-- Auto update on file changes
vim.api.nvim_create_autocmd({ "BufEnter", "CursorHold", "CursorHoldI", "FocusGained" }, {
command = "if mode() != 'c' | checktime | endif",
pattern = { "*" },
})
vim.api.nvim_create_autocmd(
{ "FileChangedShellPost" },
{ command = 'echohl WarningMsg | echo "File changed on disk. Buffer reloaded." | echohl None', pattern = { "*" } }
)
-------------------------------------------------------------------
-- Plugin management
-------------------------------------------------------------------
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
vim.fn.system({
"git",
"clone",
"--filter=blob:none",
"https://github.com/folke/lazy.nvim.git",
"--branch=stable", -- latest stable release
lazypath,
})
end
vim.opt.rtp:prepend(lazypath)
-------------------------------------------------------------------
-- Plugin installation
-------------------------------------------------------------------
require("lazy").setup({
-- Color scheme ------------------------------------------
{
dir = "~/linuxbox/nightly_cm.nvim",
lazy = false,
priority = 1000,
config = function()
vim.cmd.colorscheme("nightly_cm")
end,
},
-- LSP ----------------------------------------------------
{
"folke/lazydev.nvim",
ft = "lua", -- only load on lua files
opts = {
library = {
-- See the configuration section for more details
-- Load luvit types when the `vim.uv` word is found
{ path = "${3rd}/luv/library", words = { "vim%.uv" } },
},
},
},
{
-- Main LSP Configuration
"neovim/nvim-lspconfig",
dependencies = {
{ "j-hui/fidget.nvim", opts = {} },
},
config = function()
vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("kickstart-lsp-attach", { clear = true }),
callback = function(event)
-- 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 map = function(keys, func, desc, mode)
mode = mode or "n"
vim.keymap.set(mode, keys, func, { buffer = event.buf, desc = "LSP: " .. desc })
end
-- Rename the variable under your cursor.
-- Most Language Servers support renaming across files, etc.
map("grn", vim.lsp.buf.rename, "[R]e[n]ame")
-- Execute a code action, usually your cursor needs to be on top of an error
-- or a suggestion from your LSP for this to activate.
map("gra", vim.lsp.buf.code_action, "[G]oto Code [A]ction", { "n", "x" })
-- Find references for the word under your cursor.
map("grr", require("fzf-lua").lsp_references, "[G]oto [R]eferences")
-- Jump to the implementation of the word under your cursor.
-- Useful when your language has ways of declaring types without an actual implementation.
map("gri", require("fzf-lua").lsp_implementations, "[G]oto [I]mplementation")
-- Jump to the definition of the word under your cursor.
-- This is where a variable was first declared, or where a function is defined, etc.
-- To jump back, press <C-t>.
map("grd", require("fzf-lua").lsp_definitions, "[G]oto [D]efinition")
-- WARN: This is not Goto Definition, this is Goto Declaration.
-- For example, in C this would take you to the header.
map("grD", vim.lsp.buf.declaration, "[G]oto [D]eclaration")
-- Fuzzy find all the symbols in your current document.
-- Symbols are things like variables, functions, types, etc.
map("gO", require("fzf-lua").lsp_document_symbols, "Open Document Symbols")
-- Fuzzy find all the symbols in your current workspace.
-- Similar to document symbols, except searches over your entire project.
map("gW", require("fzf-lua").lsp_live_workspace_symbols, "Open Workspace Symbols")
-- Jump to the type of the word under your cursor.
-- Useful when you're not sure what type a variable is and you want to see
-- the definition of its *type*, not where it was *defined*.
map("grt", require("fzf-lua").lsp_typedefs, "[G]oto [T]ype Definition")
-- This function resolves a difference between neovim nightly (version 0.11) and stable (version 0.10)
---@param client vim.lsp.Client
---@param method vim.lsp.protocol.Method
---@param bufnr? integer some lsp support methods only in specific files
---@return boolean
local function client_supports_method(client, method, bufnr)
if vim.fn.has("nvim-0.11") == 1 then
return client:supports_method(method, bufnr)
else
return client.supports_method(method, { bufnr = bufnr })
end
end
-- The following two autocommands are used to highlight references of the
-- word under your cursor when your cursor rests there for a little while.
-- See `:help CursorHold` for information about when this is executed
--
-- When you move your cursor, the highlights will be cleared (the second autocommand).
local client = vim.lsp.get_client_by_id(event.data.client_id)
-- if
-- client
-- and client_supports_method(
-- client,
-- vim.lsp.protocol.Methods.textDocument_documentHighlight,
-- event.buf
-- )
-- then
-- local highlight_augroup =
-- vim.api.nvim_create_augroup("kickstart-lsp-highlight", { clear = false })
-- vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
-- buffer = event.buf,
-- group = highlight_augroup,
-- callback = vim.lsp.buf.document_highlight,
-- })
--
-- vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
-- buffer = event.buf,
-- group = highlight_augroup,
-- callback = vim.lsp.buf.clear_references,
-- })
--
-- vim.api.nvim_create_autocmd("LspDetach", {
-- group = vim.api.nvim_create_augroup("kickstart-lsp-detach", { clear = true }),
-- callback = function(event2)
-- vim.lsp.buf.clear_references()
-- vim.api.nvim_clear_autocmds({ group = "kickstart-lsp-highlight", buffer = event2.buf })
-- end,
-- })
-- end
-- The following code creates a keymap to toggle inlay hints in your
-- code, if the language server you are using supports them
--
-- This may be unwanted, since they displace some of your code
if
client
and client_supports_method(client, vim.lsp.protocol.Methods.textDocument_inlayHint, event.buf)
then
map("<leader>th", function()
vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({ bufnr = event.buf }))
end, "[T]oggle Inlay [H]ints")
end
end,
})
-- Diagnostic Config
-- See :help vim.diagnostic.Opts
vim.diagnostic.config({
severity_sort = true,
float = { border = "rounded", source = "if_many" },
underline = { severity = vim.diagnostic.severity.ERROR },
signs = vim.g.have_nerd_font and {
text = {
[vim.diagnostic.severity.ERROR] = "󰅚 ",
[vim.diagnostic.severity.WARN] = "󰀪 ",
[vim.diagnostic.severity.INFO] = "󰋽 ",
[vim.diagnostic.severity.HINT] = "󰌶 ",
},
} or {},
virtual_text = {
source = "if_many",
spacing = 2,
format = function(diagnostic)
local diagnostic_message = {
[vim.diagnostic.severity.ERROR] = diagnostic.message,
[vim.diagnostic.severity.WARN] = diagnostic.message,
[vim.diagnostic.severity.INFO] = diagnostic.message,
[vim.diagnostic.severity.HINT] = diagnostic.message,
}
return diagnostic_message[diagnostic.severity]
end,
},
})
local capabilities = require("blink.cmp").get_lsp_capabilities()
local servers = {
clangd = {},
html = {},
yamlls = {},
bashls = {},
pyright = {},
marksman = {},
rust_analyzer = {},
lua_ls = {
settings = {
Lua = {
completion = {
callSnippet = "Replace",
},
-- You can toggle below to ignore Lua_LS's noisy `missing-fields` warnings
-- diagnostics = { disable = { 'missing-fields' } },
},
},
},
}
for server_name, server in pairs(servers) do
server.capabilities = vim.tbl_deep_extend("force", {}, capabilities, server.capabilities or {})
require("lspconfig")[server_name].setup(server)
end
end,
},
{
"stevearc/conform.nvim",
opts = {
formatters_by_ft = {
lua = { "stylua" },
-- Conform will run multiple formatters sequentially
python = { "black" },
-- You can customize some of the format options for the filetype (:help conform.format)
rust = { "rustfmt", lsp_format = "fallback" },
-- Conform will run the first available formatter
javascript = { "prettier", stop_after_first = true },
},
format_on_save = {
-- These options will be passed to conform.format()
timeout_ms = 500,
lsp_format = "fallback",
},
},
},
{ -- Autocompletion
"saghen/blink.cmp",
event = "VimEnter",
version = "1.*",
dependencies = {
-- Snippet Engine
{
"L3MON4D3/LuaSnip",
version = "2.*",
build = (function()
-- Build Step is needed for regex support in snippets.
-- This step is not supported in many windows environments.
-- Remove the below condition to re-enable on windows.
if vim.fn.has("win32") == 1 or vim.fn.executable("make") == 0 then
return
end
return "make install_jsregexp"
end)(),
dependencies = {},
opts = {},
},
"folke/lazydev.nvim",
},
--- @module 'blink.cmp'
--- @type blink.cmp.Config
opts = {
keymap = {
preset = "default",
},
appearance = {
nerd_font_variant = "mono",
},
completion = {
-- By default, you may press `<c-space>` to show the documentation.
-- Optionally, set `auto_show = true` to show the documentation after a delay.
documentation = { auto_show = false, auto_show_delay_ms = 500 },
},
sources = {
default = { "lsp", "buffer", "path", "snippets", "lazydev" },
providers = {
lazydev = { module = "lazydev.integrations.blink", score_offset = 100 },
},
},
snippets = { preset = "luasnip" },
fuzzy = { implementation = "prefer_rust_with_warning" },
-- Shows a signature help window while you type arguments for a function
signature = { enabled = true },
},
},
{ -- Highlight, edit, and navigate code
"nvim-treesitter/nvim-treesitter",
build = ":TSUpdate",
main = "nvim-treesitter.configs", -- Sets main module to use for opts
opts = {
ensure_installed = {
"bash",
"c",
"diff",
"html",
"lua",
"luadoc",
"markdown",
"markdown_inline",
"query",
"vim",
"vimdoc",
"rust",
"python",
},
auto_install = true,
highlight = {
enable = true,
-- Some languages depend on vim's regex highlighting system (such as Ruby) for indent rules.
-- If you are experiencing weird indenting issues, add the language to
-- the list of additional_vim_regex_highlighting and disabled languages for indent.
additional_vim_regex_highlighting = { "ruby" },
},
indent = { enable = true, disable = { "ruby" } },
},
},
-- File explorer -------------------------------------------
{
"ibhagwan/fzf-lua",
dependencies = { "nvim-tree/nvim-web-devicons" },
opts = {},
},
{
"nvim-neo-tree/neo-tree.nvim",
branch = "v3.x",
dependencies = {
"nvim-lua/plenary.nvim",
"nvim-tree/nvim-web-devicons",
"MunifTanjim/nui.nvim",
},
opts = {
close_if_last_window = true,
popup_border_style = "rounded",
enable_git_status = true,
enable_diagnostics = true,
open_files_do_not_replace_types = { "terminal", "trouble", "qf" }, -- when opening files, do not use windows containing these filetypes or buftypes
sort_case_insensitive = false, -- used when sorting files and directories in the tree
sort_function = nil, -- use a custom function for sorting files and directories in the tree
default_component_configs = {
container = {
enable_character_fade = true,
},
indent = {
indent_size = 2,
padding = 1, -- extra padding on left hand side
-- indent guides
with_markers = true,
indent_marker = "",
last_indent_marker = "",
highlight = "NeoTreeIndentMarker",
-- expander config, needed for nesting files
with_expanders = nil, -- if nil and file nesting is enabled, will enable expanders
expander_collapsed = "",
expander_expanded = "",
expander_highlight = "NeoTreeExpander",
},
icon = {
folder_closed = "",
folder_open = "",
folder_empty_open = "",
folder_empty = "󰜌",
provider = function(icon, node, state) -- default icon provider utilizes nvim-web-devicons if available
if node.type == "file" or node.type == "terminal" then
local success, web_devicons = pcall(require, "nvim-web-devicons")
local name = node.type == "terminal" and "terminal" or node.name
if success then
local devicon, hl = web_devicons.get_icon(name)
icon.text = devicon or icon.text
icon.highlight = hl or icon.highlight
end
end
end,
-- The next two settings are only a fallback, if you use nvim-web-devicons and configure default icons there
-- then these will never be used.
default = "*",
highlight = "NeoTreeFileIcon",
},
modified = {
symbol = "[+]",
highlight = "NeoTreeModified",
},
name = {
trailing_slash = false,
use_git_status_colors = true,
highlight = "NeoTreeFileName",
},
git_status = {
symbols = {
-- Change type
added = "", -- or "✚", but this is redundant info if you use git_status_colors on the name
modified = "", -- or "", but this is redundant info if you use git_status_colors on the name
deleted = "", -- this can only be used in the git_status source
renamed = "󰁕", -- this can only be used in the git_status source
-- Status type
untracked = "",
ignored = "",
unstaged = "󰄱",
staged = "",
conflict = "",
},
},
-- -- If you don't want to use these columns, you can set `enabled = false` for each of them individually
-- file_size = {
-- enabled = true,
-- required_width = 64, -- min width of window required to show this column
-- },
-- type = {
-- enabled = true,
-- required_width = 122, -- min width of window required to show this column
-- },
-- last_modified = {
-- enabled = true,
-- required_width = 88, -- min width of window required to show this column
-- },
-- created = {
-- enabled = true,
-- required_width = 110, -- min width of window required to show this column
-- },
-- symlink_target = {
-- enabled = false,
-- },
},
commands = {
parent_or_close = function(state)
local node = state.tree:get_node()
if (node.type == "directory" or node:has_children()) and node:is_expanded() then
state.commands.toggle_node(state)
else
require("neo-tree.ui.renderer").focus_node(state, node:get_parent_id())
end
end,
child_or_open = function(state)
local node = state.tree:get_node()
if node.type == "directory" or node:has_children() then
if not node:is_expanded() then -- if unexpanded, expand
state.commands.toggle_node(state)
else -- if expanded and has children, seleect the next child
require("neo-tree.ui.renderer").focus_node(state, node:get_child_ids()[1])
end
else -- if not a directory just open it
state.commands.open(state)
end
end,
copy_selector = function(state)
local node = state.tree:get_node()
local filepath = node:get_id()
local filename = node.name
local modify = vim.fn.fnamemodify
local results = {
e = { val = modify(filename, ":e"), msg = "Extension only" },
f = { val = filename, msg = "Filename" },
F = { val = modify(filename, ":r"), msg = "Filename w/o extension" },
h = { val = modify(filepath, ":~"), msg = "Path relative to Home" },
p = { val = modify(filepath, ":."), msg = "Path relative to CWD" },
P = { val = filepath, msg = "Absolute path" },
}
local messages = {
{ "\nChoose to copy to clipboard:\n", "Normal" },
}
for i, result in pairs(results) do
if result.val and result.val ~= "" then
vim.list_extend(messages, {
{ ("%s."):format(i), "Identifier" },
{ (" %s: "):format(result.msg) },
{ result.val, "String" },
{ "\n" },
})
end
end
vim.api.nvim_echo(messages, false, {})
local result = results[vim.fn.getcharstr()]
if result and result.val and result.val ~= "" then
vim.notify("Copied: " .. result.val)
vim.fn.setreg("+", result.val)
end
end,
},
window = {
position = "float",
width = 40,
mappings = {
["<space>"] = false, -- disable space until we figure out which-key disabling
h = "parent_or_close",
l = "child_or_open",
Y = "copy_selector",
},
},
},
},
-- GUI ------------------------------------------------------
{
"mrjones2014/smart-splits.nvim",
opts = {
at_edge = "stop",
},
},
{
-- Set lualine as statusline
"nvim-lualine/lualine.nvim",
opts = {
options = {
icons_enabled = true,
theme = "nightly_cm",
component_separators = "",
section_separators = "",
globalstatus = true,
},
sections = {
lualine_a = { "mode" },
lualine_b = { "branch", "diff" },
lualine_c = { "diagnostics", "filename" },
lualine_x = {
{
-- Lsp server name .
function()
local msg = ""
local clients = vim.lsp.get_clients({ bufnr = 0 })
if next(clients) == nil then
return msg
end
for _, client in ipairs(clients) do
msg = msg .. client.name .. ", "
end
if msg == "" then
return msg
end
return msg:sub(1, -3)
end,
color = { fg = "#c6c6c6" },
},
},
lualine_y = { "filetype" },
lualine_z = { "progress" },
},
},
},
{
"lewis6991/gitsigns.nvim",
opts = {
signs = {
add = { text = "" },
change = { text = "" },
delete = { text = "" },
topdelete = { text = "" },
changedelete = { text = "" },
untracked = { text = "" },
},
preview_config = {
-- Options passed to nvim_open_win
border = "solid",
style = "minimal",
relative = "cursor",
row = 0,
col = 1,
},
},
},
{
"lukas-reineke/indent-blankline.nvim",
main = "ibl",
opts = {
scope = {
show_end = false,
},
},
},
{
"norcalli/nvim-colorizer.lua",
opts = {},
},
{
"folke/which-key.nvim",
event = "VeryLazy",
init = function()
vim.o.timeout = true
vim.o.timeoutlen = 300
end,
opts = {},
},
{
"stevearc/dressing.nvim",
opts = {},
},
-- Utils ---------------------------------------------------
{
"max397574/better-escape.nvim",
opts = { timeout = 300 },
},
{ "numToStr/Comment.nvim", opts = {} },
{
"folke/todo-comments.nvim",
dependencies = { "nvim-lua/plenary.nvim" },
opts = {},
},
{
"phaazon/hop.nvim",
branch = "v2", -- optional but strongly recommended
opts = {
keys = "etovxqpdygfblzhckisuran",
},
},
{
"folke/trouble.nvim",
dependencies = { "nvim-tree/nvim-web-devicons" },
opts = {},
},
}, {})
-- resizing splits
vim.keymap.set({ "n", "t" }, "<C-A-h>", require("smart-splits").resize_left, { desc = "Resize left" })
vim.keymap.set({ "n", "t" }, "<C-A-j>", require("smart-splits").resize_down, { desc = "Resize down" })
vim.keymap.set({ "n", "t" }, "<C-A-k>", require("smart-splits").resize_up, { desc = "Resize up" })
vim.keymap.set({ "n", "t" }, "<C-A-l>", require("smart-splits").resize_right, { desc = "Resize right" })
-- moving between splits
vim.keymap.set({ "n", "t" }, "<A-h>", require("smart-splits").move_cursor_left, { desc = "Move cursor left" })
vim.keymap.set({ "n", "t" }, "<A-j>", require("smart-splits").move_cursor_down, { desc = "Move cursor down" })
vim.keymap.set({ "n", "t" }, "<A-k>", require("smart-splits").move_cursor_up, { desc = "Move cursor up" })
vim.keymap.set({ "n", "t" }, "<A-l>", require("smart-splits").move_cursor_right, { desc = "Move cursor right" })
-- swapping buffers between windows
vim.keymap.set("n", "<leader><leader>h", require("smart-splits").swap_buf_left, { desc = "Swap buffer left" })
vim.keymap.set("n", "<leader><leader>j", require("smart-splits").swap_buf_down, { desc = "Swap buffer down" })
vim.keymap.set("n", "<leader><leader>k", require("smart-splits").swap_buf_up, { desc = "Swap buffer up" })
vim.keymap.set("n", "<leader><leader>l", require("smart-splits").swap_buf_right, { desc = "Swap buffer right" })
-- Yank, delete, and paste always use system clipboard
vim.keymap.set({ "n", "v" }, "y", '"+y', { noremap = true, silent = true })
vim.keymap.set({ "n", "v" }, "Y", '"+Y', { noremap = true, silent = true })
vim.keymap.set({ "n", "v" }, "d", '"+d', { noremap = true, silent = true })
-- vim.keymap.set({ 'n', 'v' }, 'x', '"+x', { noremap = true, silent = true })
vim.keymap.set({ "n", "v" }, "p", '"+p', { noremap = true, silent = true })
vim.keymap.set({ "n", "v" }, "P", '"+P', { noremap = true, silent = true })
-- Clear highlights on search when pressing <Esc> in normal mode
-- See `:help hlsearch`
vim.keymap.set("n", "<Esc>", "<cmd>nohlsearch<CR>")
--Other
vim.keymap.set("n", "s", ":HopWord<cr>", { desc = "hop", silent = true })
vim.keymap.set("n", "<tab>", ":tabNext<cr>", { desc = "Next tab", silent = true })
vim.keymap.set("n", "<C-s>", ":write<cr>", { desc = "Save", silent = true })
vim.keymap.set("n", "<C-q>", ":quit<cr>", { desc = "Quit", silent = true })
vim.keymap.set(
"n",
"<leader>e",
":Neotree filesystem reveal float toggle<cr>",
{ desc = "File explorer", silent = true }
)
vim.keymap.set("n", "<leader>d", ":Trouble diagnostics toggle<cr>", { desc = "Diagnostic view", silent = true })
vim.keymap.set("n", "<leader>f", ":FzfLua files cwd=~/<cr>", { desc = "Find file", silent = true })