/
nvim_comment.lua
226 lines (193 loc) · 6.23 KB
/
nvim_comment.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
local api = vim.api
local M = {}
M.config = {
-- Linters prefer comment and line to have a space in between
marker_padding = true,
-- should comment out empty or whitespace only lines
comment_empty = true,
-- trim empty comment whitespace
comment_empty_trim_whitespace = true,
-- Should key mappings be created
create_mappings = true,
-- Normal mode mapping left hand side
line_mapping = "gcc",
-- Visual/Operator mapping left hand side
operator_mapping = "gc",
-- text object mapping, comment chunk
comment_chunk_text_object = "ic",
-- Hook function to call before commenting takes place
hook = nil,
}
function M.get_comment_wrapper()
local cs = api.nvim_buf_get_option(0, "commentstring")
-- make sure comment string is understood
if cs:find("%%s") then
local left, right = cs:match("^(.*)%%s(.*)")
if right == "" then
right = nil
end
-- left comment markers should have padding as linterers preffer
if M.config.marker_padding then
if not left:match("%s$") then
left = left .. " "
end
if right and not right:match("^%s") then
right = " " .. right
end
end
return left, right
else
api.nvim_command('echom "Commentstring not understood: ' .. cs .. '"')
end
end
function M.comment_line(l, indent, left, right, comment_empty, comment_empty_trim_whitespace)
if not comment_empty and l:match("^%s*$") then
return l
end
local line_empty = l:match("^%s*$")
-- standarise indentation before adding
local line = l:gsub("^" .. indent, "")
if right then
line = line .. right
end
local ret_line = indent .. left .. line
if comment_empty_trim_whitespace and line_empty then
return ret_line:gsub("^(.*)%s+$", "%1")
end
return ret_line
end
function M.uncomment_line(l, left, right, comment_empty_trim_whitespace)
local line = l
if right and right ~= "" then
line = line:gsub(vim.pesc(right) .. "$", "")
return line:gsub(vim.pesc(left), "", 1)
end
if comment_empty_trim_whitespace and left:match("%s+$") then
local left_nw = left:match("^(%S+)%s+$")
if line:match("^%s*" .. left_nw .. "$") then
return line:gsub(vim.pesc(left_nw), "", 1)
end
end
return line:gsub(vim.pesc(left), "", 1)
end
function M.operator(mode)
local line1, line2
if not mode then
line1 = api.nvim_win_get_cursor(0)[1]
line2 = line1
elseif mode:match("[vV]") then
line1 = api.nvim_buf_get_mark(0, "<")[1]
line2 = api.nvim_buf_get_mark(0, ">")[1]
else
line1 = api.nvim_buf_get_mark(0, "[")[1]
line2 = api.nvim_buf_get_mark(0, "]")[1]
end
M.comment_toggle(line1, line2)
end
function M.comment_toggle(line_start, line_end)
if type(M.config.hook) == "function" then
M.config.hook()
end
local left, right = M.get_comment_wrapper()
if not left or (not left and not right) then
return
end
local lines = api.nvim_buf_get_lines(0, line_start - 1, line_end, false)
if not lines then
return
end
-- check if any lines commented, capture indent
local esc_left = vim.pesc(left)
if M.config.comment_empty_trim_whitespace and left:match("%s+$") then
local left_nw = left:match("^(%S+)%s+$")
esc_left = vim.pesc(left_nw) .. "%s*"
end
local commented_lines_counter = 0
local empty_counter = 0
local indent
for _, v in pairs(lines) do
if v:find("^%s*" .. esc_left) then
commented_lines_counter = commented_lines_counter + 1
elseif v:match("^%s*$") then
empty_counter = empty_counter + 1
end
if not v:match("^%s*$") then
local line_indent = v:match("^%s+") or ""
if not indent or string.len(line_indent) < string.len(indent) then
indent = line_indent
end
end
end
for i, v in pairs(lines) do
if commented_lines_counter ~= (#lines - empty_counter) then
lines[i] = M.comment_line(
v,
indent,
left,
right,
M.config.comment_empty,
M.config.comment_empty_trim_whitespace
)
else
lines[i] = M.uncomment_line(v, left, right, M.config.comment_empty_trim_whitespace)
end
end
-- the LUA API doesn't seem to keep marks for lines that are changes with
-- nvim_buf_set_lines
api.nvim_call_function("setline", {line_start, lines})
-- The lua call seems to clear the visual selection so reset it
-- 2147483647 is vimL built in
api.nvim_call_function("setpos", { "'<", { 0, line_start, 1, 0 } })
api.nvim_call_function("setpos", { "'>", { 0, line_end, 2147483647, 0 } })
end
function M.select_comment_chunk()
vim.cmd([[execute "normal! \<esc>"]])
local up = vim.fn.search("\\v^(\\s*--)@!", "Wbcn")
up = up + 1
local down = vim.fn.search("\\v^(\\s*--)@!", "Wzn")
if down ~= 0 then
down = down - 1
end
local lines = vim.api.nvim_buf_line_count(0)
local pos = vim.api.nvim_win_get_cursor(0)[1]
if down == 0 then
down = lines
end
if up <= down and up <= pos and down >= pos then
vim.api.nvim_buf_set_mark(0, "<", up, 1, {})
vim.api.nvim_buf_set_mark(0, ">", down, 1, {})
vim.cmd("normal! `<V`>")
end
end
function M.setup(user_opts)
M.config = vim.tbl_extend("force", M.config, user_opts or {})
-- Messy, change with nvim_exec once merged
local vim_func = [[
function! CommentOperator(type) abort
let reg_save = @@
execute "lua require('nvim_comment').operator('" . a:type. "')"
let @@ = reg_save
endfunction
]]
vim.api.nvim_call_function("execute", { vim_func })
vim.api.nvim_command("command! -range CommentToggle lua require('nvim_comment').comment_toggle(<line1>, <line2>)")
if M.config.create_mappings then
local opts = { noremap = true }
api.nvim_set_keymap("n", M.config.line_mapping, "<Cmd>set operatorfunc=CommentOperator<CR>g@l", opts)
api.nvim_set_keymap("n", M.config.operator_mapping, "<Cmd>set operatorfunc=CommentOperator<CR>g@", opts)
api.nvim_set_keymap("x", M.config.operator_mapping, ":<C-u>call CommentOperator(visualmode())<CR>", opts)
api.nvim_set_keymap(
"x",
M.config.comment_chunk_text_object,
"<Cmd>lua require('nvim_comment').select_comment_chunk()<CR>",
opts
)
api.nvim_set_keymap(
"o",
M.config.comment_chunk_text_object,
"<Cmd>lua require('nvim_comment').select_comment_chunk()<CR>",
opts
)
end
end
return M