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