robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local M ={} 2local s_err ---@type string? 3local s_host ---@type integer? 4 5--- @param host { name: string, orig_name: string } 6--- @param prog string 7--- @return integer 8function M . require ( host , prog ) 9local args ={ prog , '-e' , 'use Neovim::Ext; start_host();' } 10 11-- Collect registered perl plugins into args 12local perl_plugins =vim . fn [ 'remote#host#PluginsForHost' ]( host . name ) ---@type any 13---@param plugin any 14for _ , plugin in ipairs ( perl_plugins ) do 15table . insert ( args , plugin . path ) 16end 17 18return vim . fn [ 'provider#Poll' ]( args , host . orig_name , '$NVIM_PERL_LOG_FILE' ) 19end 20 21--- @return string? path to detected perl, if any; nil if not found 22--- @return string? error message if perl can't be detected; nil if success 23function M . detect () 24-- use g:perl_host_prog if set or check if perl is on the path 25local prog =vim . fn . exepath ( vim . g . perl_host_prog or 'perl' ) 26if prog == '' then 27return nil , 'No perl executable found' 28end 29 30-- if perl is available, make sure we have 5.22+ 31if vim . system ( { prog , '-e' , 'use v5.22' } ): wait (). code ~= 0 then 32return nil , 'Perl version is too old, 5.22+ required' 33end 34 35-- if perl is available, make sure the required module is available 36if vim . system ( { prog , '-W' , '-MNeovim::Ext' , '-e' , '' } ): wait (). code ~= 0 then 37return nil , '"Neovim::Ext" cpan module is not installed' 38end 39return prog , nil 40end 41 42--- @param method string 43--- @param args any[] 44function M . call ( method , args ) 45if s_err then 46return 47end 48 49if not s_host then 50-- Ensure that we can load the Perl host before bootstrapping 51local ok , result =pcall ( vim . fn [ 'remote#host#Require' ], 'legacy-perl-provider' ) ---@type any, any 52if not ok then 53s_err =result 54vim . api . nvim_echo ( { { result , 'WarningMsg' } } , true ) 55return 56end 57--- @cast result integer 58s_host =result 59end 60 61return vim . fn . rpcrequest ( s_host , 'perl_' .. method , unpack ( args )) 62end 63 64function M . start () 65-- The perl provider plugin will run in a separate instance of the perl host. 66vim . fn [ 'remote#host#RegisterClone' ]( 'legacy-perl-provider' , 'perl' ) 67vim . fn [ 'remote#host#RegisterPlugin' ]( 'legacy-perl-provider' , 'ScriptHost.pm' , {} ) 68end 69 70return M