b3nj5m1n/kommentary

Feature request: yank and comment

Closed this issue · 11 comments

Hi There.

Gr8 plugin, loving it.
Just one ask; can you add a new mapping to yank the commented range b4 actually commenting it out?
Right now, Im yanking by default like this:

diff --git a/lua/kommentary/kommentary.lua b/lua/kommentary/kommentary.lua
index 638ec2c..5e9c423 100644
--- a/lua/kommentary/kommentary.lua
+++ b/lua/kommentary/kommentary.lua
@@ -439,6 +439,7 @@ function M.toggle_comment_range(line_number_start, line_number_end, mode)
     if line_number_end < line_number_start then
         line_number_start, line_number_end = line_number_end, line_number_start
     end
+    vim.api.nvim_command(tostring(line_number_start) .. ',' .. tostring(line_number_end) .. 'y')
     local configuration = config.get_config(0)
     local modes = config.get_modes()
     mode = config.get_mode(line_number_start, line_number_end, mode)

Would be nice to have a new gcy mapping which yanks.

I don't think I'll implement this as a default mapping, sorry. You're just going to have to define a custom one.

How would you go about adding this?

Got something working:

local kconfig = require('kommentary.config')                                                                                          
                                                                                                                                      
function yank_and_comment(...)                                                                                                        
        local args = {...}                                                                                                            
        local configuration = config.get_config(0)                                                                                    
        local start_line = args[1]                                                                                                    
        local end_line = args[2]                                                                                                      
        vim.api.nvim_command(tostring(start_line) .. ',' .. tostring(end_line) .. 'y')                                                
        require('kommentary.kommentary').toggle_comment_range(...)                                                                    
end                                                                                                                                   
                                                                                                                                      
-- kconfig.add_keymap("n", "kommentary_yank_and_comment", kconfig.context.line, {}, "yank_and_comment")                               
kconfig.add_keymap("v", "kommentary_yank_and_comment_visual", kconfig.context.visual, {}, "yank_and_comment")                         
kconfig.add_keymap("n", "kommentary_yank_and_comment_motion", kconfig.context.init, {expr = true}, "yank_and_comment")                
-- Set up a regular keymapping to the new <Plug> mapping                                                                              
-- vim.api.nvim_set_keymap('n', 'gcyy', '<Plug>kommentary_yank_and_comment', { silent = true })                                       
vim.api.nvim_set_keymap('n', 'gcy', '<Plug>kommentary_yank_and_comment_motion', { silent = true })                                    
vim.api.nvim_set_keymap('v', 'gcy', '<Plug>kommentary_yank_and_comment_visual', { silent = true })                                    

However, could not get gcyy to work. Any advice?

Hi, sorry for the late reply, I tried out your code and for me it seems to work fine? Are you getting any error messages?

Peek 2021-05-01 08-30

The gif seems to lag for some reason, what I did was select the 3 lines, hit gcy, it commented those 3 lines out and I was able to paste them in their original form, I undid that, then hit gcyy on the middle line, that commented out the middle line and allowed me to paste it in its original form below.

Indeed it works, but for some reason I could not get gcyy to work on my end without a 2 seconds delay.
Probably something with all my other mapings.
Any idea?

@tzachar It's probably because another mapping also starts with gcy or something along those lines, so nvim waits to see which one you pick? It's hard to tell without seeing your config, but I think that's the most likely cause.

I dont know.
this is the output of map gcy:

n  gc            <Plug>kommentary_motion_default                                                                                                                      
v  gc            <Plug>kommentary_visual_default                                                                                                                      
v  gcy           <Plug>kommentary_yank_and_comment_visual                                                                                                             
n  gcy           <Plug>kommentary_yank_and_comment_motion                                                                                                             
n  gcyy          <Plug>kommentary_yank_and_comment                  

Doesn't make sense.
It also does not work when I map it to a different sequence, say gx or something.

Hm, sorry but atm I'm not sure what's causing that.

I don't think its something local to my setup. I tried with a minimal config with only kommentary and packer, and got the same results.
The only difference is the kconfig.context.line argument. Maybe it has something to do with that?

It's probably because another mapping also starts with gcy or something along those lines

It is along those lines!

The problem is not actually with the key mapping, but with the plug mapping. Try this, worked for me, should work for you:

kconfig.add_keymap("n", "kommentary_yank_and_comment_line", kconfig.context.line, {}, "yank_and_comment")                                                                       
vim.api.nvim_set_keymap('n', 'gcyy', '<Plug>kommentary_yank_and_comment_line', { silent = true })

10x !
So the problem is with the internal mapping....
Works now :)
Nice catch 👍