linters.lua 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. local is_registered = function(name)
  6. local query = {
  7. name = name,
  8. method = require("null-ls").methods.DIAGNOSTICS,
  9. }
  10. return require("null-ls.sources").is_registered(query)
  11. end
  12. function M.list_registered(filetype)
  13. local null_ls_methods = require "null-ls.methods"
  14. local linter_method = null_ls_methods.internal["DIAGNOSTICS"]
  15. local registered_providers = services.list_registered_providers_names(filetype)
  16. return registered_providers[linter_method] or {}
  17. end
  18. function M.list_supported(filetype)
  19. local s = require "null-ls.sources"
  20. local supported_linters = s.get_supported(filetype, "diagnostics")
  21. table.sort(supported_linters)
  22. return supported_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 name = lnt_config.exe:gsub("-", "_")
  28. local linter = null_ls.builtins.diagnostics[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. elseif is_registered(lnt_config.exe) then
  33. Log:trace "Skipping registering the source more than once"
  34. else
  35. local linter_cmd = services.find_command(linter._opts.command)
  36. if not linter_cmd then
  37. Log:warn("Not found: " .. linter._opts.command)
  38. errors[name] = {} -- Add data here when necessary
  39. else
  40. Log:debug("Using linter: " .. linter_cmd)
  41. table.insert(
  42. linters,
  43. linter.with {
  44. command = linter_cmd,
  45. extra_args = lnt_config.args,
  46. filetypes = lnt_config.filetypes,
  47. }
  48. )
  49. end
  50. end
  51. end
  52. return { supported = linters, unsupported = errors }
  53. end
  54. function M.setup(linter_configs)
  55. if vim.tbl_isempty(linter_configs) then
  56. return
  57. end
  58. local linters = M.list_configured(linter_configs)
  59. null_ls.register { sources = linters.supported }
  60. end
  61. return M