ruby.lua 1.6 KB

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