forked from zbirenbaum/copilot-cmp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern.lua
More file actions
53 lines (46 loc) · 1.17 KB
/
pattern.lua
File metadata and controls
53 lines (46 loc) · 1.17 KB
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
local pattern = {}
local ch_pairs = {
['('] = '%)',
['['] = '%]',
['{'] = '%}',
[')'] = '%(',
[']'] = '%[',
['}'] = '%{',
}
local fmt_char = {
['('] = '%(',
['['] = '%[',
['{'] = '%{',
[')'] = '%)',
[']'] = '%]',
['}'] = '%}',
}
-- check if text has pair for char c
local function text_has_pair(text, c)
if not text or not c then return false end
return text:find(ch_pairs[c]) ~= nil
end
-- check if text has char c
local function text_has_char(text, c)
if not text or not c then return false end
c = fmt_char[c] or c
return text:find(c) ~= nil
end
local function get_text_after_cursor()
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
local line_text = vim.api.nvim_buf_get_lines(0, row-1, row, false)[1]
local suffix = line_text:sub(col+1)
return suffix
end
-- get text after cursor and check if it has pair for char c
-- if present add it to text so it is there after replacement
function pattern.set_suffix(text, line_suffix)
for i = 1, #line_suffix do
local c = line_suffix:sub(i,i)
if ch_pairs[c] and text_has_pair(text, c) and not text_has_char(text, c) then
text = text .. c
end
end
return text
end
return pattern