refactor: 重构Neovim配置结构并更新文档
- 调整配置文件组织方式,新增init.lua作为入口 - 更新lazy-lock.json同步插件版本 - 完善lua/目录下的插件和配置模块结构 - 修改README.md补充配置说明和使用指南
This commit is contained in:
123
lua/plugins/lsp/cmp.lua
Normal file
123
lua/plugins/lsp/cmp.lua
Normal file
@@ -0,0 +1,123 @@
|
||||
return {
|
||||
"hrsh7th/nvim-cmp",
|
||||
dependencies = {
|
||||
"hrsh7th/cmp-nvim-lsp", -- LSP 补全源
|
||||
"hrsh7th/cmp-buffer", -- 缓冲区补全源
|
||||
"hrsh7th/cmp-path", -- 路径补全源
|
||||
"hrsh7th/cmp-cmdline", -- 命令行补全
|
||||
"saadparwaiz1/cmp_luasnip", -- snippet 补全
|
||||
"L3MON4D3/LuaSnip", -- snippet 引擎
|
||||
"rafamadriz/friendly-snippets", -- 预设 snippets
|
||||
},
|
||||
config = function()
|
||||
-- 加载友好 snippets
|
||||
require("luasnip.loaders.from_vscode").lazy_load()
|
||||
|
||||
local cmp = require("cmp")
|
||||
local luasnip = require("luasnip")
|
||||
|
||||
cmp.setup({
|
||||
snippet = {
|
||||
expand = function(args)
|
||||
luasnip.lsp_expand(args.body)
|
||||
end,
|
||||
},
|
||||
mapping = cmp.mapping.preset.insert({
|
||||
["<C-p>"] = cmp.mapping.select_prev_item(), -- 上一个补全项
|
||||
["<C-n>"] = cmp.mapping.select_next_item(), -- 下一个补全项
|
||||
["<C-b>"] = cmp.mapping.scroll_docs(-4),
|
||||
["<C-f>"] = cmp.mapping.scroll_docs(4),
|
||||
["<C-Space>"] = cmp.mapping.complete(), -- 触发补全
|
||||
["<C-e>"] = cmp.mapping.abort(), -- 退出补全
|
||||
["<CR>"] = cmp.mapping.confirm({ select = true }), -- 确认选择
|
||||
|
||||
-- 使用 Tab 和 Shift+Tab 在补全项间导航
|
||||
["<Tab>"] = cmp.mapping(function(fallback)
|
||||
if cmp.visible() then
|
||||
cmp.select_next_item()
|
||||
elseif luasnip.expand_or_jumpable() then
|
||||
luasnip.expand_or_jump()
|
||||
else
|
||||
fallback()
|
||||
end
|
||||
end, { "i", "s" }),
|
||||
|
||||
["<S-Tab>"] = cmp.mapping(function(fallback)
|
||||
if cmp.visible() then
|
||||
cmp.select_prev_item()
|
||||
elseif luasnip.jumpable(-1) then
|
||||
luasnip.jump(-1)
|
||||
else
|
||||
fallback()
|
||||
end
|
||||
end, { "i", "s" }),
|
||||
}),
|
||||
sources = cmp.config.sources({
|
||||
{ name = "nvim_lsp" }, -- LSP
|
||||
{ name = "luasnip" }, -- Snippets
|
||||
{ name = "buffer" }, -- 缓冲区
|
||||
{ name = "path" }, -- 路径
|
||||
}),
|
||||
formatting = {
|
||||
format = function(entry, item)
|
||||
-- 为不同类型的补全项添加图标
|
||||
local kind_icons = {
|
||||
Text = "",
|
||||
Method = "",
|
||||
Function = "",
|
||||
Constructor = "",
|
||||
Field = "",
|
||||
Variable = "",
|
||||
Class = "",
|
||||
Interface = "",
|
||||
Module = "",
|
||||
Property = "",
|
||||
Unit = "",
|
||||
Value = "",
|
||||
Enum = "",
|
||||
Keyword = "",
|
||||
Snippet = "",
|
||||
Color = "",
|
||||
File = "",
|
||||
Reference = "",
|
||||
Folder = "",
|
||||
EnumMember = "",
|
||||
Constant = "",
|
||||
Struct = "",
|
||||
Event = "",
|
||||
Operator = "",
|
||||
TypeParameter = "",
|
||||
}
|
||||
item.kind = kind_icons[item.kind] .. " " .. item.kind
|
||||
item.menu = ({
|
||||
nvim_lsp = "[LSP]",
|
||||
luasnip = "[Snippet]",
|
||||
buffer = "[Buffer]",
|
||||
path = "[Path]",
|
||||
})[entry.source.name]
|
||||
return item
|
||||
end,
|
||||
},
|
||||
experimental = {
|
||||
ghost_text = true, -- 显示灰色的建议文本
|
||||
},
|
||||
})
|
||||
|
||||
-- 命令行补全
|
||||
cmp.setup.cmdline("/", {
|
||||
mapping = cmp.mapping.preset.cmdline(),
|
||||
sources = {
|
||||
{ name = "buffer" },
|
||||
},
|
||||
})
|
||||
|
||||
cmp.setup.cmdline(":", {
|
||||
mapping = cmp.mapping.preset.cmdline(),
|
||||
sources = cmp.config.sources({
|
||||
{ name = "path" },
|
||||
}, {
|
||||
{ name = "cmdline" },
|
||||
}),
|
||||
})
|
||||
end,
|
||||
}
|
||||
130
lua/plugins/lsp/conform.lua
Normal file
130
lua/plugins/lsp/conform.lua
Normal file
@@ -0,0 +1,130 @@
|
||||
return {
|
||||
"stevearc/conform.nvim",
|
||||
event = { "BufWritePre" },
|
||||
cmd = { "ConformInfo" },
|
||||
keys = {
|
||||
{
|
||||
"<leader>f",
|
||||
function()
|
||||
require("conform").format({ async = true, lsp_fallback = true })
|
||||
end,
|
||||
mode = "",
|
||||
desc = "Format buffer",
|
||||
},
|
||||
},
|
||||
config = function()
|
||||
local conform = require("conform")
|
||||
|
||||
conform.setup({
|
||||
-- 定义格式化工具 - 每个工具都有正确的参数配置
|
||||
formatters_by_ft = {
|
||||
lua = { "stylua" },
|
||||
python = { "isort", "black" },
|
||||
javascript = { "prettierd", "prettier" }, -- 优先使用prettierd(守护进程版本)
|
||||
typescript = { "prettierd", "prettier" },
|
||||
javascriptreact = { "prettierd", "prettier" },
|
||||
typescriptreact = { "prettierd", "prettier" },
|
||||
css = { "prettierd", "prettier" },
|
||||
html = { "prettierd", "prettier" },
|
||||
json = { "prettierd", "prettier" },
|
||||
jsonc = { "prettierd", "prettier" },
|
||||
yaml = { "prettierd", "prettier" },
|
||||
markdown = { "prettierd", "prettier" },
|
||||
graphql = { "prettierd", "prettier" },
|
||||
scss = { "prettierd", "prettier" },
|
||||
less = { "prettierd", "prettier" },
|
||||
sh = { "shfmt" },
|
||||
["*"] = { "lsp" }, -- 任何文件类型的后备
|
||||
},
|
||||
|
||||
-- 精确配置每个格式化工具
|
||||
formatters = {
|
||||
-- 修正Stylua配置
|
||||
stylua = {
|
||||
command = "stylua",
|
||||
args = { "--stdin-filepath", "$FILENAME", "-" }, -- 关键修正:添加"-"表示从stdin读取
|
||||
stdin = true,
|
||||
},
|
||||
|
||||
-- Prettier配置
|
||||
prettier = {
|
||||
command = "prettier",
|
||||
args = { "--stdin-filepath", "$FILENAME" },
|
||||
stdin = true,
|
||||
},
|
||||
|
||||
-- Prettier守护进程版本(更快)
|
||||
prettierd = {
|
||||
command = "prettierd",
|
||||
args = { "$FILENAME" },
|
||||
stdin = true,
|
||||
},
|
||||
|
||||
-- Black (Python)
|
||||
black = {
|
||||
command = "black",
|
||||
args = { "--quiet", "-" },
|
||||
stdin = true,
|
||||
},
|
||||
|
||||
-- isort (Python导入排序)
|
||||
isort = {
|
||||
command = "isort",
|
||||
args = { "--quiet", "-" },
|
||||
stdin = true,
|
||||
},
|
||||
|
||||
-- shfmt (Shell脚本)
|
||||
shfmt = {
|
||||
command = "shfmt",
|
||||
args = { "-i", "2", "-ci", "-bn" }, -- 2空格缩进,case缩进,二元运算符换行
|
||||
stdin = true,
|
||||
},
|
||||
},
|
||||
|
||||
-- 保存时自动格式化
|
||||
format_on_save = {
|
||||
-- 可以排除特定文件类型
|
||||
ignore_filetypes = { "gitcommit", "markdown", "help", "txt" },
|
||||
-- 延迟毫秒数,避免在输入时频繁触发
|
||||
-- delay_ms = 200,
|
||||
-- 回退到LSP格式化
|
||||
lsp_fallback = true,
|
||||
},
|
||||
|
||||
-- 异步格式化
|
||||
async = true,
|
||||
timeout_ms = 1000, -- 1秒超时
|
||||
log_level = vim.log.levels.WARN,
|
||||
|
||||
-- 格式化后显示通知
|
||||
notify_on_error = true,
|
||||
})
|
||||
|
||||
-- 添加一个命令来检查格式化工具状态
|
||||
vim.api.nvim_create_user_command("FormatToolsCheck", function()
|
||||
local tools = {
|
||||
"stylua",
|
||||
"prettier",
|
||||
"prettierd",
|
||||
"black",
|
||||
"isort",
|
||||
"shfmt",
|
||||
}
|
||||
|
||||
local status = {}
|
||||
for _, tool in ipairs(tools) do
|
||||
local found = vim.fn.executable(tool) == 1
|
||||
table.insert(status, string.format("%s: %s", tool, found and "✓ installed" or "✗ not found"))
|
||||
end
|
||||
|
||||
vim.notify(table.concat(status, "\n"), found and "info" or "warn", {
|
||||
title = "Formatting Tools Status",
|
||||
timeout = 5000,
|
||||
})
|
||||
end, {})
|
||||
|
||||
-- 添加一个键映射来检查工具
|
||||
vim.keymap.set("n", "<leader>ft", "<cmd>FormatToolsCheck<cr>", { desc = "Check formatting tools" })
|
||||
end,
|
||||
}
|
||||
197
lua/plugins/lsp/dap.lua
Normal file
197
lua/plugins/lsp/dap.lua
Normal file
@@ -0,0 +1,197 @@
|
||||
-- 完整的 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 = { "<CR>", "<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", "<F6>", function()
|
||||
dap.continue()
|
||||
end, opts)
|
||||
vim.keymap.set("n", "<F10>", function()
|
||||
dap.step_over()
|
||||
end, opts)
|
||||
vim.keymap.set("n", "<F11>", function()
|
||||
dap.step_into()
|
||||
end, opts)
|
||||
vim.keymap.set("n", "<F12>", function()
|
||||
dap.step_out()
|
||||
end, opts)
|
||||
vim.keymap.set("n", "<leader>b", function()
|
||||
dap.toggle_breakpoint()
|
||||
end, opts)
|
||||
vim.keymap.set("n", "<leader>B", function()
|
||||
dap.set_breakpoint(vim.fn.input("Breakpoint condition: "))
|
||||
end, opts)
|
||||
vim.keymap.set("n", "<leader>lp", function()
|
||||
dap.set_breakpoint(nil, nil, vim.fn.input("Log point message: "))
|
||||
end, opts)
|
||||
vim.keymap.set("n", "<leader>dr", function()
|
||||
dap.repl.open()
|
||||
end, opts)
|
||||
vim.keymap.set("n", "<leader>dl", function()
|
||||
dap.run_last()
|
||||
end, opts)
|
||||
vim.keymap.set({ "n", "v" }, "<leader>dh", function()
|
||||
require("dap.ui.widgets").hover()
|
||||
end, opts)
|
||||
vim.keymap.set({ "n", "v" }, "<leader>dp", function()
|
||||
require("dap.ui.widgets").preview()
|
||||
end, opts)
|
||||
vim.keymap.set("n", "<leader>di", function()
|
||||
dapui.toggle()
|
||||
end, opts)
|
||||
|
||||
-- 增强的变量检查
|
||||
vim.keymap.set("n", "<leader>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" },
|
||||
},
|
||||
}
|
||||
12
lua/plugins/lsp/init.lua
Normal file
12
lua/plugins/lsp/init.lua
Normal file
@@ -0,0 +1,12 @@
|
||||
-- LSP 快捷键
|
||||
vim.keymap.set("n", "gd", "<cmd>lua vim.lsp.buf.definition()<cr>", { desc = "跳转到定义" })
|
||||
vim.keymap.set("n", "gD", "<cmd>lua vim.lsp.buf.declaration()<cr>", { desc = "跳转到声明" })
|
||||
vim.keymap.set("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<cr>", { desc = "跳转到实现" })
|
||||
vim.keymap.set("n", "gr", "<cmd>lua vim.lsp.buf.references()<cr>", { desc = "查找引用" })
|
||||
vim.keymap.set("n", "K", "<cmd>lua vim.lsp.buf.hover()<cr>", { desc = "显示信息" })
|
||||
vim.keymap.set("n", "<leader>rn", "<cmd>lua vim.lsp.buf.rename()<cr>", { desc = "重命名" })
|
||||
vim.keymap.set("n", "<leader>ca", "<cmd>lua vim.lsp.buf.code_action()<cr>", { desc = "代码操作" })
|
||||
vim.keymap.set("n", "<leader>so", "<cmd>lua vim.lsp.buf.signature_help()<cr>", { desc = "签名帮助" })
|
||||
vim.keymap.set("n", "[d", "<cmd>lua vim.diagnostic.goto_prev()<cr>", { desc = "上一个诊断" })
|
||||
vim.keymap.set("n", "]d", "<cmd>lua vim.diagnostic.goto_next()<cr>", { desc = "下一个诊断" })
|
||||
vim.keymap.set("n", "<leader>q", "<cmd>lua vim.diagnostic.setloclist()<cr>", { desc = "快速修复" })
|
||||
65
lua/plugins/lsp/lspconfig.lua
Normal file
65
lua/plugins/lsp/lspconfig.lua
Normal file
@@ -0,0 +1,65 @@
|
||||
return {
|
||||
"neovim/nvim-lspconfig",
|
||||
dependencies = {
|
||||
"williamboman/mason-lspconfig.nvim",
|
||||
"hrsh7th/cmp-nvim-lsp",
|
||||
"ray-x/lsp_signature.nvim",
|
||||
},
|
||||
config = function()
|
||||
local capabilities = require("cmp_nvim_lsp").default_capabilities()
|
||||
|
||||
local function on_attach(client, bufnr)
|
||||
-- 格式化配置保持不变
|
||||
if client.supports_method("textDocument/formatting") then
|
||||
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
|
||||
vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
|
||||
vim.api.nvim_create_autocmd("BufWritePre", {
|
||||
group = augroup,
|
||||
buffer = bufnr,
|
||||
callback = function()
|
||||
vim.lsp.buf.format({ async = false })
|
||||
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 = {
|
||||
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,
|
||||
}
|
||||
39
lua/plugins/lsp/mason.lua
Normal file
39
lua/plugins/lsp/mason.lua
Normal file
@@ -0,0 +1,39 @@
|
||||
return {
|
||||
"williamboman/mason.nvim",
|
||||
dependencies = {
|
||||
"williamboman/mason-lspconfig.nvim",
|
||||
"WhoIsSethDaniel/mason-tool-installer.nvim",
|
||||
},
|
||||
config = function()
|
||||
require("mason").setup({
|
||||
ui = {
|
||||
border = "rounded",
|
||||
icons = {
|
||||
package_installed = "✓",
|
||||
package_pending = "➜",
|
||||
package_uninstalled = "✗",
|
||||
},
|
||||
},
|
||||
max_concurrent_installers = 10,
|
||||
})
|
||||
|
||||
require("mason-tool-installer").setup({
|
||||
ensure_installed = {
|
||||
-- LSP 服务器
|
||||
"lua-language-server",
|
||||
"pyright",
|
||||
"vim-language-server",
|
||||
|
||||
-- 格式化工具
|
||||
"stylua",
|
||||
"prettier",
|
||||
"eslint_d",
|
||||
"black",
|
||||
"isort",
|
||||
"shfmt",
|
||||
},
|
||||
auto_update = true, -- 自动更新已安装的工具
|
||||
run_on_start = true, -- 启动时运行安装
|
||||
})
|
||||
end,
|
||||
}
|
||||
Reference in New Issue
Block a user