From 1abadc584141aa0513c7a87cb48c1d3465d13e07 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Sun, 28 Jan 2024 17:06:26 +0800 Subject: [PATCH] nixvimConfigs/fiesta/setups/snippets: init --- configs/nixvim/fiesta/default.nix | 1 + configs/nixvim/fiesta/modules/default.nix | 1 + .../modules/setups/snippets/default.nix | 90 ++++++++++ .../modules/setups/snippets/snippets/all.lua | 31 ++++ .../setups/snippets/snippets/asciidoc.lua | 170 ++++++++++++++++++ 5 files changed, 293 insertions(+) create mode 100644 configs/nixvim/fiesta/modules/setups/snippets/default.nix create mode 100644 configs/nixvim/fiesta/modules/setups/snippets/snippets/all.lua create mode 100644 configs/nixvim/fiesta/modules/setups/snippets/snippets/asciidoc.lua diff --git a/configs/nixvim/fiesta/default.nix b/configs/nixvim/fiesta/default.nix index 7130734e..964316aa 100644 --- a/configs/nixvim/fiesta/default.nix +++ b/configs/nixvim/fiesta/default.nix @@ -5,6 +5,7 @@ imports = [ ./modules ]; nixvimConfigs.fiesta.setups = { + snippets.enable = true; treesitter.enable = true; debugging.enable = true; desktop-utils.enable = true; diff --git a/configs/nixvim/fiesta/modules/default.nix b/configs/nixvim/fiesta/modules/default.nix index b43a1332..32c70ed2 100644 --- a/configs/nixvim/fiesta/modules/default.nix +++ b/configs/nixvim/fiesta/modules/default.nix @@ -2,6 +2,7 @@ imports = [ ./setups/debugging.nix ./setups/desktop-utils.nix + ./setups/snippets ./setups/treesitter.nix ]; } diff --git a/configs/nixvim/fiesta/modules/setups/snippets/default.nix b/configs/nixvim/fiesta/modules/setups/snippets/default.nix new file mode 100644 index 00000000..88f9a2ef --- /dev/null +++ b/configs/nixvim/fiesta/modules/setups/snippets/default.nix @@ -0,0 +1,90 @@ +{ config, lib, ... }: + +let + nixvimCfg = config.nixvimConfigs.fiesta; + cfg = nixvimCfg.setups.snippets; + + luasnipKeymapConfig = { + lua = true; + mode = [ "i" "s" ]; + }; +in +{ + options.nixvimConfigs.fiesta.setups.snippets.enable = + lib.mkEnableOption "snippets setup"; + + config = lib.mkIf cfg.enable { + # The main star of the show. + plugins.luasnip.enable = true; + plugins.luasnip.extraConfig = { + keep_roots = true; + link_roots = true; + link_children = true; + enable_autosnippets = false; + }; + + # Set up this collection of snippets. + plugins.friendly-snippets.enable = true; + + # Load all of the custom snippets. + plugins.luasnip.fromLua = [ + { + lazyLoad = true; + paths = ./snippets; + } + ]; + + # Set up the keymaps ourselves since LuaSnip doesn't provide one as a + # config option. + keymaps = [ + (luasnipKeymapConfig // { + key = ""; + options.desc = "Jump to next node"; + action = '' + function() + ls = require("luasnip") + if ls.jumpable(1) then + ls.jump(1) + end + end + ''; + }) + + (luasnipKeymapConfig // { + key = ""; + options.desc = "Jump to previous node"; + action = '' + function() + ls = require("luasnip") + if ls.jumpable(-1) then + ls.jump(-1) + end + end + ''; + }) + + (luasnipKeymapConfig // { + key = ""; + options.desc = "Expand or jump to next node"; + action = '' + function() + ls = require("luasnip") + if ls.expand_or_jumpable() then + ls.expand_or_jump() + end + end + ''; + }) + + (luasnipKeymapConfig // { + key = ""; + options.desc = "Show extra choices"; + action = '' + function() + require("luasnip.extras.select_choice")() + end + ''; + }) + ]; + }; +} diff --git a/configs/nixvim/fiesta/modules/setups/snippets/snippets/all.lua b/configs/nixvim/fiesta/modules/setups/snippets/snippets/all.lua new file mode 100644 index 00000000..25e04b30 --- /dev/null +++ b/configs/nixvim/fiesta/modules/setups/snippets/snippets/all.lua @@ -0,0 +1,31 @@ +return { + s( + "today", + f(function() + return os.date("%F") + end) + ), + + s( + "retrieve", + f(function() + return string.format("(retrieved %s)", os.date("%F")) + end) + ), + + s( + { trig = 'reldate (-?%d+) "(.+)"', regTrig = true }, + f(function(_, snip) + -- The point is in number of days. + local point = 60 * 60 * 24 * snip.captures[1] + + local now = os.time() + return os.date(snip.captures[2], now + point) + end) + ), + + s("#!", fmt("#!{}", i(1, "/usr/bin/env bash"))), + + parse("ie", "(i.e., $1)"), + parse("eg", "(e.g., $1)"), +} diff --git a/configs/nixvim/fiesta/modules/setups/snippets/snippets/asciidoc.lua b/configs/nixvim/fiesta/modules/setups/snippets/snippets/asciidoc.lua new file mode 100644 index 00000000..16ea6e1f --- /dev/null +++ b/configs/nixvim/fiesta/modules/setups/snippets/snippets/asciidoc.lua @@ -0,0 +1,170 @@ +function max_asciidoc_header(level) + return math.min(level, 6) +end + +return { + parse("bf", "**$1**"), + parse("it", "__$1__"), + parse("sp", "^$1^"), + parse("sb", "~$1~"), + s( + "tt", + fmt("{}{}{}", { + c(1, { t("`"), t("`+") }), + i(2, "TEXT"), + rep(1), + }) + ), + + parse("foot", "footnote:[$1]"), + + s( + "link", + fmt("link:{}[{}]", { + i(1, "LINK"), + rep(1), + }) + ), + + parse("var", ":$1: $2"), + + parse("audio", "audio::$1[$2]"), + parse("video", "video::$1[$2]"), + + s( + "img", + fmt( + [[ + .{} + image::{}[{}, {}] + {} + ]], + { + i(1, "CAPTION"), + i(2, "IMAGE_PATH"), + i(3, "ALT_TEXT"), + i(4, "width=100%,height=100%"), + i(0), + } + ) + ), + + s( + "fmt", + fmt("{}{}{}", { + c(1, { + t("**"), + t("__"), + t("`"), + }), + i(2), + rep(1), + }) + ), + + s( + "dt", + fmt( + [[ + {}:: + {} + ]], + { + i(1, "TERM"), + i(2, "DEFINITION"), + } + ) + ), + + s( + "src", + fmt( + [[ + [source, {}] + ---- + {} + ---- + {} + ]], + { + i(1, "LANGUAGE"), + i(2, "CODE"), + i(0), + } + ) + ), + + s( + { trig = "h(%d)", regTrig = true }, + fmt( + [[ + {} {} + {} + {} + ]], + { + f(function(_, snip) + local level = max_asciidoc_header(snip.captures[1]) + return string.rep("=", level) + end), + i(1, "CHAPTER"), + d(2, function(_, snip) + local nodes = {} + table.insert(nodes, t("")) + + local level = max_asciidoc_header(snip.captures[1]) + + if level == 1 then + table.insert(nodes, t(":toc:")) + end + + local parent = c(1, nodes) + if level > 1 then + parent = t("") + end + + return sn(nil, parent) + end, {}), + i(0), + } + ) + ), + + s( + "admon", + fmt("{}: {}", { + c(1, { + t("NOTE"), + t("TIP"), + t("IMPORTANT"), + t("CAUTION"), + t("WARNING"), + }), + i(0), + }) + ), + + s( + "admonB", + fmt( + [[ + [{}] + ==== + {} + ==== + {} + ]], + { + c(1, { + t("NOTE"), + t("TIP"), + t("IMPORTANT"), + t("CAUTION"), + t("WARNING"), + }), + i(2, "BODY"), + i(0), + } + ) + ), +}