null-ls.lua 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. if exe ~= "" then
  56. setup_ls(exe, type)
  57. end
  58. end
  59. end
  60. if is_string(executables) and executables ~= "" then
  61. setup_ls(executables, type)
  62. end
  63. end
  64. -- TODO: return the formatter if one was registered, then turn off the builtin formatter
  65. function M.setup(filetype)
  66. setup(filetype, "formatting")
  67. setup(filetype, "diagnostics")
  68. end
  69. return M