From 4ac31540af6bd3a1ae6f96df2df9787dd4542080 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Mon, 28 Jul 2025 20:11:50 +0100 Subject: [PATCH] Re-add luasnip snippets --- .../nixvim/plugins/completion/luasnip.nix | 15 ++- .../completion/snippets/javascript.lua | 3 + .../plugins/completion/snippets/lua.lua | 13 ++ .../plugins/completion/snippets/nix.lua | 111 ++++++++++++++++++ .../plugins/completion/snippets/php.lua | 55 +++++++++ .../plugins/completion/snippets/rst.lua | 54 +++++++++ .../plugins/completion/snippets/twig.lua | 19 +++ 7 files changed, 269 insertions(+), 1 deletion(-) create mode 100644 modules2/nixvim/plugins/completion/snippets/javascript.lua create mode 100644 modules2/nixvim/plugins/completion/snippets/lua.lua create mode 100644 modules2/nixvim/plugins/completion/snippets/nix.lua create mode 100644 modules2/nixvim/plugins/completion/snippets/php.lua create mode 100644 modules2/nixvim/plugins/completion/snippets/rst.lua create mode 100644 modules2/nixvim/plugins/completion/snippets/twig.lua diff --git a/modules2/nixvim/plugins/completion/luasnip.nix b/modules2/nixvim/plugins/completion/luasnip.nix index bec75392..5b91c029 100644 --- a/modules2/nixvim/plugins/completion/luasnip.nix +++ b/modules2/nixvim/plugins/completion/luasnip.nix @@ -1,3 +1,16 @@ { - flake.modules.nixvim.custom.plugins.luasnip.enable = true; + flake.modules.nixvim.custom.plugins = { + cmp_luasnip.enable = true; + + luasnip = { + enable = true; + + fromLua = [ + { + lazyLoad = true; + paths = ./snippets; + } + ]; + }; + }; } diff --git a/modules2/nixvim/plugins/completion/snippets/javascript.lua b/modules2/nixvim/plugins/completion/snippets/javascript.lua new file mode 100644 index 00000000..4cfb491e --- /dev/null +++ b/modules2/nixvim/plugins/completion/snippets/javascript.lua @@ -0,0 +1,3 @@ +return { + s("log", fmta("console.log(<>);", { i(1, "value") })), +} diff --git a/modules2/nixvim/plugins/completion/snippets/lua.lua b/modules2/nixvim/plugins/completion/snippets/lua.lua new file mode 100644 index 00000000..31406fe7 --- /dev/null +++ b/modules2/nixvim/plugins/completion/snippets/lua.lua @@ -0,0 +1,13 @@ +return { + s( + "req", + fmt([[local {} = require "{}"]], { + f(function(import_name) + local parts = vim.split(import_name[1][1], ".", true) + + return parts[#parts] or "" + end, { 1 }), + i(1), + }) + ) +} diff --git a/modules2/nixvim/plugins/completion/snippets/nix.lua b/modules2/nixvim/plugins/completion/snippets/nix.lua new file mode 100644 index 00000000..8e454963 --- /dev/null +++ b/modules2/nixvim/plugins/completion/snippets/nix.lua @@ -0,0 +1,111 @@ +return { + s("home_packages", fmta("home.packages = with pkgs; [ <> ];", i(0))), + + s( + "homepage_options", + fmta( + [[ + homepage.name = mkOption { + default = ""; + type = types.str; + }; + + homepage.description = mkOption { + default = ""; + type = types.str; + }; + + homepage.icon = mkOption { + default = ""; + type = types.str; + }; + + homepage.category = mkOption { + default = ""; + type = types.str; + }; + ]], + { + description = i(2), + finish = i(0), + icon = i(3), + name = i(1), + } + ) + ), + + s( + "imports", + fmta( + [[ + { + imports = [ + <> + ]; + } + ]], + { i(0) } + ) + ), + + s( + "new_flake", + fmta( + [[ + { + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + }; + + outputs = + { nixpkgs, ... }: + let + system = "x86_64-linux"; + pkgs = import nixpkgs { inherit system; }; + + inherit (pkgs) mkShell; + in + { + devShells.${system}.default = mkShell { + packages = with pkgs; [ ]; + }; + + formatter.${system} = pkgs.nixfmt-classic; + }; + } + ]], + { inputs = i(1), pkgs = i(2), finish = i(0) } + ) + ), + s( + "new_module", + fmta( + [[ + { config, lib, ... }: + + let + name = ""; + cfg = config..${name}; + in + { + options..${name} = { + enable = lib.mkEnableOption "Enable ${name}"; + }; + + config = lib.mkIf cfg.enable { + + }; + } + ]], + { + finish = i(0), + more_options = i(3), + name = i(1), + namespace = i(2), + }, + { repeat_duplicates = true } + ) + ), + + s("system_packages", fmta("environment.systemPackages = with pkgs; [<>];", i(0))), +} diff --git a/modules2/nixvim/plugins/completion/snippets/php.lua b/modules2/nixvim/plugins/completion/snippets/php.lua new file mode 100644 index 00000000..eeb9f5d9 --- /dev/null +++ b/modules2/nixvim/plugins/completion/snippets/php.lua @@ -0,0 +1,55 @@ +return { + s("func", fmta("function <>(<>)<> {\n <>\n}<>", { i(1), i(2), i(3), i(4), i(0) })), + + s( + "met", + fmta( + [[ + <> function <>(<>)<> { + <> + }<> + ]], + { c(1, { t "public", t "protected", t "private" }), i(2), i(3), i(4), i(5), i(0) } + ) + ), + + s("pest", fmta("<>('<>', function() {\n <>\n});", { c(1, { t "it", t "test" }), i(2), i(0) })), + + s( + "test", + fmta( + [[ + public function test<>(): void { + <> + }<> + ]], + { i(1), i(2), i(0) } + ) + ), + + s( + "testan", + fmta( + [[ + /** @test */ + public function <>(): void { + <> + }<> + ]], + { i(1), i(2), i(0) } + ) + ), + + s( + "testat", + fmta( + [[ + [#Test] + public function <>(): void { + <> + }<> + ]], + { i(1), i(2), i(0) } + ) + ) +} diff --git a/modules2/nixvim/plugins/completion/snippets/rst.lua b/modules2/nixvim/plugins/completion/snippets/rst.lua new file mode 100644 index 00000000..7964a542 --- /dev/null +++ b/modules2/nixvim/plugins/completion/snippets/rst.lua @@ -0,0 +1,54 @@ +local fmta = require("luasnip.extras.fmt").fmta + +local fill_line = function(char) + return function() + local row = vim.api.nvim_win_get_cursor(0)[1] + local lines = vim.api.nvim_buf_get_lines(0, row - 2, row, false) + return string.rep(char, #lines[1]) + end +end + +return { + s("class", t(".. class:: ", i(1))), + + s("footer", t(".. footer:: ", i(1))), + + s("link", t(".. _", i(1), ":")), + + s("raw", t(".. raw:: ", i(1))), + + -- TODO: add an optional new line and ":width" property. + s("image", t(".. image:: ", i(1))), + + s("head", f(fill_line "=", {})), + + s("sub", f(fill_line "-", {})), + + s("subsub", f(fill_line "^", {})), + + -- Add a page break with an optional page template. + s( + "pb", + fmta( + [[ + .. raw:: pdf + + PageBreak<> + ]], + { i(0) } + ) + ), + + -- Add a new speaker note. + s( + "ta", + fmta( + [[ + .. raw:: pdf + + TextAnnotation "<>" + ]], + { i(0) } + ) + ), +} diff --git a/modules2/nixvim/plugins/completion/snippets/twig.lua b/modules2/nixvim/plugins/completion/snippets/twig.lua new file mode 100644 index 00000000..a787a544 --- /dev/null +++ b/modules2/nixvim/plugins/completion/snippets/twig.lua @@ -0,0 +1,19 @@ +return { + s("do", fmta("{% <> %}<>", { i(1), i(0) })), + s("dump", fmta("{{ dump(<>) }}<>", { i(1), i(0) })), + s("echo", fmta("{{ <> }}<>", { i(1), i(0) })), + + s( + "for", + fmta( + [[ + {% for <> in <> %} + <> + {% endfor %}<> + ]], + { i(1), i(2), i(3), i(0) } + ) + ), + + s("if", fmta("{% if <> %}<>{% endif %}<>", { i(1), i(2), i(0) })), +}