Skip to content

Commit

Permalink
(mini.comment) FEATURE: Make textobject work in Visual mode if mappin…
Browse files Browse the repository at this point in the history
…g allows.

Details:
- Resolves #675.
  • Loading branch information
echasnovski committed Jan 30, 2024
1 parent 67adea4 commit 79b1b33
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/mini-comment.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Default values:
comment_visual = 'gc',
-- Define 'comment' textobject (like `dgc` - delete whole comment block)
-- Works also in Visual mode if mapping differs from `comment_visual`
textobject = 'gc',
},
Expand Down
7 changes: 6 additions & 1 deletion lua/mini/comment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ MiniComment.config = {
comment_visual = 'gc',

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

Expand Down Expand Up @@ -295,6 +296,9 @@ MiniComment.textobject = function()
line_end = line_end + 1
end

local is_visual = vim.tbl_contains({ 'v', 'V', '\22' }, vim.fn.mode())
if is_visual then vim.cmd('normal! \27') end

-- This visual selection doesn't seem to change `'<` and `'>` marks when
-- executed as `onoremap` mapping
vim.cmd(string.format('normal! %dGV%dG', line_start, line_end))
Expand Down Expand Up @@ -420,7 +424,8 @@ H.apply_config = function(config)
)
-- Use `<Cmd>...<CR>` to have proper dot-repeat
-- See https://github.com/neovim/neovim/issues/23406
H.map('o', config.mappings.textobject, '<Cmd>lua MiniComment.textobject()<CR>', { desc = 'Comment textobject' })
local modes = config.mappings.textobject == config.mappings.comment_visual and { 'o' } or { 'x', 'o' }
H.map(modes, config.mappings.textobject, '<Cmd>lua MiniComment.textobject()<CR>', { desc = 'Comment textobject' })
end

H.is_disabled = function() return vim.g.minicomment_disable == true or vim.b.minicomment_disable == true end
Expand Down
1 change: 1 addition & 0 deletions readmes/mini-comment.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ Here are code snippets for some common installation methods (use only one):
comment_visual = 'gc',

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

Expand Down
6 changes: 6 additions & 0 deletions tests/test_comment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,12 @@ T['Comment textobject']['works with different mapping'] = function()
set_cursor(2, 0)
type_keys('d', 'gC')
eq(get_lines(), { 'aa', 'aa' })

-- Should work in Visual mode as it differs from `comment_visual` mapping
set_lines({ 'aa', '# bb', '# cc', '# dd', 'ee' })
set_cursor(3, 0)
type_keys('v', 'gC', 'd')
eq(get_lines(), { 'aa', 'ee' })
end

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

0 comments on commit 79b1b33

Please sign in to comment.