Replies: 2 comments 3 replies
-
This is very similar to #734 (which u opened before). Since:
Haven't used AstroVim before, so I can't comment on this. Maybe you can describe some possible implementation details in #931? |
Beta Was this translation helpful? Give feedback.
-
Thanks, I just forgot your explanation in that pr and can't find it.
I will, there's a quick view. Un-expanded code -- File detection (File and GitFile) for lazy.nvim
{
{ 'BufReadPost', 'BufNewFile', 'BufWritePost' },
{
group = 'FileEvents',
callback = function(args)
if
not (vim.fn.expand '%' == '' or vim.api.nvim_get_option_value('buftype', { buf = args.buf }) == 'nofile')
then
release_event 'File'
if
require('utils').git.file_worktree()
or require('utils').command.cmd({ 'git', '-C', vim.fn.expand '%:p:h', 'rev-parse' }, false)
then
release_event 'GitFile'
vim.api.nvim_del_augroup_by_name 'FileEvents'
end
end
end,
},
}, autocmd handler if autocmd[2].group and vim.fn.exists('#' .. autocmd[2].group) == 0 then
vim.api.nvim_create_augroup(autocmd[2].group, { clear = true })
end
vim.api.nvim_create_autocmd(unpack(autocmd)) utils.command.cmd ---Run a shell command and capture the output and if the command succeeded or failed
---@param cmd string|string[] The terminal command to execute
---@param show_error? boolean Whether or not to show an unsuccessful command as an error to the user
---@return string|nil # The result of a successfully executed command or nil
function M.cmd(cmd, show_error)
if type(cmd) == 'string' then cmd = { cmd } end
if vim.fn.has 'win32' == 1 then cmd = vim.list_extend({ 'cmd.exe', '/C' }, cmd) end
local result = vim.fn.system(cmd)
local success = vim.api.nvim_get_vvar 'shell_error' == 0
if not success and (show_error == nil or show_error) then
vim.api.nvim_err_writeln(('Error running command %s\nError message:\n%s'):format(table.concat(cmd, ' '), result))
end
return success and result:gsub('[\27\155][][()#;?%d]*[A-PRZcf-ntqry=><~]', '') or nil
end utils.git.file_worktree function M.file_worktree(file, worktrees)
-- worktrees = worktrees or vim.g.git_worktrees
worktrees = worktrees
if not worktrees then return end
file = file or vim.fn.expand '%'
for _, worktree in ipairs(worktrees) do
if
vim.system({
'git',
'--work-tree',
worktree.toplevel,
'--git-dir',
worktree.gitdir,
'ls-files',
'--error-unmatch',
file,
}, false)
then
return worktree
end
end
end release_event --- Trigger an user event
---@param event string The event name to be appended to Astro
function M.release(event)
vim.schedule(
function()
vim.api.nvim_exec_autocmds('User', {
pattern = event,
modeline = false,
})
end
)
end These small feature units can be put in utils, so I did not modify them. After all, use 'User File' event and |
Beta Was this translation helpful? Give feedback.
-
lazy.nvim
provides a eventVeryLazy
, I want to know why we useCursorHold
andCursorHoldI
instead of that.File
andGitFile
like AstroVim, in order to setup plugins more precisely.Beta Was this translation helpful? Give feedback.
All reactions