kylechui / nvim-surround

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

Feat: make add operations work for list-like aliases. #190

Closed XXiaoA closed 1 year ago

XXiaoA commented 1 year ago

Checklist

Is your feature request related to a problem? Please describe. For example, now we has a aliases of quotes which is a list-like table. We can use it for changing and deleting but adding.

Describe the solution you'd like Press yswq to add first quote when we not in quotes, or add other quote if we're in the quotes.

Additional context Now we have is_inside to make do it easy, probably. https://github.com/kylechui/nvim-surround/blob/6aafeeda19a98768d1c17ff6dde5548bc77a1a2d/lua/nvim-surround/buffer.lua#L185 And it's feature is optional (in the configuration) for user.

kylechui commented 1 year ago

Is there an actual reason that you would like this to be added? As of right now it seems like it would require a lot of extra code, while bringing very little benefit.

XXiaoA commented 1 year ago

I'm bored of check wich type of quotes I should add in my daily life. Or maybe I should add a surrounds q :thinking:

XXiaoA commented 1 year ago

tried add a surround, and it's hard to do the best. maybe it does need pretty much code, but gain small benefit. Gave up.

surrounds = {
        ["q"] = {
            add = function()
                local quotes = { '"', "'", "`" }
                local selection_list = {}
                local not_inside = true
                for _, quote in ipairs(quotes) do
                    local cur_selection = config.get_selection({ motion = "a" .. quote })
                    if not cur_selection then
                        goto continue
                    end
                    cur_selection = {
                        left = {
                            first_pos = cur_selection.first_pos,
                            last_pos = cur_selection.first_pos
                        },
                        right = {
                            first_pos = cur_selection.last_pos,
                            last_pos = cur_selection.last_pos
                        },
                    }
                    local cursor = buffer.get_curpos()
                    if buffer.is_inside(cursor, cur_selection) then
                        table.insert(selection_list, cur_selection)
                        not_inside = false
                    end
                    ::continue::
                end
                local best_selection
                if not_inside then
                    return { {quotes[1]}, {quotes[1]} }
                else
                    local best_selection = utils.filter_selections_list(selection_list)
                    local best_text = buffer.get_text(best_selection.left)
                    local next_quote = vim.tbl_filter(function(value)
                        if value ~= best_text[1] then
                            return value
                        end
                    end, quotes)[1]
                    return { { next_quote }, { next_quote } }
                end
            end,
        },
    },