services.lua 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. }
  26. function M.find_command(command)
  27. if local_providers[command] then
  28. local local_command = local_providers[command].find(command)
  29. if local_command and vim.fn.executable(local_command) == 1 then
  30. return local_command
  31. end
  32. end
  33. if vim.fn.executable(command) == 1 then
  34. return command
  35. end
  36. return nil
  37. end
  38. return M