Defer heavy modules and disable built-in plugins to reduce startup time
- Disable unused built-in plugins at top of init.lua - Defer treesitter and lsp to VimEnter - Use packadd for on-demand plugin loading (conform, lspconfig, fugitive) - Defer clipboard initialization to avoid provider check blocking - Wrap LSP/diagnostic keymaps in functions to defer module loading - Defer mini.icons setup to VimEnter Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
f0c6f0a1e7
commit
5d40b6c245
41
init.lua
41
init.lua
@ -1,5 +1,35 @@
|
||||
_G.nvim_start_time = (vim.uv or vim.loop).hrtime()
|
||||
|
||||
-- 禁用不需要的内置插件,减少启动时 source 的 plugin 文件
|
||||
vim.g.loaded_2html_plugin = 1
|
||||
vim.g.loaded_getscript = 1
|
||||
vim.g.loaded_getscriptPlugin = 1
|
||||
vim.g.loaded_gzip = 1
|
||||
vim.g.loaded_logipat = 1
|
||||
vim.g.loaded_matchit = 1
|
||||
vim.g.loaded_matchparen = 1
|
||||
vim.g.loaded_netrw = 1
|
||||
vim.g.loaded_netrwFileHandlers = 1
|
||||
vim.g.loaded_netrwPlugin = 1
|
||||
vim.g.loaded_netrwSettings = 1
|
||||
vim.g.loaded_rrhelper = 1
|
||||
vim.g.loaded_spellfile_plugin = 1
|
||||
vim.g.loaded_tar = 1
|
||||
vim.g.loaded_tarPlugin = 1
|
||||
vim.g.loaded_tutor_mode_plugin = 1
|
||||
vim.g.loaded_vimball = 1
|
||||
vim.g.loaded_vimballPlugin = 1
|
||||
vim.g.loaded_zip = 1
|
||||
vim.g.loaded_zipPlugin = 1
|
||||
vim.g.loaded_rplugin = 1
|
||||
vim.g.loaded_tohtml = 1
|
||||
vim.g.loaded_syntax = 1
|
||||
vim.g.loaded_synmenu = 1
|
||||
vim.g.loaded_optwin = 1
|
||||
vim.g.loaded_compiler = 1
|
||||
vim.g.loaded_bugreport = 1
|
||||
vim.g.loaded_ftplugin = 1
|
||||
|
||||
require("vim._core.ui2").enable({})
|
||||
|
||||
require("options")
|
||||
@ -7,8 +37,15 @@ require("keymaps")
|
||||
require("autocmds")
|
||||
require("usercmds")
|
||||
require("pack")
|
||||
require("treesitter")
|
||||
require("lsp")
|
||||
|
||||
vim.g.moonflyTransparent = true
|
||||
vim.cmd("colorscheme catppuccin-mocha")
|
||||
|
||||
-- 延迟加载 heavy 模块,避免阻塞启动
|
||||
vim.api.nvim_create_autocmd("VimEnter", {
|
||||
once = true,
|
||||
callback = function()
|
||||
require("treesitter")
|
||||
require("lsp")
|
||||
end,
|
||||
})
|
||||
|
||||
19
lua/lsp.lua
19
lua/lsp.lua
@ -2,6 +2,7 @@ local lazy = require("lazy")
|
||||
|
||||
-- conform.nvim - BufWritePre 懒加载
|
||||
local function setup_conform()
|
||||
vim.cmd.packadd("conform.nvim")
|
||||
local function biome_or_prettier()
|
||||
if vim.fs.find({ "biome.json", "biome.jsonc" }, { upward = true, stop = vim.uv.os_homedir() })[1] then
|
||||
return { "biome-check", "biome", stop_after_first = true }
|
||||
@ -44,6 +45,7 @@ end)
|
||||
|
||||
-- mason + LSP 配置延迟到 VimEnter,避免启动时加载
|
||||
lazy.on_event("lsp", "VimEnter", "*", function()
|
||||
vim.cmd.packadd("nvim-lspconfig")
|
||||
require("mason").setup()
|
||||
|
||||
local capabilities = vim.lsp.protocol.make_client_capabilities()
|
||||
@ -73,15 +75,15 @@ lazy.on_event("lsp", "VimEnter", "*", function()
|
||||
})
|
||||
end)
|
||||
|
||||
-- LSP keymaps
|
||||
vim.keymap.set("n", "gd", vim.lsp.buf.definition, { desc = "Go to definition" })
|
||||
vim.keymap.set("n", "gh", vim.lsp.buf.hover, { desc = "Hover" })
|
||||
-- LSP keymaps(用 function 包装延迟 vim.lsp/vim.diagnostic 模块加载)
|
||||
vim.keymap.set("n", "gd", function() vim.lsp.buf.definition() end, { desc = "Go to definition" })
|
||||
vim.keymap.set("n", "gh", function() vim.lsp.buf.hover() end, { desc = "Hover" })
|
||||
vim.keymap.set("n", "<leader>fm", function()
|
||||
lazy.load("conform", setup_conform)
|
||||
require("conform").format({ lsp_fallback = true })
|
||||
end, { desc = "Format buffer" })
|
||||
vim.keymap.set("n", "df", vim.diagnostic.open_float, { desc = "Show line diagnostics" })
|
||||
vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, { desc = "Code action" })
|
||||
vim.keymap.set("n", "df", function() vim.diagnostic.open_float() end, { desc = "Show line diagnostics" })
|
||||
vim.keymap.set("n", "<leader>ca", function() vim.lsp.buf.code_action() end, { desc = "Code action" })
|
||||
|
||||
local diagnostic_goto = function(next, severity)
|
||||
return function()
|
||||
@ -100,4 +102,9 @@ vim.keymap.set("n", "[e", diagnostic_goto(false, "ERROR"), { desc = "Prev Error"
|
||||
vim.keymap.set("n", "]w", diagnostic_goto(true, "WARN"), { desc = "Next Warning" })
|
||||
vim.keymap.set("n", "[w", diagnostic_goto(false, "WARN"), { desc = "Prev Warning" })
|
||||
|
||||
vim.diagnostic.config({ virtual_text = true })
|
||||
vim.api.nvim_create_autocmd("VimEnter", {
|
||||
once = true,
|
||||
callback = function()
|
||||
vim.diagnostic.config({ virtual_text = true })
|
||||
end,
|
||||
})
|
||||
|
||||
@ -28,7 +28,10 @@ vim.opt.undofile = true
|
||||
|
||||
vim.opt.completeopt = "menuone,noselect,fuzzy,nosort"
|
||||
vim.opt.shortmess:append("c")
|
||||
vim.opt.clipboard:append("unnamedplus")
|
||||
-- 延迟初始化 clipboard,避免启动时 provider 检测阻塞(SSH 环境尤其明显)
|
||||
vim.schedule(function()
|
||||
vim.opt.clipboard:append("unnamedplus")
|
||||
end)
|
||||
vim.opt.isfname:append("@-@")
|
||||
vim.opt.scrolloff = 8
|
||||
|
||||
|
||||
24
lua/pack.lua
24
lua/pack.lua
@ -8,7 +8,7 @@ vim.pack.add({
|
||||
"https://github.com/mason-org/mason.nvim",
|
||||
"https://github.com/stevearc/conform.nvim",
|
||||
"https://github.com/tpope/vim-fugitive",
|
||||
})
|
||||
}, { load = false })
|
||||
|
||||
-- mini.starter 启动页
|
||||
local starter = require("mini.starter")
|
||||
@ -90,10 +90,14 @@ end, function()
|
||||
MiniFiles.reveal_cwd()
|
||||
end, { desc = "Toggle into currently opened file" })
|
||||
|
||||
-- mini.icons - 启动时加载
|
||||
lazy.load("icons", function()
|
||||
require("mini.icons").setup()
|
||||
end)
|
||||
-- mini.icons - 首次需要时加载(VimEnter 后延迟)
|
||||
vim.api.nvim_create_autocmd("VimEnter", {
|
||||
once = true,
|
||||
callback = function()
|
||||
require("mini.icons").setup()
|
||||
lazy._loaded["icons"] = true
|
||||
end,
|
||||
})
|
||||
|
||||
-- mini.notify - 首次 vim.notify 调用时加载
|
||||
vim.notify = function(msg, level, opts)
|
||||
@ -140,7 +144,7 @@ lazy.on_keys("pick", "<leader>fw", "n", load_pick, function()
|
||||
require("mini.pick").builtin.grep_live()
|
||||
end, { desc = "Live grep in project" })
|
||||
|
||||
vim.keymap.set("n", "<leader>ds", vim.diagnostic.setloclist, { desc = "LSP diagnostic loclist" })
|
||||
vim.keymap.set("n", "<leader>ds", function() vim.diagnostic.setloclist() end, { desc = "LSP diagnostic loclist" })
|
||||
|
||||
lazy.on_keys("pick", "<leader>sk", "n", load_pick, function()
|
||||
require("mini.extra").pickers.keymaps()
|
||||
@ -312,11 +316,15 @@ vim.keymap.set("n", "<leader>gD", function()
|
||||
end, { desc = "Git file history" })
|
||||
|
||||
-- vim-fugitive - 按键触发
|
||||
lazy.on_keys("fugitive", "<leader>gg", "n", nil, function()
|
||||
local function load_fugitive()
|
||||
vim.cmd.packadd("vim-fugitive")
|
||||
end
|
||||
|
||||
lazy.on_keys("fugitive", "<leader>gg", "n", load_fugitive, function()
|
||||
vim.cmd("tabnew | Git | only")
|
||||
end, { desc = "Fugitive Full Page New Tab" })
|
||||
|
||||
lazy.on_keys("fugitive", "<leader>gd", "n", nil, function()
|
||||
lazy.on_keys("fugitive", "<leader>gd", "n", load_fugitive, function()
|
||||
vim.cmd("Gvdiffsplit")
|
||||
end, { desc = "Git diff split" })
|
||||
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
local treesitter = require("nvim-treesitter")
|
||||
|
||||
local ensure_installed = {
|
||||
"lua",
|
||||
"vim",
|
||||
@ -26,27 +24,35 @@ local ensure_installed = {
|
||||
"make",
|
||||
}
|
||||
|
||||
-- 延迟安装 parser,避免启动时阻塞
|
||||
vim.defer_fn(function()
|
||||
treesitter.install(ensure_installed)
|
||||
end, 100)
|
||||
vim.api.nvim_create_autocmd("VimEnter", {
|
||||
once = true,
|
||||
callback = function()
|
||||
vim.cmd.packadd("nvim-treesitter")
|
||||
local treesitter = require("nvim-treesitter")
|
||||
|
||||
vim.api.nvim_create_autocmd("FileType", {
|
||||
pattern = "*",
|
||||
callback = function(args)
|
||||
local buf = args.buf
|
||||
local ft = vim.bo[buf].filetype
|
||||
-- 延迟安装 parser,避免阻塞
|
||||
vim.defer_fn(function()
|
||||
treesitter.install(ensure_installed)
|
||||
end, 100)
|
||||
|
||||
local lang = vim.treesitter.language.get_lang(ft)
|
||||
if not lang then
|
||||
return
|
||||
end
|
||||
vim.api.nvim_create_autocmd("FileType", {
|
||||
pattern = "*",
|
||||
callback = function(args)
|
||||
local buf = args.buf
|
||||
local ft = vim.bo[buf].filetype
|
||||
|
||||
local ok_add = pcall(vim.treesitter.language.add, lang)
|
||||
if not ok_add then
|
||||
return
|
||||
end
|
||||
local lang = vim.treesitter.language.get_lang(ft)
|
||||
if not lang then
|
||||
return
|
||||
end
|
||||
|
||||
pcall(vim.treesitter.start, buf, lang)
|
||||
local ok_add = pcall(vim.treesitter.language.add, lang)
|
||||
if not ok_add then
|
||||
return
|
||||
end
|
||||
|
||||
pcall(vim.treesitter.start, buf, lang)
|
||||
end,
|
||||
})
|
||||
end,
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user