|
| 1 | +local ls = require("luasnip") |
| 2 | +local f = ls.function_node |
| 3 | +local t = ls.text_node |
| 4 | +local snippet = require("luasnip-snippets.nodes").construct_snippet |
| 5 | +local fmta = require("luasnip.extras.fmt").fmta |
| 6 | +local extras = require("luasnip.extras") |
| 7 | +local rep = extras.rep |
| 8 | +local i = require("luasnip-snippets.nodes").insert_node |
| 9 | +local tsp = require("luasnip.extras.treesitter_postfix") |
| 10 | +local Utils = require("luasnip-snippets.utils") |
| 11 | + |
| 12 | +local identifier_query = [[ |
| 13 | +[ |
| 14 | + (identifier) |
| 15 | +] @prefix |
| 16 | +]] |
| 17 | + |
| 18 | +local function identifier_tsp(trig, expand, dscr) |
| 19 | + local name = ("(%s) %s"):format(trig, expand) |
| 20 | + if dscr == nil then |
| 21 | + dscr = ("Wraps an expression with %s"):format(expand) |
| 22 | + else |
| 23 | + dscr = dscr:format(expand) |
| 24 | + end |
| 25 | + local replaced = expand:gsub("?", "%%s") |
| 26 | + |
| 27 | + return tsp.treesitter_postfix({ |
| 28 | + trig = trig, |
| 29 | + name = name, |
| 30 | + dscr = dscr, |
| 31 | + wordTrig = false, |
| 32 | + reparseBuffer = "live", |
| 33 | + matchTSNode = { |
| 34 | + query = identifier_query, |
| 35 | + query_lang = "nix", |
| 36 | + }, |
| 37 | + }, { |
| 38 | + f(function(_, parent) |
| 39 | + return Utils.replace_all(parent.snippet.env.LS_TSMATCH, replaced) |
| 40 | + end, {}), |
| 41 | + }) |
| 42 | +end |
| 43 | + |
| 44 | +return { |
| 45 | + snippet { |
| 46 | + "@module", |
| 47 | + name = "(@module) ...", |
| 48 | + dscr = "Expands to a common module skeleton", |
| 49 | + mode = "bw", |
| 50 | + nodes = fmta( |
| 51 | + [[ |
| 52 | + { |
| 53 | + config, |
| 54 | + lib, |
| 55 | + pkgs, |
| 56 | + ... |
| 57 | + }: let |
| 58 | + cfg = config.<module>; |
| 59 | + in { |
| 60 | + options.<module_r> = { |
| 61 | + enable = lib.mkEnableOption "Enable module <module_r>"; |
| 62 | + }; |
| 63 | +
|
| 64 | + config = lib.mkIf cfg.enable { |
| 65 | + }; |
| 66 | + } |
| 67 | + ]], |
| 68 | + { |
| 69 | + module = i(1, "module", { dscr = "Module name" }), |
| 70 | + module_r = rep(1), |
| 71 | + } |
| 72 | + ), |
| 73 | + }, |
| 74 | + |
| 75 | + identifier_tsp( |
| 76 | + ".on", |
| 77 | + "? = { enable = true; };", |
| 78 | + "Completes an identifier with an enable option" |
| 79 | + ), |
| 80 | +} |
0 commit comments