Skip to content

Commit 939dacc

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 939dacc

File tree

5 files changed

+37
-24
lines changed

5 files changed

+37
-24
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ 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+
---If filetype of the buffer is part of the following list
92+
---then only block comments will be used, even for toggler.line keymap
93+
only_block_ft = {},
9194
---Lines to be ignored while (un)comment
9295
ignore = nil,
9396
---LHS of toggle mappings in NORMAL mode

doc/Comment.txt

+23-21
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+
{only_block_ft} (string[]) Always use block comments
136+
(default: '{}')
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

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
---same position. Only works in NORMAL
2929
---mode mappings (default: 'true')
3030
---@field sticky boolean
31+
---Use only block comments if the
32+
---filetype is in the following
33+
---array of filetypes (default: {})
34+
---@field only_block_ft string[]
3135
---Lua pattern used to ignore lines
3236
---during (un)comment (default: 'nil')
3337
---@field ignore string|fun():string
@@ -83,6 +87,7 @@ local Config = {
8387
config = {
8488
padding = true,
8589
sticky = true,
90+
block_comments_ft = {},
8691
mappings = {
8792
basic = true,
8893
extra = true,

lua/Comment/extra.lua

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ 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 block_ft = vim.tbl_contains(cfg.only_block_ft, vim.bo.ft)
2526

2627
---@type CommentCtx
2728
local ctx = {
2829
cmode = U.cmode.comment,
2930
cmotion = U.cmotion.line,
30-
ctype = ctype,
31+
ctype = block_ft and U.ctype.blockwise or ctype,
3132
range = { srow = row, scol = col, erow = row, ecol = col },
3233
}
3334

@@ -65,12 +66,13 @@ end
6566
---@param cfg CommentConfig
6667
function extra.insert_eol(ctype, cfg)
6768
local srow, scol = unpack(A.nvim_win_get_cursor(0))
69+
local block_ft = vim.tbl_contains(cfg.only_block_ft, vim.bo.ft)
6870

6971
---@type CommentCtx
7072
local ctx = {
7173
cmode = U.cmode.comment,
7274
cmotion = U.cmotion.line,
73-
ctype = ctype,
75+
ctype = block_ft and U.ctype.blockwise or ctype,
7476
range = { srow = srow, scol = scol, erow = srow, ecol = scol },
7577
}
7678
local lcs, rcs = U.parse_cstr(cfg, ctx)

lua/Comment/opfunc.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ 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 block_ft = vim.tbl_contains(cfg.only_block_ft, vim.bo.ft)
36+
local is_blockx = block_ft or (is_partial and range.srow == range.erow)
3637

3738
local lines = U.get_lines(range)
3839

0 commit comments

Comments
 (0)