kylechui / nvim-surround

Add/change/delete surrounding delimiter pairs with ease. Written with :heart: in Lua.
MIT License
3.16k stars 63 forks source link

default motions for surround #161

Closed ad-chaos closed 1 year ago

ad-chaos commented 1 year ago

Checklist

Is your feature request related to a problem? Please describe. Often times I am mostly surround words with quotes, or surround till the end of line with a function or make a parenthetical till the end of a sentence. In such situations I find myself having mappings like

nnoremap ys" ysw"
nnoremap ys' ysw'
nnoremap ys` ysw`
nnoremap ysf ys$f
" This a file-type specific mapping (for markdown and text only).
nnoremap ys) yst.)

Describe the solution you'd like Now those mappings feel kinda hacky 😅 (and I cannot cover all possible surrounds, like I haven't written down * that italicises words in markdown), would be nice if I could specify a default motion for them :)

This would also make it more consistent with cs{surround} and ds{surround}. So, you can just ys{surround} and it'll do the thing that you expect (since you have set a default motion)

kylechui commented 1 year ago

If I'm being honest, I don't think this idea is feasible. How would nvim-surround differentiate between whether the next character is part of a motion, or the actual character to surround with? For example, how would ysa"a be processed? Is the first a part of a motion, or signalling to surround with angle brackets?

I also think that since you have already found yourself a workaround for it (just manually setting your maps), it's unnecessary to be implemented in the core plugin. It might be annoying at first but you only really need to set this up once, while I would have to maintain the code necessary to implement this over time. I'm open to hearing any additional thoughts you have about this; if not, then feel free to close this issue.

ad-chaos commented 1 year ago

how would ysa"a be processed?

what does do currently? On my machine running the latest head it doesn't do anything, so I am unsure of the intended behaviour.

I am unsure of how nvim-surround handles the ys operation, does it wait for 2 characters after it and then do its thing? Or is there something else? I think depending on how the semantics are defined it could either be very simple or kinda challenging. Would you mind pointing me to the appropriate files and functions? Maybe I could help :)

kylechui commented 1 year ago

ysa"a should surround the nearest pair of double quotes with angle brackets, i.e. "hello world"<"hello world">.

Furthermore, as explained in the README, the ys operator takes in a motion, then a character representing some delimiter pair. The length of the motion is unknown and can be arbitrary, since the user may define their own motions that they wish to use. For example:

Note: All of the above are default motions that one may use; people may decide to use other plugins like targets.vim or nvim-treesitter-textobjects to create more motions like af, il, etc.

There are no restrictions on what characters may compose a motion, so I think it would be impossible to actually discern if the input characters are part of a motion, or representing a delimiter pair. Relevant files would be init.lua, in particular normal_surround and normal_callback. I would also look into :h opfunc, if you have not yet already.

ad-chaos commented 1 year ago

Hmm, it is not as simple as I thought indeed. And damn nvim-surround is quite capable, I should explore more into the docs 😅 (Thanks for the plugin!)

Yeah, you're right, this would be too much work for not a huge improvement. I'll close this for now. If I get some ideas I'll let you know 😄