linters.lua 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. local M = {}
  2. local linters_by_ft = {}
  3. local null_ls = require "null-ls"
  4. local services = require "lsp.null-ls.services"
  5. local logger = require("core.log"):get_default()
  6. local function list_names(linters, options)
  7. options = options or {}
  8. local names = {}
  9. local filter = options.filter or "supported"
  10. for name, _ in pairs(linters[filter]) do
  11. table.insert(names, name)
  12. end
  13. return names
  14. end
  15. function M.list_supported_names(filetype)
  16. if not linters_by_ft[filetype] then
  17. return {}
  18. end
  19. return list_names(linters_by_ft[filetype], { filter = "supported" })
  20. end
  21. function M.list_unsupported_names(filetype)
  22. if not linters_by_ft[filetype] then
  23. return {}
  24. end
  25. return list_names(linters_by_ft[filetype], { filter = "unsupported" })
  26. end
  27. function M.list_available(filetype)
  28. local linters = {}
  29. for _, provider in pairs(null_ls.builtins.diagnostics) do
  30. -- TODO: Add support for wildcard filetypes
  31. if vim.tbl_contains(provider.filetypes or {}, filetype) then
  32. table.insert(linters, provider.name)
  33. end
  34. end
  35. return linters
  36. end
  37. function M.list_configured(linter_configs)
  38. local linters, errors = {}, {}
  39. for _, lnt_config in pairs(linter_configs) do
  40. local linter = null_ls.builtins.diagnostics[lnt_config.exe]
  41. if not linter then
  42. logger.error("Not a valid linter:", lnt_config.exe)
  43. errors[lnt_config.exe] = {} -- Add data here when necessary
  44. else
  45. local linter_cmd = services.find_command(linter._opts.command)
  46. if not linter_cmd then
  47. logger.warn("Not found:", linter._opts.command)
  48. errors[lnt_config.exe] = {} -- Add data here when necessary
  49. else
  50. logger.info("Using linter:", linter_cmd)
  51. linters[lnt_config.exe] = linter.with { command = linter_cmd, args = lnt_config.args }
  52. end
  53. end
  54. end
  55. return { supported = linters, unsupported = errors }
  56. end
  57. function M.setup(filetype, options)
  58. if linters_by_ft[filetype] and not options.force_reload then
  59. return
  60. end
  61. linters_by_ft[filetype] = M.list_configured(lvim.lang[filetype].linters)
  62. null_ls.register { sources = linters_by_ft[filetype].supported }
  63. end
  64. return M