linters.lua 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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_providers(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_available(filetype)
  19. local linters = {}
  20. local tbl = require "lvim.utils.table"
  21. for _, provider in pairs(null_ls.builtins.diagnostics) do
  22. if tbl.contains(provider.filetypes or {}, function(ft)
  23. return ft == "*" or ft == filetype
  24. end) then
  25. table.insert(linters, provider.name)
  26. end
  27. end
  28. table.sort(linters)
  29. return linters
  30. end
  31. function M.list_configured(linter_configs)
  32. local linters, errors = {}, {}
  33. for _, lnt_config in pairs(linter_configs) do
  34. local name = lnt_config.exe:gsub("-", "_")
  35. local linter = null_ls.builtins.diagnostics[name]
  36. if not linter then
  37. Log:error("Not a valid linter: " .. lnt_config.exe)
  38. errors[lnt_config.exe] = {} -- Add data here when necessary
  39. elseif is_registered(lnt_config.exe) then
  40. Log:trace "Skipping registering the source more than once"
  41. else
  42. local linter_cmd = services.find_command(linter._opts.command)
  43. if not linter_cmd then
  44. Log:warn("Not found: " .. linter._opts.command)
  45. errors[name] = {} -- Add data here when necessary
  46. else
  47. Log:debug("Using linter: " .. linter_cmd)
  48. table.insert(
  49. linters,
  50. linter.with {
  51. command = linter_cmd,
  52. extra_args = lnt_config.args,
  53. filetypes = lnt_config.filetypes,
  54. }
  55. )
  56. end
  57. end
  58. end
  59. return { supported = linters, unsupported = errors }
  60. end
  61. function M.setup(linter_configs)
  62. if vim.tbl_isempty(linter_configs) then
  63. return
  64. end
  65. local linters = M.list_configured(linter_configs)
  66. null_ls.register { sources = linters.supported }
  67. end
  68. return M