refactor: 重构Neovim配置结构并更新文档

- 调整配置文件组织方式,新增init.lua作为入口
- 更新lazy-lock.json同步插件版本
- 完善lua/目录下的插件和配置模块结构
- 修改README.md补充配置说明和使用指南
This commit is contained in:
2025-12-15 09:57:50 +08:00
parent 937cff0da3
commit 11565f3c55
26 changed files with 1225 additions and 1 deletions

75
lua/config/basic.lua Normal file
View File

@@ -0,0 +1,75 @@
-- ===== 基本设置 =====
-- 行号
vim.opt.number = true
vim.opt.relativenumber = true
-- 缩进
vim.opt.tabstop = 4
vim.opt.softtabstop = 4
vim.opt.shiftwidth = 4
vim.opt.expandtab = true
vim.opt.smartindent = true
-- 搜索
vim.opt.ignorecase = true
vim.opt.smartcase = true
vim.opt.hlsearch = true
vim.opt.incsearch = true
-- 外观
vim.opt.termguicolors = true
vim.opt.background = "dark"
vim.opt.signcolumn = "yes"
vim.opt.cursorline = true
vim.opt.colorcolumn = "80" -- 在第80列显示参考线
-- 文件和备份
vim.opt.swapfile = false
vim.opt.backup = false
vim.opt.undofile = false
-- 分割窗口
vim.opt.splitright = true
vim.opt.splitbelow = true
-- 其他
vim.opt.mouse = "a" -- 启用鼠标
vim.opt.updatetime = 50 -- 更快的响应时间
vim.opt.timeoutlen = 300 -- 快捷键响应时间
vim.opt.scrolloff = 8 -- 光标距离边界8行时滚动
vim.opt.wrap = false -- 不自动换行
vim.opt.errorbells = false -- 不发出错误声音
vim.opt.visualbell = false -- 不使用视觉提示
-- 领导键
vim.g.mapleader = " "
vim.g.localleader = "\\"
-- 剪切板
vim.g.clipboard = {
name = "wl-clipboard",
copy = {
["+"] = { "wl-copy" },
["*"] = { "wl-copy" },
},
paste = {
["+"] = { "wl-paste" },
["*"] = { "wl-paste" },
},
cache_enabled = 0,
}
vim.opt.clipboard = "unnamedplus"
vim.opt.shell = "zsh"
vim.api.nvim_create_user_command("Term", "terminal", { desc = "打开终端" })
vim.keymap.set("n", "<leader>t", "<cmd>Term<cr>", { desc = "打开终端" })
-- 终端模式快捷键
vim.api.nvim_create_autocmd("TermOpen", {
pattern = "*",
callback = function()
vim.keymap.set("t", "<ESC>", "<C-\\><C-n>", { buffer = true, desc = "退出终端模式" })
vim.opt_local.number = false
vim.opt_local.relativenumber = false
end,
})

15
lua/config/lazy.lua Normal file
View File

@@ -0,0 +1,15 @@
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
local lazyrepo = "git@github.com:folke/lazy.nvim.git"
local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
if vim.v.shell_error ~= 0 then
vim.api.nvim_echo({
{ "Failed to clone lazy.nvim:\n", "ErrorMsg" },
{ out, "WarningMsg" },
{ "\nPress any key to exit..." },
}, true, {})
vim.fn.getchar()
os.exit(1)
end
end
vim.opt.rtp:prepend(lazypath)

138
lua/plugins/autopairs.lua Normal file
View File

@@ -0,0 +1,138 @@
return {
"windwp/nvim-autopairs",
event = "InsertEnter",
dependencies = {
"hrsh7th/nvim-cmp",
"nvim-treesitter/nvim-treesitter",
},
config = function()
local npairs = require("nvim-autopairs")
local Rule = require("nvim-autopairs.rule")
local ts_conds = require("nvim-autopairs.ts-conds") -- 正确导入条件模块
-- 基础配置(来自官方示例)
npairs.setup({
check_ts = true, -- 启用 Treesitter 检查
ts_config = {
lua = { "string" }, -- 在 Lua 的 string 节点中不添加配对
javascript = { "template_string" }, -- 在 JS 模板字符串中不添加配对
java = false, -- 在 Java 中不检查 Treesitter
python = { "string", "comment" }, -- 在 Python 字符串和注释中不添加配对
cpp = { "string", "comment" },
rust = { "string", "comment" },
},
disable_filetype = { "TelescopePrompt", "spectre_panel", "lspinfo", "dashboard" },
fast_wrap = {
map = "<M-e>",
chars = { "{", "[", "(", '"', "'", "`" },
pattern = string.gsub([[ [%'%"%)%>%]%)%}%,] ]], "%s+", ""),
offset = 0,
end_key = "$",
keys = "qwertyuiopzxcvbnmasdfghjkl",
check_comma = true,
highlight = "PmenuSel",
highlight_grey = "LineNr",
},
})
-- 在 string 或 comment 节点中添加 % 配对
npairs.add_rules({
Rule("%", "%", "lua"):with_pair(ts_conds.is_ts_node({ "string", "comment" })),
-- 在非 function 节点中添加 $ 配对
Rule("$", "$", "lua"):with_pair(ts_conds.is_not_ts_node({ "function" })),
-- 在非 string 和非 comment 节点中添加引号配对
Rule("'", "'", "lua"):with_pair(ts_conds.is_not_ts_node({ "string", "comment" })),
Rule('"', '"', "lua"):with_pair(ts_conds.is_not_ts_node({ "string", "comment" })),
Rule("`", "`", "lua"):with_pair(ts_conds.is_not_ts_node({ "string", "comment" })),
-- Python 规则:在非字符串、非注释、非函数参数节点中添加括号
Rule("(", ")", "python"):with_pair(ts_conds.is_not_ts_node({ "string", "comment", "parameters" })),
-- JavaScript/TS 规则
Rule("(", ")", "javascript"):with_pair(ts_conds.is_not_ts_node({ "string", "comment", "template_string" })),
Rule("(", ")", "typescript"):with_pair(ts_conds.is_not_ts_node({ "string", "comment", "template_string" })),
})
-- 与 nvim-cmp 集成
local cmp_autopairs = require("nvim-autopairs.completion.cmp")
require("cmp").event:on("confirm_done", cmp_autopairs.on_confirm_done())
-- 智能回车:在括号内按回车时自动缩进
npairs.add_rule(Rule("\n", "\n")
:with_pair(function(opts)
local line = opts.line
local before = line:sub(1, opts.col - 1)
local after = line:sub(opts.col)
local pair = before:sub(-1) .. after:sub(1)
return pair == "{}" or pair == "[]" or pair == "()"
end)
:with_move(function(opts)
return opts.char == "\n"
end)
:use_key("\n"))
-- 在 % 后面不添加额外的 ) 例如: %(xxx|) -> %(xxx|)
npairs.add_rule(Rule(")", ")"):with_pair(function(opts)
local pair = opts.line:sub(opts.col - 1, opts.col)
return pair == "(%"
end))
-- Markdown 链接规则
npairs.add_rules({
Rule("[", "]", "markdown"):with_pair(function(opts)
local before = opts.line:sub(1, opts.col - 1)
return not before:match("%[.-%]%(") -- 不在已有的链接后添加
end),
Rule("(", ")", "markdown"):with_pair(function(opts)
local before = opts.line:sub(1, opts.col - 2)
return before:match("%[.-%]$") -- 只在 [text] 后添加
end),
})
-- 快捷键映射
local opts = { noremap = true, silent = true, expr = true }
-- 智能回车(处理括号和 completion
vim.keymap.set("i", "<CR>", function()
if vim.fn.pumvisible() == 1 then
return "<C-e><CR>"
end
return npairs.autopairs_cr()
end, opts)
-- 跳过右侧括号
vim.keymap.set("i", ")", function()
if npairs.jumpable() then
return "<Plug>nvim-autopairs-jump-right"
else
return ")"
end
end, opts)
vim.keymap.set("i", "}", function()
if npairs.jumpable() then
return "<Plug>nvim-autopairs-jump-right"
else
return "}"
end
end, opts)
vim.keymap.set("i", "]", function()
if npairs.jumpable() then
return "<Plug>nvim-autopairs-jump-right"
else
return "]"
end
end, opts)
-- 智能退格
vim.keymap.set("i", "<BS>", function()
return npairs.autopairs_bs()
end, opts)
-- 快速包裹
vim.keymap.set("v", "<M-e>", "<Plug>nvim-autopairs-fast-wrap", { noremap = true })
end,
}

10
lua/plugins/comment.lua Normal file
View File

@@ -0,0 +1,10 @@
return {
"numToStr/Comment.nvim",
dependencies = { "JoosepAlviste/nvim-ts-context-commentstring" },
config = function()
require("Comment").setup({
pre_hook = require("ts_context_commentstring.integrations.comment_nvim").create_pre_hook(),
})
end,
event = "BufReadPost",
}

15
lua/plugins/gitsigns.lua Normal file
View File

@@ -0,0 +1,15 @@
return {
"lewis6991/gitsigns.nvim",
config = function()
require("gitsigns").setup({
signs = {
add = { text = "+" },
change = { text = "~" },
delete = { text = "_" },
topdelete = { text = "" },
changedelete = { text = "~" },
},
current_line_blame = true,
})
end,
}

34
lua/plugins/init.lua Normal file
View File

@@ -0,0 +1,34 @@
local opts = {
git = {
log = { "-1" },
timeout = 120,
url_format = "git@github.com:%s.git",
filter = true,
throttle = {
enabled = false,
rate = 2,
duration = 5 * 1000,
},
cooldown = 0,
},
}
require("lazy").setup({
require("plugins.nvim-tree"),
require("plugins.lualine"),
require("plugins.theme"),
require("plugins.telescope"),
require("plugins.treesitter"),
require("plugins.notify"),
require("plugins.gitsigns"),
require("plugins.autopairs"),
require("plugins.comment"),
require("plugins.project"),
require("plugins.runner"),
-- LSP
require("plugins.lsp.mason"),
require("plugins.lsp.lspconfig"),
require("plugins.lsp.cmp"),
require("plugins.lsp.dap"),
require("plugins.lsp.conform"),
}, opts)

123
lua/plugins/lsp/cmp.lua Normal file
View 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
View 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
View 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
View 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 = "快速修复" })

View 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
View 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,
}

15
lua/plugins/lualine.lua Normal file
View File

@@ -0,0 +1,15 @@
return {
"nvim-lualine/lualine.nvim",
dependencies = { "nvim-tree/nvim-web-devicons" },
config = function()
require("lualine").setup({
options = {
icons_enabled = true,
theme = "tokyonight",
component_separators = { left = "", right = "" },
section_separators = { left = "", right = "" },
disabled_filetypes = { statusline = { "dashboard", "NvimTree" } },
},
})
end,
}

12
lua/plugins/notify.lua Normal file
View File

@@ -0,0 +1,12 @@
return {
"rcarriga/nvim-notify",
config = function()
vim.notify = require("notify")
require("notify").setup({
background_colour = "#000000",
render = "minimal",
stages = "fade_in_slide_out",
})
end,
event = "VeryLazy",
}

26
lua/plugins/nvim-tree.lua Normal file
View File

@@ -0,0 +1,26 @@
return {
"nvim-tree/nvim-tree.lua",
dependencies = { "nvim-tree/nvim-web-devicons" },
config = function()
require("nvim-tree").setup({
view = {
width = 30,
},
renderer = {
group_empty = true,
},
actions = {
open_file = {
window_picker = {
enable = false,
},
},
},
filters = {
dotfiles = false,
exclude = { ".git", "node_modules" },
},
})
vim.keymap.set("n", "<leader>e", "<cmd>NvimTreeToggle<cr>", { desc = "切换文件树" })
end,
}

12
lua/plugins/project.lua Normal file
View File

@@ -0,0 +1,12 @@
return {
"ahmedkhalf/project.nvim",
config = function()
require("project_nvim").setup({
detection_methods = { "pattern", "lsp" },
patterns = { ".git", "_darcs", ".hg", ".bzr", ".svn", "Makefile", "package.json" },
})
require("telescope").load_extension("projects")
vim.keymap.set("n", "<leader>fp", "<cmd>Telescope projects<cr>", { desc = "查找项目" })
end,
dependencies = { "nvim-telescope/telescope.nvim" },
}

17
lua/plugins/runner.lua Normal file
View File

@@ -0,0 +1,17 @@
return {
"CRAG666/code_runner.nvim",
dependencies = "nvim-lua/plenary.nvim",
config = function()
require("code_runner").setup({
filetype = {
python = "python -u",
lua = "lua",
sh = "bash",
},
})
-- 添加快捷键
vim.keymap.set("n", "<leader>rr", "<cmd>RunCode<cr>", { desc = "一键运行当前文件" })
vim.keymap.set("n", "<leader>rf", "<cmd>RunFile<cr>", { desc = "运行当前文件" })
vim.keymap.set("n", "<F5>", "<cmd>RunFile tab<cr>", { desc = "在新标签页运行" })
end,
}

23
lua/plugins/telescope.lua Normal file
View File

@@ -0,0 +1,23 @@
return {
"nvim-telescope/telescope.nvim",
dependencies = { "nvim-lua/plenary.nvim" },
config = function()
local builtin = require("telescope.builtin")
vim.keymap.set("n", "<leader>ff", builtin.find_files, { desc = "查找文件" })
vim.keymap.set("n", "<leader>fg", builtin.live_grep, { desc = "全局搜索" })
vim.keymap.set("n", "<leader>fb", builtin.buffers, { desc = "切换缓冲区" })
vim.keymap.set("n", "<leader>fh", builtin.help_tags, { desc = "搜索帮助" })
require("telescope").setup({
defaults = {
file_ignore_patterns = { "node_modules", ".git" },
mappings = {
i = {
["<C-u>"] = false,
["<C-d>"] = false,
},
},
},
})
end,
}

8
lua/plugins/theme.lua Normal file
View File

@@ -0,0 +1,8 @@
return {
"folke/tokyonight.nvim",
lazy = false,
priority = 1000,
config = function()
vim.cmd("colorscheme tokyonight")
end,
}

View File

@@ -0,0 +1,17 @@
return {
"nvim-treesitter/nvim-treesitter",
build = ":TSUpdate",
config = function()
require("nvim-treesitter.configs").setup({
ensure_installed = { "vim", "lua", "javascript", "typescript", "python", "html", "css", "markdown" },
sync_install = false,
auto_install = true,
highlight = {
enable = true,
additional_vim_regex_highlighting = false,
},
indent = { enable = true },
})
end,
event = "BufReadPost",
}

View File

@@ -0,0 +1,4 @@
-- 快速编辑配置
vim.api.nvim_create_user_command("EditConfig", function()
vim.cmd("e $MYVIMRC")
end, {})

5
lua/scripts/init.lua Normal file
View File

@@ -0,0 +1,5 @@
-- 快速编辑配置
require("scripts.edit-config")
-- 重新加载配置
require("scripts.reload-config")

View File

@@ -0,0 +1,5 @@
-- 重新加载配置
vim.api.nvim_create_user_command("ReloadConfig", function()
vim.cmd("source $MYVIMRC")
vim.notify("配置已重新加载", "info")
end, {})