feat(lsp): 添加golang和rust支持
This commit is contained in:
@@ -34,7 +34,8 @@ return {
|
||||
scss = { "prettierd", "prettier" },
|
||||
less = { "prettierd", "prettier" },
|
||||
sh = { "shfmt" },
|
||||
["*"] = { "lsp" }, -- 任何文件类型的后备
|
||||
go = { "gofumpt", "golines" },
|
||||
rust = { "rustfmt" },
|
||||
},
|
||||
|
||||
-- 精确配置每个格式化工具
|
||||
@@ -80,6 +81,22 @@ return {
|
||||
args = { "-i", "2", "-ci", "-bn" }, -- 2空格缩进,case缩进,二元运算符换行
|
||||
stdin = true,
|
||||
},
|
||||
|
||||
gofumpt = {
|
||||
command = "gofumpt",
|
||||
stdin = true,
|
||||
},
|
||||
golines = {
|
||||
command = "golines",
|
||||
args = { "--max-len=120", "-w" },
|
||||
stdin = false,
|
||||
},
|
||||
|
||||
rustfmt = {
|
||||
command = "rustfmt",
|
||||
args = { "--emit=stdout" },
|
||||
stdin = true,
|
||||
},
|
||||
},
|
||||
|
||||
-- 保存时自动格式化
|
||||
@@ -110,6 +127,10 @@ return {
|
||||
"black",
|
||||
"isort",
|
||||
"shfmt",
|
||||
|
||||
"gofumpt",
|
||||
"golines",
|
||||
"rustfmt",
|
||||
}
|
||||
|
||||
local status = {}
|
||||
|
||||
@@ -17,18 +17,87 @@ return {
|
||||
-- 5. Mason 集成
|
||||
"williamboman/mason.nvim",
|
||||
"jay-babu/mason-nvim-dap.nvim",
|
||||
-- 6. 调试适配器安装 (Python)
|
||||
-- 调试适配器安装
|
||||
-- Python
|
||||
"mfussenegger/nvim-dap-python",
|
||||
-- Rust
|
||||
"simrat39/rust-tools.nvim",
|
||||
},
|
||||
config = function()
|
||||
local dap = require("dap")
|
||||
local dapui = require("dapui")
|
||||
|
||||
-- 配置 Python 调试器
|
||||
-- Python 调试配置
|
||||
require("dap-python").setup("python", {
|
||||
console = "integratedTerminal",
|
||||
})
|
||||
|
||||
-- Rust 调试配置
|
||||
local rust_lsp_config = {
|
||||
name = "rust_analyzer",
|
||||
capabilities = capabilities,
|
||||
on_attach = function(client, bufnr)
|
||||
-- 保留原有的 on_attach 逻辑
|
||||
on_attach(client, bufnr)
|
||||
|
||||
-- Rust 特定快捷键
|
||||
vim.keymap.set(
|
||||
"n",
|
||||
"<leader>rr",
|
||||
"<cmd>RustRunnables<CR>",
|
||||
{ buffer = bufnr, desc = "Rust 运行选项" }
|
||||
)
|
||||
vim.keymap.set(
|
||||
"n",
|
||||
"<leader>rc",
|
||||
"<cmd>RustOpenCargo<CR>",
|
||||
{ buffer = bufnr, desc = "打开 Cargo.toml" }
|
||||
)
|
||||
end,
|
||||
settings = {
|
||||
["rust-analyzer"] = {
|
||||
cargo = {
|
||||
allFeatures = true,
|
||||
},
|
||||
checkOnSave = {
|
||||
command = "clippy", -- 使用 clippy 进行代码检查
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
-- Golang 调试配置
|
||||
dap.adapters.go = {
|
||||
type = "server",
|
||||
port = "${port}",
|
||||
executable = {
|
||||
command = "dlv",
|
||||
args = { "dap", "-l", "127.0.0.1:${port}" },
|
||||
},
|
||||
}
|
||||
dap.configurations.go = {
|
||||
{
|
||||
type = "go",
|
||||
name = "Launch",
|
||||
request = "launch",
|
||||
program = "${file}",
|
||||
},
|
||||
{
|
||||
type = "go",
|
||||
name = "Launch test",
|
||||
request = "launch",
|
||||
mode = "test",
|
||||
program = "${file}",
|
||||
},
|
||||
{
|
||||
type = "go",
|
||||
name = "Launch test (package)",
|
||||
request = "launch",
|
||||
mode = "test",
|
||||
program = "${workspaceFolder}",
|
||||
},
|
||||
}
|
||||
|
||||
-- 虚拟文本配置
|
||||
require("nvim-dap-virtual-text").setup({
|
||||
highlight_changed_variables = true,
|
||||
@@ -85,7 +154,7 @@ return {
|
||||
|
||||
-- Mason DAP 配置
|
||||
require("mason-nvim-dap").setup({
|
||||
ensure_installed = { "python" },
|
||||
ensure_installed = { "python", "delve", "codelldb" },
|
||||
handlers = {
|
||||
function(config)
|
||||
require("mason-nvim-dap").default_setup(config)
|
||||
|
||||
@@ -21,28 +21,11 @@ return {
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
-- 其他 on_attach 配置...
|
||||
end
|
||||
|
||||
-- ✅ 修正: 这里使用 LSP 服务器名称 (不是 Mason 包名)
|
||||
require("mason-lspconfig").setup({
|
||||
ensure_installed = {
|
||||
"lua_ls", -- LSP 服务器名称
|
||||
"pyright", -- LSP 服务器名称 (与包名相同)
|
||||
"jsonls", -- JSON LSP 服务器
|
||||
"vimls", -- Vim Script LSP 服务器
|
||||
},
|
||||
handlers = {
|
||||
function(server_name)
|
||||
local opts = {
|
||||
capabilities = capabilities,
|
||||
on_attach = on_attach,
|
||||
}
|
||||
|
||||
-- 特定服务器配置
|
||||
if server_name == "lua_ls" then
|
||||
opts.settings = {
|
||||
local server_configs = {
|
||||
lua_ls = {
|
||||
settings = {
|
||||
Lua = {
|
||||
runtime = { version = "LuaJIT" },
|
||||
diagnostics = { globals = { "vim" } },
|
||||
@@ -54,10 +37,57 @@ return {
|
||||
},
|
||||
telemetry = { enable = false },
|
||||
},
|
||||
},
|
||||
},
|
||||
gopls = {
|
||||
settings = {
|
||||
gopls = {
|
||||
gofumpt = true, -- 使用 gofumpt 格式化
|
||||
usePlaceholders = true,
|
||||
completeUnimported = true,
|
||||
analyses = {
|
||||
unusedparams = true,
|
||||
shadow = true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
rust_analyzer = {
|
||||
settings = {
|
||||
["rust-analyzer"] = {
|
||||
cargo = {
|
||||
allFeatures = true,
|
||||
},
|
||||
checkOnSave = {
|
||||
command = "clippy", -- 使用 clippy 进行代码检查
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
end
|
||||
|
||||
require("lspconfig")[server_name].setup(opts)
|
||||
require("mason-lspconfig").setup({
|
||||
ensure_installed = {
|
||||
"lua_ls",
|
||||
"pyright",
|
||||
"jsonls",
|
||||
"vimls",
|
||||
|
||||
"gopls",
|
||||
"rust_analyzer",
|
||||
},
|
||||
handlers = {
|
||||
function(server_name)
|
||||
vim.lsp.config({
|
||||
servers = {
|
||||
[server_name] = {
|
||||
cmd = require("lspconfig")[server_name].document_config.default_config.cmd,
|
||||
capabilities = capabilities,
|
||||
on_attach = on_attach,
|
||||
settings = server_configs[server_name] and server_configs[server_name].settings or {},
|
||||
},
|
||||
},
|
||||
})
|
||||
end,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -24,6 +24,13 @@ return {
|
||||
"pyright",
|
||||
"vim-language-server",
|
||||
|
||||
"gopls",
|
||||
"golines",
|
||||
"golangci-lint",
|
||||
|
||||
"rust-analyzer",
|
||||
"rustfmt",
|
||||
|
||||
-- 格式化工具
|
||||
"stylua",
|
||||
"prettier",
|
||||
|
||||
@@ -7,6 +7,8 @@ return {
|
||||
python = "python -u",
|
||||
lua = "lua",
|
||||
sh = "bash",
|
||||
go = "go run",
|
||||
rust = "cargo run",
|
||||
},
|
||||
})
|
||||
-- 添加快捷键
|
||||
|
||||
@@ -3,7 +3,20 @@ return {
|
||||
build = ":TSUpdate",
|
||||
config = function()
|
||||
require("nvim-treesitter.configs").setup({
|
||||
ensure_installed = { "vim", "lua", "javascript", "typescript", "python", "html", "css", "markdown" },
|
||||
ensure_installed = {
|
||||
"c",
|
||||
"c++",
|
||||
"vim",
|
||||
"lua",
|
||||
"javascript",
|
||||
"typescript",
|
||||
"python",
|
||||
"html",
|
||||
"css",
|
||||
"markdown",
|
||||
"go",
|
||||
"rust",
|
||||
},
|
||||
sync_install = false,
|
||||
auto_install = true,
|
||||
highlight = {
|
||||
|
||||
Reference in New Issue
Block a user