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

Surround jumps to parent surrounds instead of actual surrounding pair if some keybinds are set #197

Closed mnpqraven closed 1 year ago

mnpqraven commented 1 year ago

Checklist

Neovim Version

NVIM v0.8.2
Build type: Release
LuaJIT 2.1.0-beta3

Plugin Version

Tagged (Stable)

Minimal Configuration

vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

vim.opt.termguicolors = true

local lazypath = vim.fn.stdpath("data") .. "/lazy/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",
    "--branch=stable", -- latest stable release
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)

vim.g.mapleader = " "
vim.g.maplocalleader = "\\"

require('lazy').setup({
  { 'kylechui/nvim-surround', tag = '*' },

})

local M = {}

local function bind(op, outer_opts)
    outer_opts = outer_opts or {noremap = true}
    return function (lhs, rhs, opts)
        opts = vim.tbl_extend( "force", outer_opts, opts or {} )
        vim.keymap.set(op, lhs, rhs, opts)
    end
end

M.map      = bind("", {noremap = false})
M.noremap      = bind("", {noremap = true})

M.map('N', 'K')
M.map('n', 'gj')
M.map('E', 'J')
M.map('e', 'l')
M.map('k', 'nzz')
M.map('K', 'Nzz')
M.map('l', 'gk')
M.map('E', '$')
M.map('H', '0')
M.map('N', 'L')

Sample Buffer

impl DatabaseRequirement {
    pub fn generate_resource(&self) -> GrandResource {
        // Vec<UnitRequirement> > GrandResource
        let mut sum = GrandResource::new();
        for unit in &self.unit_req {
            sum.combine(unit.get_req());
            dbg!(&sum);
            // dbg!(sum.combine(unit.get_req()));
        }
        dbg!(&sum);
        sum
    }
}

Keystroke Sequence

cursor inside the for loop, changing the surrounds with cs{ttest<CR>

Expected behavior

only the for loop's brackets get changed

Actual behavior

the for loop's parent (the function brackets) is selected instead if my cursor is at specific position noted below

Additional context

https://user-images.githubusercontent.com/8957788/212528698-74667943-2e3d-4e59-86aa-1c2f892d6ca3.mp4

https://user-images.githubusercontent.com/8957788/212528699-754e48b1-6431-493f-b9af-08fe9b84efcd.mp4

I'm using a colemak keyboard and therefore needed to change some of my basic vim keys to another layout and cause the surround to select the parent surrounds in some cases. I've noticed 2 things when trying to reproduce this bug(?)

mnpqraven commented 1 year ago

Update on the issue: I've found out that the key map that caused the issue was the l key triggering the cursor to move up if I also remap it to all the modes. When I press cs the cursor would somehow move up just move up and the above surround would get selected instead. The problem can be resolved by only mapping the l keys for navigation in strictly normal and insert mode. Not really sure myself how the l key can cause conflicts with the plugin though.

vim.keymap.set({ 'n', 'v' }, 'l', 'gk')
kylechui commented 1 year ago

Hmm... I think this might be related to the fact that some of the functions use g@l as a return value in order to enable dot-repeating. I'll look into this a bit more and see if there's something that can help with that.

xorander00 commented 1 year ago

Just dropping my $0.02 to say that I'm seeing something similar, though I'm not sure if it's same issue.

Given "this" string cs"' just jumps to the first double quote character. Oddly enough, it works fine with backticks (that is, if the initial surround is ', and I do cs'`, then it replaces the single quotes with backticks just fine. It's just with single and double quotes that's messing up for some reason. Trying to see if it's something in my config that could be doing it.

kylechui commented 1 year ago

Just dropping my $0.02 to say that I'm seeing something similar, though I'm not sure if it's same issue.

Given "this" string cs"' just jumps to the first double quote character. Oddly enough, it works fine with backticks (that is, if the initial surround is ', and I do cs'`, then it replaces the single quotes with backticks just fine. It's just with single and double quotes that's messing up for some reason. Trying to see if it's something in my config that could be doing it.

@xorander00 I don't think that your situation is related, unless you too are using some other keyboard layout that requires you to remap most keys (in particular, l). Feel free to open a different issue for this.

kylechui commented 1 year ago

I've cleared up some of the other more pressing issues; I'll try my best to dedicate more time to thinking of ways to work around this problem.

kylechui commented 1 year ago

@mnpqraven @sxyazi Can both of you please switch to branch fix-l-mapping and see if that resolves your issue? For some reason I had some remap = true in the keymaps, when I think removing them should avoid your default mappings, instead utilizing the default l key.

sxyazi commented 1 year ago

I switched to the fix-l-mapping branch and tested it, it now works fine with my remapping of l, thanks for your work!

kylechui commented 1 year ago

Of course, thanks for opening the issue and being so patient.