Skip to content

Commit f9df984

Browse files
authored
feat!: <Plug> mappings (#98)
Following are the plug mappings that are now available `<Plug>(comment_toggle_linewise_count)` - Toggles line comment with count `<Plug>(comment_toggle_blockwise_count)` - Toggles block comment with count `<Plug>(comment_toggle_current_linewise)` - Toggles line comment on the current line `<Plug>(comment_toggle_current_blockwise)` - Toggles block comment on the current line `<Plug>(comment_toggle_linewise)` - Toggles line comment via Operator pending mode `<Plug>(comment_toggle_blockwise)` - Toggles block comment via Operator pending mode `<Plug>(comment_toggle_linewise_visual)` - Toggles line comment in VISUAL mode `<Plug>(comment_toggle_blockwise_visual)` - Toggles block comment in VISUAL mode Read https://github.com/numToStr/Comment.nvim/blob/master/doc/plugs.md --- This is a breaking change for nvim 0.6 users. So If you are using 0.6 please follow https://github.com/numToStr/Comment.nvim/releases/tag/v0.6
1 parent 0aaea32 commit f9df984

File tree

7 files changed

+134
-83
lines changed

7 files changed

+134
-83
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ Following are the **default** config for the [`setup()`](#setup). If you want to
111111
},
112112

113113
---Create basic (operator-pending) and extended mappings for NORMAL + VISUAL mode
114-
---@type table
114+
---NOTE: If `mappings = false` then the plugin won't create any mappings
115+
---@type boolean|table
115116
mappings = {
116117
---Operator-pending mapping
117118
---Includes `gcc`, `gbc`, `gc[count]{motion}` and `gb[count]{motion}`
@@ -225,7 +226,9 @@ These mappings are disabled by default. (config: `mappings.extended`)
225226

226227
### ⚙️ API
227228

228-
Read [doc/API.md](./doc/API.md) to see all the API/functions that are exported from the plugin.
229+
- [Plug Mappings](./doc/plugs.md) - Excellent for creating custom keybindings
230+
231+
- [Lua API](./doc/API.md) - Details the Lua API. Great for making custom comment function.
229232

230233
<a id="treesitter"></a>
231234

after/plugin/Comment.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
local K = vim.keymap.set
2+
3+
-- Count mappings
4+
K(
5+
'n',
6+
'<Plug>(comment_toggle_linewise_count)',
7+
'<CMD>lua require("Comment.api").call("toggle_linewise_count_op")<CR>g@$'
8+
)
9+
K(
10+
'n',
11+
'<Plug>(comment_toggle_blockwise_count)',
12+
'<CMD>lua require("Comment.api").call("toggle_blockwise_count_op")<CR>g@$'
13+
)
14+
15+
-- Toggle mappings
16+
K(
17+
'n',
18+
'<Plug>(comment_toggle_current_linewise)',
19+
'<CMD>lua require("Comment.api").call("toggle_current_linewise_op")<CR>g@$'
20+
)
21+
K(
22+
'n',
23+
'<Plug>(comment_toggle_current_blockwise)',
24+
'<CMD>lua require("Comment.api").call("toggle_current_blockwise_op")<CR>g@$'
25+
)
26+
27+
-- Operator-Pending mappings
28+
K('n', '<Plug>(comment_toggle_linewise)', '<CMD>lua require("Comment.api").call("toggle_linewise_op")<CR>g@')
29+
K('n', '<Plug>(comment_toggle_blockwise)', '<CMD>lua require("Comment.api").call("toggle_blockwise_op")<CR>g@')
30+
31+
-- Visual-Mode mappings
32+
K(
33+
'x',
34+
'<Plug>(comment_toggle_linewise_visual)',
35+
'<ESC><CMD>lua require("Comment.api").locked.toggle_linewise_op(vim.fn.visualmode())<CR>'
36+
)
37+
K(
38+
'x',
39+
'<Plug>(comment_toggle_blockwise_visual)',
40+
'<ESC><CMD>lua require("Comment.api").locked.toggle_blockwise_op(vim.fn.visualmode())<CR>'
41+
)

doc/API.md

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,23 @@ These APIs powers the [extra-mappings](../README.md#extra-mappings) and also pro
6363
---@param cfg? Config
6464
require('Comment.api').insert_linewise_below(cfg)
6565

66-
---Insert a blockwise-comment below
67-
---@param cfg? Config
68-
require('Comment.api').insert_blockwise_below(cfg)
69-
7066
---Insert a linewise-comment above
7167
---@param cfg? Config
7268
require('Comment.api').insert_linewise_above(cfg)
7369

70+
---Insert a linewise-comment at the end-of-line
71+
---@param cfg? Config
72+
require('Comment.api').insert_linewise_eol(cfg)
73+
7474
--######### BLOCKWISE #########--
7575

76-
---Insert a blockwise-comment above
76+
---Insert a blockwise-comment below
7777
---@param cfg? Config
78-
require('Comment.api').insert_blockwise_above(cfg)
78+
require('Comment.api').insert_blockwise_below(cfg)
7979

80-
---Insert a linewise-comment at the end-of-line
80+
---Insert a blockwise-comment above
8181
---@param cfg? Config
82-
require('Comment.api').insert_linewise_eol(cfg)
82+
require('Comment.api').insert_blockwise_above(cfg)
8383

8484
---Insert a blockwise-comment at the end-of-line
8585
---@param cfg? Config
@@ -158,37 +158,31 @@ require('Comment.api').call(cb)
158158
Following are some example keybindings using the APIs.
159159

160160
```lua
161-
local function map(mode, lhs, rhs)
162-
vim.api.nvim_set_keymap(mode, lhs, rhs, { noremap = true, silent = true })
163-
end
164-
165-
---
166-
167161
-- # NORMAL mode
168162

169163
-- Linewise toggle current line using C-/
170-
map('n', '<C-_>', '<CMD>lua require("Comment.api").toggle_current_linewise()<CR>')
164+
vim.keymap.set('n', '<C-_>', '<CMD>lua require("Comment.api").toggle_current_linewise()<CR>')
171165
-- or with dot-repeat support
172-
-- map('n', '<C-_>', '<CMD>lua require("Comment.api").call("toggle_current_linewise_op")<CR>g@$')
166+
-- vim.keymap.set('n', '<C-_>', '<CMD>lua require("Comment.api").call("toggle_current_linewise_op")<CR>g@$')
173167

174168
-- Blockwise toggle current line using C-\
175-
map('n', '<C-\\>', '<CMD>lua require("Comment.api").toggle_current_blockwise()<CR>')
169+
vim.keymap.set('n', '<C-\\>', '<CMD>lua require("Comment.api").toggle_current_blockwise()<CR>')
176170
-- or with dot-repeat support
177-
-- map('n', '<C-\\>', '<CMD>lua require("Comment.api").call("toggle_current_blockwise_op")<CR>g@$')
171+
-- vim.keymap.set('n', '<C-\\>', '<CMD>lua require("Comment.api").call("toggle_current_blockwise_op")<CR>g@$')
178172

179173
-- Linewise toggle multiple line using <leader>gc with dot-repeat support
180174
-- Example: <leader>gc3j will comment 4 lines
181-
map('n', '<leader>gc', '<CMD>lua require("Comment.api").call("toggle_linewise_op")<CR>g@')
175+
vim.keymap.set('n', '<leader>gc', '<CMD>lua require("Comment.api").call("toggle_linewise_op")<CR>g@')
182176

183177
-- Blockwise toggle multiple line using <leader>gc with dot-repeat support
184178
-- Example: <leader>gb3j will comment 4 lines
185-
map('n', '<leader>gb', '<CMD>lua require("Comment.api").call("toggle_blockwise_op")<CR>g@')
179+
vim.keymap.set('n', '<leader>gb', '<CMD>lua require("Comment.api").call("toggle_blockwise_op")<CR>g@')
186180

187181
-- # VISUAL mode
188182

189183
-- Linewise toggle using C-/
190-
map('x', '<C-_>', '<ESC><CMD>lua require("Comment.api").toggle_linewise_op(vim.fn.visualmode())<CR>')
184+
vim.keymap.set('x', '<C-_>', '<ESC><CMD>lua require("Comment.api").toggle_linewise_op(vim.fn.visualmode())<CR>')
191185

192186
-- Blockwise toggle using <leader>gb
193-
map('x', '<leader>gb', '<ESC><CMD>lua require("Comment.api").toggle_blockwise_op(vim.fn.visualmode())<CR>')
187+
vim.keymap.set('x', '<leader>gb', '<ESC><CMD>lua require("Comment.api").toggle_blockwise_op(vim.fn.visualmode())<CR>')
194188
```

doc/plugs.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## 🔌 Plug Mappings
2+
3+
Following are the `<Plug>` mappings which you can use to quickly setup your custom keybindings
4+
5+
- `<Plug>(comment_toggle_linewise_count)` - Toggles line comment with count
6+
- `<Plug>(comment_toggle_blockwise_count)` - Toggles block comment with count
7+
- `<Plug>(comment_toggle_current_linewise)` - Toggles line comment on the current line
8+
- `<Plug>(comment_toggle_current_blockwise)` - Toggles block comment on the current line
9+
- `<Plug>(comment_toggle_linewise)` - Toggles line comment via Operator pending mode
10+
- `<Plug>(comment_toggle_blockwise)` - Toggles block comment via Operator pending mode
11+
- `<Plug>(comment_toggle_linewise_visual)` - Toggles line comment in VISUAL mode
12+
- `<Plug>(comment_toggle_blockwise_visual)` - Toggles block comment in VISUAL mode
13+
14+
> NOTE: There are only meant for custom keybindings but If you want to create a custom comment function then be sure to check out all the [API](./API.md).
15+
16+
#### Usage
17+
18+
Following snippets is same as the default mappings set by the plugin.
19+
20+
```lua
21+
local opt = { expr = true, remap = true }
22+
23+
-- Toggle using count
24+
vim.keymap.set('n', 'gcc', "v:count == 0 ? '<Plug>(comment_toggle_current_linewise)' : '<Plug>(comment_toggle_linewise_count)'", opt)
25+
vim.keymap.set('n', 'gbc', "v:count == 0 ? '<Plug>(comment_toggle_current_blockwise)' : '<Plug>(comment_toggle_blockwise_count)'", opt)
26+
27+
-- Toggle in Op-pending mode
28+
vim.keymap.set('n', 'gc', '<Plug>(comment_toggle_linewise)')
29+
vim.keymap.set('n', 'gb', '<Plug>(comment_toggle_blockwise)')
30+
31+
-- Toggle in VISUAL mode
32+
vim.keymap.set('x', 'gc', '<Plug>(comment_toggle_linewise_visual)')
33+
vim.keymap.set('x', 'gb', '<Plug>(comment_toggle_blockwise_visual)')
34+
```

dump.lua

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
-- [x] Disable `extra` mapping by default
2121
-- [x] Provide more arguments to pre and post hooks
2222
-- [x] `ignore` as a function
23+
-- [x] Return the operator's starting and ending position in pre and post hook
24+
-- [x] Restore cursor position in some motion operator (try `gcip`)
2325
-- [ ] Doc comment ie. /** */ (for js)
2426
-- [ ] Header comment
25-
-- [ ] Dot support for `[count]gcc`
2627

2728
-- FIXME
2829
-- [x] visual mode not working correctly
@@ -37,20 +38,19 @@
3738
-- [x] Optimize blockwise mode (just modifiy the start and end line)
3839
-- [x] Weird commenting when the first line is empty and the whole is indented
3940
-- [x] no padding support in block-x
40-
-- [ ] Dot repeat support for visual mode mappings
41-
-- [ ] Weird comments, if you do comments on already commented lines incl. an extra empty line
4241

4342
-- THINK:
44-
-- [x] Should i return the operator's starting and ending position in pre-hook
45-
-- [x] Restore cursor position in some motion operator (try `gcip`)
46-
-- 3. It is possible that, commentstring is updated inside pre-hook as we want to use it but we can't
47-
-- bcz the filetype is also present in the lang-table (and it has high priority than vim.bo.commentstring)
48-
-- 4. When there is an uncommented empty line b/w two commented blocks. It should uncomment instead of commenting again in toggle.
49-
-- 5. Conflict when uncommenting interchangebly with line/block wise comment
50-
-- 6. `ignore` is missing in blockwise and blockwise_x but on the other hand this doesn't make much sense
51-
-- 7. Parse `comments` if block comment is missing in the plugin
43+
-- [ ] Dot support for `[count]gcc` and `[count]gbc`
44+
-- [ ] Parse `comments` if block comment is missing in the plugin
45+
46+
-- ITS_OK:
47+
-- 1. Weird comments, if you do comments on already commented lines incl. an extra empty line
48+
-- 2. Conflict when uncommenting interchangebly with line/block wise comment
49+
-- 3. `ignore` doesn't work in blockwise and blockwise_x
50+
-- 4. When a empty line is b/w two commented blocks then it should uncomment instead of commenting again in toggle.
5251

5352
-- DROPPED:
54-
-- Insert mode mapping (also move the cursor after commentstring)
55-
-- Use `nvim_buf_get_text` instead of `nvim_buf_get_lines`. Blocked by https://github.com/neovim/neovim/pull/15181
56-
-- Use `nvim_buf_set_text` instead of `nvim_buf_set_lines`
53+
-- 1. Insert mode mapping (also move the cursor after commentstring)
54+
-- 2. Use `nvim_buf_get_text` instead of `nvim_buf_get_lines`. Blocked by https://github.com/neovim/neovim/pull/15181
55+
-- 3. Use `nvim_buf_set_text` instead of `nvim_buf_set_lines`
56+
-- 4. Dot repeat support for visual mode mappings (Doesn't make sense)

lua/Comment/api.lua

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -234,74 +234,53 @@ function C.setup(config)
234234
local cfg = Config:set(config):get()
235235

236236
if cfg.mappings then
237-
local map = A.nvim_set_keymap
238-
local map_opt = { noremap = true, silent = true }
237+
local K = vim.keymap.set
239238

240239
-- Basic Mappings
241240
if cfg.mappings.basic then
242-
local expr = { noremap = true, silent = true, expr = true }
243241
-- NORMAL mode mappings
244-
map(
242+
K(
245243
'n',
246244
cfg.toggler.line,
247-
[[v:count == 0 ? '<CMD>lua require("Comment.api").call("toggle_current_linewise_op")<CR>g@$' : '<CMD>lua require("Comment.api").call("toggle_linewise_count_op")<CR>g@$']],
248-
expr
245+
"v:count == 0 ? '<Plug>(comment_toggle_current_linewise)' : '<Plug>(comment_toggle_linewise_count)'",
246+
{ expr = true, remap = true }
249247
)
250-
map(
248+
K(
251249
'n',
252250
cfg.toggler.block,
253-
[[v:count == 0 ? '<CMD>lua require("Comment.api").call("toggle_current_blockwise_op")<CR>g@$' : '<CMD>lua require("Comment.api").call("toggle_blockwise_count_op")<CR>g@$']],
254-
expr
251+
"v:count == 0 ? '<Plug>(comment_toggle_current_blockwise)' : '<Plug>(comment_toggle_blockwise_count)'",
252+
{ expr = true, remap = true }
255253
)
256-
map('n', cfg.opleader.line, '<CMD>lua require("Comment.api").call("toggle_linewise_op")<CR>g@', map_opt)
257-
map('n', cfg.opleader.block, '<CMD>lua require("Comment.api").call("toggle_blockwise_op")<CR>g@', map_opt)
254+
255+
K('n', cfg.opleader.line, '<Plug>(comment_toggle_linewise)')
256+
K('n', cfg.opleader.block, '<Plug>(comment_toggle_blockwise)')
258257

259258
-- VISUAL mode mappings
260-
map(
261-
'x',
262-
cfg.opleader.line,
263-
'<ESC><CMD>lua require("Comment.api").locked.toggle_linewise_op(vim.fn.visualmode())<CR>',
264-
map_opt
265-
)
266-
map(
267-
'x',
268-
cfg.opleader.block,
269-
'<ESC><CMD>lua require("Comment.api").locked.toggle_blockwise_op(vim.fn.visualmode())<CR>',
270-
map_opt
271-
)
259+
K('x', cfg.opleader.line, '<Plug>(comment_toggle_linewise_visual)')
260+
K('x', cfg.opleader.block, '<Plug>(comment_toggle_blockwise_visual)')
272261
end
273262

274263
-- Extra Mappings
275264
if cfg.mappings.extra then
276-
map('n', cfg.extra.below, '<CMD>lua require("Comment.api").locked.insert_linewise_below()<CR>', map_opt)
277-
map('n', cfg.extra.above, '<CMD>lua require("Comment.api").locked.insert_linewise_above()<CR>', map_opt)
278-
map('n', cfg.extra.eol, '<CMD>lua require("Comment.api").locked.insert_linewise_eol()<CR>', map_opt)
265+
K('n', cfg.extra.below, '<CMD>lua require("Comment.api").locked.insert_linewise_below()<CR>')
266+
K('n', cfg.extra.above, '<CMD>lua require("Comment.api").locked.insert_linewise_above()<CR>')
267+
K('n', cfg.extra.eol, '<CMD>lua require("Comment.api").locked.insert_linewise_eol()<CR>')
279268
end
280269

281270
-- Extended Mappings
282271
if cfg.mappings.extended then
283272
-- NORMAL mode extended
284-
map('n', 'g>', '<CMD>lua require("Comment.api").call("comment_linewise_op")<CR>g@', map_opt)
285-
map('n', 'g>c', '<CMD>lua require("Comment.api").call("comment_current_linewise_op")<CR>g@$', map_opt)
286-
map('n', 'g>b', '<CMD>lua require("Comment.api").call("comment_current_blockwise_op")<CR>g@$', map_opt)
273+
K('n', 'g>', '<CMD>lua require("Comment.api").call("comment_linewise_op")<CR>g@')
274+
K('n', 'g>c', '<CMD>lua require("Comment.api").call("comment_current_linewise_op")<CR>g@$')
275+
K('n', 'g>b', '<CMD>lua require("Comment.api").call("comment_current_blockwise_op")<CR>g@$')
287276

288-
map('n', 'g<', '<CMD>lua require("Comment.api").call("uncomment_linewise_op")<CR>g@', map_opt)
289-
map('n', 'g<c', '<CMD>lua require("Comment.api").call("uncomment_current_linewise_op")<CR>g@$', map_opt)
290-
map('n', 'g<b', '<CMD>lua require("Comment.api").call("uncomment_current_blockwise_op")<CR>g@$', map_opt)
277+
K('n', 'g<', '<CMD>lua require("Comment.api").call("uncomment_linewise_op")<CR>g@')
278+
K('n', 'g<c', '<CMD>lua require("Comment.api").call("uncomment_current_linewise_op")<CR>g@$')
279+
K('n', 'g<b', '<CMD>lua require("Comment.api").call("uncomment_current_blockwise_op")<CR>g@$')
291280

292281
-- VISUAL mode extended
293-
map(
294-
'x',
295-
'g>',
296-
'<ESC><CMD>lua require("Comment.api").locked.comment_linewise_op(vim.fn.visualmode())<CR>',
297-
map_opt
298-
)
299-
map(
300-
'x',
301-
'g<',
302-
'<ESC><CMD>lua require("Comment.api").locked.uncomment_linewise_op(vim.fn.visualmode())<CR>',
303-
map_opt
304-
)
282+
K('x', 'g>', '<ESC><CMD>lua require("Comment.api").locked.comment_linewise_op(vim.fn.visualmode())<CR>')
283+
K('x', 'g<', '<ESC><CMD>lua require("Comment.api").locked.uncomment_linewise_op(vim.fn.visualmode())<CR>')
305284
end
306285
end
307286

lua/Comment/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
---Could be a regex string or a function that returns a regex string.
3838
---Example: Use '^$' to ignore empty lines
3939
---@field ignore string|fun():string
40-
---@field mappings Mappings
40+
---@field mappings boolean|Mappings
4141
---@field toggler Toggler
4242
---@field opleader Opleader
4343
---@field extra ExtraMapping

0 commit comments

Comments
 (0)