summaryrefslogtreecommitdiff
path: root/lua/telescope/_compat.lua
diff options
context:
space:
mode:
authorTJ DeVries <devries.timothyj@gmail.com>2020-10-06 21:57:49 -0400
committerGitHub <noreply@github.com>2020-10-06 21:57:49 -0400
commitd32d4a6e0f0c571941f1fd37759ca1ebbdd5f488 (patch)
tree8cef3a32081bd0758d349a5d810d6f366def5f17 /lua/telescope/_compat.lua
parentcaf370cc378e7c606fd4f59c46533b0b0decbcea (diff)
fix: Reduce memory leaks (#148)
It is not 100% clear that we've gotten ALL the memory leaks, but it seems much much much better and doesn't look like it's going to die immediately or as often anymore :)
Diffstat (limited to 'lua/telescope/_compat.lua')
-rw-r--r--lua/telescope/_compat.lua71
1 files changed, 71 insertions, 0 deletions
diff --git a/lua/telescope/_compat.lua b/lua/telescope/_compat.lua
new file mode 100644
index 0000000..3d280ef
--- /dev/null
+++ b/lua/telescope/_compat.lua
@@ -0,0 +1,71 @@
+
+vim.deepcopy = (function()
+ local function _id(v)
+ return v
+ end
+
+ local deepcopy_funcs = {
+ table = function(orig)
+ local copy = {}
+
+ if vim._empty_dict_mt ~= nil and getmetatable(orig) == vim._empty_dict_mt then
+ copy = vim.empty_dict()
+ end
+
+ for k, v in pairs(orig) do
+ copy[vim.deepcopy(k)] = vim.deepcopy(v)
+ end
+
+ if getmetatable(orig) then
+ setmetatable(copy, getmetatable(orig))
+ end
+
+ return copy
+ end,
+ ['function'] = _id or function(orig)
+ local ok, dumped = pcall(string.dump, orig)
+ if not ok then
+ error(debug.traceback(dumped))
+ end
+
+ local cloned = loadstring(dumped)
+ local i = 1
+ while true do
+ local name = debug.getupvalue(orig, i)
+ if not name then
+ break
+ end
+ debug.upvaluejoin(cloned, i, orig, i)
+ i = i + 1
+ end
+ return cloned
+ end,
+ number = _id,
+ string = _id,
+ ['nil'] = _id,
+ boolean = _id,
+ }
+
+ return function(orig)
+ local f = deepcopy_funcs[type(orig)]
+ if f then
+ return f(orig)
+ else
+ error("Cannot deepcopy object of type "..type(orig))
+ end
+ end
+end)()
+
+
+
+table.clear = table.clear or function(t)
+ for k in pairs (t) do
+ t[k] = nil
+ end
+end
+
+table.pop = table.pop or function(t, k)
+ local val = t[k]
+ t[k] = nil
+ return val
+end