tangled
alpha
login
or
join now
boltless.me
/
nvim_rocks
0
fork
atom
neovim configuration using rocks.nvim plugin manager
0
fork
atom
overview
issues
pulls
pipelines
feat(snippets): cleanup, update
Seongmin Lee
1 year ago
6d67e48e
108d4b23
+4
-98
2 changed files
expand all
collapse all
unified
split
lua
snippets
all.lua
go.lua
+3
-98
lua/snippets/all.lua
···
7
7
local t = ls.text_node
8
8
local c = ls.choice_node
9
9
local r = ls.restore_node
10
10
-
local fmt = require("luasnip.extras.fmt").fmt
11
10
12
12
-
local function prevent_reexpand(_line_to_cursor, matched_trigger)
13
13
-
local current_node = ls.session.current_nodes[vim.api.nvim_get_current_buf()]
14
14
-
if not current_node then
15
15
-
return true
16
16
-
end
17
17
-
local is_same_trigger = current_node.parent.snippet.trigger == matched_trigger
18
18
-
local current_is_0 = current_node.pos == 0
19
19
-
if not (is_same_trigger and current_is_0) then
20
20
-
return true
21
21
-
end
22
22
-
local node_pos = current_node:get_buf_position()
23
23
-
local cur_pos = vim.api.nvim_win_get_cursor(0)
24
24
-
local same_pos = node_pos[1] + 1 == cur_pos[1] and node_pos[2] == cur_pos[2]
25
25
-
return not same_pos
26
26
-
end
27
27
-
28
28
-
local function create_autopair(open, close)
29
29
-
return s({
30
30
-
trig = open .. close,
31
31
-
wordTrig = false,
32
32
-
}, {
33
33
-
t(open),
34
34
-
c(1, {
35
35
-
r(1, "content", i(1)),
36
36
-
sn(nil, { t({ "", "\t" }), r(1, "content", i(1)), t({ "", "" }) }),
37
37
-
}),
38
38
-
t(close),
39
39
-
}, { condition = prevent_reexpand })
40
40
-
end
41
41
-
42
42
-
-- stylua: ignore
43
43
-
ls.add_snippets(nil, {
44
44
-
create_autopair("(", ")"),
45
45
-
create_autopair("[", "]"),
46
46
-
create_autopair("{", "}"),
47
47
-
create_autopair("(", "),"),
48
48
-
create_autopair("[", "],"),
49
49
-
create_autopair("{", "},"),
50
50
-
s({ trig = '""', wordTrig = false }, { t('"'), i(1), t('"') }, { condition = prevent_reexpand }),
51
51
-
s({ trig = "''", wordTrig = false }, { t("'"), i(1), t("'") }, { condition = prevent_reexpand }),
52
52
-
s({ trig = "``", wordTrig = false }, { t("`"), i(1), t("`") }, { condition = prevent_reexpand }),
53
53
-
})
54
54
-
55
55
-
-- autopair code from Luasnip's wiki
56
56
-
57
57
-
local function char_count_same(c1, c2)
58
58
-
local line = vim.api.nvim_get_current_line()
59
59
-
-- '%'-escape chars to force explicit match (gsub accepts patterns).
60
60
-
-- second return value is number of substitutions.
61
61
-
local _, ct1 = string.gsub(line, "%" .. c1, "")
62
62
-
local _, ct2 = string.gsub(line, "%" .. c2, "")
63
63
-
return ct1 == ct2
64
64
-
end
65
65
-
66
66
-
local function even_count(ch)
67
67
-
local line = vim.api.nvim_get_current_line()
68
68
-
local _, ct = string.gsub(line, ch, "")
69
69
-
return ct % 2 == 0
70
70
-
end
71
71
-
72
72
-
local function neg(fn, ...)
73
73
-
return not fn(...)
74
74
-
end
75
75
-
76
76
-
local function posi()
77
77
-
return true
78
78
-
end
79
79
-
80
80
-
local function part(fn, ...)
81
81
-
local args = { ... }
82
82
-
return function()
83
83
-
return fn(unpack(args))
84
84
-
end
85
85
-
end
86
86
-
87
87
-
-- This makes creation of pair-type snippets easier.
88
88
-
local function pair(pair_begin, pair_end, expand_func, ...)
89
89
-
-- triggerd by opening part of pair, wordTrig=false to trigger anywhere.
90
90
-
-- ... is used to pass any args following the expand_func to it.
11
11
+
local function pair(pair_begin, pair_end)
12
12
+
-- autopair implementation inspired by Luasnip wiki
13
13
+
-- https://github.com/L3MON4D3/LuaSnip/wiki/Cool-Snippets#all---pairs
91
14
return s({ trig = pair_begin, wordTrig = false }, {
92
15
t({ pair_begin }),
93
16
c(1, {
···
95
18
sn(nil, { t({ "", "\t" }), r(1, "content", i(1)), t({ "", "" }) }),
96
19
}),
97
20
t({ pair_end }),
98
98
-
}, {
99
99
-
condition = expand_func and part(expand_func, part(..., pair_begin, pair_end)),
100
21
})
101
22
end
102
102
-
103
103
-
-- FIXME: pair() also doesn't work bc of this case:
104
104
-
-- ```
105
105
-
-- vim.schedule(function (|)
106
106
-
-- end)
107
107
-
-- ```
108
108
-
-- here, I get unwanted autopair (|))
109
109
-
ls.add_snippets(nil, {
110
110
-
pair("(", ")", neg, char_count_same),
111
111
-
pair("{", "}", neg, char_count_same),
112
112
-
pair("[", "]", neg, char_count_same),
113
113
-
pair("<", ">", neg, char_count_same),
114
114
-
pair("'", "'", neg, even_count),
115
115
-
pair('"', '"', neg, even_count),
116
116
-
pair("`", "`", neg, even_count),
117
117
-
})
118
23
119
24
ls.add_snippets("all", {
120
25
pair("(", ")"),
+1
lua/snippets/go.lua
···
15
15
t"\t", c(1, {
16
16
sn(nil, { t"return", i(1), t" err" }),
17
17
t"log.Fatalln(err)",
18
18
+
t"panic(err)",
18
19
}),
19
20
t{"", "}"}, i(0),
20
21
}),