From 79b1b3350f1cac7cef9e46a637f9adf2a14b4923 Mon Sep 17 00:00:00 2001 From: Evgeni Chasnovski Date: Tue, 30 Jan 2024 14:19:24 +0200 Subject: [PATCH] (mini.comment) FEATURE: Make textobject work in Visual mode if mapping allows. Details: - Resolves #675. --- doc/mini-comment.txt | 1 + lua/mini/comment.lua | 7 ++++++- readmes/mini-comment.md | 1 + tests/test_comment.lua | 6 ++++++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/doc/mini-comment.txt b/doc/mini-comment.txt index cbe0c7ed..bfa17fb7 100644 --- a/doc/mini-comment.txt +++ b/doc/mini-comment.txt @@ -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', }, diff --git a/lua/mini/comment.lua b/lua/mini/comment.lua index 683417fe..557c9a18 100644 --- a/lua/mini/comment.lua +++ b/lua/mini/comment.lua @@ -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', }, @@ -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)) @@ -420,7 +424,8 @@ H.apply_config = function(config) ) -- Use `...` to have proper dot-repeat -- See https://github.com/neovim/neovim/issues/23406 - H.map('o', config.mappings.textobject, 'lua MiniComment.textobject()', { desc = 'Comment textobject' }) + local modes = config.mappings.textobject == config.mappings.comment_visual and { 'o' } or { 'x', 'o' } + H.map(modes, config.mappings.textobject, 'lua MiniComment.textobject()', { desc = 'Comment textobject' }) end H.is_disabled = function() return vim.g.minicomment_disable == true or vim.b.minicomment_disable == true end diff --git a/readmes/mini-comment.md b/readmes/mini-comment.md index 83c4d9e7..d9d4c563 100644 --- a/readmes/mini-comment.md +++ b/readmes/mini-comment.md @@ -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', }, diff --git a/tests/test_comment.lua b/tests/test_comment.lua index 0ef58730..39523fed 100644 --- a/tests/test_comment.lua +++ b/tests/test_comment.lua @@ -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()