diff --git a/lua/plugins/lsp/conform.lua b/lua/plugins/lsp/conform.lua index 7960853..3c6e030 100644 --- a/lua/plugins/lsp/conform.lua +++ b/lua/plugins/lsp/conform.lua @@ -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 = {} diff --git a/lua/plugins/lsp/dap.lua b/lua/plugins/lsp/dap.lua index 0767d84..b6219a8 100644 --- a/lua/plugins/lsp/dap.lua +++ b/lua/plugins/lsp/dap.lua @@ -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", + "rr", + "RustRunnables", + { buffer = bufnr, desc = "Rust 运行选项" } + ) + vim.keymap.set( + "n", + "rc", + "RustOpenCargo", + { 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) diff --git a/lua/plugins/lsp/lspconfig.lua b/lua/plugins/lsp/lspconfig.lua index ea791a8..ec0f8a0 100644 --- a/lua/plugins/lsp/lspconfig.lua +++ b/lua/plugins/lsp/lspconfig.lua @@ -21,43 +21,73 @@ return { end, }) end - - -- 其他 on_attach 配置... 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({ ensure_installed = { - "lua_ls", -- LSP 服务器名称 - "pyright", -- LSP 服务器名称 (与包名相同) - "jsonls", -- JSON LSP 服务器 - "vimls", -- Vim Script LSP 服务器 + "lua_ls", + "pyright", + "jsonls", + "vimls", + + "gopls", + "rust_analyzer", }, handlers = { function(server_name) - local opts = { - capabilities = capabilities, - on_attach = on_attach, - } - - -- 特定服务器配置 - if server_name == "lua_ls" then - 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 }, + 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 - - require("lspconfig")[server_name].setup(opts) + }, + }) end, }, }) diff --git a/lua/plugins/lsp/mason.lua b/lua/plugins/lsp/mason.lua index df425be..120a6f8 100644 --- a/lua/plugins/lsp/mason.lua +++ b/lua/plugins/lsp/mason.lua @@ -24,6 +24,13 @@ return { "pyright", "vim-language-server", + "gopls", + "golines", + "golangci-lint", + + "rust-analyzer", + "rustfmt", + -- 格式化工具 "stylua", "prettier", diff --git a/lua/plugins/runner.lua b/lua/plugins/runner.lua index ea371c6..dd9cbd6 100644 --- a/lua/plugins/runner.lua +++ b/lua/plugins/runner.lua @@ -7,6 +7,8 @@ return { python = "python -u", lua = "lua", sh = "bash", + go = "go run", + rust = "cargo run", }, }) -- 添加快捷键 diff --git a/lua/plugins/treesitter.lua b/lua/plugins/treesitter.lua index 1d5c096..490b0c7 100644 --- a/lua/plugins/treesitter.lua +++ b/lua/plugins/treesitter.lua @@ -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 = {