services.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. local M = {}
  2. local Log = require "lvim.core.log"
  3. local function find_root_dir()
  4. local util = require "lspconfig/util"
  5. local lsp_utils = require "lvim.lsp.utils"
  6. local ts_client = lsp_utils.is_client_active "typescript"
  7. if ts_client then
  8. return ts_client.config.root_dir
  9. end
  10. local dirname = vim.fn.expand "%:p:h"
  11. return util.root_pattern "package.json"(dirname)
  12. end
  13. local function from_node_modules(command)
  14. local root_dir = find_root_dir()
  15. if not root_dir then
  16. return nil
  17. end
  18. local join_paths = require("lvim.utils").join_paths
  19. return join_paths(root_dir, "node_modules", ".bin", command)
  20. end
  21. local local_providers = {
  22. prettier = { find = from_node_modules },
  23. prettierd = { find = from_node_modules },
  24. prettier_d_slim = { find = from_node_modules },
  25. eslint_d = { find = from_node_modules },
  26. eslint = { find = from_node_modules },
  27. stylelint = { find = from_node_modules },
  28. }
  29. function M.find_command(command)
  30. if local_providers[command] then
  31. local local_command = local_providers[command].find(command)
  32. if local_command and vim.fn.executable(local_command) == 1 then
  33. return local_command
  34. end
  35. end
  36. if command and vim.fn.executable(command) == 1 then
  37. return command
  38. end
  39. return nil
  40. end
  41. function M.list_registered_providers_names(filetype)
  42. local s = require "null-ls.sources"
  43. local available_sources = s.get_available(filetype)
  44. local registered = {}
  45. for _, source in ipairs(available_sources) do
  46. for method in pairs(source.methods) do
  47. registered[method] = registered[method] or {}
  48. table.insert(registered[method], source.name)
  49. end
  50. end
  51. return registered
  52. end
  53. function M.register_sources(configs, method)
  54. local null_ls = require "null-ls"
  55. local is_registered = require("null-ls.sources").is_registered
  56. local sources, registered_names = {}, {}
  57. for _, config in ipairs(configs) do
  58. local cmd = config.exe or config.command
  59. local name = config.name or cmd:gsub("-", "_")
  60. local type = method == null_ls.methods.CODE_ACTION and "code_actions" or null_ls.methods[method]:lower()
  61. local source = type and null_ls.builtins[type][name]
  62. Log:debug(string.format("Received request to register [%s] as a %s source", name, type))
  63. if not source then
  64. Log:error("Not a valid source: " .. name)
  65. elseif is_registered { name = source.name or name, method = method } then
  66. Log:trace(string.format("Skipping registering [%s] more than once", name))
  67. else
  68. local command = M.find_command(source._opts.command) or source._opts.command
  69. -- treat `args` as `extra_args` for backwards compatibility. Can otherwise use `generator_opts.args`
  70. local compat_opts = vim.deepcopy(config)
  71. if config.args then
  72. compat_opts.extra_args = config.args or config.extra_args
  73. compat_opts.args = nil
  74. end
  75. local opts = vim.tbl_deep_extend("keep", { command = command }, compat_opts)
  76. Log:debug("Registering source " .. name)
  77. Log:trace(vim.inspect(opts))
  78. table.insert(sources, source.with(opts))
  79. vim.list_extend(registered_names, { source.name })
  80. end
  81. end
  82. if #sources > 0 then
  83. null_ls.register { sources = sources }
  84. end
  85. return registered_names
  86. end
  87. return M