kylechui / nvim-surround

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

Multi-character string delimiters #146

Closed ObserverOfTime closed 1 year ago

ObserverOfTime commented 2 years ago

Checklist

Is your feature request related to a problem? Please describe.

Several languages support strings that use multiple characters as delimiters.

Describe the solution you'd like

It'd be nice if we could change single-character delimited strings to multi-character and vice versa.

Additional context

Some languages also support prefixes but that would probably be too complicated.

kylechui commented 2 years ago

To my knowledge (if I'm understanding correctly) this should already be possible in the plugin. It's certainly not the most elegant, but here's a code sample for Lua strings:

require("nvim-surround").setup({
    surrounds = {
        ["s"] = {
            add = function()
                local count = require("nvim-surround.config").get_input("Enter a count: ")
                return { { "[" .. ("="):rep(count) .. "[" }, { "]" .. ("="):rep(count) .. "]" } }
            end,
            find = "%[(=*)%[.-%]%1%]",
            delete = "^(%[=*%[)().-(%]=*%])()$",
        },
    },
    aliases = {
        ["s"] = false,
    },
})

The other ones that you mentioned should also be possible, through a mix of using config.get_input() and good Lua pattern-matching.

ObserverOfTime commented 2 years ago

I guess this could also be implemented using counts like #147. It would have to support two counts, one for selecting and one for replacing.

For example:

(The first count could come either before or after cs, whichever you think makes more sense.)


Workarounds for $/string/$ and [=[string]=] which can't be implemented that way:

 -- add this to init.lua because it's not built-in
vim.keymap.set({'o', 'x'}, 'a/', ':<C-U>normal F/vf/<CR>', {silent = true})
kylechui commented 1 year ago

Going to close this issue in favor of #147, which seems to be a good implementation strategy for this idea.