robin/lynn.nvim
native-first neovim plugin manager with charm
git clone https://git.robinwobin.dev/robin/lynn.nvim
b0bc793
main
1---@module 'lynn' 2 3local nonnil =vim . nonnil or vim . F . if_nil 4 5local lzrq =function ( modname ) 6return setmetatable ( { 7modname =modname , 8} , { 9__index =function ( t , k ) 10local m =rawget ( t , "modname" ) 11if not m then 12return 13end 14return require ( m )[ k ] 15end , 16} ) 17end 18 19local utils =lzrq ( "lynn.utils" ) 20 21local lynn ={} 22 23-- utils -- 24 25local did_ui_enter =false 26local notif_queue =nil 27local function notify ( ... ) 28if did_ui_enter then 29return vim . notify ( ... ) 30end 31 32notif_queue =notif_queue or {} 33notif_queue [ # notif_queue + 1 ] ={ ... } 34end 35 36lynn . do_notify =function () 37did_ui_enter =true 38 39if not notif_queue then 40return 41end 42 43vim . schedule ( function () 44for _ , v in ipairs ( notif_queue ) do 45notify ( unpack ( v )) 46end 47notif_queue =nil 48end ) 49end 50 51---@param v any 52---@return string 53local function inspect ( v ) 54if type ( v ) == "table" and not vim . islist ( v ) then 55return table . concat ( 56vim 57. iter ( pairs ( v )) 58: map ( function ( key , value ) 59return string . format ( "%s=%q" , inspect ( key ), inspect ( value )) 60end ) 61: totable (), 62" " 63) 64end 65 66return type ( v ) ~= "string" and vim . inspect ( v ) or v 67end 68 69---@param ... any 70---@return string 71local function logfmt ( ... ) 72local c ={ ... } 73local t =vim 74. iter ( ipairs ( c )) 75: map ( function ( _ , item ) 76return inspect ( item ) 77end ) 78: totable () 79return table . concat ( t , " " ) 80end 81 82---@param ... any 83local function logdebug ( ... ) 84if vim . o . debug ~= "" then 85notify ( logfmt ( ... ), vim . log . levels . DEBUG ) 86end 87end 88 89---@param ... any 90---@return string? 91local function logerr ( ... ) 92local l =logfmt ( ... ) 93 94notify ( l , vim . log . levels . ERROR ) 95 96if vim . o . debug == "throw" then 97return error ( l ) 98end 99end 100 101---@param msg string 102---@param trace string 103---@return string? 104local function logtrace ( msg , trace ) 105return logerr ( string . format ( "lynn: %s:\n\t%s" , msg , trace )) 106end 107 108---@class lynn.plug 109---@field name string 110---@field url string 111---@field path string 112---@field version? string|vim.VersionRange 113---@field lazy? boolean 114---@field event? string|string[] 115---@field init? function function run before loading the plugin 116---@field load? function function run to load the plugin 117---@field config? function function run after loading the plugin 118---@field build? string|function 119---@field ensure? boolean defaults to true, ensure the package is installed 120---@field deps? string[] 121 122---@class lynn.plug.spec : lynn.plug 123---@field [1] string 124---@field name? string 125---@field url? string 126---@field path? string 127 128--- { [path]: { plug: <plug>, id: <id> } } 129---@type table<string, { plug: lynn.plug, id: number }> 130lynn . loaded ={} 131 132lynn . packdir =vim . fs . joinpath ( vim . fn . stdpath ( "data" ), "site" , "pack" , "core" , "opt" ) 133 134lynn . group =vim . api . nvim_create_augroup ( "@lynn.lazy" , { clear =true } ) 135 136local n_loaded =0 137 138--- { [name]: <plug> } 139---@class table<string, lynn.plug> 140lynn . plugins ={} 141 142-- hooks -- 143 144---@alias lynn.hook fun(spec: lynn.plug) 145---@alias lynn.hook.builtin 146---|'init' 147---|'load' 148---|'config' 149---|'build' 150 151---@type table<lynn.hook.builtin, lynn.hook> 152lynn . default_hooks ={ 153init =function ( _ ) end , 154load =function ( spec ) 155local ok , result =pcall ( vim . cmd . packadd , { spec . name , bang =vim . v . vim_did_init == 0 } ) 156if not ok then 157logerr ( "error running |:packadd|" , { [ "spec.name" ] =spec . name } ) 158return logtrace ( "load" , result ) 159end 160 161if not vim . v . vim_did_init == 0 then 162local after_paths =vim . fn . glob ( spec . path .. "/after/plugin/**/*.{vim,lua}" , false , true ) 163vim . tbl_map ( function ( path ) 164pcall ( vim . cmd . source , vim . fn . fnameescape ( path )) 165end , after_paths ) 166end 167end , 168config =function ( spec ) 169vim . cmd . runtime ( { "config/" .. spec . name .. ".lua" , bang =true } ) 170end , 171build =function ( spec ) 172if type ( spec . build ) ~= "string" then 173return 174end 175---@type string 176---@diagnostic disable-next-line: assign-type-mismatch 177local buildstr =spec . build 178if not buildstr or buildstr == "" then 179return 180end 181 182if string . sub ( buildstr , 0 , 1 ) == ":" then 183vim . cmd ( string . sub ( buildstr , 2 )) 184return 185end 186 187local result =vim 188. system ( vim . split ( buildstr , " " ), { 189cwd =spec . path , 190} ) 191: wait () 192if result . code == 0 then 193return 194end 195 196logerr ( "error running build command" , { [ "spec.name" ] =spec . name } ) 197return logtrace ( "build" , result . stderr ) 198end , 199} 200 201---@param plug lynn.plug 202---@param hook string 203---@param fn function 204local function wraphook ( plug , hook , fn ) 205if not fn or type ( fn ) ~= "function" then 206return 207end 208 209logdebug ( "running hook" , { hook =hook , [ "plug.name" ] =plug . name } ) 210do 211local ok , result =pcall ( fn , plug ) 212if not ok then 213logerr ( "error running hook" , { hook =hook , [ "plug.name" ] =plug . name } ) 214return logtrace ( "wraphook" , result ) 215end 216end 217end 218 219---@param plug lynn.plug 220---@param hook string 221---@param use_default? boolean 222function lynn . runhook ( plug , hook , use_default ) 223if type ( hook ) ~= "string" then 224return 225end 226local fn =plug [ hook ] 227if not fn or type ( fn ) ~= "function" then 228if not use_default then 229return 230end 231wraphook ( plug , hook , lynn . default_hooks [ hook ]) 232return 233end 234 235wraphook ( plug , hook , fn ) 236end 237 238---@param plug lynn.plug 239---@return integer id 240local function pack_lazy ( plug ) 241if type ( plug . event ) == "string" then 242local event =vim . split ( plug . event , " " ) 243return vim . api . nvim_create_autocmd ( event [ 1 ], { 244group =lynn . group , 245pattern =event [ 2 ], 246once =true , 247callback =function () 248lynn . plugadd ( plug ) 249end , 250} ) 251end 252 253return vim . api . nvim_create_autocmd ( plug . event , { 254group =lynn . group , 255once =true , 256callback =function () 257lynn . plugadd ( plug ) 258end , 259} ) 260end 261 262--- load a plugin 263--- - add the plugin to the loaded list 264--- - check deps 265--- - run |:packadd| 266--- - run init/config hooks 267--- - check if `after/` dirs should be loaded 268---@param plug lynn.plug 269function lynn . plugadd ( plug ) 270if type ( plug ) == "string" then 271plug =lynn . plugins [ plug ] 272end 273if lynn . loaded [ plug . path ] then 274return 275end 276 277-- add dependencies 278if plug . deps then 279vim . tbl_map ( lynn . load , plug . deps ) 280end 281 282-- add to loaded 283n_loaded =n_loaded + 1 284lynn . loaded [ plug . path ] ={ plug =plug , id =n_loaded } 285 286lynn . runhook ( plug , "init" , true ) 287lynn . runhook ( plug , "load" , true ) 288lynn . runhook ( plug , "config" , true ) 289end 290 291---@param plug lynn.plug.spec|string 292---@return lynn.plug 293function lynn . norm ( plug ) 294if type ( plug ) == "string" then 295plug ={ plug } 296end 297plug . url =plug . url or plug [ 1 ] 298if plug . path and not plug . url then 299plug . url ="file://" .. vim . fs . normalize ( plug . path ) 300end 301plug . url =utils . norm_url ( plug . url ) 302plug . name =plug . name or utils . get_name ( plug . url ) 303plug . path =plug . path or vim . fs . joinpath ( lynn . packdir , plug . name ) 304---@diagnostic disable-next-line: cast-type-mismatch 305---@cast plug lynn.plug 306 307plug . ensure =nonnil ( plug . ensure , true ) 308return plug 309end 310 311--- translate lynn.plug to vim.pack.Spec format 312---@param plug lynn.plug 313---@return vim.pack.Spec 314function lynn . translate ( plug ) 315return { 316src =plug . url , 317name =plug . name , 318version =plug . version , 319data ={ 320init =plug . init , 321load =plug . load , 322config =plug . config , 323build =plug . build , 324ensure =plug . ensure , 325} , 326} 327end 328 329--- register a plugin 330---@param plug lynn.plug.spec|string 331function lynn . register ( plug ) 332local p =lynn . norm ( plug ) 333 334lynn . plugins [ p . name ] =vim . tbl_extend ( "keep" , p , lynn . plugins [ p . name ] or {} ) 335if p . ensure then 336vim . pack . add ( { lynn . translate ( p ) } , { load =false } ) 337end 338end 339 340--- load a plugin by either running `lynn.plugadd` or wrapping the callback in 341--- an autocmd 342---@param name string 343function lynn . load ( name ) 344local plug =lynn . plugins [ name ] 345if not plug then 346return logerr ( "plugin not found" , { name =name } ) 347end 348 349if lynn . loaded [ plug . path ] then 350return 351end 352 353if not plug . lazy and not plug . event then 354return lynn . plugadd ( plug ) 355elseif plug . event then 356return pack_lazy ( plug ) 357end 358end 359 360--- load all plugins 361function lynn . loadall () 362vim . iter ( pairs ( lynn . plugins )): each ( function ( name , _ ) 363local ok , result =pcall ( lynn . load , name ) 364if not ok then 365logerr ( "error while loading plugin" , { name =name } ) 366return logtrace ( "load" , result ) 367end 368end ) 369end 370 371--- import a list of plugins from a module 372---@param plugins lynn.plug.spec[]|string plugins|modname 373---@param nopack? boolean avoid adding the plugin to `vim.pack` until later 374function lynn . import ( plugins , nopack ) 375if type ( plugins ) == "string" then 376logdebug ( "importing plugins" , { modname =plugins , nopack =not not nopack } ) 377 378local ok , result =pcall ( require , plugins ) 379if not ok then 380logerr ( "failed to import module" , { modname =plugins } ) 381return logtrace ( "import" , result ) 382end 383return lynn . import ( result , nopack ) 384end 385 386local packspecs =vim 387. iter ( ipairs ( plugins )) 388: map ( function ( _ , p ) 389p =lynn . norm ( p ) 390if nopack then 391p . ensure =false 392end 393return p 394end ) 395: map ( function ( p ) 396local ok , result =pcall ( lynn . register , p ) 397if not ok then 398logerr ( "error while registering plugin" , { [ "plug.name" ] =p . name } ) 399return logtrace ( "register" , result or "" ) 400end 401return lynn . translate ( p ) 402end ) 403: filter ( function ( p ) 404return p . ensure 405end ) 406: totable () 407 408if # packspecs == 0 then 409return 410end 411 412vim . pack . add ( packspecs , { load =false } ) 413end 414 415--- run |lynn.import()| and run packload for all plugins 416---@param plugins? lynn.plug.spec[]|string plugins|modname 417---@param nopack? boolean avoid adding the plugin to `vim.pack` until later 418function lynn . setup ( plugins , nopack ) 419if plugins then 420lynn . import ( plugins , nopack ) 421end 422 423lynn . loadall () 424 425vim . api . nvim_exec_autocmds ( "User" , { 426pattern ="PackDone" , 427} ) 428end 429 430function lynn . sync () 431-- update 432vim . pack . update () 433end 434 435--- delete inactive plugins. 436--- these include all plugin directories that are not in `vim.pack`. 437function lynn . clean () 438local inactive =utils . get_inactive () 439 440if # inactive == 0 then 441return 442end 443 444local pluglist =vim 445. iter ( inactive ) 446: map ( function ( p ) 447return " - " .. p . spec . name .. "\n\t" .. p . path 448end ) 449: totable () 450local plugstr =table . concat ( pluglist , "\n" ) 451 452vim . ui . input ( { 453prompt ="Delete inactive plugins? (y/N)\n" .. plugstr .. "\n" , 454} , function ( input ) 455if string . lower ( input ) == "y" then 456vim . tbl_map ( function ( p ) 457vim . fs . rm ( p . path , { recursive =true } ) 458end , inactive ) 459end 460end ) 461end 462 463return lynn