Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for entering new surrounding in insert mode instead of prompt #145

Open
1 task done
tom-anders opened this issue Aug 25, 2022 · 7 comments
Open
1 task done
Labels
enhancement New feature or request

Comments

@tom-anders
Copy link

tom-anders commented Aug 25, 2022

Checklist

  • 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.

When using something like csf to change a function call, there is a prompt for entering the new function name. This means I can't take advantage of LSP autocompletion when typing the new name.

Describe the solution you'd like

Have an option to enter insert mode for typing in the new name instead of the prompt.

@tom-anders tom-anders added the enhancement New feature or request label Aug 25, 2022
@kylechui
Copy link
Owner

kylechui commented Aug 26, 2022

Hi there, adding this in would require a non-trivial refactor and I probably won't do it anytime soon (although it is quite an interesting idea). However, a workaround you could try is to hit the key combination csf<CR>i. That way it should just delete the function name (but leaving the parentheses), and then you can use LSP auto-completion to your heart's content. For your convenience, here's a keymap that you can use for the time being, although note that it's not part of the official API and could break in subsequent updates:

vim.keymap.set("n", "csf", function()
    vim.schedule(function()
        require("nvim-surround").change_surround({
            del_char = "f",
            add_delimiters = function()
                return { { "" }, { "" } }
            end,
            curpos = require("nvim-surround.buffer").get_curpos(),
        })
    end)
    return "i"
end, { silent = true, expr = true })

I'll be leaving this issue open as the above is just a workaround; please let me know if it helped!

@tom-anders
Copy link
Author

Sounds cool, thanks for the workaround!

@VioletJewel
Copy link

VioletJewel commented Dec 10, 2023

this would be a useful feature if only to insert xml-style tags (when you're not in xml/html/etc). So, something like <C-s>t in insert mode would spawn some sort of input() (like s<motion>t in normal mode) and then typing "code" would give you

<code>|</code>

where | is the cursor of course ("on" the '<' of "</code>" but in insert mode).

cough cough I'm writing documentation, and I am very tired of typing <code>bla bla bla then remembering to write </code> at the end.

@kylechui
Copy link
Owner

As of right now, using the default bindings in insert mode, <C-g>stcode<CR> does the behavior that you want. However, if you find yourself doing a lot of surrounds involving <code> tags, I would recommend making a custom surround.

@VioletJewel
Copy link

I am so sorry. I was using a different plugin, and I really thought I had come to the right repo. Thanks & sorry

@kylechui
Copy link
Owner

No worries! Maybe now you'll have some incentive to switch to nvim-surround 😉

@jdrupal-dev
Copy link

jdrupal-dev commented Nov 26, 2024

I have solved this in my config by inserting [CURSOR] and then replace it after performing the surrounding.

--- Places the cursor on the nearest [CURSOR] occurrence.
--- This is useful when marking the cursor position in custom surroundings.
M.set_cursor = function()
  local position = vim.fn.searchpos('\\[CURSOR\\]', 'n')
  if position[1] == 0 and position[2] == 0 then
    return
  end

  local line = position[1] - 1
  local col = position[2] - 1

  vim.api.nvim_buf_set_text(0, line, col, line, col + #"[CURSOR]", { "" })
  vim.api.nvim_win_set_cursor(0, { line + 1, col })
  vim.cmd('startinsert')
end
return {
  "kylechui/nvim-surround",
  event = "VeryLazy",
  config = function()
    -- Override the default 'S' keymap to allow setting the cursor position
    -- after surrounding.
    vim.keymap.set("x", "S", function()
      local curpos = require("nvim-surround.buffer").get_curpos()
      return string.format(
        ":lua require('nvim-surround').visual_surround({ line_mode = false, curpos = { %d, %d }, curswant = %d }) require('custom.utils.surround').set_cursor()<CR>",
        curpos[1],
        curpos[2],
        vim.fn.winsaveview().curswant
      )
    end, { desc = "Add a surrounding pair around a visual selection", silent = true, expr = true })

    require("nvim-surround").setup({
      keymaps = {
        visual = false,
      },
      surrounds = {
        ["s"] = {
          add = function()
            local filetype = vim.bo.filetype
            if filetype == "typescript" or filetype == "typescriptreact" or filetype == "javascriptreact" then
              filetype = "javascript"
            end

            local statement_type = require("nvim-surround.config").get_input("Enter the statement type: ")
            local map = {
              ["if"] = {
                lua = { { "if [" .. "CURSOR" .. "] then " }, { " end" } },
                rust = { { "if [" .. "CURSOR" .. "] { " }, { " }" } },
                php = { { "if ([" .. "CURSOR" .. "]) { " }, { " }" } },
                javascript = { { "if ([" .. "CURSOR" .. "]) { " }, { " }" } },
              },
              ["try"] = {
                php = { { "try { " }, { " }", "catch (\\Exception $e) {[" .. "CURSOR" .. "]}" } },
                javascript = { { "try { " }, { " }", "catch (error) {[" .. "CURSOR" .. "]}" } },
              },
            }

            if map[statement_type] and map[statement_type][filetype] then
              return map[statement_type][filetype]
            end
          end,
        },
      },
    })
  end,
}
Screen.Recording.2024-11-26.at.13.31.29.mov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants