about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorSoispha <soispha@vhack.eu>2023-08-27 16:17:30 +0200
committerSoispha <soispha@vhack.eu>2023-08-27 16:17:30 +0200
commitc8904741ca643a00c260b680c3fd6252a1b75a37 (patch)
tree85882a3a15e11b445500c4817b6445c8299ea9de
parentFeat(hm/conf/nvim/plugins/luasnip/all): Add snippets for todo comments (diff)
downloadnixos-config-c8904741ca643a00c260b680c3fd6252a1b75a37.tar.gz
nixos-config-c8904741ca643a00c260b680c3fd6252a1b75a37.zip
Fix(hm/conf/nvim/plugins/luasnip/snippets/all): Fix pairs in certain fts
-rw-r--r--hm/soispha/conf/nvim/plugins/luasnip/lua/snippets/all.lua62
1 files changed, 26 insertions, 36 deletions
diff --git a/hm/soispha/conf/nvim/plugins/luasnip/lua/snippets/all.lua b/hm/soispha/conf/nvim/plugins/luasnip/lua/snippets/all.lua
index 8e3a409b..d73ce7ae 100644
--- a/hm/soispha/conf/nvim/plugins/luasnip/lua/snippets/all.lua
+++ b/hm/soispha/conf/nvim/plugins/luasnip/lua/snippets/all.lua
@@ -1,3 +1,5 @@
+local ls = require("luasnip");
+-- auto_pairs  {{{
 local get_visual = function(args, parent)
   if #parent.snippet.env.SELECT_RAW > 0 then
     return sn(nil, i(1, parent.snippet.env.SELECT_RAW))
@@ -14,56 +16,44 @@ local function char_count_same(c1, c2)
   return ct1 == ct2
 end
 
-local function even_count(c)
+local function even_count(c, ...)
   local line = vim.api.nvim_get_current_line()
   local _, ct = string.gsub(line, c, "")
   return ct % 2 == 0
 end
 
-local function neg(fn, ...)
-  return not fn(...)
-end
-
-local function part(fn, ...)
-  local args = { ...; }
-  return function()
-    return fn(unpack(args))
-  end
-end
-
 -- This makes creation of pair-type snippets easier.
-local function pair(pair_begin, pair_end, expand_func, ...)
-  -- triggerd by opening part of pair, wordTrig=false to trigger anywhere.
-  -- ... is used to pass any args following the expand_func to it.
+local function pair(pair_begin, pair_end, file_types, condition_function)
+  -- FIXME(@Soispha): This only works if file_types == nil, otherwise the snippet does not expand.
+  -- It would be nice, if it would support both an empty array (`{}`) and nil <2023-08-27>
+  -- file_types = file_types or {};
+
   return s(
     { trig = pair_begin; wordTrig = false; snippetType = "autosnippet"; },
     { t({ pair_begin; }); d(1, get_visual); t({ pair_end; }); },
-    { condition = part(expand_func, part(..., pair_begin, pair_end)); }
+    {
+      condition = function()
+        local filetype_check = true;
+        if file_types ~= nil then
+          filetype_check = file_types[vim.bo.filetype] or false;
+        end;
+        return (not condition_function(pair_begin, pair_end)) and filetype_check
+      end;
+    }
   )
 end
 
-local maybe = { pair = nil; }
-if vim.bo.filetype == "tex" then
-  maybe.pair = pair("<", ">", neg, char_count_same)
-end
-
 local auto_pairs = {
-  pair("(", ")", neg, char_count_same);
-  pair("{", "}", neg, char_count_same);
-  pair("[", "]", neg, char_count_same);
-  pair("'", "'", neg, even_count);
-  pair('"', '"', neg, even_count);
-  pair("`", "`", neg, even_count);
+  pair("(", ")", nil, char_count_same);
+  pair("{", "}", nil, char_count_same);
+  pair("[", "]", nil, char_count_same);
+  pair("<", ">", { ["rust"] = true; ["tex"] = true; }, char_count_same);
+  pair("'", "'", nil, even_count);
+  pair('"', '"', nil, even_count);
+  pair("`", "`", nil, even_count);
 }
-auto_pairs[#auto_pairs + 1] = maybe.pair
 
-local todo_comments = {
-  s({ trig = "fix"; }, t("FIXME(@Soispha): "));
-  s({ trig = "todo"; }, t("TODO(@Soispha): "));
-  s({ trig = "note"; }, t("NOTE(@Soispha): "));
-};
+ls.add_snippets("all", auto_pairs, { type = "snippets"; key = "auto_pairs"; })
+-- }}}
 
--- merge the output tables together
-for k, v in pairs(auto_pairs) do todo_comments[k] = v end
 
-return todo_comments