-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathultimate-autopair.lua
231 lines (220 loc) · 5.22 KB
/
ultimate-autopair.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
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
230
231
local ap_utils = require('ultimate-autopair.utils')
---Filetype options memoization
---@type table<string, table<string, string|integer|boolean|table>>
local ft_opts = vim.defaulttable(function()
return {}
end)
---Get option value for given filetype, with memoization for performance
---This fixes sluggish `<CR>` in markdown files
---TODO: upstream to ultimate-autopair
---@param ft string
---@param opt string
---@diagnostic disable-next-line: duplicate-set-field
ap_utils.ft_get_option = function(ft, opt)
local opts = ft_opts[ft]
local opt_val = opts[opt]
if opt_val ~= nil then
return opt_val
end
opt_val = vim.F.npcall(vim.filetype.get_option, ft, opt) or vim.bo[opt]
opts[opt] = opt_val
return opt_val
end
ap_utils.getsmartft = (function(cb)
return function(o, notree, ...)
return cb(o, vim.b.bigfile or notree, ...)
end
end)(ap_utils.getsmartft)
---Get next two characters after cursor
---@return string: next two characters
local function get_suffix()
local col, line
if vim.startswith(vim.fn.mode(), 'c') then
col = vim.fn.getcmdpos()
line = vim.fn.getcmdline()
else
col = vim.fn.col('.')
line = vim.api.nvim_get_current_line()
end
return line:sub(col, col + 1)
end
-- Matches strings that start with:
-- keywords: \k
-- opening pairs: (, [, {, \(, \[, \{
local IGNORE_REGEX = vim.regex([=[^\%(\k\|\\\?[([{]\)]=])
---Record previous cmdline completion types,
---`cmdcompltype[1]` is the current completion type,
---`cmdcompltype[2]` is the previous completion type
---@type string[]
local compltype = {}
vim.api.nvim_create_autocmd('CmdlineChanged', {
desc = 'Record cmd compltype to determine whether to autopair.',
group = vim.api.nvim_create_augroup('AutopairRecordCmdCompltype', {}),
callback = function()
local type = vim.fn.getcmdcompltype()
if compltype[1] == type then
return
end
compltype[2] = compltype[1]
compltype[1] = type
end,
})
require('ultimate-autopair').setup({
extensions = {
suround = false,
-- Improve performance when typing fast, see
-- https://github.com/altermo/ultimate-autopair.nvim/issues/74
utf8 = false,
cond = {
cond = function(f)
-- Disable autopairs when inserting a regex,
-- e.g. `:s/{pattern}/{string}/[flags]` or
-- `:g/{pattern}/[cmd]`, etc.
if f.in_cmdline() then
return compltype[2] ~= 'command' or compltype[1] ~= ''
end
return not f.in_macro()
-- Disable autopairs if followed by a keyword or an opening pair
and not IGNORE_REGEX:match_str(get_suffix())
end,
},
},
{ '\\(', '\\)', newline = true },
{ '\\[', '\\]', newline = true },
{ '\\{', '\\}', newline = true },
{ '[=[', ']=]', ft = { 'lua' } },
{ '<<<', '>>>', ft = { 'cuda' } },
{
'/*',
'*/',
ft = { 'c', 'cpp', 'cuda', 'go' },
newline = true,
space = true,
},
{
'<',
'>',
disable_start = true,
disable_end = true,
},
{
'>',
'<',
ft = { 'html', 'xml', 'markdown' },
disable_start = true,
disable_end = true,
newline = true,
space = true,
},
-- Paring '$' and '*' are handled by snippets,
-- only use autopair to delete matched pairs here
{
'$',
'$',
ft = { 'markdown', 'tex' },
disable_start = true,
disable_end = true,
},
{
'*',
'*',
ft = { 'markdown' },
disable_start = true,
disable_end = true,
},
{
'\\left(',
'\\right)',
newline = true,
space = true,
ft = { 'markdown', 'tex' },
},
{
'\\left[',
'\\right]',
newline = true,
space = true,
ft = { 'markdown', 'tex' },
},
{
'\\left{',
'\\right}',
newline = true,
space = true,
ft = { 'markdown', 'tex' },
},
{
'\\left<',
'\\right>',
newline = true,
space = true,
ft = { 'markdown', 'tex' },
},
{
'\\left\\lfloor',
'\\right\\rfloor',
newline = true,
space = true,
ft = { 'markdown', 'tex' },
},
{
'\\left\\lceil',
'\\right\\rceil',
newline = true,
space = true,
ft = { 'markdown', 'tex' },
},
{
'\\left\\vert',
'\\right\\vert',
newline = true,
space = true,
ft = { 'markdown', 'tex' },
},
{
'\\left\\lVert',
'\\right\\rVert',
newline = true,
space = true,
ft = { 'markdown', 'tex' },
},
{
'\\left\\lVert',
'\\right\\rVert',
newline = true,
space = true,
ft = { 'markdown', 'tex' },
},
{
'\\begin{bmatrix}',
'\\end{bmatrix}',
newline = true,
space = true,
ft = { 'markdown', 'tex' },
},
{
'\\begin{pmatrix}',
'\\end{pmatrix}',
newline = true,
space = true,
ft = { 'markdown', 'tex' },
},
})
local has_cmp, cmp = pcall(require, 'cmp')
if has_cmp then
local format = require('cmp.config').get().formatting.format
cmp.setup({
formatting = {
format = function(entry, item)
-- Don't complete left parenthesis when calling functions or
-- expressions in cmdline, e.g. `:call func(...`
local type = compltype[1]
if type == 'function' or type == 'expression' then
item.word = string.gsub(item.word, '%($', '')
item.abbr = item.word
end
return format(entry, item)
end,
},
})
end