kylechui / nvim-surround

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

Delimiter.pairs modified after usage #26

Closed NoahTheDuke closed 2 years ago

NoahTheDuke commented 2 years ago

Hey Kyle!

I decided to try to fix #20 and ran into a separate bug that's outside of my knowledge of Lua to understand. In short, cs)( changes (x, y) to ( x, y ). And cs)( changes ( x, y ) to ( x, y ) , which is expected. But if I type cs() (which works but is effectively a NOOP), then cs)( stops working altogether.

Before typing cs(), vim.pretty_print(utils.delimiters.pairs) produces:

{
  ["("] = { "( ", " )" },
  [")"] = { "(", ")" },
  ["<"] = { "< ", " >" },
  [">"] = { "<", ">" },
  ["["] = { "[ ", " ]" },
  ["]"] = { "[", "]" },
  f = <function 1>,
  i = <function 2>,
  ["{"] = { "{ ", " }" },
  ["}"] = { "{", "}" }
}

And after typing cs():

{
  ["("] = { "(", ")" },
  [")"] = { "(", ")" },
  ["<"] = { "< ", " >" },
  [">"] = { "<", ">" },
  ["["] = { "[ ", " ]" },
  ["]"] = { "[", "]" },
  f = <function 1>,
  i = <function 2>,
  ["{"] = { "{ ", " }" },
  ["}"] = { "{", "}" }
}

As you can see, for some reason the ["("] mapping is changed from { "( ", " )" } to { "(", ")" }. I won't post the output for brevity but this happens to every single pair that has surrounding spaces.

kylechui commented 2 years ago

I'm not 100% sure what you did, but my guess is that it's probably related to the following: when you make a "copy" of a table in Lua, it actually makes a copy of the reference to the table, not a copy of the values. So if you have the code

local x = { 1, 2 }
local y = x
y[1] = 2
print(x) -- Should be { 2, 2 }

I'm pretty sure that's the case, so perhaps somewhere in your modified code you're modifying a reference and not the values?

BTW thanks for taking the time to contribute!

Edit: Oh wait is this a part of the main code right now‽ If so, oops

NoahTheDuke commented 2 years ago

Yeah, this happens directly on master. (At last I thought so but now that I've stepped away for the night I'm unsure lol.) I looked around the rest of the code but I'm new to Lua as of this evening so I don't quite follow it all yet.

kylechui commented 2 years ago

I'll take another look at it tomorrow morning; also with respect to #20 I think I should probably refactor a few other things, like using g@i instead of g@a for setting operator marks

kylechui commented 2 years ago

Hi there, I think the latest commit in add-buffer-local-mappings should resolve the issue; I've done some testing on my end and it seems to behave just fine

Edit: The other branch has since been merged into main, so please see if the issue persists there, thanks!

NoahTheDuke commented 2 years ago

Hey hey, looks like this is fixed now. Nicely done.