perl.lua 2.0 KB

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