-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathinit.lua
174 lines (141 loc) · 4.85 KB
/
init.lua
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
local ls = require("luasnip")
local conds = require("luasnip.extras.expand_conditions")
local utils = require("luasnip-latex-snippets.util.utils")
local pipe = utils.pipe
local no_backslash = utils.no_backslash
local M = {}
local default_opts = {
use_treesitter = false,
}
M.setup = function(opts)
opts = vim.tbl_deep_extend("force", default_opts, opts or {})
local augroup = vim.api.nvim_create_augroup("luasnip-latex-snippets", {})
vim.api.nvim_create_autocmd("FileType", {
pattern = "tex",
group = augroup,
once = true,
callback = function()
local is_math = utils.with_opts(utils.is_math, opts.use_treesitter)
local not_math = utils.with_opts(utils.not_math, opts.use_treesitter)
M.setup_tex(is_math, not_math)
end,
})
vim.api.nvim_create_autocmd("FileType", {
pattern = "markdown",
group = augroup,
once = true,
callback = function()
M.setup_markdown()
end,
})
end
local _autosnippets = function(is_math, not_math)
local match_pattern = require("luasnip/nodes/util/trig_engines").pattern()
local autosnippets = {}
for _, snip in ipairs(require("luasnip-latex-snippets/math_wRA_no_backslash")) do
snip.trig_matcher = match_pattern
snip.condition = pipe({ is_math, no_backslash })
table.insert(autosnippets, snip)
end
for _, snip in ipairs(require("luasnip-latex-snippets/math_rA_no_backslash")) do
snip.wordTrig = false
snip.trig_matcher = match_pattern
snip.condition = pipe({ is_math, no_backslash })
table.insert(autosnippets, snip)
end
for _, snip in ipairs(require("luasnip-latex-snippets/normal_wA")) do
snip.condition = pipe({ not_math })
table.insert(autosnippets, snip)
end
for _, snip in ipairs(require("luasnip-latex-snippets/math_wrA")) do
snip.trig_matcher = match_pattern
snip.condition = pipe({ is_math })
table.insert(autosnippets, snip)
end
for _, snip in ipairs(require("luasnip-latex-snippets/math_wA_no_backslash")) do
snip.condition = pipe({ is_math, no_backslash })
table.insert(autosnippets, snip)
end
for _, snip in ipairs(require("luasnip-latex-snippets/math_iA")) do
snip.wordTrig = false
snip.condition = pipe({ is_math, no_backslash })
table.insert(autosnippets, snip)
end
for _, snip in ipairs(require("luasnip-latex-snippets/math_iA_no_backslash")) do
snip.wordTrig = false
snip.condition = pipe({ is_math, no_backslash })
table.insert(autosnippets, snip)
end
for _, snip in ipairs(require("luasnip-latex-snippets/math_bwA")) do
snip.condition = pipe({ conds.line_begin, is_math })
table.insert(autosnippets, snip)
end
for _, snip in ipairs(require("luasnip-latex-snippets/bwA")) do
snip.condition = pipe({ conds.line_begin, not_math })
table.insert(autosnippets, snip)
end
return autosnippets
end
M.setup_tex = function(is_math, not_math)
ls.add_snippets("tex", {
ls.parser.parse_snippet(
{ trig = "pac", name = "Package" },
"\\usepackage[${1:options}]{${2:package}}$0"
),
-- ls.parser.parse_snippet({ trig = "nn", name = "Tikz node" }, {
-- "$0",
-- -- "\\node[$5] (${1/[^0-9a-zA-Z]//g}${2}) ${3:at (${4:0,0}) }{$${1}$};",
-- "\\node[$5] (${1}${2}) ${3:at (${4:0,0}) }{$${1}$};",
-- }),
})
local math_i = require("luasnip-latex-snippets/math_i")
for _, snip in ipairs(math_i) do
snip.condition = pipe({ is_math })
snip.show_condition = is_math
snip.wordTrig = false
end
ls.add_snippets("tex", math_i, { default_priority = 0 })
ls.add_snippets("tex", _autosnippets(is_math, not_math), {
type = "autosnippets",
default_priority = 0,
})
end
M.setup_markdown = function()
local is_math = utils.with_opts(utils.is_math, true)
local not_math = utils.with_opts(utils.not_math, true)
local autosnippets = _autosnippets(is_math, not_math)
local trigger_of_snip = function(s)
return s.trigger
end
local normal_wA = vim.tbl_map(trigger_of_snip, require("luasnip-latex-snippets/normal_wA"))
local bwa = vim.tbl_map(trigger_of_snip, require("luasnip-latex-snippets/bwA"))
local to_filter = { bwa, normal_wA }
local filtered = vim.tbl_filter(function(s)
for _, t in pairs(to_filter) do
for _, v in pairs(t) do
if s.trigger == v then
return false
end
end
end
return true
end, autosnippets)
-- tex delimiters
local normal_wA_tex = {
ls.parser.parse_snippet({ trig = "mk", name = "Math" }, "$${1:${TM_SELECTED_TEXT}}$"),
ls.parser.parse_snippet(
{ trig = "dm", name = "Block Math" },
"$$\n\t${1:${TM_SELECTED_TEXT}}\n.$$"
),
}
local not_math = utils.with_opts(utils.not_math, true)
for _, snip in ipairs(normal_wA_tex) do
snip.condition = pipe({ not_math })
table.insert(filtered, snip)
end
ls.add_snippets("markdown", filtered, {
type = "autosnippets",
default_priority = 0,
})
end
return M