From 86598b431645895bd808eb2c15ba6b1eb4628a6c Mon Sep 17 00:00:00 2001 From: philw Date: Mon, 13 Jan 2025 17:58:09 +0100 Subject: Rewrite config in pure lua --- init.lua | 412 ++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 212 insertions(+), 200 deletions(-) (limited to 'init.lua') diff --git a/init.lua b/init.lua index d5e104d..d8813b9 100644 --- a/init.lua +++ b/init.lua @@ -1,6 +1,8 @@ ---@diagnostic disable: undefined-global, local option = vim.opt local keymap = vim.keymap +local api = vim.api +local fn = vim.fn vim.loader.enable() @@ -9,11 +11,15 @@ vim.g.netrw_liststyle = 3 vim.g.netrw_altv = 0 vim.g.netrw_winsize = 25 vim.g.netrw_browse_split = 4 - vim.g.rustfmt_autosave = 1 vim.g.mkdp_auto_start = 1 +vim.g.vimtex_view_method = "zathura" +vim.g.vimtex_compiler_method = "latexmk" +vim.g.vimtex_quickfix_mode = 0 +vim.g.tex_conceal = "abdmg" +vim.g.tex_flavor = "latex" +vim.g.gitgutter_set_sign_backgrounds = 1 ---Basic Editor Setup option.nu = true option.tabstop = 2 option.softtabstop = 2 @@ -31,232 +37,238 @@ option.signcolumn = "number" option.autoindent = true option.clipboard = "unnamedplus" option.termguicolors = true -option.fillchars = 'eob: ' +option.fillchars = "eob: " option.autoindent = true option.lazyredraw = false option.updatetime = 300 option.timeoutlen = 500 -vim.cmd [[autocmd BufWritePre lua vim.lsp.buf.format()]] - -vim.cmd([[ -augroup RunPfOnSave -autocmd! -autocmd BufWritePost *.js,*.ts,*.jsx,*json,*.tsx,*.css,*.html,*.yaml,*.md silent! !prettier --write % -autocmd BufWritePost *.tex silent! :VimtexCompile -augroup END -]]) +-- Format on save +api.nvim_create_autocmd( + "BufWritePre", + { + callback = function() + vim.lsp.buf.format() + end + } +) +-- Run prettier and compile tex on save +api.nvim_create_augroup("RunPfOnSave", {clear = true}) +api.nvim_create_autocmd( + "BufWritePost", + { + group = "RunPfOnSave", + pattern = {"*.js", "*.ts", "*.jsx", "*.json", "*.tsx", "*.css", "*.html", "*.yaml", "*.md"}, + command = "silent! !prettier --write %" + } +) +api.nvim_create_autocmd( + "BufWritePost", + { + group = "RunPfOnSave", + pattern = "*.tex", + command = "silent! VimtexCompile" + } +) -vim.cmd([[ -augroup AutoDeleteNetrwHiddenBuffers -au! -au FileType netrw setlocal bufhidden=wipe -augroup end -]]) +-- Auto-delete Netrw hidden buffers +api.nvim_create_augroup("AutoDeleteNetrwHiddenBuffers", {clear = true}) +api.nvim_create_autocmd( + "FileType", + { + group = "AutoDeleteNetrwHiddenBuffers", + pattern = "netrw", + callback = function() + vim.opt_local.bufhidden = "wipe" + end + } +) -vim.cmd([[ -function! ToggleVExplorer() -if exists("t:expl_buf_num") - let expl_win_num = bufwinnr(t:expl_buf_num) - if expl_win_num != -1 - let cur_win_id = win_getid() - exec expl_win_num . 'windo close' - let prev_win_num = win_id2win(cur_win_id) - if prev_win_num != 0 - exec prev_win_num . 'wincmd w' - endif - endif - unlet t:expl_buf_num +-- Toggle VExplorer +function ToggleVExplorer() + if vim.t.expl_buf_num then + local expl_win_num = fn.bufwinnr(vim.t.expl_buf_num) + if expl_win_num ~= -1 then + local cur_win_id = fn.win_getid() + vim.cmd(expl_win_num .. "windo close") + local prev_win_num = fn.win_id2win(cur_win_id) + if prev_win_num ~= 0 then + vim.cmd(prev_win_num .. "wincmd w") + end + end + vim.t.expl_buf_num = nil else - exec '1wincmd w' - Vexplore - let t:expl_buf_num = bufnr("%") - endif - endfunction - ]]) - - - -- Gui options - vim.g.vimtex_view_method = "zathura" - vim.g.vimtex_compiler_method = "latexmk" - vim.g.vimtex_quickfix_mode = 0 - vim.g.tex_conceal = "abdmg" - vim.g.tex_flavor = "latex" - vim.g.gitgutter_set_sign_backgrounds = 1 - - - 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", - lazypath, - }) - end + vim.cmd("1wincmd w") + vim.cmd("Vexplore") + vim.t.expl_buf_num = fn.bufnr("%") + end +end + +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", + lazypath + } + ) +end - vim.opt.rtp:prepend(lazypath) - require("lazy").setup({ +vim.opt.rtp:prepend(lazypath) +require("lazy").setup( + { "airblade/vim-gitgutter", {"lervag/vimtex", lazy = true}, - "norcalli/nvim-colorizer.lua", + {"norcalli/nvim-colorizer.lua"}, "nvim-treesitter/nvim-treesitter", { - "neovim/nvim-lspconfig", - lazy = false, - dependencies = { - { "ms-jpq/coq_nvim", branch = "coq" }, - { "ms-jpq/coq.artifacts", branch = "artifacts" }, - { 'ms-jpq/coq.thirdparty', branch = "3p" }, - "williamboman/mason.nvim", - "williamboman/mason-lspconfig.nvim", - }, - init = function() - vim.g.coq_settings = { - auto_start = 'shut-up' - } - end, - config = function() - end, + "neovim/nvim-lspconfig", + lazy = false, + dependencies = { + {"ms-jpq/coq_nvim", branch = "coq"}, + {"ms-jpq/coq.artifacts", branch = "artifacts"}, + {"ms-jpq/coq.thirdparty", branch = "3p"}, + {"williamboman/mason.nvim"}, + {"williamboman/mason-lspconfig.nvim"} + }, + init = function() + vim.g.coq_settings = {auto_start = "shut-up"} + end }, - 'nvim-telescope/telescope.nvim', - lazy = true, - tag = '0.1.5', - dependencies = { 'nvim-lua/plenary.nvim' }, { - 'nvim-telescope/telescope-fzf-native.nvim', - lazy = true, - build = - 'cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release && cmake --build build --config Release && cmake --install build --prefix build', - keys = { - { "/", false }, - { "f", "Telescope find_files", desc = "Find Files" }, - { "g", "Telescope live_grep", desc = "Grep" }, - { "b", "Telescope buffers", desc = "Grep" }, - { "s", "Telescope buffers", desc = "Grep" }, - } - }}, + "nvim-telescope/telescope.nvim", + lazy = false, + tag = "0.1.5", + dependencies = {"nvim-lua/plenary.nvim"} + }, { - 'windwp/nvim-autopairs', - event = "InsertEnter", - opts = {} + "nvim-telescope/telescope-fzf-native.nvim", + lazy = false, + build = "cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release && cmake --build build --config Release && cmake --install build --prefix build", + }, + { + "windwp/nvim-autopairs", + event = "InsertEnter", + opts = {} } + } ) +require("colorizer").setup() - require("colorizer").setup() - - vim.api.nvim_create_autocmd("BufWritePre", { +vim.api.nvim_create_autocmd( + "BufWritePre", + { callback = function() - vim.lsp.buf.format() - end, - }) + vim.lsp.buf.format() + end + } +) - local lsp_servers = { - rust_analyzer = { - imports = { +local lsp_servers = { + rust_analyzer = { + imports = { granularity = { - group = 'module', + group = "module" }, - prefix = 'self', - }, - cargo = { + prefix = "self" + }, + cargo = { buildScripts = { - enable = true, - }, - }, - procMacro = { - enable = true, - }, + enable = true + } }, - } + procMacro = { + enable = true + } + } +} - require("mason").setup({ +require("mason").setup( + { ui = { - icons = { - package_installed = "✓", - package_pending = "➜", - package_uninstalled = "✗" - } + icons = { + package_installed = "✓", + package_pending = "➜", + package_uninstalled = "✗" + } } - }) - - local server_maps = function(opts) - end - - for lsp, settings in pairs(lsp_servers) do - require("lspconfig")[lsp].setup(coq.lsp_ensure_capabilities({ - on_attach = function(_, buffer) - server_maps({ buffer = buffer }) - end, - settings = settings, - })) - end - - vim.cmd([[ - highlight GitGutterAdd ctermbg=none guibg=none - highlight GitGutterDelete guibg=none ctermbg=none - highlight GitGutterChange guibg=none ctermbg=none - highlight GitGutterChangeAdd guibg=none ctermbg=none - highlight GitGutterChangeDelete guibg=none ctermbg=none - highlight NonText guifg=none - highlight EndOfBuffer ctermfg=none ctermbg=none - highlight Normal ctermbg=none guibg=none - highlight signcolumn ctermbg=none guibg=none - highlight StatusLine ctermbg=none guibg=none - highlight DiagnosticError ctermbg=none guibg=none - highlight DiagnosticWarn ctermbg=none guibg=none - highlight DiagnosticInfo ctermbg=none guibg=none - highlight DiagnosticHint ctermbg=none guibg=none - highlight Pmenu ctermbg=none guibg=none - - highlight DiagnosticVirtualError ctermbg=none guibg=none - highlight DiagnosticVirtualWarn ctermbg=none guibg=none - highlight DiagnosticVirtualInfo ctermbg=none guibg=none - highlight DiagnosticVirtualHint ctermbg=none guibg=none - - highlight LspFloatWinNormal ctermbg=none guibg=none - highlight NormalFloat ctermbg=none guibg=none - - highlight LineNr ctermbg=none guibg=none - highlight StatusLineNc ctermbg=none guibg=none - highlight StatusLineNc ctermfg=none guifg=none - highlight StatusLine ctermbg=none guibg=none - highlight StatusLine ctermfg=none guifg=none - - highlight NvimTreeWinSeparator ctermfg=none guifg=none - highlight CursorColumn ctermfg=none guifg=none - highlight CursorLine ctermbg=none guibg=none - - highlight VertSplit ctermbg=none guibg=none - - highlight DiagnosticVirtualTextError ctermbg=none guibg=none - highlight DiagnosticVirtualTextWarn ctermbg=none guibg=none - highlight DiagnosticVirtualTextInfo ctermbg=none guibg=none - highlight DiagnosticVirtualTextHint ctermbg=none guibg=none - ]]) - - - - vim.g.mapleader = " " - keymap.set("n", "e", ":call ToggleVExplorer() ") - keymap.set("n", "z", [[:%s/\<\>//gI]]) - keymap.set("n", "a", "ggVG") - keymap.set("n", "y", "cc") - keymap.set("n", "", "yy") - keymap.set("n", "", "p") - keymap.set("n", "", "u") - keymap.set("v", "J", ":m '>+1gv=gv") - keymap.set("n", "j", "5j") - keymap.set("n", "k", "5k") - keymap.set("v", "K", ":m '<-2gv=gv") - keymap.set("n", "ca", "Lspsaga code_action") - keymap.set("n", "f", "Telescope find_files") - keymap.set("n", "g", "Telescope live_grep") - keymap.set("n", "t", "Lspsaga term_toggle ") - keymap.set("n", "d", vim.lsp.buf.definition, opts) -- goto def - keymap.set("n", "k", vim.lsp.buf.hover, opts) + } +) +local server_maps = function(opts) +end + +for lsp, settings in pairs(lsp_servers) do + require("lspconfig")[lsp].setup( + coq.lsp_ensure_capabilities( + { + on_attach = function(_, buffer) + server_maps({buffer = buffer}) + end, + settings = settings + } + ) + ) +end + +local highlights = { + {"GitGutterAdd", {bg = "none"}}, + {"GitGutterDelete", {bg = "none"}}, + {"GitGutterChange", {bg = "none"}}, + {"GitGutterChangeAdd", {bg = "none"}}, + {"GitGutterChangeDelete", {bg = "none"}}, + {"NonText", {fg = "none"}}, + {"EndOfBuffer", {fg = "none", bg = "none"}}, + {"Normal", {bg = "none"}}, + {"SignColumn", {bg = "none"}}, + {"StatusLine", {bg = "none", fg = "none"}}, + {"StatusLineNc", {bg = "none", fg = "none"}}, + {"NvimTreeWinSeparator", {fg = "none"}}, + {"CursorColumn", {fg = "none"}}, + {"CursorLine", {bg = "none"}}, + {"VertSplit", {bg = "none"}}, + {"DiagnosticError", {bg = "none"}}, + {"DiagnosticWarn", {bg = "none"}}, + {"DiagnosticInfo", {bg = "none"}}, + {"DiagnosticHint", {bg = "none"}}, + {"Pmenu", {bg = "none"}}, + {"DiagnosticVirtualTextError", {bg = "none"}}, + {"DiagnosticVirtualTextWarn", {bg = "none"}}, + {"DiagnosticVirtualTextInfo", {bg = "none"}}, + {"DiagnosticVirtualTextHint", {bg = "none"}}, + {"LspFloatWinNormal", {bg = "none"}}, + {"NormalFloat", {bg = "none"}}, + {"LineNr", {bg = "none"}}, + {"DiagnosticVirtualError", {bg = "none"}}, + {"DiagnosticVirtualWarn", {bg = "none"}}, + {"DiagnosticVirtualInfo", {bg = "none"}}, + {"DiagnosticVirtualHint", {bg = "none"}} +} + +for _, hl in ipairs(highlights) do + vim.api.nvim_set_hl(0, hl[1], hl[2]) +end + +vim.g.mapleader = " " + +keymap.set("n", "e", ToggleVExplorer, {desc = "Toggle VExplorer"}) +keymap.set("n", "z", [[:%s/\<\>//gI]]) +keymap.set("n", "a", "ggVG") +keymap.set("n", "y", "cc") +keymap.set("n", "", "yy") +keymap.set("n", "", "p") +keymap.set("n", "", "u") +keymap.set("v", "J", ":m '>+1gv=gv") +keymap.set("n", "j", "5j") +keymap.set("n", "k", "5k") +keymap.set("v", "K", ":m '<-2gv=gv") +keymap.set("n", "f", "Telescope find_files") +keymap.set("n", "g", "Telescope live_grep") +keymap.set("n", "d", vim.lsp.buf.definition, opts) +keymap.set("n", "k", vim.lsp.buf.hover, opts) -- cgit v1.2.3