return { "neovim/nvim-lspconfig", dependencies = { "hrsh7th/cmp-nvim-lsp", }, config = function() local capabilities = require('cmp_nvim_lsp').default_capabilities() -- 統一處理 LspAttach (當 LSP 連接到緩衝區時觸發) vim.api.nvim_create_autocmd('LspAttach', { group = vim.api.nvim_create_augroup('UserLspConfig', {}), callback = function(ev) local bufnr = ev.buf local client = vim.lsp.get_client_by_id(ev.data.client_id) -- 啟動 lsp_signature require('lsp_signature').on_attach({}, bufnr) -- 啟用 omnifunc vim.bo[bufnr].omnifunc = 'v:lua.vim.lsp.omnifunc' -- 按鍵對映優化 local opts = { buffer = bufnr, noremap = true, silent = true } local map = vim.keymap.set map('n', 'gd', vim.lsp.buf.definition, opts) map('n', 'gt', 'tab splitlua vim.lsp.buf.definition()', opts) map('n', 'gv', 'vsplitlua vim.lsp.buf.definition()', opts) map('n', 'K', vim.lsp.buf.hover, opts) map('n', 'gi', vim.lsp.buf.implementation, opts) map('n', '', vim.lsp.buf.signature_help, opts) map('n', 'D', vim.lsp.buf.type_definition, opts) map('n', 'rn', vim.lsp.buf.rename, opts) map('n', 'ca', vim.lsp.buf.code_action, opts) map('n', 'gr', vim.lsp.buf.references, opts) map('n', 'ge', vim.diagnostic.open_float, opts) -- 使用現代 API map('n', 'do', vim.diagnostic.open_float, opts) map('n', 'd[', vim.diagnostic.goto_prev, opts) map('n', 'd]', vim.diagnostic.goto_next, opts) map('n', 'gf', function() vim.lsp.buf.format { async = true } end, opts) end, }) -- 伺服器列表 local servers = { 'gopls', 'ts_ls', 'rust_analyzer', 'bashls', 'svelte', 'pylsp', 'zls', 'lua_ls', 'clangd', 'pyright' } -- 批量配置伺服器能力 for _, lsp in ipairs(servers) do vim.lsp.config(lsp, { capabilities = capabilities, }) end -- 批量啟用伺服器 vim.lsp.enable(servers) end }