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

View file

@ -4,46 +4,45 @@ map:cmd('tu', 'enew')
-- 关闭当前缓冲区neovim自带的完整命令bdelete -- 关闭当前缓冲区neovim自带的完整命令bdelete
map:cmd('tq', 'bd') 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) === -- === 顶部标签栏 (Bufferline) ===
if vim.g.vscode then return end if vim.g.vscode then return end
local P = { local P = {
name = "bufferline.nvim", name = "bufferline.nvim",
module = "bufferline", module = "bufferline",
deps = { "nvim-web-devicons" }, -- 确保图标库先加载 deps = { "nvim-web-devicons" }, -- 确保图标库先加载
} }
-- 懒加载触发器:打开或新建文件时触发 -- 懒加载触发器:打开或新建文件时触发
vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, { vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, {
once = true, once = true,
callback = function() callback = function()
PackUtils.load(P, function(plugin) PackUtils.load(P, function(plugin)
plugin.setup({ -- 在缓冲区之间移动
options = { map:cmd('tn', 'BufferLineCyclePrev')
modified_icon = "", map:cmd('ti', 'BufferLineCycleNext')
buffer_close_icon = "×",
-- show_buffer_close_icons = false, -- 移动缓冲区的位置
max_name_length = 14, map:cmd('tmn', 'BufferLineMovePrev')
max_prefix_length = 13, map:cmd('tmi', 'BufferLineMoveNext')
tab_size = 10,
indicator = { -- 关闭缓冲区
style = "none", map:cmd('tN', 'BufferLineCloseLeft')
}, map:cmd('tI', 'BufferLineCloseRight')
}, map:cmd('tQ', 'BufferLineCloseOthers')
}) plugin.setup({
end) options = {
end 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" } local servers = { "lua_ls", "rust_analyzer", "pylsp" }
if not is_arm then 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 end
-- 2. 插件配置清单 -- 2. 插件配置清单
local P = { local P = {
name = "nvim-lspconfig", name = "nvim-lspconfig",
module = "lspconfig", module = "lspconfig",
deps = { "mason.nvim", "mason-lspconfig.nvim", "inlay-hints.nvim" }, deps = { "mason.nvim", "mason-lspconfig.nvim", "inlay-hints.nvim" },
} }
-- 3. 懒加载触发器:当打开文件时触发 -- 3. 懒加载触发器:当打开文件时触发
vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, { vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, {
callback = function() callback = function()
PackUtils.load(P, function() PackUtils.load(P, function()
-- === A. 基础依赖初始化 (Mason) === -- === A. 基础依赖初始化 (Mason) ===
require("mason").setup() require("mason").setup()
require("mason-lspconfig").setup({ require("mason-lspconfig").setup({
ensure_installed = servers, ensure_installed = servers,
}) })
require("inlay-hints").setup() require("inlay-hints").setup()
-- === B. 全局诊断设置 === -- === B. 全局诊断设置 ===
vim.diagnostic.config({ vim.diagnostic.config({
signs = { signs = {
text = { text = {
[vim.diagnostic.severity.ERROR] = "", [vim.diagnostic.severity.ERROR] = "",
[vim.diagnostic.severity.WARN] = "", [vim.diagnostic.severity.WARN] = "",
[vim.diagnostic.severity.HINT] = "", [vim.diagnostic.severity.HINT] = "",
[vim.diagnostic.severity.INFO] = "»", [vim.diagnostic.severity.INFO] = "»",
}, },
}, },
}) })
-- === C. 全局快捷键映射 === -- === C. 全局快捷键映射 ===
-- local opts = { noremap = true, silent = true } -- local opts = { noremap = true, silent = true }
vim.keymap.set("n", "<leader>h", vim.lsp.buf.hover, opts) -- <space>h显示提示文档 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.definition, opts) -- gd跳转到定义
vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts) -- gD跳转到声明(例如c语言中的头文件中的原型、一个变量的extern声明) 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", "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>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>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>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_prev, opts) -- <space>-跳转到上一处警告或错误的地方
vim.keymap.set("n", "<leader>=", vim.diagnostic.goto_next, 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", "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进行代码格式化 -- 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 虚拟环境自适应 -- Python (pylsp) + uv 虚拟环境自适应
vim.lsp.config("pylsp", { vim.lsp.config("pylsp", {
on_init = function(client) on_init = function(client)
-- 1. 安全获取 root_dir -- 1. 安全获取 root_dir
local root_dir = client.config.root_dir local root_dir = client.config.root_dir
-- 2. 只有当 root_dir 不为 nil 时才进行后续探测 -- 2. 只有当 root_dir 不为 nil 时才进行后续探测
if root_dir then if root_dir then
local venv_python = root_dir .. "/.venv/bin/python" local venv_python = root_dir .. "/.venv/bin/python"
-- 3. 检查虚拟环境文件是否真的存在且可读 -- 3. 检查虚拟环境文件是否真的存在且可读
if vim.fn.filereadable(venv_python) == 1 then if vim.fn.filereadable(venv_python) == 1 then
client.config.settings.pylsp.plugins.jedi.environment = venv_python client.config.settings.pylsp.plugins.jedi.environment = venv_python
-- 只有修改了设置才发送通知 -- 只有修改了设置才发送通知
client.notify("workspace/didChangeConfiguration", { client.notify("workspace/didChangeConfiguration", {
settings = client.config.settings settings = client.config.settings
}) })
end end
end end
-- 无论是否找到虚拟环境,都返回 true 让 LSP 继续启动 -- 无论是否找到虚拟环境,都返回 true 让 LSP 继续启动
return true return true
end, end,
settings = { settings = {
pylsp = { pylsp = {
plugins = { plugins = {
jedi = { environment = nil } jedi = { environment = nil }
} }
} }
}, },
}) })
-- Lua (lua_ls) -- Lua (lua_ls)
vim.lsp.config("lua_ls", { vim.lsp.config("lua_ls", {
settings = { settings = {
["Lua"] = { ["Lua"] = {
hint = { enable = true }, hint = { enable = true },
diagnostics = { globals = { "vim", "require", "opts", "PackUtils", "jit" } }, diagnostics = { globals = { "vim", "require", "opts", "PackUtils", "jit" } },
}, },
}, },
}) })
-- Go (gopls) -- Go (gopls)
vim.lsp.config("gopls", { vim.lsp.config("gopls", {
settings = { settings = {
["gopls"] = { ["gopls"] = {
hints = { hints = {
rangeVariableTypes = true, rangeVariableTypes = true,
parameterNames = true, parameterNames = true,
constantValues = true, constantValues = true,
assignVariableTypes = true, assignVariableTypes = true,
compositeLiteralFields = true, compositeLiteralFields = true,
compositeLiteralTypes = true, compositeLiteralTypes = true,
functionTypeParameters = true, functionTypeParameters = true,
}, },
}, },
}, },
}) })
-- 遍历列表并正式启用服务器 -- 遍历列表并正式启用服务器
for _, server in ipairs(servers) do for _, server in ipairs(servers) do
vim.lsp.enable(server) vim.lsp.enable(server)
end end
end) end)
end end
}) })

View file

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

View file

@ -28,10 +28,6 @@
"rev": "11be32be3761c6263df2311afb6baa0de0863967", "rev": "11be32be3761c6263df2311afb6baa0de0863967",
"src": "https://github.com/MysticalDevil/inlay-hints.nvim" "src": "https://github.com/MysticalDevil/inlay-hints.nvim"
}, },
"kommentary": {
"rev": "d5a111a3bc4109a8f913a5863c9092b3b3801482",
"src": "https://github.com/b3nj5m1n/kommentary"
},
"mason-lspconfig.nvim": { "mason-lspconfig.nvim": {
"rev": "25f609e7fca78af7cede4f9fa3af8a94b1c4950b", "rev": "25f609e7fca78af7cede4f9fa3af8a94b1c4950b",
"src": "https://github.com/williamboman/mason-lspconfig.nvim" "src": "https://github.com/williamboman/mason-lspconfig.nvim"