Skip to content

Commit d75d4f4

Browse files
committed
feat: add option always_use_block
Add an option so every action uses block comments instead of line. Useful for respecting coding rules enforcing to always use block comments.
1 parent e30b7f2 commit d75d4f4

File tree

5 files changed

+43
-24
lines changed

5 files changed

+43
-24
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ Following are the **default** config for the [`setup()`](#setup). If you want to
8888
padding = true,
8989
---Whether the cursor should stay at its position
9090
sticky = true,
91+
---Whether the cursor should stay at its position
92+
always_use_block = false,
9193
---Lines to be ignored while (un)comment
9294
ignore = nil,
9395
---LHS of toggle mappings in NORMAL mode

doc/Comment.txt

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -127,27 +127,29 @@ CommentConfig *comment.config.CommentConfig*
127127
Plugin's configuration
128128

129129
Fields: ~
130-
{padding} (boolean|fun():boolean) Controls space between the comment
131-
and the line (default: 'true')
132-
{sticky} (boolean) Whether cursor should stay at the
133-
same position. Only works in NORMAL
134-
mode mappings (default: 'true')
135-
{ignore} (string|fun():string) Lua pattern used to ignore lines
136-
during (un)comment (default: 'nil')
137-
{mappings} (Mappings|false) Enables |comment.keybindings|
138-
NOTE: If given 'false', then the
139-
plugin won't create any mappings
140-
{toggler} (Toggler) See |comment.config.Toggler|
141-
{opleader} (Opleader) See |comment.config.Opleader|
142-
{extra} (ExtraMapping) See |comment.config.ExtraMapping|
143-
{pre_hook} (fun(c:CommentCtx):string) Function to call before (un)comment.
144-
It is called with a {ctx} argument
145-
of type |comment.utils.CommentCtx|
146-
(default: 'nil')
147-
{post_hook} (fun(c:CommentCtx)) Function to call after (un)comment.
148-
It is called with a {ctx} argument
149-
of type |comment.utils.CommentCtx|
150-
(default: 'nil')
130+
{padding} (boolean|fun():boolean) Controls space between the comment
131+
and the line (default: 'true')
132+
{sticky} (boolean) Whether cursor should stay at the
133+
same position. Only works in NORMAL
134+
mode mappings (default: 'true')
135+
{always_use_block} (boolean) Always use block comments
136+
(default: 'true')
137+
{ignore} (string|fun():string) Lua pattern used to ignore lines
138+
during (un)comment (default: 'nil')
139+
{mappings} (Mappings|false) Enables |comment.keybindings|
140+
NOTE: If given 'false', then the
141+
plugin won't create any mappings
142+
{toggler} (Toggler) See |comment.config.Toggler|
143+
{opleader} (Opleader) See |comment.config.Opleader|
144+
{extra} (ExtraMapping) See |comment.config.ExtraMapping|
145+
{pre_hook} (fun(c:CommentCtx):string) Function to call before (un)comment.
146+
It is called with a {ctx} argument
147+
of type |comment.utils.CommentCtx|
148+
(default: 'nil')
149+
{post_hook} (fun(c:CommentCtx)) Function to call after (un)comment.
150+
It is called with a {ctx} argument
151+
of type |comment.utils.CommentCtx|
152+
(default: 'nil')
151153

152154

153155
Mappings *comment.config.Mappings*

lua/Comment/config.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
---same position. Only works in NORMAL
2929
---mode mappings (default: 'true')
3030
---@field sticky boolean
31+
---Use block comments for everything
32+
---@field always_use_block boolean
3133
---Lua pattern used to ignore lines
3234
---during (un)comment (default: 'nil')
3335
---@field ignore string|fun():string
@@ -83,6 +85,7 @@ local Config = {
8385
config = {
8486
padding = true,
8587
sticky = true,
88+
always_use_block = false,
8689
mappings = {
8790
basic = true,
8891
extra = true,

lua/Comment/extra.lua

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@ end
2222
---@param cfg CommentConfig
2323
local function ins_on_line(lnum, ctype, cfg)
2424
local row, col = unpack(A.nvim_win_get_cursor(0))
25+
local cmotion = U.cmotion.line
26+
27+
if cfg.always_use_block then
28+
ctype = U.ctype.blockwise
29+
cmotion = U.cmotion.block
30+
end
2531

2632
---@type CommentCtx
2733
local ctx = {
2834
cmode = U.cmode.comment,
29-
cmotion = U.cmotion.line,
35+
cmotion = cmotion,
3036
ctype = ctype,
3137
range = { srow = row, scol = col, erow = row, ecol = col },
3238
}
@@ -65,11 +71,17 @@ end
6571
---@param cfg CommentConfig
6672
function extra.insert_eol(ctype, cfg)
6773
local srow, scol = unpack(A.nvim_win_get_cursor(0))
74+
local cmotion = U.cmotion.line
75+
76+
if cfg.always_use_block then
77+
ctype = U.ctype.blockwise
78+
cmotion = U.cmotion.block
79+
end
6880

6981
---@type CommentCtx
7082
local ctx = {
7183
cmode = U.cmode.comment,
72-
cmotion = U.cmotion.line,
84+
cmotion = cmotion,
7385
ctype = ctype,
7486
range = { srow = srow, scol = scol, erow = srow, ecol = scol },
7587
}

lua/Comment/opfunc.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function Op.opfunc(motion, cfg, cmode, ctype)
3232
-- If we are doing char or visual motion on the same line
3333
-- then we would probably want block comment instead of line comment
3434
local is_partial = cmotion == U.cmotion.char or cmotion == U.cmotion.v
35-
local is_blockx = is_partial and range.srow == range.erow
35+
local is_blockx = cfg.always_use_block or (is_partial and range.srow == range.erow)
3636

3737
local lines = U.get_lines(range)
3838

0 commit comments

Comments
 (0)