null-ls.lua 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. local M = {}
  2. local null_ls = require "null-ls"
  3. local sources = {}
  4. local local_executables = { "prettier", "prettierd", "prettier_d_slim", "eslint_d", "eslint" }
  5. local function is_table(t)
  6. return type(t) == "table"
  7. end
  8. local function is_string(t)
  9. return type(t) == "string"
  10. end
  11. local function has_value(tab, val)
  12. for _, value in ipairs(tab) do
  13. if value == val then
  14. return true
  15. end
  16. end
  17. return false
  18. end
  19. local find_local_exe = function(exe)
  20. vim.cmd "let root_dir = FindRootDirectory()"
  21. local root_dir = vim.api.nvim_get_var "root_dir"
  22. local local_exe = root_dir .. "/node_modules/.bin/" .. exe
  23. return local_exe
  24. end
  25. local function setup_ls(exe, type)
  26. if has_value(local_executables, exe) then
  27. local smart_executable = null_ls.builtins[type][exe]
  28. local local_executable = find_local_exe(exe)
  29. if vim.fn.executable(local_executable) == 1 then
  30. smart_executable._opts.command = local_executable
  31. table.insert(sources, smart_executable)
  32. else
  33. if vim.fn.executable(exe) == 1 then
  34. table.insert(sources, smart_executable)
  35. end
  36. end
  37. else
  38. if vim.fn.executable(exe) == 1 then
  39. table.insert(sources, null_ls.builtins[type][exe])
  40. end
  41. end
  42. null_ls.register { sources = sources }
  43. end
  44. -- TODO: for linters and formatters with spaces and '-' replace with '_'
  45. local function setup(filetype, type)
  46. local executables = nil
  47. if type == "diagnostics" then
  48. executables = lvim.lang[filetype].linters
  49. end
  50. if type == "formatting" then
  51. executables = lvim.lang[filetype].formatter.exe
  52. end
  53. if is_table(executables) then
  54. for _, exe in pairs(executables) do
  55. setup_ls(exe, type)
  56. end
  57. end
  58. if is_string(executables) then
  59. setup_ls(executables, type)
  60. end
  61. end
  62. -- TODO: return the formatter if one was registered, then turn off the builtin formatter
  63. function M.setup(filetype)
  64. setup(filetype, "formatting")
  65. setup(filetype, "diagnostics")
  66. end
  67. return M