-- ============================================================================ -- LSP 语言服务器 -- ============================================================================ return { -- Mason: 外部工具管理 (:Mason 打开管理界面) { "williamboman/mason.nvim", cmd = "Mason", opts = {}, }, -- Mason 与 LSP 桥接,自动安装指定服务器 { "williamboman/mason-lspconfig.nvim", dependencies = { "williamboman/mason.nvim" }, opts = { ensure_installed = { "lua_ls", -- Lua "bashls", -- Bash "clangd", -- C/C++ "neocmake", -- CMake -- rust_analyzer 由 rustup 管理,不通过 Mason }, }, }, -- lspsaga: LSP UI 增强(hover 滚动、智能定位、诊断、重命名等) -- 替代手写 hover 滚动逻辑,自带 VSCode 风格滚动键 /// { "nvimdev/lspsaga.nvim", event = "LspAttach", dependencies = { "nvim-treesitter/nvim-treesitter", "nvim-tree/nvim-web-devicons", }, opts = { lightbulb = { enable = false }, hover = { max_width = 0.6, max_height = 0.8, }, ui = { border = "rounded", code_action = "bulb", }, scroll_preview = { scroll_down = "", scroll_up = "", }, }, }, -- LSP 配置(Neovim 0.11+ vim.lsp.config API) { "neovim/nvim-lspconfig", event = { "BufReadPre", "BufNewFile" }, dependencies = { "williamboman/mason.nvim", "williamboman/mason-lspconfig.nvim", "saghen/blink.cmp", "nvimdev/lspsaga.nvim", }, config = function() local caps = require("blink.cmp").get_lsp_capabilities() -- LSP 附加时绑定 keymap + 开 inlay hints vim.api.nvim_create_autocmd("LspAttach", { callback = function(args) local map = function(keys, func, desc) vim.keymap.set("n", keys, func, { buffer = args.buf, desc = desc }) end map("gd", vim.lsp.buf.definition, "跳转到定义") map("gr", vim.lsp.buf.references, "查找引用") map("K", "Lspsaga hover_doc", "悬浮文档") map("rn", "Lspsaga rename", "重命名") map("", "Lspsaga code_action", "代码操作") map("d", "Lspsaga show_line_diagnostics", "行诊断") map("[d", "Lspsaga diagnostic_jump_prev", "上一个诊断") map("]d", "Lspsaga diagnostic_jump_next", "下一个诊断") -- 开启 inlay hints vim.lsp.inlay_hint.enable(true, { bufnr = args.buf }) end, }) -- 诊断全局配置 vim.diagnostic.config({ virtual_text = false, signs = true, underline = true, update_in_insert = false, float = { border = "rounded" }, }) -- Lua vim.lsp.config['lua_ls'] = { cmd = { 'lua-language-server' }, filetypes = { 'lua' }, root_markers = { '.luarc.json', '.luarc.jsonc', '.git', 'init.lua' }, capabilities = caps, settings = { Lua = { runtime = { version = "LuaJIT" }, workspace = { checkThirdParty = false }, diagnostics = { globals = { "vim" } }, }, }, } -- Bash vim.lsp.config['bashls'] = { cmd = { 'bash-language-server', 'start' }, filetypes = { 'sh', 'bash' }, root_markers = { '.git' }, capabilities = caps, } -- C/C++ (clangd) vim.lsp.config['clangd'] = { cmd = { 'clangd' }, filetypes = { 'c', 'cpp', 'objc', 'objcpp' }, root_markers = { '.clangd', 'compile_commands.json', '.git' }, capabilities = caps, } -- CMake vim.lsp.config['neocmake'] = { cmd = { 'neocmakelsp', 'stdio' }, filetypes = { 'cmake' }, root_markers = { 'CMakeLists.txt', '.git' }, capabilities = caps, init_options = { format = { enable = true }, lint = { enable = true }, }, } -- 启用所有 LSP(Rust 由 rustaceanvim 管理) vim.lsp.enable({ 'lua_ls', 'bashls', 'clangd', 'neocmake' }) end, }, }