feat(lsp): 添加golang和rust支持
This commit is contained in:
@@ -34,7 +34,8 @@ return {
|
|||||||
scss = { "prettierd", "prettier" },
|
scss = { "prettierd", "prettier" },
|
||||||
less = { "prettierd", "prettier" },
|
less = { "prettierd", "prettier" },
|
||||||
sh = { "shfmt" },
|
sh = { "shfmt" },
|
||||||
["*"] = { "lsp" }, -- 任何文件类型的后备
|
go = { "gofumpt", "golines" },
|
||||||
|
rust = { "rustfmt" },
|
||||||
},
|
},
|
||||||
|
|
||||||
-- 精确配置每个格式化工具
|
-- 精确配置每个格式化工具
|
||||||
@@ -80,6 +81,22 @@ return {
|
|||||||
args = { "-i", "2", "-ci", "-bn" }, -- 2空格缩进,case缩进,二元运算符换行
|
args = { "-i", "2", "-ci", "-bn" }, -- 2空格缩进,case缩进,二元运算符换行
|
||||||
stdin = true,
|
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",
|
"black",
|
||||||
"isort",
|
"isort",
|
||||||
"shfmt",
|
"shfmt",
|
||||||
|
|
||||||
|
"gofumpt",
|
||||||
|
"golines",
|
||||||
|
"rustfmt",
|
||||||
}
|
}
|
||||||
|
|
||||||
local status = {}
|
local status = {}
|
||||||
|
|||||||
@@ -17,18 +17,87 @@ return {
|
|||||||
-- 5. Mason 集成
|
-- 5. Mason 集成
|
||||||
"williamboman/mason.nvim",
|
"williamboman/mason.nvim",
|
||||||
"jay-babu/mason-nvim-dap.nvim",
|
"jay-babu/mason-nvim-dap.nvim",
|
||||||
-- 6. 调试适配器安装 (Python)
|
-- 调试适配器安装
|
||||||
|
-- Python
|
||||||
"mfussenegger/nvim-dap-python",
|
"mfussenegger/nvim-dap-python",
|
||||||
|
-- Rust
|
||||||
|
"simrat39/rust-tools.nvim",
|
||||||
},
|
},
|
||||||
config = function()
|
config = function()
|
||||||
local dap = require("dap")
|
local dap = require("dap")
|
||||||
local dapui = require("dapui")
|
local dapui = require("dapui")
|
||||||
|
|
||||||
-- 配置 Python 调试器
|
-- Python 调试配置
|
||||||
require("dap-python").setup("python", {
|
require("dap-python").setup("python", {
|
||||||
console = "integratedTerminal",
|
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({
|
require("nvim-dap-virtual-text").setup({
|
||||||
highlight_changed_variables = true,
|
highlight_changed_variables = true,
|
||||||
@@ -85,7 +154,7 @@ return {
|
|||||||
|
|
||||||
-- Mason DAP 配置
|
-- Mason DAP 配置
|
||||||
require("mason-nvim-dap").setup({
|
require("mason-nvim-dap").setup({
|
||||||
ensure_installed = { "python" },
|
ensure_installed = { "python", "delve", "codelldb" },
|
||||||
handlers = {
|
handlers = {
|
||||||
function(config)
|
function(config)
|
||||||
require("mason-nvim-dap").default_setup(config)
|
require("mason-nvim-dap").default_setup(config)
|
||||||
|
|||||||
@@ -21,43 +21,73 @@ return {
|
|||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 其他 on_attach 配置...
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ✅ 修正: 这里使用 LSP 服务器名称 (不是 Mason 包名)
|
local server_configs = {
|
||||||
|
lua_ls = {
|
||||||
|
settings = {
|
||||||
|
Lua = {
|
||||||
|
runtime = { version = "LuaJIT" },
|
||||||
|
diagnostics = { globals = { "vim" } },
|
||||||
|
workspace = {
|
||||||
|
library = {
|
||||||
|
[vim.fn.expand("$VIMRUNTIME/lua")] = true,
|
||||||
|
[vim.fn.expand("$VIMRUNTIME/lua/vim/lsp")] = true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
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 进行代码检查
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
require("mason-lspconfig").setup({
|
require("mason-lspconfig").setup({
|
||||||
ensure_installed = {
|
ensure_installed = {
|
||||||
"lua_ls", -- LSP 服务器名称
|
"lua_ls",
|
||||||
"pyright", -- LSP 服务器名称 (与包名相同)
|
"pyright",
|
||||||
"jsonls", -- JSON LSP 服务器
|
"jsonls",
|
||||||
"vimls", -- Vim Script LSP 服务器
|
"vimls",
|
||||||
|
|
||||||
|
"gopls",
|
||||||
|
"rust_analyzer",
|
||||||
},
|
},
|
||||||
handlers = {
|
handlers = {
|
||||||
function(server_name)
|
function(server_name)
|
||||||
local opts = {
|
vim.lsp.config({
|
||||||
capabilities = capabilities,
|
servers = {
|
||||||
on_attach = on_attach,
|
[server_name] = {
|
||||||
}
|
cmd = require("lspconfig")[server_name].document_config.default_config.cmd,
|
||||||
|
capabilities = capabilities,
|
||||||
-- 特定服务器配置
|
on_attach = on_attach,
|
||||||
if server_name == "lua_ls" then
|
settings = server_configs[server_name] and server_configs[server_name].settings or {},
|
||||||
opts.settings = {
|
|
||||||
Lua = {
|
|
||||||
runtime = { version = "LuaJIT" },
|
|
||||||
diagnostics = { globals = { "vim" } },
|
|
||||||
workspace = {
|
|
||||||
library = {
|
|
||||||
[vim.fn.expand("$VIMRUNTIME/lua")] = true,
|
|
||||||
[vim.fn.expand("$VIMRUNTIME/lua/vim/lsp")] = true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
telemetry = { enable = false },
|
|
||||||
},
|
},
|
||||||
}
|
},
|
||||||
end
|
})
|
||||||
|
|
||||||
require("lspconfig")[server_name].setup(opts)
|
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -24,6 +24,13 @@ return {
|
|||||||
"pyright",
|
"pyright",
|
||||||
"vim-language-server",
|
"vim-language-server",
|
||||||
|
|
||||||
|
"gopls",
|
||||||
|
"golines",
|
||||||
|
"golangci-lint",
|
||||||
|
|
||||||
|
"rust-analyzer",
|
||||||
|
"rustfmt",
|
||||||
|
|
||||||
-- 格式化工具
|
-- 格式化工具
|
||||||
"stylua",
|
"stylua",
|
||||||
"prettier",
|
"prettier",
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ return {
|
|||||||
python = "python -u",
|
python = "python -u",
|
||||||
lua = "lua",
|
lua = "lua",
|
||||||
sh = "bash",
|
sh = "bash",
|
||||||
|
go = "go run",
|
||||||
|
rust = "cargo run",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
-- 添加快捷键
|
-- 添加快捷键
|
||||||
|
|||||||
@@ -3,7 +3,20 @@ return {
|
|||||||
build = ":TSUpdate",
|
build = ":TSUpdate",
|
||||||
config = function()
|
config = function()
|
||||||
require("nvim-treesitter.configs").setup({
|
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,
|
sync_install = false,
|
||||||
auto_install = true,
|
auto_install = true,
|
||||||
highlight = {
|
highlight = {
|
||||||
|
|||||||
Reference in New Issue
Block a user