| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- 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)
- local handle = io.popen("pwd")
- local pwd = handle:read("a*")
- handle:close()
- local cspell_dir = os.getenv("HOME") .. "/.config/cspell/"
- vim.notify("dir" .. cspell_dir, vim.log.levels.INFO)
- local cc = os.execute("cd " .. cspell_dir)
- vim.notify("Created a new cspell.json file at " .. cc, vim.log.levels.INFO)
- local cc = os.execute("git add .")
- vim.notify("add" .. cc, vim.log.levels.INFO)
- local cc = os.execute("git commit -m 'word added'")
- vim.notify("comm" .. cc, vim.log.levels.INFO)
- local cc = os.execute("git push")
- vim.notify("push " .. cc, vim.log.levels.INFO)
- os.execute("cd " .. pwd)
- -- 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")
- end
- end,
- }
- end
|