null-ls.lua 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 index, 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) then
  30. smart_executable._opts.command = local_executable
  31. end
  32. table.insert(sources, smart_executable)
  33. else
  34. table.insert(sources, null_ls.builtins[type][exe])
  35. end
  36. null_ls.register { sources = sources }
  37. end
  38. local function setup(filetype, type)
  39. local executables = nil
  40. if type == "diagnostics" then
  41. executables = lvim.lang[filetype].linters
  42. end
  43. if type == "formatting" then
  44. executables = lvim.lang[filetype].formatter.exe
  45. end
  46. if is_table(executables) then
  47. for _, exe in pairs(executables) do
  48. setup_ls(exe, type)
  49. end
  50. end
  51. if is_string(executables) then
  52. setup_ls(executables, type)
  53. end
  54. end
  55. function M.setup(filetype)
  56. setup(filetype, "formatting")
  57. setup(filetype, "diagnostics")
  58. end
  59. return M