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
|
local M = {}
M.exec = function(cmd)
return io.popen(cmd):read'*l'
end
local function find_sub(s, replacement, pattern, start, special)
local x1, x2 = s:find(pattern, start, special)
if x1 then
return s:sub(1, x1-1) .. replacement .. s:sub(x2+1), x1, x2
end
return s
end
-- Only works with prefix comments.
local function get_line_comment()
local cms = vim.bo.commentstring
-- This whole pesc dance is a bummer.
local pattern = '^(%s*)' ..
(find_sub(vim.pesc((find_sub(cms, '\0', '%s', 1, true))), '(%s*).*', '\0', 1, true))
local pre_ws, inner_ws = vim.api.nvim_get_current_line():match(pattern)
if pre_ws then
return pre_ws .. cms:format('') .. inner_ws
end
return ''
end
M.comment = function(str)
-- add space
local prefix = vim.bo.commentstring:format(''):gsub('%S$', '%0 ')
local comment = get_line_comment()
if comment ~= '' then
prefix = ''
end
return prefix .. str
end
return M
|