linters.lua 2.0 KB

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