完美!我们按顺序来,先从 Python 环境 开始。


🐍 Python 环境专用配置

目录结构

~/.config/nvim/lua/envs/python.lua

配置内容

-- ~/.config/nvim/lua/envs/python.lua
return {
  -- 🔧 LSP (Pyright)
  {
    "neovim/nvim-lspconfig",
    version = "v2.9.0",
    config = function()
      local on_attach = require("nvchad.configs.lspconfig").on_attach
      local capabilities = require("nvchad.configs.lspconfig").capabilities
      
      require("lspconfig").pyright.setup({
        on_attach = on_attach,
        capabilities = capabilities,
        settings = {
          python = {
            analysis = {
              typeCheckingMode = "basic",
              autoSearchPaths = true,
              useLibraryCodeForTypes = true,
              diagnosticMode = "workspace",
            }
          }
        }
      })
    end,
  },
  
  -- 🐞 调试 (debugpy)
  {
    "mfussenegger/nvim-dap",
    dependencies = {
      "rcarriga/nvim-dap-ui",
      "mxsdev/nvim-dap-python",
    },
    config = function()
      local dap = require("dap")
      
      -- debugpy 适配器
      dap.adapters.python = {
        type = "executable",
        command = "python",
        args = { "-m", "debugpy.adapter" },
      }
      
      -- 调试配置
      dap.configurations.python = {
        {
          type = "python",
          request = "launch",
          name = "Launch file",
          program = "${file}",
          console = "integratedTerminal",
          justMyCode = true,
        },
        {
          type = "python", 
          request = "launch",
          name = "Launch module",
          module = vim.fn.input('Module: '),
          console = "integratedTerminal",
          justMyCode = true,
        },
        {
          type = "python",
          request = "attach",
          name = "Attach to process",
          processId = require("dap.utils").pick_process,
          justMyCode = true,
        },
      }
    end,
  },
  
  -- 🧪 测试 (pytest)
  {
    "nvim-neotest/neotest",
    dependencies = {
      "nvim-neotest/neotest-python",
      "nvim-lua/plenary.nvim",
    },
    config = function()
      require("neotest").setup({
        adapters = {
          require("neotest-python")({
            runner = "pytest",
            python = "python",
          }),
        },
        output = {
          enabled = true,
          expand_on_run = true,
        },
        quickfix = {
          enabled = true,
          open = true,
        },
      })
    end,
  },
  
  -- 📏 代码质量 (ruff)
  {
    "mfussenegger/nvim-lint",
    config = function()
      require("lint").linters_by_ft = {
        python = { "ruff" }
      }
      vim.api.nvim_create_autocmd("BufWritePost", {
        callback = function()
          require("lint").try_lint()
        end,
      })
    end,
  },
  
  -- 📐 格式化 (ruff-format)
  {
    "stevearc/conform.nvim",
    config = function()
      require("conform").setup({
        formatters_by_ft = {
          python = { "ruff_format" },
        },
        format_on_save = {
          timeout_ms = 1000,
        },
      })
    end,
  },
}

🛠️ Python 环境依赖安装

# 安装 Python 工具链
pip install pyright ruff debugpy pytest
 
# 验证安装
pyright --version
ruff --version
python -m debugpy --help

✅ Python 环境验证清单

  • 切换环境:echo "python" > ~/.config/nvim/.nvim-env
  • 启动 Neovim:nvim test.py
  • LSP: <leader>gd 跳转定义
  • 调试: <F5> 启动调试
  • 测试: <leader>tn 运行测试
  • 格式化: 保存文件自动格式化
  • 代码质量: 保存后显示 ruff 警告

接下来是 TypeScript 环境,准备好了吗?