改用neovim内置的注释功能

This commit is contained in:
caprain 2026-04-12 17:08:36 +08:00
parent 63f1d16bc4
commit a7c0a23077
6 changed files with 160 additions and 183 deletions

View file

@ -2,8 +2,8 @@
-- === map function
-- ===
local function mapkey(mode, lhs, rhs)
vim.keymap.set(mode, lhs, rhs, { silent = true, nowait = true })
local function mapkey(mode, lhs, rhs, opts)
vim.keymap.set(mode, lhs, rhs, vim.tbl_extend("force", { silent = true, nowait = true }, opts or {}))
end
local function mapcmd(key, cmd)
@ -90,6 +90,8 @@ maplua({ "x", "o", "n" }, "<CR>", smart_select("select_parent", 1), "扩大 Tree
-- 缩小范围 (退格键):不断向下寻找子节点
maplua({ "x", "o" }, "<BS>", smart_select("select_child", -1), "缩小 Treesitter/LSP 范围")
-- ===
-- === Cursor Movement
-- ===
@ -181,13 +183,9 @@ mapkey("x", "<tab>", ">gv")
-- === 批量替换
-- ===
-- 设置快捷键,替换所有文件内容
mapcmd("<leader>sa", "lua search_and_replace()")
-- 设置快捷键,替换当前文件内容
mapcmd("<leader>sr", "lua search_and_replace_current_file()")
-- 替换当前目录及子目录下所有文件内容
function search_and_replace()
local function search_and_replace()
return function ()
-- 获取用户输入的查找内容,使用 input() 函数动态输入替换内容
local search_text = vim.fn.input("Search for: ")
@ -208,27 +206,33 @@ function search_and_replace()
else
print("Search or replace text cannot be empty.")
end
end
end
-- 替换当前文件内容
function search_and_replace_current_file()
-- 获取用户输入的查找内容
local search_text = vim.fn.input("Search for in current file: ")
local function search_and_replace_current_file()
return function()
-- 获取用户输入的查找内容
local search_text = vim.fn.input("Search for in current file: ")
-- 获取用户输入的替换内容
local replace_text = vim.fn.input("Replace with: ")
-- 获取用户输入的替换内容
local replace_text = vim.fn.input("Replace with: ")
-- 执行替换命令
if search_text ~= "" and replace_text ~= "" then
-- 使用 sed 替换当前文件中的匹配内容,并正确转义引号
local cmd = string.format("!sed -i 's/%s/%s/g' %%", search_text, replace_text)
vim.cmd(cmd)
print("Replaced all occurrences of '" .. search_text .. "' with '" .. replace_text .. "' in current file.")
else
print("Search or replace text cannot be empty.")
-- 执行替换命令
if search_text ~= "" and replace_text ~= "" then
-- 使用 sed 替换当前文件中的匹配内容,并正确转义引号
local cmd = string.format("!sed -i 's/%s/%s/g' %%", search_text, replace_text)
vim.cmd(cmd)
print("Replaced all occurrences of '" .. search_text .. "' with '" .. replace_text .. "' in current file.")
else
print("Search or replace text cannot be empty.")
end
end
end
maplua("n","<leader>sa",search_and_replace(), "替换当前目录及子目录下所有文件内容")
maplua("n","<leader>sr",search_and_replace_current_file(), "替换当前文件内容")
-- ===
-- === 临时“存档”文件当前的版本,并与后续的修改进行 diff 对比
-- ===
@ -264,6 +268,12 @@ mapkey({ "n", "x", "o" }, "<LEADER><LEADER>", "<Esc>/<++><CR>:nohlsearch<CR>c4l"
-- 拼写检查
mapcmd("<LEADER>sc", "set spell!")
-- 注释快捷键
mapkey("n", "<leader>cn", "gcc", { remap = true })
mapkey("x", "<leader>cn", "gc", { remap = true })
mapkey("n", "<leader>cu", "gcc", { remap = true })
mapkey("x", "<leader>cu", "gc", { remap = true })
-- ===
-- === 运行代码(该功能已经迁移到plugins/coderunner.lua)
-- ===

View file

@ -4,46 +4,45 @@ map:cmd('tu', 'enew')
-- 关闭当前缓冲区neovim自带的完整命令bdelete
map:cmd('tq', 'bd')
-- 在缓冲区之间移动
map:cmd('tn', 'BufferLineCyclePrev')
map:cmd('ti', 'BufferLineCycleNext')
-- 移动缓冲区的位置
map:cmd('tmn', 'BufferLineMovePrev')
map:cmd('tmi', 'BufferLineMoveNext')
-- 关闭缓冲区
map:cmd('tN', 'BufferLineCloseLeft')
map:cmd('tI', 'BufferLineCloseRight')
map:cmd('tQ', 'BufferLineCloseOthers')
-- === 顶部标签栏 (Bufferline) ===
if vim.g.vscode then return end
local P = {
name = "bufferline.nvim",
module = "bufferline",
deps = { "nvim-web-devicons" }, -- 确保图标库先加载
name = "bufferline.nvim",
module = "bufferline",
deps = { "nvim-web-devicons" }, -- 确保图标库先加载
}
-- 懒加载触发器:打开或新建文件时触发
vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, {
once = true,
callback = function()
PackUtils.load(P, function(plugin)
plugin.setup({
options = {
modified_icon = "",
buffer_close_icon = "×",
-- show_buffer_close_icons = false,
max_name_length = 14,
max_prefix_length = 13,
tab_size = 10,
indicator = {
style = "none",
},
},
})
end)
end
once = true,
callback = function()
PackUtils.load(P, function(plugin)
-- 在缓冲区之间移动
map:cmd('tn', 'BufferLineCyclePrev')
map:cmd('ti', 'BufferLineCycleNext')
-- 移动缓冲区的位置
map:cmd('tmn', 'BufferLineMovePrev')
map:cmd('tmi', 'BufferLineMoveNext')
-- 关闭缓冲区
map:cmd('tN', 'BufferLineCloseLeft')
map:cmd('tI', 'BufferLineCloseRight')
map:cmd('tQ', 'BufferLineCloseOthers')
plugin.setup({
options = {
modified_icon = "",
buffer_close_icon = "×",
-- show_buffer_close_icons = false,
max_name_length = 14,
max_prefix_length = 13,
tab_size = 10,
indicator = {
style = "none",
},
},
})
end)
end
})

View file

@ -1,26 +0,0 @@
-- 注释快捷键
local map = require("core.keymap")
map:key("n", "<space>cn", "<Plug>kommentary_line_increase")
map:key("x", "<space>cn", "<Plug>kommentary_visual_increase<esc>") -- 选择模式下注释/反注释后退出该模式
map:key("n", "<space>cu", "<Plug>kommentary_line_decrease")
map:key("x", "<space>cu", "<plug>kommentary_visual_decrease<esc>")
local P = {
name = "b3nj5m1n/kommentary", -- 仓库名
module = "kommentary.config", -- require模块名
}
-- 懒加载触发器
vim.api.nvim_create_autocmd({
"UIEnter",
}, {
callback = function()
vim.schedule(function()
PackUtils.load(P, function(plugin)
plugin.configure_language("default", {
prefer_single_line_comments = true,
})
end)
end)
end
})

View file

@ -7,115 +7,115 @@ local is_arm = arch:match("arm") or arch:match("aarch64")
local servers = { "lua_ls", "rust_analyzer", "pylsp" }
if not is_arm then
vim.list_extend(servers, { "marksman", "ts_ls", "svelte", "cssls", "html" })
vim.list_extend(servers, { "marksman", "ts_ls", "svelte", "cssls", "html" })
end
-- 2. 插件配置清单
local P = {
name = "nvim-lspconfig",
module = "lspconfig",
deps = { "mason.nvim", "mason-lspconfig.nvim", "inlay-hints.nvim" },
name = "nvim-lspconfig",
module = "lspconfig",
deps = { "mason.nvim", "mason-lspconfig.nvim", "inlay-hints.nvim" },
}
-- 3. 懒加载触发器:当打开文件时触发
vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, {
callback = function()
PackUtils.load(P, function()
-- === A. 基础依赖初始化 (Mason) ===
require("mason").setup()
require("mason-lspconfig").setup({
ensure_installed = servers,
})
require("inlay-hints").setup()
callback = function()
PackUtils.load(P, function()
-- === A. 基础依赖初始化 (Mason) ===
require("mason").setup()
require("mason-lspconfig").setup({
ensure_installed = servers,
})
require("inlay-hints").setup()
-- === B. 全局诊断设置 ===
vim.diagnostic.config({
signs = {
text = {
[vim.diagnostic.severity.ERROR] = "",
[vim.diagnostic.severity.WARN] = "",
[vim.diagnostic.severity.HINT] = "",
[vim.diagnostic.severity.INFO] = "»",
},
},
})
-- === B. 全局诊断设置 ===
vim.diagnostic.config({
signs = {
text = {
[vim.diagnostic.severity.ERROR] = "",
[vim.diagnostic.severity.WARN] = "",
[vim.diagnostic.severity.HINT] = "",
[vim.diagnostic.severity.INFO] = "»",
},
},
})
-- === C. 全局快捷键映射 ===
-- local opts = { noremap = true, silent = true }
vim.keymap.set("n", "<leader>h", vim.lsp.buf.hover, opts) -- <space>h显示提示文档
vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts) -- gd跳转到定义
vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts) -- gD跳转到声明(例如c语言中的头文件中的原型、一个变量的extern声明)
vim.keymap.set("n", "go", vim.lsp.buf.type_definition, opts) -- go跳转到变量类型定义的位置(例如一些自定义类型)
vim.keymap.set("n", "<leader>rn", vim.lsp.buf.rename, opts) -- <space>rn变量重命名
vim.keymap.set("n", "<leader>aw", vim.lsp.buf.code_action, opts) -- <space>aw可以在出现警告或错误的地方打开建议的修复方法
vim.keymap.set("n", "<leader>d", vim.diagnostic.open_float, opts) -- <space>d浮动窗口显示所在行警告或错误信息
vim.keymap.set("n", "<leader>-", vim.diagnostic.goto_prev, opts) -- <space>-跳转到上一处警告或错误的地方
vim.keymap.set("n", "<leader>=", vim.diagnostic.goto_next, opts) -- <space>+跳转到下一处警告或错误的地方
-- vim.keymap.set("n", "gr", vim.lsp.buf.references, opts) -- gr跳转到引用了对应变量或函数的位置改用snacks
-- vim.keymap.set({ 'n', 'x' }, '<leader>f', function() vim.lsp.buf.format({ async = true }) end, opts) -- <space>f进行代码格式化
-- === C. 全局快捷键映射 ===
-- local opts = { noremap = true, silent = true }
vim.keymap.set("n", "<leader>h", vim.lsp.buf.hover, opts) -- <space>h显示提示文档
vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts) -- gd跳转到定义
vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts) -- gD跳转到声明(例如c语言中的头文件中的原型、一个变量的extern声明)
vim.keymap.set("n", "go", vim.lsp.buf.type_definition, opts) -- go跳转到变量类型定义的位置(例如一些自定义类型)
vim.keymap.set("n", "<leader>rn", vim.lsp.buf.rename, opts) -- <space>rn变量重命名
vim.keymap.set("n", "<leader>aw", vim.lsp.buf.code_action, opts) -- <space>aw可以在出现警告或错误的地方打开建议的修复方法
vim.keymap.set("n", "<leader>d", vim.diagnostic.open_float, opts) -- <space>d浮动窗口显示所在行警告或错误信息
vim.keymap.set("n", "<leader>-", vim.diagnostic.goto_prev, opts) -- <space>-跳转到上一处警告或错误的地方
vim.keymap.set("n", "<leader>=", vim.diagnostic.goto_next, opts) -- <space>+跳转到下一处警告或错误的地方
-- vim.keymap.set("n", "gr", vim.lsp.buf.references, opts) -- gr跳转到引用了对应变量或函数的位置改用snacks
-- vim.keymap.set({ 'n', 'x' }, '<leader>f', function() vim.lsp.buf.format({ async = true }) end, opts) -- <space>f进行代码格式化
-- === D. 特定 LSP 配置 (使用 Neovim 0.11+ vim.lsp.config 语法) ===
-- === D. 特定 LSP 配置 (使用 Neovim 0.11+ vim.lsp.config 语法) ===
-- Python (pylsp) + uv 虚拟环境自适应
vim.lsp.config("pylsp", {
on_init = function(client)
-- 1. 安全获取 root_dir
local root_dir = client.config.root_dir
-- 2. 只有当 root_dir 不为 nil 时才进行后续探测
if root_dir then
local venv_python = root_dir .. "/.venv/bin/python"
-- 3. 检查虚拟环境文件是否真的存在且可读
if vim.fn.filereadable(venv_python) == 1 then
client.config.settings.pylsp.plugins.jedi.environment = venv_python
-- 只有修改了设置才发送通知
client.notify("workspace/didChangeConfiguration", {
settings = client.config.settings
})
end
end
-- 无论是否找到虚拟环境,都返回 true 让 LSP 继续启动
return true
end,
settings = {
pylsp = {
plugins = {
jedi = { environment = nil }
}
}
},
})
-- Python (pylsp) + uv 虚拟环境自适应
vim.lsp.config("pylsp", {
on_init = function(client)
-- 1. 安全获取 root_dir
local root_dir = client.config.root_dir
-- 2. 只有当 root_dir 不为 nil 时才进行后续探测
if root_dir then
local venv_python = root_dir .. "/.venv/bin/python"
-- 3. 检查虚拟环境文件是否真的存在且可读
if vim.fn.filereadable(venv_python) == 1 then
client.config.settings.pylsp.plugins.jedi.environment = venv_python
-- 只有修改了设置才发送通知
client.notify("workspace/didChangeConfiguration", {
settings = client.config.settings
})
end
end
-- 无论是否找到虚拟环境,都返回 true 让 LSP 继续启动
return true
end,
settings = {
pylsp = {
plugins = {
jedi = { environment = nil }
}
}
},
})
-- Lua (lua_ls)
vim.lsp.config("lua_ls", {
settings = {
["Lua"] = {
hint = { enable = true },
diagnostics = { globals = { "vim", "require", "opts", "PackUtils", "jit" } },
},
},
})
-- Lua (lua_ls)
vim.lsp.config("lua_ls", {
settings = {
["Lua"] = {
hint = { enable = true },
diagnostics = { globals = { "vim", "require", "opts", "PackUtils", "jit" } },
},
},
})
-- Go (gopls)
vim.lsp.config("gopls", {
settings = {
["gopls"] = {
hints = {
rangeVariableTypes = true,
parameterNames = true,
constantValues = true,
assignVariableTypes = true,
compositeLiteralFields = true,
compositeLiteralTypes = true,
functionTypeParameters = true,
},
},
},
})
-- Go (gopls)
vim.lsp.config("gopls", {
settings = {
["gopls"] = {
hints = {
rangeVariableTypes = true,
parameterNames = true,
constantValues = true,
assignVariableTypes = true,
compositeLiteralFields = true,
compositeLiteralTypes = true,
functionTypeParameters = true,
},
},
},
})
-- 遍历列表并正式启用服务器
for _, server in ipairs(servers) do
vim.lsp.enable(server)
end
end)
end
-- 遍历列表并正式启用服务器
for _, server in ipairs(servers) do
vim.lsp.enable(server)
end
end)
end
})

View file

@ -23,8 +23,6 @@ local specs = {
"https://github.com/kevinhwang91/promise-async",
-- conform.lua 格式化工具formatter
"https://github.com/stevearc/conform.nvim",
-- kommentary.lua 注释插件
"https://github.com/b3nj5m1n/kommentary",
-- noice.lua 取代消息、命令行和弹出菜单的 UI
"https://github.com/folke/noice.nvim",
"https://github.com/MunifTanjim/nui.nvim",