forked from nvim-mini/mini.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomment.lua
More file actions
373 lines (322 loc) · 11.8 KB
/
Copy pathcomment.lua
File metadata and controls
373 lines (322 loc) · 11.8 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
-- MIT License Copyright (c) 2021 Evgeni Chasnovski
---@brief [[
--- Custom minimal and fast Lua module for code commenting. This is basically a
--- reimplementation of "tpope/vim-commentary". Commenting in Normal mode
--- respects |count| and is dot-repeatable. Comment structure is inferred
--- from 'commentstring'. Handles both tab and space indenting (but not when
--- they are mixed).
---
--- What it doesn't do:
--- - Block and sub-line comments. This will only support per-line commenting.
--- - Configurable (from module) comment structure. Modify |commentstring|
--- instead.
--- - Handle indentation with mixed tab and space.
--- - Preserve trailing whitespace in empty lines.
---
--- # Setup
---
--- This module needs a setup with `require('mini.comment').setup({})` (replace
--- `{}` with your `config` table). It will create global Lua table
--- `MiniComment` which you can use for scripting or manually (with
--- `:lua MiniComment.*`).
---
--- Default `config`:
--- <code>
--- {
--- -- Module mappings. Use `''` (empty string) to disable one.
--- mappings = {
--- -- Toggle comment (like `gcip` - comment inner paragraph) for both
--- -- Normal and Visual modes
--- comment = 'gc',
---
--- -- Toggle comment on current line
--- comment_line = 'gcc',
---
--- -- Define 'comment' textobject (like `dgc` - delete whole comment block)
--- textobject = 'gc',
--- }
--- }
--- </code>
--- # Disabling
---
--- To disable core functionality, set `g:minicomment_disable` (globally) or
--- `b:minicomment_disable` (for a buffer) to `v:true`.
---@brief ]]
---@tag MiniComment mini.comment
-- Module and its helper
local MiniComment = {}
local H = {}
--- Module setup
---
---@param config table: Module config table.
---@usage `require('mini.comment').setup({})` (replace `{}` with your `config` table)
function MiniComment.setup(config)
-- Export module
_G.MiniComment = MiniComment
-- Setup config
config = H.setup_config(config)
-- Apply config
H.apply_config(config)
end
-- Module config
MiniComment.config = {
-- Module mappings. Use `''` (empty string) to disable one.
mappings = {
-- Toggle comment (like `gcip` - comment inner paragraph) for both Normal
-- and Visual modes
comment = 'gc',
-- Toggle comment on current line
comment_line = 'gcc',
-- Define 'comment' textobject (like `dgc` - delete whole comment block)
textobject = 'gc',
},
}
-- Module functionality
--- Main function to be mapped
---
--- It is meant to be used in expression mappings (see |map-<expr>|) to enable
--- dot-repeatability and commenting on range. There is no need to do this
--- manually, everything is done inside |MiniComment.setup()|.
---
--- It has a somewhat unintuitive logic (because of how expression mapping with
--- dot-repeatability works): it should be called without arguments inside
--- expression mapping and with argument when action should be performed.
---
---@param mode string: Optional string with 'operatorfunc' mode (see |g@|).
---@return string: 'g@' if called without argument, '' otherwise (but after performing action).
function MiniComment.operator(mode)
if H.is_disabled() then
return ''
end
-- If used without arguments inside expression mapping:
-- - Set itself as `operatorfunc` to be called later to perform action.
-- - Return 'g@' which will then be executed resulting into waiting for a
-- motion or text object. This textobject will then be recorded using `'[`
-- and `']` marks. After that, `operatorfunc` is called with `mode` equal
-- to one of "line", "char", or "block".
-- NOTE: setting `operatorfunc` inside this function enables usage of 'count'
-- like `10gc_` toggles comments of 10 lines below (starting with current).
if mode == nil then
vim.cmd('set operatorfunc=v:lua.MiniComment.operator')
return 'g@'
end
-- If called with non-nil `mode`, get target region and perform comment
-- toggling over it.
local mark1, mark2
if mode == 'visual' then
mark1, mark2 = '<', '>'
else
mark1, mark2 = '[', ']'
end
local l1 = vim.api.nvim_buf_get_mark(0, mark1)[1]
local l2 = vim.api.nvim_buf_get_mark(0, mark2)[1]
-- Using `vim.cmd()` wrapper to allow usage of `lockmarks` command, because
-- raw execution will delete marks inside region (due to
-- `vim.api.nvim_buf_set_lines()`).
vim.cmd(string.format('lockmarks lua MiniComment.toggle_lines(%d, %d)', l1, l2))
return ''
end
--- Toggle comments between two line numbers
---
--- It uncomments if lines are comment (every line is a comment) and comments
--- otherwise. It respects indentation and doesn't insert trailing
--- whitespace. Toggle commenting not in visual mode is also dot-repeatable
--- and respects |count|.
---
--- # Notes
--- 1. Currently call to this function will remove marks inside written range.
--- Use |lockmarks| to preserve marks.
---
---@param line_start number: Start line number.
---@param line_end number: End line number.
function MiniComment.toggle_lines(line_start, line_end)
if H.is_disabled() then
return
end
local comment_parts = H.make_comment_parts()
local lines = vim.api.nvim_buf_get_lines(0, line_start - 1, line_end, false)
local indent, is_comment = H.get_lines_info(lines, comment_parts)
local f
if is_comment then
f = H.make_uncomment_function(comment_parts)
else
f = H.make_comment_function(comment_parts, indent)
end
for n, l in pairs(lines) do
lines[n] = f(l)
end
-- NOTE: This function call removes marks inside written range. To write
-- lines in a way that saves marks, use one of:
-- - `lockmarks` command when doing mapping (current approach).
-- - `vim.fn.setline(line_start, lines)`, but this is **considerably**
-- slower: on 10000 lines 280ms compared to 40ms currently.
vim.api.nvim_buf_set_lines(0, line_start - 1, line_end, false, lines)
end
--- Comment textobject
--- This selects all commented lines adjacent to cursor line (if it itself is commented).
--- Designed to be used with operator mode mappings (see |mapmode-o|).
function MiniComment.textobject()
if H.is_disabled() then
return
end
local comment_parts = H.make_comment_parts()
local comment_check = H.make_comment_check(comment_parts)
local line_cur = vim.api.nvim_win_get_cursor(0)[1]
if not comment_check(vim.fn.getline(line_cur)) then
return
end
local line_start = line_cur
while (line_start >= 2) and comment_check(vim.fn.getline(line_start - 1)) do
line_start = line_start - 1
end
local line_end = line_cur
local n_lines = vim.api.nvim_buf_line_count(0)
while (line_end <= n_lines - 1) and comment_check(vim.fn.getline(line_end + 1)) do
line_end = line_end + 1
end
-- This visual selection doesn't seem to change `'<` and `'>` marks when
-- executed as `onoremap` mapping
vim.cmd(string.format('normal! %dGV%dG', line_start, line_end))
end
-- Helpers
---- Module default config
H.default_config = MiniComment.config
---- Settings
function H.setup_config(config)
-- General idea: if some table elements are not present in user-supplied
-- `config`, take them from default config
vim.validate({ config = { config, 'table', true } })
config = vim.tbl_deep_extend('force', H.default_config, config or {})
vim.validate({
mappings = { config.mappings, 'table' },
['mappings.comment'] = { config.mappings.comment, 'string' },
['mappings.comment_line'] = { config.mappings.comment_line, 'string' },
['mappings.textobject'] = { config.mappings.textobject, 'string' },
})
return config
end
function H.apply_config(config)
MiniComment.config = config
-- Make mappings
H.keymap('n', config.mappings.comment, 'v:lua.MiniComment.operator()', {
expr = true,
noremap = true,
silent = true,
})
H.keymap(
'x',
config.mappings.comment,
-- Using `:<c-u>` instead of `<cmd>` as latter results into executing before
-- proper update of `'<` and `'>` marks which is needed to work correctly.
[[:<c-u>lua MiniComment.operator('visual')<cr>]],
{ noremap = true, silent = true }
)
H.keymap(
'n',
config.mappings.comment_line,
'v:lua.MiniComment.operator() . "_"',
{ expr = true, noremap = true, silent = true }
)
H.keymap(
'o',
config.mappings.textobject,
[[<cmd>lua MiniComment.textobject()<cr>]],
{ noremap = true, silent = true }
)
end
function H.is_disabled()
return vim.g.minicomment_disable == true or vim.b.minicomment_disable == true
end
---- Core implementations
function H.make_comment_parts()
local cs = vim.api.nvim_buf_get_option(0, 'commentstring')
if cs == '' then
vim.notify([[(mini.comment) Option 'commentstring' is empty.]])
return { left = '', right = '' }
end
-- Assumed structure of 'commentstring':
-- <space> <left> <space> <'%s'> <space> <right> <space>
-- So this extracts parts without surrounding whitespace
local left, right = cs:match('^%s*(.-)%s*%%s%s*(.-)%s*$')
return { left = left, right = right }
end
function H.make_comment_check(comment_parts)
local l, r = comment_parts.left, comment_parts.right
-- String is commented if it has structure:
-- <space> <left> <anything> <right> <space>
local regex = string.format([[^%%s-%s.*%s%%s-$]], vim.pesc(l), vim.pesc(r))
return function(line)
return line:find(regex) ~= nil
end
end
function H.get_lines_info(lines, comment_parts)
local n_indent, n_indent_cur = math.huge, math.huge
local indent, indent_cur
local is_comment = true
local comment_check = H.make_comment_check(comment_parts)
for _, l in pairs(lines) do
-- Update lines indent: minimum of all indents except empty lines
if n_indent > 0 then
_, n_indent_cur, indent_cur = l:find('^(%s*)')
-- Condition "current n-indent equals line length" detects empty line
if (n_indent_cur < n_indent) and (n_indent_cur < l:len()) then
-- NOTE: Copy of actual indent instead of recreating it with `n_indent`
-- allows to handle both tabs and spaces
n_indent = n_indent_cur
indent = indent_cur
end
end
-- Update comment info: lines are comment if every single line is comment
if is_comment then
is_comment = comment_check(l)
end
end
-- `indent` can still be `nil` in case all `lines` are empty
return indent or '', is_comment
end
function H.make_comment_function(comment_parts, indent)
-- NOTE: this assumes that indent doesn't mix tabs with spaces
local nonindent_start = indent:len() + 1
local l, r = comment_parts.left, comment_parts.right
local lpad = (l == '') and '' or ' '
local rpad = (r == '') and '' or ' '
local empty_comment = indent .. l .. r
local nonempty_format = indent .. l .. lpad .. '%s' .. rpad .. r
return function(line)
-- Line is empty if it doesn't have anything except whitespace
if line:find('^%s*$') ~= nil then
-- If doesn't want to comment empty lines, return `line` here
return empty_comment
else
return string.format(nonempty_format, line:sub(nonindent_start))
end
end
end
function H.make_uncomment_function(comment_parts)
local l, r = comment_parts.left, comment_parts.right
local lpad = (l == '') and '' or '[ ]?'
local rpad = (r == '') and '' or '[ ]?'
-- Usage of `lpad` and `rpad` as possbile single space enables uncommenting
-- of commented empty lines without trailing whitespace (like ' #').
local uncomment_regex = string.format([[^(%%s-)%s%s(.-)%s%s%%s-$]], vim.pesc(l), lpad, rpad, vim.pesc(r))
return function(line)
local indent, new_line = string.match(line, uncomment_regex)
-- Return original if line is not commented
if new_line == nil then
return line
end
-- Remove indent if line is a commented empty line
if new_line == '' then
indent = ''
end
return ('%s%s'):format(indent, new_line)
end
end
---- Utilities
function H.keymap(mode, keys, cmd, opts)
if keys == '' then
return
end
vim.api.nvim_set_keymap(mode, keys, cmd, opts)
end
return MiniComment