nvim-surround icon indicating copy to clipboard operation
nvim-surround copied to clipboard

Ability to delete a surround similar to how we can add a surround(re: textobjects)

Open pejrich opened this issue 1 year ago • 4 comments

Checklist

  • [X] Have you read through :h nvim-surround to see if there might be any relevant information there?

Is your feature request related to a problem? Please describe. If I have a buffer like this:

("here_are_|some_words")

I can do ysa"_ to give me:

(_"here_are_|some_words"_)

if I want to delete a surround ds_, I get:

(_"here_are|somewords"_)

What would be awesome is if I could delete similar to how I can add. So something like dsa"_([D]elete [S]urround [A]round ["]quote [_]undescore`

("here_are_|some_words")

I understand that these keymaps are a bit unvim-like, and would conflict with the existing ones(i.e. dsa"_ would trigger dsa which would delete the closest surrounding a), so maybe an alternative, more vim-like approach, would be to have this only work in visual mode. So it would be more like va"D_(D being an example key for triggered surround delete in visual mode). Then it would work just like the existing delete functionality, except instead of finding the closest surround to the cursor, it finds the closest surround to the edges of the visual selection.

Additional context I did take a look at the existing code to see if I could easily implement it with existing functions, but it seemed like the current delete functionality is all cursor/single position based. I would be happy to take a stab at it, but wanted to check first if this was something you'd be interested in adding to the plugin or not, or if you had any suggestions on the best way to implement it.

Thanks! And I appreciate all your work on this plugin. It's one of my most used, if the not the most used plugin for me.

EDIT: I changed the (spaces) to _ underscore to make it more clear.

pejrich avatar Aug 05 '24 23:08 pejrich

At the moment, I don't have any plans to implement such a feature. If this is particular case is something that happens often, one could create their own surround that specifically handles this _"..."_ case. Additionally, I'm largely opposed to it because of the breaking change that it would cause (setup function incompatibility).

kylechui avatar Aug 06 '24 20:08 kylechui

@kylechui Why would there be a breaking change? Since it would be an additional, opt-in, visual mode-only binding. I'm also a bit unsure how I would go about creating a surround that would handle this. If I want to remove the spaces between a given element ( { " word " } ), say I want ({ " word " }), my proposal is that va}D would remove that space. Likewise va)D would give ({ " word " }). To my knowledge there's no way to currently do this.

But point taken if it's not something you think fits the plugin.

pejrich avatar Aug 07 '24 07:08 pejrich

On the topic of breaking changes, it could be a new Lua API only, without default keybind 🤔

bew avatar Aug 07 '24 09:08 bew

Ah I see, I accidentally missed the part where you suggested using a visual binding and not just using ds... in normal mode. I'm not totally opposed to the idea, but at the moment it doesn't seem like a basic change to the codebase either. My best guess is that the get_best_selection function (or whatever I called it) would need to be updated to take a range (i.e. a selection) as input, and all existing function calls would pass in a selection with first_pos = last_pos = curpos or something. Definitely not the easiest thing to get right I imagine.

kylechui avatar Aug 07 '24 12:08 kylechui