null-ls.lua 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. local M = {}
  2. local _, null_ls = pcall(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. -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint
  13. local get_normalized_exe = function(exe, type)
  14. if type == "diagnostics" and exe == "eslint_d" then
  15. return "eslint"
  16. end
  17. return exe
  18. end
  19. local function setup_ls(exe, type)
  20. if utils.has_value(local_executables, exe) then
  21. local normalized_exe = get_normalized_exe(exe, type)
  22. local smart_executable = null_ls.builtins[type][normalized_exe]
  23. local local_executable = find_local_exe(exe)
  24. if vim.fn.executable(local_executable) == 1 then
  25. smart_executable._opts.command = local_executable
  26. table.insert(sources, smart_executable)
  27. else
  28. if vim.fn.executable(exe) == 1 then
  29. smart_executable._opts.command = exe
  30. table.insert(sources, smart_executable)
  31. end
  32. end
  33. else
  34. if null_ls.builtins[type][exe] and vim.fn.executable(null_ls.builtins[type][exe]._opts.command) then
  35. table.insert(sources, null_ls.builtins[type][exe])
  36. end
  37. end
  38. null_ls.register { sources = sources }
  39. end
  40. -- TODO: for linters and formatters with spaces and '-' replace with '_'
  41. local function setup(filetype, type)
  42. local executables = nil
  43. if type == "diagnostics" then
  44. executables = lvim.lang[filetype].linters
  45. end
  46. if type == "formatting" then
  47. executables = lvim.lang[filetype].formatter.exe
  48. end
  49. if utils.is_table(executables) then
  50. for _, exe in pairs(executables) do
  51. if exe ~= "" then
  52. setup_ls(exe, type)
  53. end
  54. end
  55. end
  56. if utils.is_string(executables) and executables ~= "" then
  57. setup_ls(executables, type)
  58. end
  59. end
  60. -- TODO: return the formatter if one was registered, then turn off the builtin formatter
  61. function M.setup(filetype)
  62. setup(filetype, "formatting")
  63. setup(filetype, "diagnostics")
  64. lvim.sources = sources
  65. return sources
  66. end
  67. return M