aboutsummaryrefslogtreecommitdiff
path: root/lua/extregs/storage.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/extregs/storage.lua')
-rw-r--r--lua/extregs/storage.lua24
1 files changed, 24 insertions, 0 deletions
diff --git a/lua/extregs/storage.lua b/lua/extregs/storage.lua
new file mode 100644
index 0000000..c532485
--- /dev/null
+++ b/lua/extregs/storage.lua
@@ -0,0 +1,24 @@
+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