kylechui / nvim-surround

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

Visual line-wise surround adds extra newlines #341

Closed tammersaleh closed 3 months ago

tammersaleh commented 3 months ago

Checklist

Neovim Version

NVIM v0.11.0-dev-138+g88fe467b1-Homebrew
Build type: Release
LuaJIT 2.1.1716656478
Run "nvim -V1 -v" for more info

Plugin Version

Tagged (Stable)

Minimal Configuration

vim.g.mapleader = ' '
vim.g.maplocalleader = ' '

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable", -- latest stable release
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
    "kylechui/nvim-surround",
    version = "*",
    event = "VeryLazy",
    opts = {
        move_cursor = false,
        surrounds = {
            ["~"] = { -- Markdown fenced code blocks
                add = function()
                    local config = require("nvim-surround.config")
                    local result = config.get_input("Markdown code block language: ")
                    return {
                        { "```" .. result, '' },
                        { "", "```" },
                    }
                end,
            },
        },
    },
})

Sample Buffer

foo

Keystroke Sequence

VS~ruby<CR>

Expected behavior

``` ruby
foo
```

Actual behavior

``` ruby

foo

```

Additional context

Got the code from https://github.com/kylechui/nvim-surround/discussions/53#discussioncomment-8028781

kylechui commented 3 months ago

Using V for visual line mode will automatically add extra lines before and after the surrounded text. You should either use v for regular visual mode, or remove the extra beginning/ending lines from the configuration, i.e.

return { "```" .. result, "```" }
tammersaleh commented 3 months ago

Thanks for the quick response! I'm struggling with the configuration structure... I tried what I think you're suggesting:

surrounds = {
  ["~"] = { -- Markdown fenced code blocks
    add = function()
      local config = require("nvim-surround.config")
      local result = config.get_input("Markdown code block language: ")
      -- 👇 Is this what you mean? 
      return { "```" .. result, "```" }
    end,
  },
},

But it gives this error when I try to use it:

Markdown code block language: rE5108: Error executing lua ...are/nvim/lazy/nvimsurround/lua/nvim-surround/config.lua:369: attempt to index a nil value stack traceback:
        ...are/nvim/lazy/nvim-surround/lua/nvim-surround/config.lua:369: in function 'get_delimiters'
        ...share/nvim/lazy/nvim-surround/lua/nvim-surround/init.lua:89: in function 'visual_surround'
        [string ":lua"]:1: in main chunk

You've been so helpful, but can I ask you to show me where I got the config wrong?

kylechui commented 3 months ago

Ah I forgot a set of curly braces, sorry about that! It should be:

surrounds = {
  ["~"] = { -- Markdown fenced code blocks
    add = function()
      local config = require("nvim-surround.config")
      local result = config.get_input("Markdown code block language: ")
      return { { "```" .. result }, { "```" } }
    end,
  },
},

Note to self: Probably parse the output of the add function call in get_delimiters to improve UX and avoid this footgun.

kylechui commented 3 months ago

You should be able to set the tag to 2.3.0 (or just fetch latest from main) for the original configuration I sent to work.

tammersaleh commented 3 months ago

Thanks! The fixed configuration worked a charm!