Skip to content

Add option always_use_block #478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ 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,
---If filetype of the buffer is part of the following list
---then only block comments will be used, even for toggler.line keymap
only_block_ft = {},
---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')
{only_block_ft} (string[]) Always use block comments
(default: '{}')
{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
5 changes: 5 additions & 0 deletions lua/Comment/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
---same position. Only works in NORMAL
---mode mappings (default: 'true')
---@field sticky boolean
---Use only block comments if the
---filetype is in the following
---array of filetypes (default: {})
---@field only_block_ft string[]
---Lua pattern used to ignore lines
---during (un)comment (default: 'nil')
---@field ignore string|fun():string
Expand Down Expand Up @@ -83,6 +87,7 @@ local Config = {
config = {
padding = true,
sticky = true,
block_comments_ft = {},
mappings = {
basic = true,
extra = true,
Expand Down
6 changes: 4 additions & 2 deletions lua/Comment/extra.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ end
---@param cfg CommentConfig
local function ins_on_line(lnum, ctype, cfg)
local row, col = unpack(A.nvim_win_get_cursor(0))
local block_ft = vim.tbl_contains(cfg.only_block_ft, vim.bo.ft)

---@type CommentCtx
local ctx = {
cmode = U.cmode.comment,
cmotion = U.cmotion.line,
ctype = ctype,
ctype = block_ft and U.ctype.blockwise or ctype,
range = { srow = row, scol = col, erow = row, ecol = col },
}

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

---@type CommentCtx
local ctx = {
cmode = U.cmode.comment,
cmotion = U.cmotion.line,
ctype = ctype,
ctype = block_ft and U.ctype.blockwise or ctype,
range = { srow = srow, scol = scol, erow = srow, ecol = scol },
}
local lcs, rcs = U.parse_cstr(cfg, ctx)
Expand Down
3 changes: 2 additions & 1 deletion lua/Comment/opfunc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ 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 block_ft = vim.tbl_contains(cfg.only_block_ft, vim.bo.ft)
local is_blockx = block_ft or (is_partial and range.srow == range.erow)

local lines = U.get_lines(range)

Expand Down