robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
2.1 KiB70 linesraw
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)
9  local args = { prog, '-e', 'use Neovim::Ext; start_host();' }
10
11  -- Collect registered perl plugins into args
12  local perl_plugins = vim.fn['remote#host#PluginsForHost'](host.name) ---@type any
13  ---@param plugin any
14  for _, plugin in ipairs(perl_plugins) do
15    table.insert(args, plugin.path)
16  end
17
18  return 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
25  local prog = vim.fn.exepath(vim.g.perl_host_prog or 'perl')
26  if prog == '' then
27    return nil, 'No perl executable found'
28  end
29
30  -- if perl is available, make sure we have 5.22+
31  if vim.system({ prog, '-e', 'use v5.22' }):wait().code ~= 0 then
32    return nil, 'Perl version is too old, 5.22+ required'
33  end
34
35  -- if perl is available, make sure the required module is available
36  if vim.system({ prog, '-W', '-MNeovim::Ext', '-e', '' }):wait().code ~= 0 then
37    return nil, '"Neovim::Ext" cpan module is not installed'
38  end
39  return prog, nil
40end
41
42--- @param method string
43--- @param args any[]
44function M.call(method, args)
45  if s_err then
46    return
47  end
48
49  if not s_host then
50    -- Ensure that we can load the Perl host before bootstrapping
51    local ok, result = pcall(vim.fn['remote#host#Require'], 'legacy-perl-provider') ---@type any, any
52    if not ok then
53      s_err = result
54      vim.api.nvim_echo({ { result, 'WarningMsg' } }, true)
55      return
56    end
57    --- @cast result integer
58    s_host = result
59  end
60
61  return 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.
66  vim.fn['remote#host#RegisterClone']('legacy-perl-provider', 'perl')
67  vim.fn['remote#host#RegisterPlugin']('legacy-perl-provider', 'ScriptHost.pm', {})
68end
69
70return M