1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 10:40:03 +08:00

feat(projectmanager): make reg_callback support description

This commit is contained in:
Eric Wong 2024-02-24 16:28:33 +08:00
parent 031a6a2497
commit ea76ba75aa
3 changed files with 48 additions and 31 deletions

View File

@ -34,10 +34,17 @@ if has('nvim-0.5.0')
function! SpaceVim#plugins#projectmanager#RootchandgeCallback() abort
lua require("spacevim.plugin.projectmanager").RootchandgeCallback()
endfunction
function! SpaceVim#plugins#projectmanager#reg_callback(func) abort
lua require("spacevim.plugin.projectmanager").reg_callback(
\ require("spacevim").eval("string(a:func)")
\ )
function! SpaceVim#plugins#projectmanager#reg_callback(func, ...) abort
if a:0 == 0
lua require("spacevim.plugin.projectmanager").reg_callback(
\ require("spacevim").eval("string(a:func)")
\ )
else
lua require("spacevim.plugin.projectmanager").reg_callback(
\ require("spacevim").eval("string(a:func)"),
\ require("spacevim").eval("a:1")
\ )
endif
endfunction
function! SpaceVim#plugins#projectmanager#current_root() abort
return luaeval('require("spacevim.plugin.projectmanager").current_root()')
@ -333,11 +340,12 @@ else
endfunction
let s:project_callback = []
function! SpaceVim#plugins#projectmanager#reg_callback(func) abort
function! SpaceVim#plugins#projectmanager#reg_callback(func, ...) abort
" support second argv
if type(a:func) == 2
call add(s:project_callback, a:func)
else
call SpaceVim#logger#warn('can not register the project callback: ' . string(a:func))
call SpaceVim#logger#warn('can not register the project callback: ' . get(a:000, 0, string(a:func)))
endif
endfunction
@ -384,13 +392,13 @@ else
if name !=# ''
call s:BUFFER.filter_do(
\ {
\ 'expr' : [
\ 'buflisted(v:val)',
\ 'getbufvar(v:val, "_spacevim_project_name") == "' . name . '"',
\ ],
\ 'do' : 'bd %d'
\ }
\ )
\ 'expr' : [
\ 'buflisted(v:val)',
\ 'getbufvar(v:val, "_spacevim_project_name") == "' . name . '"',
\ ],
\ 'do' : 'bd %d'
\ }
\ )
endif
endfunction

View File

@ -276,7 +276,7 @@ end
function M.open()
if not project_manager_registered then
require('spacevim.plugin.projectmanager').reg_callback(M.on_cwd_changed)
require('spacevim.plugin.projectmanager').reg_callback(M.on_cwd_changed, 'git_remote_on_cwd_changed')
project_manager_registered = true
end
if bufnr ~= -1 and vim.api.nvim_buf_is_valid(bufnr) then

View File

@ -343,29 +343,38 @@ function M.RootchandgeCallback()
-- let b:_spacevim_project_name = g:_spacevim_project_name
fn.setbufvar('%', '_spacevim_project_name', project.name)
for _, Callback in pairs(project_callback) do
if type(Callback) == 'string' then
logger.debug(' run callback:' .. Callback)
fn.call(Callback, {})
elseif type(Callback) == 'function' then
logger.debug(' run callback:' .. tostring(Callback))
pcall(Callback)
if type(Callback.func) == 'string' then
if Callback.desc then
logger.debug(' run callback:' .. Callback.desc)
else
logger.debug(' run callback:' .. Callback.func)
end
fn.call(Callback.func, {})
elseif type(Callback.func) == 'function' then
if Callback.desc then
logger.debug(' run callback:' .. Callback.desc)
else
logger.debug(' run callback:' .. tostring(Callback.func))
end
pcall(Callback.func)
end
end
end
function M.reg_callback(func)
if type(func) == 'string' then
if string.match(func, '^function%(') ~= nil then
table.insert(project_callback, string.sub(func, 11, -3))
else
table.insert(project_callback, func)
function M.reg_callback(func, ...)
local callback = { func = func }
local argv = { ... }
if argv[1] then
callback.desc = argv[1]
end
if type(callback.func) == 'string' or type(callback.func) == 'function' then
if type(callback.func) == 'string' and string.match(callback.func, '^function%(') ~= nil then
callback.func = string.sub(callback.func, 11, -3)
end
elseif type(func) == 'function' then
-- support lua function
table.insert(project_callback, func)
table.insert(project_callback, callback)
else
logger.warn('type of func is:' .. type(func))
logger.warn('can not register the project callback: ' .. fn.string(func))
logger.warn('type of func is:' .. type(callback.func))
logger.warn('can not register the project callback: ' .. fn.string(callback.func))
end
end