-- 完整的 DAP 配置 (保存为 lua/plugins/dap.lua 或类似路径) return { -- 1. 必须首先加载 nvim-nio { "nvim-neotest/nvim-nio", lazy = true, }, -- 2. 核心 DAP 插件 { "mfussenegger/nvim-dap", dependencies = { -- 3. DAP UI (依赖 nvim-nio) "rcarriga/nvim-dap-ui", -- 4. 虚拟文本显示 "theHamsta/nvim-dap-virtual-text", -- 5. Mason 集成 "williamboman/mason.nvim", "jay-babu/mason-nvim-dap.nvim", -- 6. 调试适配器安装 (Python) "mfussenegger/nvim-dap-python", }, config = function() local dap = require("dap") local dapui = require("dapui") -- 配置 Python 调试器 require("dap-python").setup("python", { console = "integratedTerminal", }) -- 虚拟文本配置 require("nvim-dap-virtual-text").setup({ highlight_changed_variables = true, highlight_new_as_changed = true, show_stop_reason = true, commented = false, only_first_definition = true, all_references = false, }) -- DAP UI 配置 dapui.setup({ icons = { expanded = "▾", collapsed = "▸" }, mappings = { expand = { "", "<2-LeftMouse>" }, open = "o", remove = "d", edit = "e", repl = "r", toggle = "t", }, layouts = { { elements = { { id = "scopes", size = 0.33 }, { id = "breakpoints", size = 0.17 }, { id = "stacks", size = 0.25 }, { id = "watches", size = 0.25 }, }, size = 40, position = "left", }, { elements = { { id = "repl", size = 0.45 }, { id = "console", size = 0.55 }, }, size = 0.25, position = "bottom", }, }, }) -- 自动打开/关闭 UI dap.listeners.after.event_initialized["dapui_config"] = function() dapui.open() end dap.listeners.before.event_terminated["dapui_config"] = function() dapui.close() end dap.listeners.before.event_exited["dapui_config"] = function() dapui.close() end -- Mason DAP 配置 require("mason-nvim-dap").setup({ ensure_installed = { "python" }, handlers = { function(config) require("mason-nvim-dap").default_setup(config) end, }, }) -- Python 调试配置 dap.configurations.python = { { type = "python", request = "launch", name = "Launch file", program = "${file}", pythonPath = function() -- 智能检测 Python 路径 local cwd = vim.fn.getcwd() local paths = { cwd .. "/venv/bin/python", cwd .. "/.venv/bin/python", vim.fn.expand("~/.virtualenvs") .. "/*/bin/python", vim.fn.expand("~/.pyenv/shims/python"), "/usr/bin/python3", "/usr/local/bin/python3", } for _, path in ipairs(paths) do if vim.fn.executable(path:gsub("%*", "")) == 1 then return path:gsub("%*", "") end end return "python" end, console = "integratedTerminal", justMyCode = true, }, { type = "python", request = "attach", name = "Attach to remote", host = "127.0.0.1", port = 5678, pathMappings = { { localRoot = "${workspaceFolder}", remoteRoot = ".", }, }, }, } -- 调试快捷键 local opts = { noremap = true, silent = true } vim.keymap.set("n", "", function() dap.continue() end, opts) vim.keymap.set("n", "", function() dap.step_over() end, opts) vim.keymap.set("n", "", function() dap.step_into() end, opts) vim.keymap.set("n", "", function() dap.step_out() end, opts) vim.keymap.set("n", "b", function() dap.toggle_breakpoint() end, opts) vim.keymap.set("n", "B", function() dap.set_breakpoint(vim.fn.input("Breakpoint condition: ")) end, opts) vim.keymap.set("n", "lp", function() dap.set_breakpoint(nil, nil, vim.fn.input("Log point message: ")) end, opts) vim.keymap.set("n", "dr", function() dap.repl.open() end, opts) vim.keymap.set("n", "dl", function() dap.run_last() end, opts) vim.keymap.set({ "n", "v" }, "dh", function() require("dap.ui.widgets").hover() end, opts) vim.keymap.set({ "n", "v" }, "dp", function() require("dap.ui.widgets").preview() end, opts) vim.keymap.set("n", "di", function() dapui.toggle() end, opts) -- 增强的变量检查 vim.keymap.set("n", "de", function() local widget = require("dap.ui.widgets") widget.centered_float(widget.scopes) end, opts) end, }, -- 3. 可选:DAP 安装助手 { "ravenxrz/DAPInstall.nvim", dependencies = { "mfussenegger/nvim-dap" }, config = function() require("dap-install").setup({}) end, cmd = { "DAPInstall", "DAPInstallConfig" }, }, }