nvim/lua/autocmds.lua
xfy 9c8deeafc8 Reorganize config modules into lua/plugins/ and add comprehensive docs
- Move lsp.lua, pick.lua, treesitter.lua, git.lua to lua/plugins/
- Update all require() paths in pack.lua and init.lua
- Unify code style: tab indentation, line length control, empty braces
- Add detailed Chinese module documentation to all config files
- Add grug-far.nvim plugin with <leader>sr binding
- Enable vim._core.ui2 for Neovim 0.12+ UI enhancements
- Disable additional built-in plugins to reduce startup overhead

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 16:42:02 +08:00

82 lines
3.6 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- =============================================================================
-- 自动命令配置 (lua/autocmds.lua)
-- =============================================================================
-- 自动命令autocmd是事件驱动的回调机制
-- 在特定编辑器事件(如打开文件、切换 buffer、进入插入模式等发生时自动执行。
--
-- 所有 autocmd 归属于 "UserAutocmds" 组,
-- 使用 { clear = true } 确保每次重载配置时先清除旧的 autocmd
-- 避免重复注册导致命令被多次触发。
-- =============================================================================
-- 创建用户 autocmd 组clear = true 表示创建前清空组内所有现有命令
local group = vim.api.nvim_create_augroup("UserAutocmds", { clear = true })
-- ---------------------------------------------------------------------------
-- 1. 恢复上次编辑位置
-- ---------------------------------------------------------------------------
-- 打开文件后,将光标恢复到上次关闭时的位置。
-- 原理Vim 在关闭文件时会保存光标位置到 `"` 标记(双引号标记),
-- 下次打开时可通过此标记恢复。
--
-- 跳过的情况:
-- - commit / gitcommitGit 提交消息文件,总是从头开始编辑
-- - xxd十六进制编辑模式
-- - gitrebaseGit rebase 交互式编辑
vim.api.nvim_create_autocmd("BufReadPost", {
group = group,
callback = function()
-- 获取 `"` 标记的位置(行, 列)
local mark = vim.api.nvim_buf_get_mark(0, '"')
local row = mark[1] -- 行号1-based
local ft = vim.bo.filetype
-- 仅当标记有效(行号 > 1 且在文件范围内)时恢复光标
if row > 1 and row <= vim.fn.line("$") then
-- 排除特定文件类型
if ft ~= "commit" and ft ~= "gitcommit" and not ft:match("xxd") and not ft:match("gitrebase") then
vim.api.nvim_win_set_cursor(0, mark)
end
end
end,
})
-- ---------------------------------------------------------------------------
-- 2. 注释延续行为
-- ---------------------------------------------------------------------------
-- 控制按回车或 o/O 时是否自动延续注释符号(如 //、#、--)。
--
-- formatoptions 说明:
-- o - 使用 o/O 换行时延续注释(被移除,避免在 normal 模式下意外延续)
-- r - 按回车时延续注释保留insert 模式下回车延续注释符)
--
-- 注意此设置对自动格式化conform.nvim的行为也有影响。
vim.api.nvim_create_autocmd("FileType", {
group = group,
callback = function()
-- 移除 'o':按 o/O 时不自动插入注释符号
vim.opt_local.formatoptions:remove("o")
-- 追加 'r':按回车时自动插入注释符号
vim.opt_local.formatoptions:append("r")
end,
})
-- ---------------------------------------------------------------------------
-- 3. 延迟设置 Treesitter 折叠
-- ---------------------------------------------------------------------------
-- 使用 treesitter 的 foldexpr 进行语法感知的代码折叠。
-- 延迟到 BufEnter 时设置(而非启动时),避免在 startup 阶段加载 treesitter 模块。
--
-- once = true 表示此 autocmd 只触发一次,首次 BufEnter 后自动销毁。
-- foldmethod = "expr" 表示使用表达式foldexpr计算折叠范围。
-- foldexpr = "v:lua.vim.treesitter.foldexpr()" 是 Neovim 0.10+ 的内置函数,
-- 基于 treesitter 语法树计算折叠边界。
vim.api.nvim_create_autocmd("BufEnter", {
group = group,
once = true,
callback = function()
vim.o.foldmethod = "expr"
vim.o.foldexpr = "v:lua.vim.treesitter.foldexpr()"
end,
})