summaryrefslogtreecommitdiff
path: root/config/essentials/nvim/after/plugin/luasnip.lua
blob: 5cf54d544e513ef227e1f3877bc85780f9dfc727 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
local ls = require("luasnip")
local s = ls.snippet
local sn = ls.snippet_node
local isn = ls.indent_snippet_node
local t = ls.text_node
local i = ls.insert_node
local f = ls.function_node
local c = ls.choice_node
local d = ls.dynamic_node
local r = ls.restore_node
local events = require("luasnip.util.events")
local ai = require("luasnip.nodes.absolute_indexer")
local extras = require("luasnip.extras")
local l = extras.lambda
local rep = extras.rep
local p = extras.partial
local m = extras.match
local n = extras.nonempty
local dl = extras.dynamic_lambda
local fmt = require("luasnip.extras.fmt").fmt
local fmta = require("luasnip.extras.fmt").fmta
local conds = require("luasnip.extras.expand_conditions")
local postfix = require("luasnip.extras.postfix").postfix
local types = require("luasnip.util.types")
local parse = require("luasnip.util.parser").parse_snippet

-- keymaps
vim.keymap.set({"i", "s"}, "<C-k>", "<Plug>luasnip-expand-or-jump", { noremap = true })
vim.keymap.set({"i", "s"}, "<C-j>", "<Plug>luasnip-jump-prev", { noremap = true })
vim.keymap.set({"i", "s"}, "<C-l>", "<Plug>luasnip-next-choice", { noremap = true })
vim.keymap.set("n", "<leader><leader>s", function()
    ls.cleanup()
    vim.cmd("source ~/.config/nvim/after/plugin/luasnip.lua")
    print("snippets reloaded.")
end, { noremap = true })

ls.add_snippets("lua", {
    -- print
    s("pt", fmt("print({}){}", { i(1, "\"Hello World!\"") , i(0) })),
    -- local function
    parse("lf", "local $1 = function($2)\n\t$3\nend$0", {}),
        -- require
        s("lrq", fmt("local {} = require('{}')", { i(1), rep(1) })),
        parse("rq", "require('$1')$0", {}),
        parse("rqs", "require('$1').setup {\n\t$2\n}$0", {}),
        parse("use", "use('$1')$0", {}),
        -- function
        parse("fn",
        [[
        function $1($2)
    end$0
    ]], {}),
    parse("sn", "s(\"$1\", fmt(\n[[\n$2\n]],\n{ $3 })),$0", {}),
})

local languages = {'php', 'html'}
for language = 1,#languages do
ls.add_snippets(languages[language], {
	s("<!DOCTYPE>", fmt(
	[[
	<!DOCTYPE html>
	<html lang="en">
		<head>
			<meta charset="UTF-8">
			<meta name="viewport" content="width-device-width, initial-scale=1.0">
			<meta http-equiv="X-UA-Compatible" content="ie=edge">
			<title>{}</title>
		</head>
		<body>
			<h1>{}</h1>{}
		</body>
	</html>
	]],
	{i(1, "title"), rep(2), i(0)})),
	parse("sty",
	[[
	<link rel="stylesheet" type="text/css" href="$1">$0
	]], {}),
})
end

ls.add_snippets("java", {
    -- function
    s("fn", fmt(
    [[
    {}{} {}({})
    {{
        {}
    }}
    ]],
    {
        c(1, {t "public ", t "private ", t ""}),
        i(2, "type"),
        i(3, "f"),
        i(4), i(0)
    })),
    -- setter function
    s("psv", fmt(
    [[
    public class Main
    {{
        public static void main (String[] args)
        {{
            {}
        }}
    }}
    ]],
    { i(0) })),
    -- constructor
    s("class", fmt(
    [[
    {}class {}
    {{
        {}
    }}{}
    ]],
    { c(1, {t "public ", t "private ", t ""}), i(2), i(3), i(0)})),
    -- StringBuilder
    s("sb", fmt(
    [[
    public void print()
    {{
        StringBuilder sb = new StringBuilder(30);
        sb.append({});
        sb.append(", ").append({});{}
        System.out.print(sb.toString());
    }}{}
    ]],
    { i(1), i(2), i(3), i(0)})),
    -- print
    parse("pt", "System.out.println($1);$0", {}),
    parse("pti", "System.out.println(\"$1: \" + $1);$0", {}),
    -- quickies 
    s("pr", t "private "),
    s("ob", fmt(
    [[
    {} {} = new {}({});
    {}
    ]],
    { i(1), i(2), rep(1), i(3), i(0) })),
    parse("abs", "Math.abs($1);$0", {}),
})

ls.add_snippets("sh", {
    s("TD", t "THISDIR=\"$(dirname \"$(readlink -f \"$0\")\")\""),
    parse("pf", ">&2 printf '$1\\n'$0", {}),
    parse("fn", "$1 ()\n{\n\t$2\n}$0", {}),
    -- Functions
    parse("rchar",
    [[
    read_char ()
    {
        old_stty_cfg=$(stty -g)
        stty raw -echo 
        dd ibs=1 count=1 2> /dev/null
        stty \$old_stty_cfg
    }
    ]], {}),
    parse("fdie",
    [[
    die () { >&2 printf '%s\n' "\$@"; exit 1; }
    ]], {}),
    parse("flogn",
    [[
    logn () { >&2 printf '%s\n' "\$@"; }
    ]], {}),
    parse("flog",
    [[
    log () { >&2 printf '%s' "\$@"; }
    ]], {}),
    s("inp", fmt(
    [[
    test -z "${{{}:=$1}}" && 
        {}="$(cat /dev/stdin)"
    echo "{}: ${}" 1>&2{}
    ]],
    { i(1), rep(1), rep(1), rep(1), i(0) })),
})

ls.add_snippets("javascript", {
    -- print
    s("pt", fmt("console.log({});{}", { i(1, "\"Hello World!\"") , i(0) })),
    s("rq", fmt("const {} = require('{}');", { i(1), rep(1) })),
    s("dbconn", fmt(
    [[
        let conn = null;
        try {{
            conn = await dbConnect();{}
            conn.end()
        }} catch(err) {{
            console.error('Error:', err);
        }}
    ]],
    { i(0) })),
    s("apr", fmt(
    [[
    app.get('{}', (req, res) => {{
        {}
    }});{}
    ]],
    { i(1), i(2, "res.send(\"Hello world!\")"), i(0) })),
    s("cerr", t "console.error('Error:', err);"),
    s("gel", fmt(
    [[
    let {} = document.getElementById('{}');{}
    ]],
    { i(1), rep(1), i(0) })),
})

ls.add_snippets("cs", {
    parse("cw", "Console.WriteLine($1);$0"),
})

ls.add_snippets("telekasten", {
    --link
    parse("ln", "[[$0]]", {}),
    s("cln", fmt(
    [[
    [{}]({}){}
    ]],
    {
        i(1),
        f(function ()
            return vim.fn.getreg('+')
        end),
        i(0)
    }
    )),
})