Skip to content

Commit 79ae229

Browse files
committed
more stuff
1 parent 847f207 commit 79ae229

File tree

8 files changed

+89
-75
lines changed

8 files changed

+89
-75
lines changed

.github/workflows/ci.yaml

Lines changed: 0 additions & 1 deletion
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 23 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -68,41 +72,26 @@ Following are the **default** config for the [`setup()`](#setup). If you want to
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` (Read `comment.utils.CommentCtx`) 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` (Read `comment.utils.CommentCtx`) 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,8 +326,6 @@ vim.bo.commentstring = '//%s'
356326
vim.api.nvim_command('set commentstring=//%s')
357327
```
358328

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

363331
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.
@@ -367,16 +335,14 @@ 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

lua/Comment/api.lua

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,6 @@ end
325325
---api.toggle.blockwise.current(motion?, config?)
326326
---api.toggle.blockwise.count(count, config?)
327327
---
328-
----- NORMAL mode
329-
---
330328
----- Toggle current line (linewise) using C-/
331329
---vim.keymap.set('n', '<C-_>', api.toggle.linewise.current)
332330
---
@@ -347,19 +345,17 @@ end
347345
--- { expr = true }
348346
---)
349347
---
350-
----- VISUAL mode
351-
---
352348
---local esc = vim.api.nvim_replace_termcodes(
353349
--- '<ESC>', true, false, true
354350
---)
355351
---
356-
----- Linewise toggle (linewise)
352+
----- Toggle selection (linewise)
357353
---vim.keymap.set('x', '<leader>c', function()
358354
--- vim.api.nvim_feedkeys(esc, 'nx', false)
359355
--- api.toggle.linewise(vim.fn.visualmode())
360356
---end)
361357
---
362-
----- Blockwise toggle (blockwise)
358+
----- Toggle selection (blockwise)
363359
---vim.keymap.set('x', '<leader>b', function()
364360
--- vim.api.nvim_feedkeys(esc, 'nx', false)
365361
--- api.toggle.blockwise(vim.fn.visualmode())

lua/Comment/config.lua

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,65 @@
11
---@mod comment.config Configuration
2+
---@tag comment.config.defaults
3+
---@brief [[
4+
---Following is the default config for the |comment.usage.setup|. If you want to
5+
---override, just modify the option that you want, then it will be merged with the
6+
---default config.
7+
---
8+
--->
9+
--- {
10+
--- padding = true,
11+
--- sticky = true,
12+
--- ignore = nil,
13+
--- toggler = {
14+
--- line = 'gcc',
15+
--- block = 'gbc',
16+
--- },
17+
--- opleader = {
18+
--- line = 'gc',
19+
--- block = 'gb',
20+
--- },
21+
--- extra = {
22+
--- above = 'gcO',
23+
--- below = 'gco',
24+
--- eol = 'gcA',
25+
--- },
26+
--- mappings = {
27+
--- basic = true,
28+
--- extra = true,
29+
--- extended = false,
30+
--- },
31+
--- pre_hook = nil,
32+
--- post_hook = nil,
33+
--- }
34+
---<
35+
---@brief ]]
236

337
---@class CommentConfig Plugin's configuration
438
---Controls space between the comment
539
---and the line (default: 'true')
6-
---@field padding boolean
40+
---@field padding boolean|fun():boolean
741
---Whether cursor should stay at the
8-
---same position. Only works with NORMAL
42+
---same position. Only works in NORMAL
943
---mode mappings (default: 'true')
1044
---@field sticky boolean
1145
---Lua pattern used to ignore lines
1246
---during (un)comment (default: 'nil')
1347
---@field ignore string|fun():string
14-
---@field mappings Mappings|boolean
48+
---Enables |comment.keybindings|
49+
---NOTE: If given 'false', then the
50+
---plugin won't create any mappings
51+
---@field mappings Mappings|false
1552
---@field toggler Toggler
1653
---@field opleader Opleader
1754
---@field extra ExtraMapping
18-
---Function to call before (un)comment
55+
---Function to call before (un)comment.
56+
---It is called with a {ctx} argument
57+
---of type |comment.utils.CommentCtx|
1958
---(default: 'nil')
2059
---@field pre_hook fun(ctx):string
21-
---Function to call after (un)comment
60+
---Function to call after (un)comment.
61+
---It is called with a {ctx} argument
62+
---of type |comment.utils.CommentCtx|
2263
---(default: 'nil')
2364
---@field post_hook fun(ctx)
2465

lua/Comment/extra.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---@mod comment.extra Insert API
1+
---@mod comment.extra Extra API
22
---@brief [[
33
---Underlying functions that powers the |comment.api.insert| lua API.
44
---@brief ]]
@@ -33,7 +33,7 @@ local function ins_on_line(lnum, ctype, cfg)
3333

3434
local srow = row + lnum
3535
local lcs, rcs = U.parse_cstr(cfg, ctx)
36-
local padding = U.get_pad(cfg.padding)
36+
local padding = U.get_pad(U.is_fn(cfg.padding))
3737

3838
-- We need RHS of cstr, if we are doing block comments or if RHS exists
3939
-- because even in line comment RHS do exists for some filetypes like jsx_element, ocaml
@@ -76,7 +76,7 @@ function extra.insert_eol(ctype, cfg)
7676
local lcs, rcs = U.parse_cstr(cfg, ctx)
7777

7878
local line = A.nvim_get_current_line()
79-
local padding = U.get_pad(cfg.padding)
79+
local padding = U.get_pad(U.is_fn(cfg.padding))
8080

8181
-- We need RHS of cstr, if we are doing block comments or if RHS exists
8282
-- because even in line comment RHS do exists for some filetypes like jsx_element, ocaml

lua/Comment/init.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
---@brief [[
1919
---Comment.nvim is a smart and powerful comment plugin for neovim. It supports
2020
---dot-repeat, counts, line ('//') and block ('/* */') comments, and can be used
21-
---with motion and text-objects. It has native integration with tressitter to
21+
---with motion and text-objects. It has native integration with |tressitter| to
2222
---support embedded filetypes like html, vue, markdown with codeblocks etc.
2323
---@brief ]]
2424
---@tag comment.dotrepeat
@@ -49,6 +49,11 @@
4949
---the string in |comment.ft| instead of using 'commentstring'. To override this
5050
---behavior, you have to manually return the 'commentstring' from 'pre_hook'.
5151
---@brief ]]
52+
---@tag comment.sourcecode
53+
---@brief [[
54+
---Comment.nvim is FOSS provided under MIT license. All the source code is avaiable
55+
---at https://github.com/numToStr/Comment.nvim
56+
---@brief ]]
5257

5358
---@mod comment.usage Usage
5459
---@brief [[
@@ -65,6 +70,10 @@ local C = {}
6570
---@return CommentConfig _ Returns the mutated config
6671
---@see comment.config
6772
---@usage [[
73+
----- Use default configuration
74+
---require('Comment').setup()
75+
---
76+
----- or with custom configuration
6877
---require('Comment').setup({
6978
--- ignore = '^$',
7079
--- toggler = {

lua/Comment/utils.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ end
128128

129129
---Get lines from the current position to the given count
130130
---@param count integer Probably 'vim.v.count'
131-
---@return string[] List of lines
131+
---@return string[] _ List of lines
132132
---@return CommentRange
133133
function U.get_count_lines(count)
134134
local srow = unpack(A.nvim_win_get_cursor(0))
@@ -140,7 +140,7 @@ end
140140

141141
---Get lines from a NORMAL/VISUAL mode
142142
---@param range CommentRange
143-
---@return string[] List of lines
143+
---@return string[] _ List of lines
144144
function U.get_lines(range)
145145
-- If start and end is same, then just return the current line
146146
if range.srow == range.erow then

0 commit comments

Comments
 (0)