services.lua 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. local M = {}
  2. local function find_root_dir()
  3. local util = require "lspconfig/util"
  4. local lsp_utils = require "lsp.utils"
  5. local status_ok, ts_client = lsp_utils.is_client_active "typescript"
  6. if status_ok then
  7. return ts_client.config.root_dir
  8. end
  9. local dirname = vim.fn.expand "%:p:h"
  10. return util.root_pattern "package.json"(dirname)
  11. end
  12. local function from_node_modules(command)
  13. local root_dir = find_root_dir()
  14. if not root_dir then
  15. return nil
  16. end
  17. return root_dir .. "/node_modules/.bin/" .. command
  18. end
  19. local local_providers = {
  20. prettier = { find = from_node_modules },
  21. prettierd = { find = from_node_modules },
  22. prettier_d_slim = { find = from_node_modules },
  23. eslint_d = { find = from_node_modules },
  24. eslint = { find = from_node_modules },
  25. stylelint = { find = from_node_modules },
  26. }
  27. function M.find_command(command)
  28. if local_providers[command] then
  29. local local_command = local_providers[command].find(command)
  30. if local_command and vim.fn.executable(local_command) == 1 then
  31. return local_command
  32. end
  33. end
  34. if vim.fn.executable(command) == 1 then
  35. return command
  36. end
  37. return nil
  38. end
  39. function M.list_registered_providers_names(filetype)
  40. local u = require "null-ls.utils"
  41. local c = require "null-ls.config"
  42. local registered = {}
  43. for method, source in pairs(c.get()._methods) do
  44. for name, filetypes in pairs(source) do
  45. if u.filetype_matches(filetypes, filetype) then
  46. registered[method] = registered[method] or {}
  47. table.insert(registered[method], name)
  48. end
  49. end
  50. end
  51. return registered
  52. end
  53. return M