kylechui / nvim-surround

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

Visual surround: selection disappears when using number toggle plugin #315

Open simonmandlik opened 2 months ago

simonmandlik commented 2 months ago

Checklist

Neovim Version

NVIM v0.10.0-dev-2274+g1d5f86f69-Homebrew

Plugin Version

Tagged (Stable)

Minimal Configuration

local root = vim.fn.fnamemodify("./.repro", ":p")
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath, })
end
vim.opt.runtimepath:prepend(lazypath)

local plugins = {
    {
        "kylechui/nvim-surround",
        version = "*", -- Use for stability; omit to use `main` branch for the latest features
        config = function()
            require("nvim-surround").setup {}
        end
    },
}

require("lazy").setup(plugins, { root = root .. "/plugins", })

vim.o.number = true

-- from https://github.com/sitiom/nvim-numbertoggle/blob/main/plugin/numbertoggle.lua
local augroup = vim.api.nvim_create_augroup("numbertoggle", {})

vim.api.nvim_create_autocmd({ "BufEnter", "FocusGained", "InsertLeave", "CmdlineLeave", "WinEnter" }, {
   pattern = "*",
   group = augroup,
   callback = function()
      if vim.o.nu and vim.api.nvim_get_mode().mode ~= "i" then
         vim.opt.relativenumber = true
      end
   end,
})

vim.api.nvim_create_autocmd({ "BufLeave", "FocusLost", "InsertEnter", "CmdlineEnter", "WinLeave" }, {
   pattern = "*",
   group = augroup,
   callback = function()
      if vim.o.nu then
         vim.opt.relativenumber = false
         vim.cmd "redraw"
      end
   end,
})

Sample Buffer

test test test test

Keystroke Sequence

ejS ### Expected behavior This is what happens after I comment out everything after line 23 in the setup: image ### Actual behavior image ### Additional context The surrounding still works, this is only a visual issue in the "pending" phase. The code is from https://github.com/sitiom/nvim-numbertoggle/blob/main/plugin/numbertoggle.lua, but I copied it for easier reproduction
kylechui commented 1 month ago

Sorry for missing this for... several weeks. The sample config you sent worked well and I was able to reproduce it on my machine. Unfortunately, I have no clue where/why exactly this happened, and I vaguely remember dealing with something like this in a previous issue. I'll take another look; note to myself is to check the save/restore view calls.

kylechui commented 1 month ago

Relevant issues/PRs: #278 #299 #312

kylechui commented 1 month ago

I did some snooping around with the minimal configuration and I believe I have figured out the problem. The forced redraw in numbertoggle autocommand is nuking the highlights from the buffer; removing it resolves the issue. This also applies to the highlight when, you enter command mode after creating a visual selection, e.g. :foobarbaz in nvim -u NONE vs. nvim -u minimal_config.lua. I'm not entirely sure what the fix is here, as I imagine the forced redraw is to ensure that the toggled line numbers update properly whenever the mode changes.