init.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. local h = require("null-ls.helpers")
  2. local methods = require("null-ls.methods")
  3. local helpers = require("cspell.helpers")
  4. local DIAGNOSTICS = methods.internal.DIAGNOSTICS
  5. local custom_user_data = {
  6. user_data = function(entries, _)
  7. if not entries then
  8. return
  9. end
  10. local suggestions = {}
  11. for suggestion in string.gmatch(entries["_suggestions"], "[^, ]+") do
  12. table.insert(suggestions, suggestion)
  13. end
  14. return {
  15. suggestions = suggestions,
  16. misspelled = entries["_quote"],
  17. }
  18. end,
  19. }
  20. local needs_warning = true
  21. return h.make_builtin({
  22. name = "cspell",
  23. meta = {
  24. url = "https://github.com/streetsidesoftware/cspell",
  25. description = "cspell is a spell checker for code.",
  26. },
  27. method = DIAGNOSTICS,
  28. filetypes = {},
  29. generator_opts = {
  30. command = "cspell",
  31. ---@param params GeneratorParams
  32. args = function(params)
  33. params.cwd = params.cwd or vim.loop.cwd()
  34. params.cwd = '~/.config/cspell/'
  35. local cspell_args = {
  36. "lint",
  37. "--language-id",
  38. params.ft,
  39. "stdin://" .. params.bufname,
  40. }
  41. local config_path = helpers.get_config_path(params)
  42. if config_path then
  43. cspell_args = vim.list_extend({ "-c", config_path }, cspell_args)
  44. end
  45. local code_action_source = require("null-ls.sources").get({
  46. name = "cspell",
  47. method = methods.internal.CODE_ACTION,
  48. })[1]
  49. if code_action_source ~= nil then
  50. -- only enable suggestions when using the code actions built-in, since they slow down the command
  51. cspell_args = vim.list_extend({ "--show-suggestions" }, cspell_args)
  52. local code_action_config = code_action_source.config or {}
  53. local diagnostics_config = params and params:get_config() or {}
  54. if helpers.matching_configs(code_action_config, diagnostics_config) then
  55. -- warm up the config cache so we have the config ready by the time we call the code action
  56. helpers.async_get_config_info(params)
  57. elseif needs_warning then
  58. needs_warning = false
  59. vim.notify(
  60. "You should use the same config for both sources",
  61. vim.log.levels.WARN,
  62. { title = "cspell.nvim" }
  63. )
  64. end
  65. end
  66. return cspell_args
  67. end,
  68. to_stdin = true,
  69. ignore_stderr = true,
  70. format = "line",
  71. check_exit_code = function(code)
  72. return code <= 1
  73. end,
  74. on_output = h.diagnostics.from_patterns({
  75. {
  76. pattern = ".*:(%d+):(%d+)%s*-%s*(.*%((.*)%))%s*Suggestions:%s*%[(.*)%]",
  77. groups = { "row", "col", "message", "_quote", "_suggestions" },
  78. overrides = {
  79. adapters = {
  80. h.diagnostics.adapters.end_col.from_quote,
  81. custom_user_data,
  82. },
  83. },
  84. },
  85. {
  86. pattern = [[.*:(%d+):(%d+)%s*-%s*(.*%((.*)%))]],
  87. groups = { "row", "col", "message", "_quote" },
  88. overrides = {
  89. adapters = {
  90. h.diagnostics.adapters.end_col.from_quote,
  91. },
  92. },
  93. },
  94. }),
  95. },
  96. factory = h.generator_factory,
  97. })