linters.lua 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. local M = {}
  2. local null_ls = require "null-ls"
  3. local services = require "lsp.null-ls.services"
  4. local Log = require "core.log"
  5. function M.list_supported_names(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 "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. return linters
  22. end
  23. function M.list_configured(linter_configs)
  24. local linters, errors = {}, {}
  25. for _, lnt_config in pairs(linter_configs) do
  26. local linter_name = lnt_config.exe:gsub("-", "_")
  27. local linter = null_ls.builtins.diagnostics[linter_name]
  28. if not linter then
  29. Log:error("Not a valid linter: " .. lnt_config.exe)
  30. errors[lnt_config.exe] = {} -- Add data here when necessary
  31. else
  32. local linter_cmd = services.find_command(linter._opts.command)
  33. if not linter_cmd then
  34. Log:warn("Not found: " .. linter._opts.command)
  35. errors[lnt_config.exe] = {} -- Add data here when necessary
  36. else
  37. Log:debug("Using linter: " .. linter_cmd)
  38. linters[lnt_config.exe] = linter.with {
  39. command = linter_cmd,
  40. extra_args = lnt_config.args,
  41. filetypes = lnt_config.filetypes,
  42. }
  43. end
  44. end
  45. end
  46. return { supported = linters, unsupported = errors }
  47. end
  48. function M.setup(linter_configs)
  49. if vim.tbl_isempty(linter_configs) then
  50. return
  51. end
  52. local linters = M.list_configured(linter_configs)
  53. null_ls.register { sources = linters.supported }
  54. end
  55. return M