Skip to content

Commit 089b972

Browse files
authored
feat: help docs :h comment-nvim (#205)
Finally! We now have proper help docs for everything from keybindings to Lua API. It's auto generated from emmylua annotation so it would always be up to date. Credits to https://github.com/phaazon/hop.nvim for excellent docs and reference.
1 parent ae8c440 commit 089b972

File tree

15 files changed

+1445
-592
lines changed

15 files changed

+1445
-592
lines changed

.github/workflows/ci.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ on:
66
- "**.lua"
77
branches:
88
- master
9-
- docs
109

1110
env:
1211
PLUGIN_NAME: Comment

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,6 @@ luac.out
4141

4242
tmp
4343
scratch
44+
45+
# ignore generated doc tags
46+
doc/tags

README.md

+26-110
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
### ✨ Features
77

88
- Supports treesitter. [Read more](#treesitter)
9-
- Supports `commentstring`. [Read more](#commentstring)
9+
- Supports `commentstring`. Read `:h comment.commentstring`
1010
- Supports line (`//`) and block (`/* */`) comments
1111
- Dot (`.`) repeat support for `gcc`, `gbc` and friends
1212
- Count support for `[count]gcc` and `[count]gbc`
@@ -37,6 +37,10 @@ Plug 'numToStr/Comment.nvim'
3737
lua require('Comment').setup()
3838
```
3939

40+
### 📖 Getting Help
41+
42+
`Comment.nvim` provides help docs which can be accessed by running `:help comment-nvim`
43+
4044
<a id="setup"></a>
4145

4246
### ⚒️ Setup
@@ -63,46 +67,31 @@ EOF
6367

6468
#### Configuration (optional)
6569

66-
Following are the **default** config for the [`setup()`](#setup). If you want to override, just modify the option that you want then it will be merged with the default config.
70+
Following are the **default** config for the [`setup()`](#setup). If you want to override, just modify the option that you want then it will be merged with the default config. Read `:h comment.config` for more info.
6771

6872
```lua
6973
{
7074
---Add a space b/w comment and the line
71-
---@type boolean|fun():boolean
7275
padding = true,
73-
7476
---Whether the cursor should stay at its position
75-
---NOTE: This only affects NORMAL mode mappings and doesn't work with dot-repeat
76-
---@type boolean
7777
sticky = true,
78-
79-
---Lines to be ignored while comment/uncomment.
80-
---Could be a regex string or a function that returns a regex string.
81-
---Example: Use '^$' to ignore empty lines
82-
---@type string|fun():string
78+
---Lines to be ignored while (un)comment
8379
ignore = nil,
84-
8580
---LHS of toggle mappings in NORMAL mode
86-
---@type table
8781
toggler = {
8882
---Line-comment toggle keymap
8983
line = 'gcc',
9084
---Block-comment toggle keymap
9185
block = 'gbc',
9286
},
93-
94-
---LHS of operator-pending mappings in NORMAL mode
95-
---LHS of mapping in VISUAL mode
96-
---@type table
87+
---LHS of operator-pending mappings in NORMAL and VISUAL mode
9788
opleader = {
9889
---Line-comment keymap
9990
line = 'gc',
10091
---Block-comment keymap
10192
block = 'gb',
10293
},
103-
10494
---LHS of extra mappings
105-
---@type table
10695
extra = {
10796
---Add comment on the line above
10897
above = 'gcO',
@@ -111,29 +100,19 @@ Following are the **default** config for the [`setup()`](#setup). If you want to
111100
---Add comment at the end of line
112101
eol = 'gcA',
113102
},
114-
115-
---Create basic (operator-pending) and extended mappings for NORMAL + VISUAL mode
116-
---NOTE: If `mappings = false` then the plugin won't create any mappings
117-
---@type boolean|table
103+
---Enable keybindings
104+
---NOTE: If given `false` then the plugin won't create any mappings
118105
mappings = {
119-
---Operator-pending mapping
120-
---Includes `gcc`, `gbc`, `gc[count]{motion}` and `gb[count]{motion}`
121-
---NOTE: These mappings can be changed individually by `opleader` and `toggler` config
106+
---Operator-pending mapping; `gcc` `gbc` `gc[count]{motion}` `gb[count]{motion}`
122107
basic = true,
123-
---Extra mapping
124-
---Includes `gco`, `gcO`, `gcA`
108+
---Extra mapping; `gco`, `gcO`, `gcA`
125109
extra = true,
126-
---Extended mapping
127-
---Includes `g>`, `g<`, `g>[count]{motion}` and `g<[count]{motion}`
110+
---Extended mapping; `g>` `g<` `g>[count]{motion}` `g<[count]{motion}`
128111
extended = false,
129112
},
130-
131-
---Pre-hook, called before commenting the line
132-
---@type fun(ctx: CommentCtx):string
113+
---Function to call before (un)comment
133114
pre_hook = nil,
134-
135-
---Post-hook, called after commenting is done
136-
---@type fun(ctx: CommentCtx)
115+
---Function to call after (un)comment
137116
post_hook = nil,
138117
}
139118
```
@@ -224,14 +203,6 @@ These mappings are disabled by default. (config: `mappings.extended`)
224203
`gbac` - Toggle comment around a class (w/ LSP/treesitter support)
225204
```
226205

227-
<a id="api"></a>
228-
229-
### ⚙️ API
230-
231-
- [Plug Mappings](./doc/plugs.md) - Excellent for creating custom keybindings
232-
233-
- [Lua API](./doc/API.md) - Details the Lua API. Great for making custom comment function.
234-
235206
<a id="treesitter"></a>
236207

237208
### 🌳 Treesitter
@@ -254,12 +225,10 @@ There are two hook methods i.e `pre_hook` and `post_hook` which are called befor
254225

255226
<a id="pre-hook"></a>
256227

257-
- `pre_hook` - This method is called with a [`ctx`](#comment-context) argument before comment/uncomment is started. It can be used to return a custom `commentstring` which will be used for comment/uncomment the lines. You can use something like [nvim-ts-context-commentstring](https://github.com/JoosepAlviste/nvim-ts-context-commentstring) to compute the commentstring using treesitter.
228+
- `pre_hook` - Called with a `ctx` argument (Read `:h comment.utils.CommentCtx`) before (un)comment. Can optionally return a `commentstring` to be used for (un)commenting. You can use [nvim-ts-context-commentstring](https://github.com/JoosepAlviste/nvim-ts-context-commentstring) to easily comment `tsx/jsx` files.
258229

259230
```lua
260-
-- NOTE: The example below is a proper integration and it is RECOMMENDED.
261231
{
262-
---@param ctx CommentCtx
263232
pre_hook = function(ctx)
264233
-- Only calculate commentstring for tsx filetypes
265234
if vim.bo.filetype == 'typescriptreact' then
@@ -285,13 +254,14 @@ There are two hook methods i.e `pre_hook` and `post_hook` which are called befor
285254
}
286255
```
287256

257+
> **Note** - `Comment.nvim` already supports [`treesitter`](#treesitter) out-of-the-box except for `tsx/jsx`.
258+
288259
<a id="post-hook"></a>
289260

290-
- `post_hook` - This method is called after commenting is done. It receives the same [`ctx`](#comment-context) argument as [`pre_hook`](#pre_hook).
261+
- `post_hook` - This method is called after (un)commenting. It receives the same `ctx` (Read `:h comment.utils.CommentCtx`) argument as [`pre_hook`](#pre_hook).
291262

292263
```lua
293264
{
294-
---@param ctx CommentCtx
295265
post_hook = function(ctx)
296266
if ctx.range.srow == ctx.range.erow then
297267
-- do something with the current line
@@ -347,7 +317,7 @@ ignore = '^const(.*)=(%s?)%((.*)%)(%s?)=>'
347317

348318
Most languages/filetypes have native support for comments via `commentstring` but there might be a filetype that is not supported. There are two ways to enable commenting for unsupported filetypes:
349319

350-
1. You can set `commentstring` for that particular filetype like the following
320+
1. You can set `commentstring` for that particular filetype like the following. Read `:h commentstring` for more info.
351321

352322
```lua
353323
vim.bo.commentstring = '//%s'
@@ -356,27 +326,23 @@ vim.bo.commentstring = '//%s'
356326
vim.api.nvim_command('set commentstring=//%s')
357327
```
358328

359-
> Run `:h commentstring` for more help
360-
361329
<a id="ft-lua"></a>
362330

363-
2. You can also use this plugin interface to store both line and block commentstring for the filetype. You can treat this as a more powerful version of the `commentstring`
331+
2. You can also use this plugin interface to store both line and block commentstring for the filetype. You can treat this as a more powerful version of the `commentstring`. Read `:h comment.ft` for more info.
364332

365333
```lua
366334
local ft = require('Comment.ft')
367335

368336
-- 1. Using set function
369337

370-
-- Just set only line comment
371-
ft.set('yaml', '#%s')
372-
373-
-- Or set both line and block commentstring
374-
-- You can also chain the set calls
375-
ft.set('javascript', {'//%s', '/*%s*/'}).set('conf', '#%s')
338+
ft
339+
-- Set only line comment
340+
.set('yaml', '#%s')
341+
-- Or set both line and block commentstring
342+
.set('javascript', {'//%s', '/*%s*/'})
376343

377344
-- 2. Metatable magic
378345

379-
-- One filetype at a time
380346
ft.javascript = {'//%s', '/*%s*/'}
381347
ft.yaml = '#%s'
382348

@@ -391,56 +357,6 @@ ft.lang('javascript') -- { '//%s', '/*%s*/' }
391357

392358
> PR(s) are welcome to add more commentstring inside the plugin
393359
394-
<a id="commentstring"></a>
395-
396-
### 🧵 Comment String
397-
398-
Although, `Comment.nvim` supports neovim's `commentstring` but unfortunately it has the least priority. The commentstring is taken from the following place in the respective order.
399-
400-
- [`pre_hook`](#hooks) - If a string is returned from this method then it will be used for commenting.
401-
402-
- [`ft.lua`](#ft-lua) - If the current filetype is found in the table, then the string there will be used.
403-
404-
- `commentstring` - Neovim's native commentstring for the filetype
405-
406-
<a id="commentstring-caveat"></a>
407-
408-
> There is one caveat with this approach. If someone sets the `commentstring` (w/o returning a string) from the `pre_hook` method and if the current filetype also exists in the `ft_table` then the commenting will be done using the string in `ft_table` instead of using `commentstring`
409-
410-
<a id="comment-context"></a>
411-
412-
### 🧠 Comment Context
413-
414-
The following object is provided as an argument to `pre_hook` and `post_hook` functions.
415-
416-
> I am just placing it here just for documentation purpose
417-
418-
```lua
419-
---Comment context
420-
---@class CommentCtx
421-
---@field ctype CommentType
422-
---@field cmode CommentMode
423-
---@field cmotion CommentMotion
424-
---@field range CommentRange
425-
426-
---Range of the selection that needs to be commented
427-
---@class CommentRange
428-
---@field srow integer Starting row
429-
---@field scol integer Starting column
430-
---@field erow integer Ending row
431-
---@field ecol integer Ending column
432-
```
433-
434-
`CommentType`, `CommentMode` and `CommentMotion` all of them are exported from the plugin's utils for reuse
435-
436-
```lua
437-
require('Comment.utils').ctype.{linewise,blockwise}
438-
439-
require('Comment.utils').cmode.{toggle,comment,uncomment}
440-
441-
require('Comment.utils').cmotion.{line,char,v,V}
442-
```
443-
444360
### 🤝 Contributing
445361

446362
There are multiple ways to contribute reporting/fixing bugs, feature requests. You can also submit commentstring to this plugin by updating [ft.lua](./lua/Comment/ft.lua) and sending PR.

0 commit comments

Comments
 (0)