make_add_to_json.lua 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. local h = require("cspell.helpers")
  2. ---@class AddToJSONAction
  3. ---@field diagnostic Diagnostic
  4. ---@field word string
  5. ---@field params GeneratorParams
  6. ---@field cspell CSpellConfigInfo|nil
  7. ---@param opts AddToJSONAction
  8. ---@return CodeAction
  9. return function(opts)
  10. ---@type CSpellSourceConfig
  11. local code_action_config = opts.params:get_config()
  12. local on_success = code_action_config.on_success
  13. local encode_json = code_action_config.encode_json or vim.json.encode
  14. return {
  15. title = 'Add "' .. opts.word .. '" to cspell json file',
  16. action = function()
  17. local cspell = opts.cspell or h.create_cspell_json(opts.params)
  18. if not cspell.config.words then
  19. cspell.config.words = {}
  20. end
  21. table.insert(cspell.config.words, opts.word)
  22. local encoded = encode_json(cspell.config) or ""
  23. local lines = {}
  24. for line in encoded:gmatch("[^\r\n]+") do
  25. table.insert(lines, line)
  26. end
  27. vim.fn.writefile(lines, cspell.path)
  28. -- replace word in buffer to trigger cspell to update diagnostics
  29. h.set_word(opts.diagnostic, opts.word)
  30. if on_success then
  31. on_success(cspell.path, opts.params, "add_to_json")
  32. local handle = io.popen("pwd")
  33. local cc = handle:read("a*")
  34. handle:close()
  35. vim.notify(cc,vim.log.levels.WARN)
  36. require("notifyu")("My super important message: "..cc)
  37. end
  38. end,
  39. }
  40. end