linters.lua 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. for _, provider in pairs(null_ls.builtins.diagnostics) do
  14. -- TODO: Add support for wildcard filetypes
  15. if vim.tbl_contains(provider.filetypes or {}, filetype) then
  16. table.insert(linters, provider.name)
  17. end
  18. end
  19. return linters
  20. end
  21. function M.list_configured(linter_configs)
  22. local linters, errors = {}, {}
  23. for _, lnt_config in pairs(linter_configs) do
  24. local linter = null_ls.builtins.diagnostics[lnt_config.exe]
  25. if not linter then
  26. Log:error("Not a valid linter: " .. lnt_config.exe)
  27. errors[lnt_config.exe] = {} -- Add data here when necessary
  28. else
  29. local linter_cmd = services.find_command(linter._opts.command)
  30. if not linter_cmd then
  31. Log:warn("Not found: " .. linter._opts.command)
  32. errors[lnt_config.exe] = {} -- Add data here when necessary
  33. else
  34. Log:debug("Using linter: " .. linter_cmd)
  35. linters[lnt_config.exe] = linter.with { command = linter_cmd, extra_args = lnt_config.args }
  36. end
  37. end
  38. end
  39. return { supported = linters, unsupported = errors }
  40. end
  41. function M.setup(linter_configs, filetype)
  42. if vim.tbl_isempty(linter_configs) then
  43. return
  44. end
  45. local linters_by_ft = {}
  46. linters_by_ft[filetype] = M.list_configured(linter_configs)
  47. null_ls.register { sources = linters_by_ft[filetype].supported }
  48. end
  49. return M