From 103961e8922c895f616f82df73278a957eb5ca9e Mon Sep 17 00:00:00 2001 From: Felipe Contreras Salinas Date: Thu, 21 Sep 2023 10:20:40 -0300 Subject: [PATCH] refactor and some lazy loading --- .gitmodules | 9 +- init.lua | 264 +++++++++++++----------- pack/general/opt/crates.nvim | 1 + pack/general/{start => opt}/nvim-dap | 0 pack/general/{start => opt}/nvim-dap-go | 0 pack/general/{start => opt}/nvim-dap-ui | 0 pack/general/start/crates.nvim | 1 - 7 files changed, 155 insertions(+), 120 deletions(-) create mode 160000 pack/general/opt/crates.nvim rename pack/general/{start => opt}/nvim-dap (100%) rename pack/general/{start => opt}/nvim-dap-go (100%) rename pack/general/{start => opt}/nvim-dap-ui (100%) delete mode 160000 pack/general/start/crates.nvim diff --git a/.gitmodules b/.gitmodules index eee33aa..15b4a30 100644 --- a/.gitmodules +++ b/.gitmodules @@ -62,13 +62,13 @@ path = pack/general/start/git-blame.nvim url = https://github.com/f-person/git-blame.nvim [submodule "nvim-dap"] - path = pack/general/start/nvim-dap + path = pack/general/opt/nvim-dap url = https://github.com/mfussenegger/nvim-dap [submodule "pack/general/start/nvim-dap-go"] - path = pack/general/start/nvim-dap-go + path = pack/general/opt/nvim-dap-go url = https://github.com/leoluz/nvim-dap-go [submodule "pack/general/start/nvim-dap-ui"] - path = pack/general/start/nvim-dap-ui + path = pack/general/opt/nvim-dap-ui url = https://github.com/rcarriga/nvim-dap-ui [submodule "pack/general/start/vim-helm"] path = pack/general/start/vim-helm @@ -109,3 +109,6 @@ [submodule "pack/general/start/lsp_lines.nvim"] path = pack/general/start/lsp_lines.nvim url = https://git.sr.ht/~whynothugo/lsp_lines.nvim +[submodule "pack/general/opt/crates.nvim"] + path = pack/general/opt/crates.nvim + url = https://github.com/Saecki/crates.nvim diff --git a/init.lua b/init.lua index fbe99c0..63140cc 100644 --- a/init.lua +++ b/init.lua @@ -1,3 +1,4 @@ +vim.loader.enable() ---- Aliases ---- local cmd = vim.cmd local g = vim.g @@ -115,8 +116,7 @@ vim.keymap.set("v", "<", "", ":nohlsearch") ---- Treesitter ---- -local ts = require("nvim-treesitter.configs") -ts.setup({ +require("nvim-treesitter.configs").setup({ auto_install = true, sync_install = false, ignore_install = {}, @@ -203,8 +203,7 @@ vim.api.nvim_create_autocmd({ "FileType" }, { }) ---- formatter.nvim ---- -local formatter = require("formatter") -formatter.setup({ +require("formatter").setup({ filetype = { lua = { require("formatter.filetypes.lua").stylua, @@ -220,6 +219,57 @@ formatter.setup({ } }) +---- LuaSnip +local luasnip = require("luasnip") +require("luasnip.loaders.from_lua").lazy_load() +luasnip.config.set_config({ + store_selection_keys = "", +}) +vim.keymap.set({ "i", "s" }, "", function() + if luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + else + vim.api.nvim_feedkeys( + vim.api.nvim_replace_termcodes("", true, false, true), + "n", -- noremap to avoid infinite recursion + true + ) + end +end, { silent = true }) +vim.keymap.set({ "i", "s" }, "", function() + if luasnip.jumpable(-1) then + luasnip.jump(-1) + end +end, { silent = true }) + +---- completion-nvim +local cmp = require("cmp") +cmp.setup({ + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) + end, + }, + window = { + -- completion = cmp.config.window.bordered(), + -- documentation = cmp.config.window.bordered(), + }, + mapping = cmp.mapping.preset.insert({ + [""] = cmp.mapping.scroll_docs(-4), + [""] = cmp.mapping.scroll_docs(4), + [""] = cmp.mapping.complete(), + [""] = cmp.mapping.abort(), + -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. + [""] = cmp.mapping.confirm({ select = false }), + }), + sources = cmp.config.sources({ + { name = "nvim_lsp" }, + { name = "luasnip" }, + { name = "path" }, + { name = "buffer" }, + { name = "crates" }, + }), +}) ---- LSP ---- local lsp = require("lspconfig") @@ -277,8 +327,21 @@ local servers = { tflint = {}, tsserver = {}, } --- Setup loop moved to the cmp section, since calling setup multiple times --- overrides the previous calls. + +local capabilities = require('cmp_nvim_lsp').default_capabilities() +lsp.util.default_config = vim.tbl_deep_extend("force", lsp.util.default_config, { capabilities = capabilities }) +for server, add_to_config in pairs(servers) do + local config = { + on_attach = on_lsp_attach, + flags = { + debounce_text_changes = 150, + }, + } + for k, v in pairs(add_to_config) do + config[k] = v + end + lsp[server].setup(config) +end -- Auto format on save vim.api.nvim_create_autocmd({ "BufWritePre" }, { @@ -298,7 +361,6 @@ vim.api.nvim_create_autocmd({ "BufWritePre" }, { end, }) ----- Plugins ---- ---- nvim-lint ---- local lint = require("lint") local clippy_format = "%E%f:%l:%c: %\\d%#:%\\d%# %.%\\{-}" @@ -385,73 +447,7 @@ for ft, ft_linters in pairs(linters) do }) end end ----- LuaSnip -local luasnip = require("luasnip") -require("luasnip.loaders.from_lua").lazy_load() -luasnip.config.set_config({ - store_selection_keys = "", -}) -vim.keymap.set({ "i", "s" }, "", function() - if luasnip.expand_or_jumpable() then - luasnip.expand_or_jump() - else - vim.api.nvim_feedkeys( - vim.api.nvim_replace_termcodes("", true, false, true), - "n", -- noremap to avoid infinite recursion - true - ) - end -end, { silent = true }) -vim.keymap.set({ "i", "s" }, "", function() - if luasnip.jumpable(-1) then - luasnip.jump(-1) - end -end, { silent = true }) ----- completion-nvim -local cmp = require("cmp") -cmp.setup({ - snippet = { - expand = function(args) - luasnip.lsp_expand(args.body) - end, - }, - window = { - -- completion = cmp.config.window.bordered(), - -- documentation = cmp.config.window.bordered(), - }, - mapping = cmp.mapping.preset.insert({ - [""] = cmp.mapping.scroll_docs(-4), - [""] = cmp.mapping.scroll_docs(4), - [""] = cmp.mapping.complete(), - [""] = cmp.mapping.abort(), - -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. - [""] = cmp.mapping.confirm({ select = false }), - }), - sources = cmp.config.sources({ - { name = "nvim_lsp" }, - { name = "luasnip" }, - { name = "path" }, - { name = "buffer" }, - { name = "crates" }, - }), -}) - --- Setup lspconfig. -local capabilities = vim.lsp.protocol.make_client_capabilities() -lsp.util.default_config = vim.tbl_deep_extend("force", lsp.util.default_config, { capabilities = capabilities }) -for server, add_to_config in pairs(servers) do - local config = { - on_attach = on_lsp_attach, - flags = { - debounce_text_changes = 150, - }, - } - for k, v in pairs(add_to_config) do - config[k] = v - end - lsp[server].setup(config) -end ---- gitblame local gitblame = require("gitblame") gitblame.setup({ @@ -515,30 +511,41 @@ vim.keymap.set("n", "cg", builtin.grep_string) vim.keymap.set("n", ":", builtin.commands) vim.keymap.set("n", "s", telescope.extensions.luasnip.luasnip) ---- nvim-dap -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 -vim.keymap.set("n", "dc", dap.continue) -vim.keymap.set("n", "do", dap.step_over) -vim.keymap.set("n", "di", dap.step_into) -vim.keymap.set("n", "dO", dap.step_out) -vim.keymap.set("n", "db", dap.toggle_breakpoint) -vim.keymap.set("n", "dB", function() - dap.set_breakpoint(vim.fn.input("Breakpoint condition: ")) -end) -local dapui = require("dapui") -dap.listeners.after.event_initialized["dapui_config"] = function() - dapui.open() -end -dap.listeners.before.event_terminated["dapui_config"] = function() - dapui.close() -end -dap.listeners.before.event_exited["dapui_config"] = function() - dapui.close() -end --- nvim-dap-go -require("dap-go").setup() +vim.g.dap_loaded = false +vim.api.nvim_create_autocmd({ "FileType" }, { + pattern = { "go" }, + callback = function() + vim.g.dap_loaded = true + vim.cmd("packadd nvim-dap") + vim.cmd("packadd nvim-dap-ui") + vim.cmd("packadd nvim-go") + + 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 debugger + vim.keymap.set("n", "dc", dap.continue) + vim.keymap.set("n", "do", dap.step_over) + vim.keymap.set("n", "di", dap.step_into) + vim.keymap.set("n", "dO", dap.step_out) + vim.keymap.set("n", "db", dap.toggle_breakpoint) + vim.keymap.set("n", "dB", function() + dap.set_breakpoint(vim.fn.input("Breakpoint condition: ")) + end) + local dapui = require("dapui") + dap.listeners.after.event_initialized["dapui_config"] = function() + dapui.open() + end + dap.listeners.before.event_terminated["dapui_config"] = function() + dapui.close() + end + dap.listeners.before.event_exited["dapui_config"] = function() + dapui.close() + end + -- nvim-dap-go + require("dap-go").setup() + end, +}) ---- web-devicons require("nvim-web-devicons").setup() @@ -549,31 +556,56 @@ vim.keymap.set('n', 'yy', 'y_', { remap = true }) vim.keymap.set('v', 'y', require('osc52').copy_visual) ---- crates -local crates = require('crates') -crates.setup() -vim.keymap.set('n', 'cv', crates.show_versions_popup) -vim.keymap.set('n', 'cf', crates.show_features_popup) -vim.keymap.set('n', 'cd', crates.show_dependencies_popup) -vim.keymap.set('n', 'cu', crates.update_crate) -vim.keymap.set('v', 'cu', crates.update_crates) -vim.keymap.set('n', 'ca', crates.update_all_crates) -vim.keymap.set('n', 'cD', crates.open_documentation) +vim.g.crates_loaded = false +vim.api.nvim_create_autocmd({ "BufEnter" }, { + pattern = { "Cargo.toml" }, + callback = function() + if vim.g.crates_loaded then + return + end + vim.g.crates_loaded = true + vim.cmd("packadd crates.nvim") + + local crates = require('crates') + crates.setup() + vim.keymap.set('n', 'cv', crates.show_versions_popup) + vim.keymap.set('n', 'cf', crates.show_features_popup) + vim.keymap.set('n', 'cd', crates.show_dependencies_popup) + vim.keymap.set('n', 'cu', crates.update_crate) + vim.keymap.set('v', 'cu', crates.update_crates) + vim.keymap.set('n', 'ca', crates.update_all_crates) + vim.keymap.set('n', 'cD', crates.open_documentation) + end, +}) ---- neotest -local neotest = require("neotest") -neotest.setup({ - adapters = { - require("neotest-rust") - } +vim.g.neotest_loaded = false +vim.api.nvim_create_autocmd({ "FileType" }, { + pattern = { "rust" }, + callback = function() + if vim.g.neotest_loaded then + return + end + vim.g.neotest_loaded = true + vim.cmd("packadd neotest") + vim.cmd("packadd neotest-rust") + + local neotest = require("neotest") + neotest.setup({ + adapters = { + require("neotest-rust") + } + }) + vim.keymap.set('n', 'tr', function() neotest.run.run() end) + vim.keymap.set('n', 'tf', function() neotest.run.run(vim.fn.expand("%")) end) + vim.keymap.set('n', 'ts', function() neotest.run.stop() end) + vim.keymap.set('n', 'tt', function() neotest.summary.toggle() end) + end }) -vim.keymap.set('n', 'tr', function() neotest.run.run() end) -vim.keymap.set('n', 'tf', function() neotest.run.run(vim.fn.expand("%")) end) -vim.keymap.set('n', 'ts', function() neotest.run.stop() end) -vim.keymap.set('n', 'tt', function() neotest.summary.toggle() end) + ---- lsp_lines require("lsp_lines").setup() -- Disable virtual_text since it's redundant due to lsp_lines. vim.diagnostic.config({ virtual_text = false, - virtual_lines = { only_current_line = true }, }) diff --git a/pack/general/opt/crates.nvim b/pack/general/opt/crates.nvim new file mode 160000 index 0000000..db629b5 --- /dev/null +++ b/pack/general/opt/crates.nvim @@ -0,0 +1 @@ +Subproject commit db629b5cfb2aa8de9e44efb795657297ee95ca91 diff --git a/pack/general/start/nvim-dap b/pack/general/opt/nvim-dap similarity index 100% rename from pack/general/start/nvim-dap rename to pack/general/opt/nvim-dap diff --git a/pack/general/start/nvim-dap-go b/pack/general/opt/nvim-dap-go similarity index 100% rename from pack/general/start/nvim-dap-go rename to pack/general/opt/nvim-dap-go diff --git a/pack/general/start/nvim-dap-ui b/pack/general/opt/nvim-dap-ui similarity index 100% rename from pack/general/start/nvim-dap-ui rename to pack/general/opt/nvim-dap-ui diff --git a/pack/general/start/crates.nvim b/pack/general/start/crates.nvim deleted file mode 160000 index d5caf28..0000000 --- a/pack/general/start/crates.nvim +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d5caf28aba49e81ac4099426231f3cf3c151013a