|
| 1 | +-- [ |
| 2 | +-- snip_env + autosnippets |
| 3 | +-- ] |
| 4 | +local ls = require("luasnip") |
| 5 | +local s = ls.snippet |
| 6 | +local sn = ls.snippet_node |
| 7 | +local isn = ls.indent_snippet_node |
| 8 | +local t = ls.text_node |
| 9 | +local i = ls.insert_node |
| 10 | +local f = ls.function_node |
| 11 | +local c = ls.choice_node |
| 12 | +local d = ls.dynamic_node |
| 13 | +local r = ls.restore_node |
| 14 | +local events = require("luasnip.util.events") |
| 15 | +local ai = require("luasnip.nodes.absolute_indexer") |
| 16 | +local extras = require("luasnip.extras") |
| 17 | +local l = extras.lambda |
| 18 | +local rep = extras.rep |
| 19 | +local p = extras.partial |
| 20 | +local m = extras.match |
| 21 | +local n = extras.nonempty |
| 22 | +local dl = extras.dynamic_lambda |
| 23 | +local fmt = require("luasnip.extras.fmt").fmt |
| 24 | +local fmta = require("luasnip.extras.fmt").fmta |
| 25 | +local conds = require("luasnip.extras.expand_conditions") |
| 26 | +local postfix = require("luasnip.extras.postfix").postfix |
| 27 | +local types = require("luasnip.util.types") |
| 28 | +local parse = require("luasnip.util.parser").parse_snippet |
| 29 | +local ms = ls.multi_snippet |
| 30 | +local autosnippet = ls.extend_decorator.apply(s, { snippetType = "autosnippet" }) |
| 31 | + |
| 32 | +--[ |
| 33 | +-- personal imports |
| 34 | +--] |
| 35 | +local tex = require("luasnip-latex-snippets.luasnippets.tex.utils.conditions") |
| 36 | +local single_command_snippet = require("luasnip-latex-snippets.luasnippets.tex.utils.scaffolding").single_command_snippet |
| 37 | +local reference_snippet_table = { |
| 38 | + a = "auto", |
| 39 | + c = "c", |
| 40 | + C = "C", |
| 41 | + e = "eq", |
| 42 | + r = "" |
| 43 | +} |
| 44 | + |
| 45 | +M = { |
| 46 | + autosnippet({ trig='alab', name='label', dscr='add a label'}, |
| 47 | + fmta([[ |
| 48 | + \label{<>:<>} |
| 49 | + ]], |
| 50 | + { i(1), i(0) })), |
| 51 | + autosnippet({ trig='([acCer])ref', name='(acC|eq)?ref', dscr='add a reference (with autoref, cref, eqref)', regTrig = true, hidden = true}, |
| 52 | + fmta([[ |
| 53 | + \<>ref{<>:<>} |
| 54 | + ]], |
| 55 | + { f(function (_, snip) |
| 56 | + return reference_snippet_table[snip.captures[1]] |
| 57 | + end), |
| 58 | + i(1), i(0) }), |
| 59 | + { condition = tex.in_text, show_condition = tex.in_text }), |
| 60 | +} |
| 61 | + |
| 62 | +local single_command_specs = { |
| 63 | + sec = { |
| 64 | + context = { |
| 65 | + name = "section", |
| 66 | + dscr = "section", |
| 67 | + priority = 250, |
| 68 | + }, |
| 69 | + command = [[\section]], |
| 70 | + ext = { label = true, short = "sec" }, |
| 71 | + }, |
| 72 | + ssec = { |
| 73 | + context = { |
| 74 | + name = "subsection", |
| 75 | + dscr = "subsection", |
| 76 | + priority = 500 |
| 77 | + }, |
| 78 | + command = [[\subsection]], |
| 79 | + ext = { label = true, short = "subsec" }, |
| 80 | + }, |
| 81 | + sssec = { |
| 82 | + context = { |
| 83 | + name = "subsubsection", |
| 84 | + dscr = "subsubsection", |
| 85 | + }, |
| 86 | + command = [[\subsubsection]], |
| 87 | + ext = { label = true, short = "sssec" }, |
| 88 | + }, |
| 89 | + ["sec*"] = { |
| 90 | + context = { |
| 91 | + name = "section*", |
| 92 | + dscr = "section*", |
| 93 | + priority = 250, |
| 94 | + }, |
| 95 | + command = [[\section*]], |
| 96 | + ext = { label = true, short = "sec" }, |
| 97 | + }, |
| 98 | + ["ssec*"] = { |
| 99 | + context = { |
| 100 | + name = "subsection*", |
| 101 | + dscr = "subsection*", |
| 102 | + priority = 500 |
| 103 | + }, |
| 104 | + command = [[\subsection*]], |
| 105 | + ext = { label = true, short = "subsec" }, |
| 106 | + }, |
| 107 | + ["sssec*"] = { |
| 108 | + context = { |
| 109 | + name = "subsubsection*", |
| 110 | + dscr = "subsubsection*", |
| 111 | + }, |
| 112 | + command = [[\subsubsection*]], |
| 113 | + ext = { label = true, short = "sssec" }, |
| 114 | + }, |
| 115 | + |
| 116 | + sq = { -- requires csquotes! |
| 117 | + context = { |
| 118 | + name = "enquote*", |
| 119 | + dscr = "single quotes", |
| 120 | + }, |
| 121 | + command = [[\enquote*]], |
| 122 | + }, |
| 123 | + qq = { |
| 124 | + context = { |
| 125 | + name = "enquote", |
| 126 | + dscr = "double quotes", |
| 127 | + }, |
| 128 | + command = [[\enquote]], |
| 129 | + }, |
| 130 | + bf = { |
| 131 | + context = { |
| 132 | + name = "textbf", |
| 133 | + dscr = "bold text", |
| 134 | + hidden = true, |
| 135 | + }, |
| 136 | + command = [[\textbf]], |
| 137 | + }, |
| 138 | + it = { |
| 139 | + context = { |
| 140 | + name = "textit", |
| 141 | + dscr = "italic text", |
| 142 | + hidden = true, |
| 143 | + }, |
| 144 | + command = [[\textit]], |
| 145 | + }, |
| 146 | + sc = { |
| 147 | + context = { |
| 148 | + name = "textsc", |
| 149 | + dscr = "small caps", |
| 150 | + hidden = true, |
| 151 | + }, |
| 152 | + command = [[\textsc]], |
| 153 | + }, |
| 154 | + tu = { |
| 155 | + context = { |
| 156 | + name = "underline (text)", |
| 157 | + dscr = "underlined text in non-math mode", |
| 158 | + hidden = true, |
| 159 | + }, |
| 160 | + command = [[\underline]], |
| 161 | + }, |
| 162 | + tov = { |
| 163 | + context = { |
| 164 | + name = "overline (text)", |
| 165 | + dscr = "overline text in non-math mode", |
| 166 | + hidden = true, |
| 167 | + }, |
| 168 | + command = [[\overline]], |
| 169 | + }, |
| 170 | +} |
| 171 | + |
| 172 | +local single_command_snippets = {} |
| 173 | +for k, v in pairs(single_command_specs) do |
| 174 | + table.insert( |
| 175 | + single_command_snippets, |
| 176 | + single_command_snippet( |
| 177 | + vim.tbl_deep_extend("keep", { trig = k }, v.context), |
| 178 | + v.command, |
| 179 | + v.opt or { condition = tex.in_text }, |
| 180 | + v.ext or {} |
| 181 | + ) |
| 182 | + ) |
| 183 | +end |
| 184 | +vim.list_extend(M, single_command_snippets) |
| 185 | + |
| 186 | +return M |
0 commit comments