JoosepAlviste / nvim-ts-context-commentstring

Neovim treesitter plugin for setting the commentstring based on the cursor location in a file.
MIT License
1.16k stars 35 forks source link

[Feature Request] Integrate caw.vim #12

Open yuki-yano opened 3 years ago

yuki-yano commented 3 years ago

caw.vim is an excellent comment management plugin and I would like to see it integrated. I am new to Lua, is there anything I can do to help?

https://github.com/tyru/caw.vim

JoosepAlviste commented 3 years ago

Hey! There are 2 main ways that I can see how integrations might work:

  1. (Preferred) The commenting plugin allows configuring a "hook" function that will be called before the commenting is done. For example, b3nj5m1n/kommentary has this option available. Then, it's just a matter of configuring the commenting plugin to run lua require('ts_context_commentstring.internal').update_commentstring() before the commentstring is used.
  2. Adding custom key mappings which would first run lua require('ts_context_commentstring.internal').update_commentstring() and then trigger the commenting plugin functions. There is a useful utility function in this project update_commentstring_and_run which can be used to run <Plug> mappings after updating the commentstring.

When it comes to caw.vim, both options are viable. Since it uses <Plug>, it should be quite simple to first disable the default mappings and then define your own mappings like the vim-commentary integration in this project:

nmap <buffer><expr> gc v:lua.context_commentstring.update_commentstring_and_run('caw:prefix')
nmap <buffer><expr> gcc v:lua.context_commentstring.update_commentstring_and_run('caw:hatpos:toggle')
nmap <buffer><expr> gci v:lua.context_commentstring.update_commentstring_and_run('caw:hatpos:comment')
nmap <buffer><expr> gcui v:lua.context_commentstring.update_commentstring_and_run('caw:hatpos:uncomment')
nmap <buffer><expr> gcI v:lua.context_commentstring.update_commentstring_and_run('caw:zeropos:comment')
nmap <buffer><expr> gcuI v:lua.context_commentstring.update_commentstring_and_run('caw:zeropos:uncomment')
nmap <buffer><expr> gca v:lua.context_commentstring.update_commentstring_and_run('caw:dollarpos:comment')
nmap <buffer><expr> gcua v:lua.context_commentstring.update_commentstring_and_run('caw:dollarpos:uncomment')
nmap <buffer><expr> gcw v:lua.context_commentstring.update_commentstring_and_run('caw:wrap:comment')
nmap <buffer><expr> gcuw v:lua.context_commentstring.update_commentstring_and_run('caw:wrap:uncomment')
nmap <buffer><expr> gcb v:lua.context_commentstring.update_commentstring_and_run('caw:box:comment')
nmap <buffer><expr> gco v:lua.context_commentstring.update_commentstring_and_run('caw:jump:comment-next')
nmap <buffer><expr> gcO v:lua.context_commentstring.update_commentstring_and_run('caw:jump:comment-prev')

I just copied the mappings from the docs and I'm not sure if that definition is 100% correct. Some nmaps might need to be omaps or xmaps, etc. Also, make sure to map them only in files where treesitter is active. I would test those mappings out and if they work well, we could add them to this plugin if you'd like.

The other option would be to ask caw.vim maintainers to add a possibility to configure a hook, like kommentary did. Since I don't know almost anything about caw.vim, I'm not sure if they would be open to this idea, you could ask them if you'd like :)

Anyways, let me know how it goes with these mappings or if there are any other questions!

tyru commented 3 years ago

Hi @JoosepAlviste, I'm a caw.vim author.

I'd like to implement caw.vim integration but I don't know how to implement a hook function in caw.vim, because current caw.vim has only vim script source codes. I have to write a hook function by lua?

tyru commented 3 years ago

run lua require('ts_context_commentstring.internal').update_commentstring() before the commentstring is used.

@JoosepAlviste I didn't understand well but now I understand above function changes commentstring option. I'm working on https://github.com/tyru/caw.vim/pull/165 Above PR calls the function before firing commenting / uncommenting.

JoosepAlviste commented 3 years ago

Hey @tyru! How is the integration going on caw.vim side?