Skip to content

Commit

Permalink
feat: add option always_use_block
Browse files Browse the repository at this point in the history
Add an option so every action uses block comments instead of line.
Useful for respecting coding rules enforcing to always use block comments.
  • Loading branch information
ilan-schemoul committed Jul 20, 2024
1 parent e30b7f2 commit d75d4f4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 24 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ Following are the **default** config for the [`setup()`](#setup). If you want to
padding = true,
---Whether the cursor should stay at its position
sticky = true,
---Whether the cursor should stay at its position
always_use_block = false,
---Lines to be ignored while (un)comment
ignore = nil,
---LHS of toggle mappings in NORMAL mode
Expand Down
44 changes: 23 additions & 21 deletions doc/Comment.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,29 @@ CommentConfig *comment.config.CommentConfig*
Plugin's configuration

Fields: ~
{padding} (boolean|fun():boolean) Controls space between the comment
and the line (default: 'true')
{sticky} (boolean) Whether cursor should stay at the
same position. Only works in NORMAL
mode mappings (default: 'true')
{ignore} (string|fun():string) Lua pattern used to ignore lines
during (un)comment (default: 'nil')
{mappings} (Mappings|false) Enables |comment.keybindings|
NOTE: If given 'false', then the
plugin won't create any mappings
{toggler} (Toggler) See |comment.config.Toggler|
{opleader} (Opleader) See |comment.config.Opleader|
{extra} (ExtraMapping) See |comment.config.ExtraMapping|
{pre_hook} (fun(c:CommentCtx):string) Function to call before (un)comment.
It is called with a {ctx} argument
of type |comment.utils.CommentCtx|
(default: 'nil')
{post_hook} (fun(c:CommentCtx)) Function to call after (un)comment.
It is called with a {ctx} argument
of type |comment.utils.CommentCtx|
(default: 'nil')
{padding} (boolean|fun():boolean) Controls space between the comment
and the line (default: 'true')
{sticky} (boolean) Whether cursor should stay at the
same position. Only works in NORMAL
mode mappings (default: 'true')
{always_use_block} (boolean) Always use block comments
(default: 'true')
{ignore} (string|fun():string) Lua pattern used to ignore lines
during (un)comment (default: 'nil')
{mappings} (Mappings|false) Enables |comment.keybindings|
NOTE: If given 'false', then the
plugin won't create any mappings
{toggler} (Toggler) See |comment.config.Toggler|
{opleader} (Opleader) See |comment.config.Opleader|
{extra} (ExtraMapping) See |comment.config.ExtraMapping|
{pre_hook} (fun(c:CommentCtx):string) Function to call before (un)comment.
It is called with a {ctx} argument
of type |comment.utils.CommentCtx|
(default: 'nil')
{post_hook} (fun(c:CommentCtx)) Function to call after (un)comment.
It is called with a {ctx} argument
of type |comment.utils.CommentCtx|
(default: 'nil')


Mappings *comment.config.Mappings*
Expand Down
3 changes: 3 additions & 0 deletions lua/Comment/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
---same position. Only works in NORMAL
---mode mappings (default: 'true')
---@field sticky boolean
---Use block comments for everything
---@field always_use_block boolean
---Lua pattern used to ignore lines
---during (un)comment (default: 'nil')
---@field ignore string|fun():string
Expand Down Expand Up @@ -83,6 +85,7 @@ local Config = {
config = {
padding = true,
sticky = true,
always_use_block = false,
mappings = {
basic = true,
extra = true,
Expand Down
16 changes: 14 additions & 2 deletions lua/Comment/extra.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ end
---@param cfg CommentConfig
local function ins_on_line(lnum, ctype, cfg)
local row, col = unpack(A.nvim_win_get_cursor(0))
local cmotion = U.cmotion.line

if cfg.always_use_block then
ctype = U.ctype.blockwise
cmotion = U.cmotion.block
end

---@type CommentCtx
local ctx = {
cmode = U.cmode.comment,
cmotion = U.cmotion.line,
cmotion = cmotion,
ctype = ctype,
range = { srow = row, scol = col, erow = row, ecol = col },
}
Expand Down Expand Up @@ -65,11 +71,17 @@ end
---@param cfg CommentConfig
function extra.insert_eol(ctype, cfg)
local srow, scol = unpack(A.nvim_win_get_cursor(0))
local cmotion = U.cmotion.line

if cfg.always_use_block then
ctype = U.ctype.blockwise
cmotion = U.cmotion.block
end

---@type CommentCtx
local ctx = {
cmode = U.cmode.comment,
cmotion = U.cmotion.line,
cmotion = cmotion,
ctype = ctype,
range = { srow = srow, scol = scol, erow = srow, ecol = scol },
}
Expand Down
2 changes: 1 addition & 1 deletion lua/Comment/opfunc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function Op.opfunc(motion, cfg, cmode, ctype)
-- If we are doing char or visual motion on the same line
-- then we would probably want block comment instead of line comment
local is_partial = cmotion == U.cmotion.char or cmotion == U.cmotion.v
local is_blockx = is_partial and range.srow == range.erow
local is_blockx = cfg.always_use_block or (is_partial and range.srow == range.erow)

local lines = U.get_lines(range)

Expand Down

0 comments on commit d75d4f4

Please sign in to comment.