Skip to content

Commit 79b1b33

Browse files
committed
(mini.comment) FEATURE: Make textobject work in Visual mode if mapping allows.
Details: - Resolves #675.
1 parent 67adea4 commit 79b1b33

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

doc/mini-comment.txt

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Default values:
9797
comment_visual = 'gc',
9898
9999
-- Define 'comment' textobject (like `dgc` - delete whole comment block)
100+
-- Works also in Visual mode if mapping differs from `comment_visual`
100101
textobject = 'gc',
101102
},
102103

lua/mini/comment.lua

+6-1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ MiniComment.config = {
126126
comment_visual = 'gc',
127127

128128
-- Define 'comment' textobject (like `dgc` - delete whole comment block)
129+
-- Works also in Visual mode if mapping differs from `comment_visual`
129130
textobject = 'gc',
130131
},
131132

@@ -295,6 +296,9 @@ MiniComment.textobject = function()
295296
line_end = line_end + 1
296297
end
297298

299+
local is_visual = vim.tbl_contains({ 'v', 'V', '\22' }, vim.fn.mode())
300+
if is_visual then vim.cmd('normal! \27') end
301+
298302
-- This visual selection doesn't seem to change `'<` and `'>` marks when
299303
-- executed as `onoremap` mapping
300304
vim.cmd(string.format('normal! %dGV%dG', line_start, line_end))
@@ -420,7 +424,8 @@ H.apply_config = function(config)
420424
)
421425
-- Use `<Cmd>...<CR>` to have proper dot-repeat
422426
-- See https://github.com/neovim/neovim/issues/23406
423-
H.map('o', config.mappings.textobject, '<Cmd>lua MiniComment.textobject()<CR>', { desc = 'Comment textobject' })
427+
local modes = config.mappings.textobject == config.mappings.comment_visual and { 'o' } or { 'x', 'o' }
428+
H.map(modes, config.mappings.textobject, '<Cmd>lua MiniComment.textobject()<CR>', { desc = 'Comment textobject' })
424429
end
425430

426431
H.is_disabled = function() return vim.g.minicomment_disable == true or vim.b.minicomment_disable == true end

readmes/mini-comment.md

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ Here are code snippets for some common installation methods (use only one):
169169
comment_visual = 'gc',
170170

171171
-- Define 'comment' textobject (like `dgc` - delete whole comment block)
172+
-- Works also in Visual mode if mapping differs from `comment_visual`
172173
textobject = 'gc',
173174
},
174175

tests/test_comment.lua

+6
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,12 @@ T['Comment textobject']['works with different mapping'] = function()
885885
set_cursor(2, 0)
886886
type_keys('d', 'gC')
887887
eq(get_lines(), { 'aa', 'aa' })
888+
889+
-- Should work in Visual mode as it differs from `comment_visual` mapping
890+
set_lines({ 'aa', '# bb', '# cc', '# dd', 'ee' })
891+
set_cursor(3, 0)
892+
type_keys('v', 'gC', 'd')
893+
eq(get_lines(), { 'aa', 'ee' })
888894
end
889895

890896
T['Comment textobject']['respects tree-sitter injections'] = function()

0 commit comments

Comments
 (0)