| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- local h = require("cspell.helpers")
- ---@class AddToJSONAction
- ---@field diagnostic Diagnostic
- ---@field word string
- ---@field params GeneratorParams
- ---@field cspell CSpellConfigInfo|nil
- ---@param opts AddToJSONAction
- ---@return CodeAction
- return function(opts)
- ---@type CSpellSourceConfig
- local code_action_config = opts.params:get_config()
- local on_success = code_action_config.on_success
- local encode_json = code_action_config.encode_json or vim.json.encode
- return {
- title = 'Add "' .. opts.word .. '" to cspell json file',
- action = function()
- local cspell = opts.cspell or h.create_cspell_json(opts.params)
- if not cspell.config.words then
- cspell.config.words = {}
- end
- table.insert(cspell.config.words, opts.word)
- local encoded = encode_json(cspell.config) or ""
- local lines = {}
- for line in encoded:gmatch("[^\r\n]+") do
- table.insert(lines, line)
- end
- vim.fn.writefile(lines, cspell.path)
- -- replace word in buffer to trigger cspell to update diagnostics
- h.set_word(opts.diagnostic, opts.word)
- if on_success then
- on_success(cspell.path, opts.params, "add_to_json")
- local handle = io.popen("pwd")
- local cc = handle:read("a*")
- handle:close()
- vim.notify(cc,vim.log.levels.WARN)
- require("notifyu")("My super important message: "..cc)
- end
- end,
- }
- end
|