linters.lua 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. local M = {}
  2. local null_ls = require "null-ls"
  3. local services = require "lvim.lsp.null-ls.services"
  4. local Log = require "lvim.core.log"
  5. function M.list_registered_providers(filetype)
  6. local null_ls_methods = require "null-ls.methods"
  7. local linter_method = null_ls_methods.internal["DIAGNOSTICS"]
  8. local registered_providers = services.list_registered_providers_names(filetype)
  9. return registered_providers[linter_method] or {}
  10. end
  11. function M.list_available(filetype)
  12. local linters = {}
  13. local tbl = require "lvim.utils.table"
  14. for _, provider in pairs(null_ls.builtins.diagnostics) do
  15. if tbl.contains(provider.filetypes or {}, function(ft)
  16. return ft == "*" or ft == filetype
  17. end) then
  18. table.insert(linters, provider.name)
  19. end
  20. end
  21. table.sort(linters)
  22. return linters
  23. end
  24. function M.list_configured(linter_configs)
  25. local linters, errors = {}, {}
  26. for _, lnt_config in pairs(linter_configs) do
  27. local linter_name = lnt_config.exe:gsub("-", "_")
  28. local linter = null_ls.builtins.diagnostics[linter_name]
  29. if not linter then
  30. Log:error("Not a valid linter: " .. lnt_config.exe)
  31. errors[lnt_config.exe] = {} -- Add data here when necessary
  32. else
  33. local linter_cmd = services.find_command(linter._opts.command)
  34. if not linter_cmd then
  35. Log:warn("Not found: " .. linter._opts.command)
  36. errors[lnt_config.exe] = {} -- Add data here when necessary
  37. else
  38. Log:debug("Using linter: " .. linter_cmd)
  39. linters[lnt_config.exe] = linter.with {
  40. command = linter_cmd,
  41. extra_args = lnt_config.args,
  42. filetypes = lnt_config.filetypes,
  43. }
  44. end
  45. end
  46. end
  47. return { supported = linters, unsupported = errors }
  48. end
  49. function M.setup(linter_configs)
  50. if vim.tbl_isempty(linter_configs) then
  51. return
  52. end
  53. local linters = M.list_configured(linter_configs)
  54. null_ls.register { sources = linters.supported }
  55. end
  56. return M