Files
dotfiles/nvim/lua/plugins/lsp.lua
2026-06-18 11:36:12 +08:00

101 lines
3.5 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- ============================================================================
-- 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
},
},
},
-- 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",
},
config = function()
-- LSP 附加时自动开启 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", vim.lsp.buf.hover, "悬浮文档")
map("<leader>rn", vim.lsp.buf.rename, "重命名")
map("<M-CR>", vim.lsp.buf.code_action, "代码操作")
map("<leader>d", vim.diagnostic.open_float, "行诊断")
map("[d", vim.diagnostic.goto_prev, "上一个诊断")
map("]d", vim.diagnostic.goto_next, "下一个诊断")
-- 开启 inlay hints
vim.lsp.inlay_hint.enable(true, { bufnr = args.buf })
end,
})
local caps = require("blink.cmp").get_lsp_capabilities()
-- Lua
vim.lsp.config("lua_ls", {
cmd = { "lua-language-server" },
capabilities = caps,
settings = {
Lua = {
runtime = { version = "Lua5.5" },
workspace = { checkThirdParty = false },
diagnostics = { globals = { "vim" } },
},
},
})
-- Bash
vim.lsp.config("bashls", {
cmd = { "bash-language-server", "start" },
capabilities = caps,
})
-- C/C++ (clangd)
vim.lsp.config("clangd", {
cmd = { "clangd" },
capabilities = caps,
})
-- CMake
vim.lsp.config("neocmake", {
cmd = { "neocmakelsp", "stdio" },
filetypes = { "cmake" },
root_dir = vim.fs.root(0, { ".git", "CMakeLists.txt" }),
single_file_support = true,
init_options = {
format = { enable = true },
lint = { enable = true },
},
})
-- 启用所有 LSPRust 由 rustaceanvim 管理,不在此启用)
vim.lsp.enable({ "lua_ls", "bashls", "clangd", "neocmake" })
end,
},
}