robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
1.7 KiB67 linesraw
1local M = {}
2local s_err ---@type string?
3local s_host ---@type integer?
4
5--- @param host { name: string, orig_name: string }
6--- @return integer
7function M.require(host)
8  local prog = M.detect()
9  local args = { prog }
10  local ruby_plugins = vim.fn['remote#host#PluginsForHost'](host.name) ---@type any
11
12  ---@param plugin any
13  for _, plugin in ipairs(ruby_plugins) do
14    table.insert(args, plugin.path)
15  end
16
17  return vim.fn['provider#Poll'](args, host.orig_name, '$NVIM_RUBY_LOG_FILE')
18end
19
20---@param method string
21---@param args any[]
22function M.call(method, args)
23  if s_err then
24    return
25  end
26
27  if not s_host then
28    local ok, result = pcall(vim.fn['remote#host#Require'], 'legacy-ruby-provider') ---@type any, any
29    if not ok then
30      s_err = result
31      vim.api.nvim_echo({ { result, 'WarningMsg' } }, true)
32      return
33    end
34    --- @cast result integer
35    s_host = result
36  end
37
38  return vim.fn.rpcrequest(s_host, 'ruby_' .. method, unpack(args))
39end
40
41function M.detect()
42  local prog ---@type string
43  if vim.g.ruby_host_prog then
44    prog = vim.fn.expand(vim.g.ruby_host_prog, true)
45  elseif vim.fn.has('win32') == 1 then
46    prog = vim.fn.exepath('neovim-ruby-host.bat')
47  else
48    local p = vim.fn.exepath('neovim-ruby-host')
49    if p == '' then
50      prog = ''
51    else
52      -- neovim-ruby-host could be an rbenv shim for another Ruby version.
53      local result = vim.system({ p }):wait()
54      prog = result.code ~= 0 and '' or p
55    end
56  end
57  local err = prog == '' and 'missing ruby or ruby-host' or ''
58  return prog, err
59end
60
61--- @param plugin_path string
62function M.start(plugin_path)
63  vim.fn['remote#host#RegisterClone']('legacy-ruby-provider', 'ruby')
64  vim.fn['remote#host#RegisterPlugin']('legacy-ruby-provider', plugin_path, {})
65end
66
67return M