local M = {} M.cookie = 'NVIM_EXTREGS_JSON_V1' local function validate(reg) if type(reg) ~= 'table' or type(reg.lines) ~= 'table' or type(reg.type) ~= 'string' then error('invalid yankreg: expected { lines = {...}, type = "v"|"V"|blockwise }') end return reg end function M.encode(reg) return M.cookie .. '\n' .. vim.json.encode(validate(reg)) .. '\n' end function M.decode(data) if data:sub(1, #M.cookie + 1) == M.cookie .. '\n' then return validate(vim.json.decode(data:sub(#M.cookie + 2))) end local lines = vim.split(data, '\n', { plain = true }) if #lines > 1 and lines[#lines] == '' then table.remove(lines) end return { lines = lines, type = #lines > 1 and 'V' or 'v' } end return M