Skip to content

Commit 2d068c0

Browse files
committed
feat: help docs :h comment-nvim
1 parent 0932d0c commit 2d068c0

File tree

9 files changed

+313
-185
lines changed

9 files changed

+313
-185
lines changed

doc/plugs.md

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1 @@
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)` - Toggles line comment via Operator pending mode
6-
- `<Plug>(comment_toggle_blockwise)` - Toggles block comment via Operator pending mode
7-
- `<Plug>(comment_toggle_linewise_current)` - Toggles line comment on the current line
8-
- `<Plug>(comment_toggle_blockwise_current)` - Toggles block comment on the current line
9-
- `<Plug>(comment_toggle_linewise_count)` - Toggles line comment with count
10-
- `<Plug>(comment_toggle_blockwise_count)` - Toggles block comment with count
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, replace_keycodes = false }
22-
23-
-- Toggle using count
24-
vim.keymap.set('n', 'gcc', "v:count == 0 ? '<Plug>(comment_toggle_linewise_current)' : '<Plug>(comment_toggle_linewise_count)'", opt)
25-
vim.keymap.set('n', 'gbc', "v:count == 0 ? '<Plug>(comment_toggle_blockwise_current)' : '<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-
```
1+
`Comment.nvim` now has `:help` docs 🎉. Check `:h comment.plugmap` for the `<Plug>` mappings documentation and usage.

dump.lua

Lines changed: 0 additions & 56 deletions
This file was deleted.

lua/Comment/api.lua

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -303,56 +303,60 @@ end
303303
---
304304
---Following are the API functions that are available:
305305
---
306-
---require('Comment.api').toggle.linewise({motion}, {cfg?})
307-
---require('Comment.api').toggle.linewise.current({motion?}, {cfg?})
308-
---require('Comment.api').toggle.linewise.count({count}, {cfg?})
306+
--- require('Comment.api').toggle.linewise({motion}, {cfg?})
307+
--- require('Comment.api').toggle.linewise.current({motion?}, {cfg?})
308+
--- require('Comment.api').toggle.linewise.count({count}, {cfg?})
309309
---
310-
---require('Comment.api').toggle.blockwise({motion}, {cfg?})
311-
---require('Comment.api').toggle.blockwise.current({motion?}, {cfg?})
312-
---require('Comment.api').toggle.blockwise.count({count}, {cfg?})
310+
--- require('Comment.api').toggle.blockwise({motion}, {cfg?})
311+
--- require('Comment.api').toggle.blockwise.current({motion?}, {cfg?})
312+
--- require('Comment.api').toggle.blockwise.count({count}, {cfg?})
313313
---@type table A metatable containing API functions
314+
---@see comment.config
314315
api.toggle = setmetatable({ cmode = U.cmode.toggle }, core)
315316

316317
---API to (only) comment using line or block comment string
317318
---
318319
---Following are the API functions that are available:
319320
---
320-
---require('Comment.api').comment.linewise({motion}, {cfg?})
321-
---require('Comment.api').comment.linewise.current({motion?}, {cfg?})
322-
---require('Comment.api').comment.linewise.count({count}, {cfg?})
321+
--- require('Comment.api').comment.linewise({motion}, {cfg?})
322+
--- require('Comment.api').comment.linewise.current({motion?}, {cfg?})
323+
--- require('Comment.api').comment.linewise.count({count}, {cfg?})
323324
---
324-
---require('Comment.api').comment.blockwise({motion}, {cfg?})
325-
---require('Comment.api').comment.blockwise.current({motion?}, {cfg?})
326-
---require('Comment.api').comment.blockwise.count({count}, {cfg?})
325+
--- require('Comment.api').comment.blockwise({motion}, {cfg?})
326+
--- require('Comment.api').comment.blockwise.current({motion?}, {cfg?})
327+
--- require('Comment.api').comment.blockwise.count({count}, {cfg?})
327328
---@type table A metatable containing API functions
329+
---@see comment.config
328330
api.comment = setmetatable({ cmode = U.cmode.comment }, core)
329331

330332
---API to (only) uncomment using line or block comment string
331333
---
332334
---Following are the API functions that are available:
333335
---
334-
---require('Comment.api').uncomment.linewise({motion}, {cfg?})
335-
---require('Comment.api').uncomment.linewise.current({motion?}, {cfg?})
336-
---require('Comment.api').uncomment.linewise.count({count}, {cfg?})
336+
--- require('Comment.api').uncomment.linewise({motion}, {cfg?})
337+
--- require('Comment.api').uncomment.linewise.current({motion?}, {cfg?})
338+
--- require('Comment.api').uncomment.linewise.count({count}, {cfg?})
337339
---
338-
---require('Comment.api').uncomment.blockwise({motion}, {cfg?})
339-
---require('Comment.api').uncomment.blockwise.current({motion?}, {cfg?})
340-
---require('Comment.api').uncomment.blockwise.count({count}, {cfg?})
340+
--- require('Comment.api').uncomment.blockwise({motion}, {cfg?})
341+
--- require('Comment.api').uncomment.blockwise.current({motion?}, {cfg?})
342+
--- require('Comment.api').uncomment.blockwise.count({count}, {cfg?})
341343
---@type table A metatable containing API functions
344+
---@see comment.config
342345
api.uncomment = setmetatable({ cmode = U.cmode.comment }, core)
343346

344347
---API to insert comment on previous, next or at the end-of-line
345348
---
346349
---Following are the API functions that are available:
347350
---
348-
---require('Comment.api').insert.linewise.above({cfg?})
349-
---require('Comment.api').insert.linewise.below({cfg?})
350-
---require('Comment.api').insert.linewise.eol({cfg?})
351+
--- require('Comment.api').insert.linewise.above({cfg?})
352+
--- require('Comment.api').insert.linewise.below({cfg?})
353+
--- require('Comment.api').insert.linewise.eol({cfg?})
351354
---
352-
---require('Comment.api').insert.blockwise.above({cfg?})
353-
---require('Comment.api').insert.blockwise.below({cfg?})
354-
---require('Comment.api').insert.blockwise.eol({cfg?})
355+
--- require('Comment.api').insert.blockwise.above({cfg?})
356+
--- require('Comment.api').insert.blockwise.below({cfg?})
357+
--- require('Comment.api').insert.blockwise.eol({cfg?})
355358
---@type table A metatable containing API functions
359+
---@see comment.config
356360
api.insert = setmetatable({}, {
357361
__index = function(_, ctype)
358362
return {
@@ -369,17 +373,29 @@ api.insert = setmetatable({}, {
369373
end,
370374
})
371375

372-
---Wraps a given function with `lockmarks` to preserve marks/jumps when commenting
376+
-- TODO: After removing old API
377+
-- 1. make `api.locked` a simple function call
378+
-- 2. fix emmylua doc
379+
380+
---Wraps the given API function name with `lockmarks` to preserve marks/jumps
373381
---@type fun(cb:string):fun(motion:OpMotion)
374-
---@usage `require('Comment.api').locked('toggle.linewise.current')()`
382+
---@see lockmarks
383+
---@usage [[
384+
---local api = require('Comment.api')
385+
---vim.keymap.set(
386+
--- 'n', '<leader>c', api.locked('toggle.linewise.current')
387+
---)
388+
---vim.keymap.set(
389+
--- 'n', '<leader>o', api.locked('insert.linewise.below')
390+
---)
391+
---@usage ]]
375392
api.locked = setmetatable({}, {
376393
__index = function(this, cb)
377394
D(string.format('locker.%s(args...)', cb), string.format('locked(%q)(args...)', cb))
378395
return this(cb)
379396
end,
380-
-- TODO: After removal of the old api functions, make `api.locked` a simple function call
381397
__call = function(_, cb)
382-
---Actual function which will be attached to operatorfunc
398+
---operatorfunc callback
383399
---@param motion OpMotion
384400
return function(motion)
385401
return A.nvim_command(
@@ -390,13 +406,23 @@ api.locked = setmetatable({}, {
390406
})
391407

392408
---Callback function which does the following
393-
--- 1. Sets operatorfunc for dot-repeat
409+
--- 1. Sets 'operatorfunc' for dot-repeat
394410
--- 2. Preserves jumps and marks
395411
--- 3. Stores last cursor position
396412
---@param cb string Name of the API function to call
397413
---@param op 'g@'|'g@$' Operator string to execute
398414
---@return fun():string _ Keymap RHS callback
399-
---@usage `vim.keymap.set('n', 'gc', api.call('toggle.linewise', 'g@'), { expr = true })`
415+
---@usage [[
416+
---local api = require('Comment.api')
417+
---vim.keymap.set(
418+
--- 'n', 'gc', api.call('toggle.linewise', 'g@'),
419+
--- { expr = true }
420+
---)
421+
---vim.keymap.set(
422+
--- 'n', 'gcc', api.call('toggle.linewise.current', 'g@$'),
423+
--- { expr = true }
424+
---)
425+
---@usage ]]
400426
function api.call(cb, op)
401427
return function()
402428
A.nvim_set_option('operatorfunc', ("v:lua.require'Comment.api'.locked'%s'"):format(cb))

lua/Comment/config.lua

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,51 @@
11
---@mod comment.config Configuration
22

3-
---@class Toggler LHS of toggle mappings in NORMAL + VISUAL mode
4-
---@field line string Linewise comment keymap
5-
---@field block string Blockwise comment keymap
6-
7-
---@class Opleader LHS of operator-mode mappings in NORMAL + VISUAL mode
8-
---@field line string Linewise comment keymap
9-
---@field block string Blockwise comment keymap
10-
11-
---@class ExtraMapping LHS of extra mappings
12-
---@field above string Mapping to add comment on the line above
13-
---@field below string Mapping to add comment on the line below
14-
---@field eol string Mapping to add comment at the end of line
15-
16-
---@class Mappings Create default mappings
17-
---Enable operator-pending mapping
18-
---Includes `gcc`, `gbc`, `gc[count]{motion}` and `gb[count]{motion}`
19-
---NOTE: These mappings can be changed individually by `opleader` and `toggler` config
20-
---@field basic boolean
21-
---Enable extra mapping
22-
---Includes `gco`, `gcO`, `gcA`
23-
---@field extra boolean
24-
---Enable extended mapping
25-
---Includes `g>`, `g<`, `g>[count]{motion}` and `g<[count]{motion}`
26-
---@field extended boolean
27-
283
---@class CommentConfig Plugin's configuration
29-
---@field padding boolean Add a space b/w comment and the line
30-
---Whether the cursor should stay at its position
31-
---NOTE: This only affects NORMAL mode mappings and doesn't work with dot-repeat
4+
---Controls space between the comment
5+
---and the line (default: 'true')
6+
---@field padding boolean
7+
---Whether cursor should stay at the
8+
---same position. Only works with NORMAL
9+
---mode mappings (default: 'true')
3210
---@field sticky boolean
33-
---Lines to be ignored while comment/uncomment.
34-
---Could be a regex string or a function that returns a regex string.
35-
---Example: Use '^$' to ignore empty lines
11+
---Lua pattern used to ignore lines
12+
---during (un)comment (default: 'nil')
3613
---@field ignore string|fun():string
37-
---@field mappings boolean|Mappings
14+
---@field mappings Mappings|boolean
3815
---@field toggler Toggler
3916
---@field opleader Opleader
4017
---@field extra ExtraMapping
41-
---@field pre_hook fun(ctx:CommentCtx):string Function to be called before comment/uncomment
42-
---@field post_hook fun(ctx:CommentCtx) Function to be called after comment/uncomment
18+
---Function to call before (un)comment
19+
---(default: 'nil')
20+
---@field pre_hook fun(ctx):string
21+
---Function to call after (un)comment
22+
---(default: 'nil')
23+
---@field post_hook fun(ctx)
24+
25+
---@class Mappings Create default mappings
26+
---Enables operator-pending mapping; `gcc`, `gbc`,
27+
---`gc{motion}` and `gb{motion}` (default: 'true')
28+
---@field basic boolean
29+
---Enable extra mapping; `gco`, `gcO` and `gcA`
30+
---(default: 'true')
31+
---@field extra boolean
32+
---Enable extended mapping; `g>`, `g<c`, 'g<b',
33+
---'g<', 'g<c', 'g<b', `g>{motion}` and `g<{motion}`
34+
---(default: 'false')
35+
---@field extended boolean
36+
37+
---@class Toggler LHS of toggle mappings in NORMAL
38+
---@field line string Linewise comment (default: 'gcc')
39+
---@field block string Blockwise comment (default: 'gbc')
40+
41+
---@class Opleader LHS of operator-mode mappings in NORMAL and VISUAL mode
42+
---@field line string Linewise comment (default: 'gc')
43+
---@field block string Blockwise comment (default: 'gb')
44+
45+
---@class ExtraMapping LHS of extra mappings
46+
---@field below string Inserts comment below (default: 'gco')
47+
---@field above string Inserts comment above (default: 'gcO')
48+
---@field eol string Inserts comment at the end of line (default: 'gcA')
4349

4450
---@private
4551
---@class RootConfig
@@ -72,9 +78,12 @@ local Config = {
7278
},
7379
}
7480

75-
---Update the config
81+
---@private
82+
---Updates the default config
7683
---@param cfg? CommentConfig
7784
---@return RootConfig
85+
---@see comment.usage.setup
86+
---@usage `require('Comment.config'):set({config})`
7887
function Config:set(cfg)
7988
if cfg then
8089
self.config = vim.tbl_deep_extend('force', self.config, cfg)
@@ -84,11 +93,12 @@ end
8493

8594
---Get the config
8695
---@return CommentConfig
96+
---@usage `require('Comment.config'):get()`
8797
function Config:get()
8898
return self.config
8999
end
90100

91-
---@export ft
101+
---@export Config
92102
return setmetatable(Config, {
93103
__index = function(this, k)
94104
return this.state[k]

0 commit comments

Comments
 (0)