null-ls.lua 1.9 KB

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