From 1caea8049224d52dded4b363ab4c4b797e2d7bb3 Mon Sep 17 00:00:00 2001 From: Federico Mengozzi <19249682+fedemengo@users.noreply.github.com> Date: Fri, 31 Mar 2023 01:01:42 +0200 Subject: [PATCH] omg --- fnl/init.fnl | 25 +++++++++++++++++++++++++ fnl/mods/tools/telescope.fnl | 2 +- init.lua | 2 +- tests/test.fnl | 14 ++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/fnl/init.fnl b/fnl/init.fnl index cfbe887..80d2d8e 100644 --- a/fnl/init.fnl +++ b/fnl/init.fnl @@ -1,5 +1,30 @@ (module init) +(global omit + (fn [t keys] + "Returns a copy of without the keys in " + (local nt {}) + (each [k v (pairs t)] + (when (not (in k keys)) + (tset nt k v))) + nt)) + +(global deep-copy + (fn [orig] + (let [copy (if (table? orig) + {} + orig)] + (when (table? orig) + (each [orig-key orig-value (pairs orig)] + (tset copy (deep-copy orig-key) (deep-copy orig-value))) + (setmetatable copy (deep-copy (getmetatable orig)))) + copy))) + +(global table? + (fn [t] + "Returns true if is a table" + (= (type! t) :table))) + (global lengtht (fn [t] "Returns the length of a proper table" diff --git a/fnl/mods/tools/telescope.fnl b/fnl/mods/tools/telescope.fnl index 81c4a3e..a4d0ca2 100644 --- a/fnl/mods/tools/telescope.fnl +++ b/fnl/mods/tools/telescope.fnl @@ -195,7 +195,7 @@ (var magic (fn [_opts] - (var opts (or _opts {})) + (var opts (deep-copy (or _opts {}))) (var cwd (utils.buffer_dir)) (tset opts :fcmd_depth 4) (tset opts :cwd cwd) diff --git a/init.lua b/init.lua index c495b29..d285399 100644 --- a/init.lua +++ b/init.lua @@ -25,7 +25,7 @@ require("impatient").enable_profile() -- caller = false --}) -- ---log.trace("Hello world", { "foo", "bar" }, 1, 2, 3) +--log.trace("Hello world", { "foo", "bar", key ="value" }, 1, 2, 3) vim.g["aniseed#env"] = { module = "init", diff --git a/tests/test.fnl b/tests/test.fnl index a58cced..8c7fffe 100644 --- a/tests/test.fnl +++ b/tests/test.fnl @@ -125,3 +125,17 @@ (assert-eq [4 5 6] nums2 "cons should not modify the element") (assert-eq [1 2 3 4 5 6] (cons [1 2 3 4 5] 6)) +(assert-eq {:a 1 :b 2} (omit {:a 1 :b 2 :c 3} [:c]) "omit should remove the key") +(assert-eq {:z 1} (omit {:a 1 :b 2 :c 3 :z 1} [:a :b :c]) "omit should remove the key") + +(assert-eq {:a 1 :b 2 :c 3} (deep-copy {:a 1 :b 2 :c 3}) "deep copy should copy the table") +(var q {:a 1 :b 2 :c 3}) +(var t1 (deep-copy q)) +(var t2 (deep-copy q)) +(assert-eq t1 t2 "deep copied tables should be equal") +(tset t1 :a 2) +(assert-neq t1 t2 "tables should be different") +(assert-neq t1 q "tables should be different") +(assert-eq t2 q "tables should remain equal") +(assert-eq t1 {:a 2 :b 2 :c 3} "deep copy should copy the table") +