kylechui / nvim-surround

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

fix: Error on invalid key, bad indent in insert_surround() #103

Closed Diomendius closed 2 years ago

Diomendius commented 2 years ago

Firstly, this fixes the insert mode surround feature causing a …/nvim-surround/lua/nvim-surround/init.lua:49: attempt to index local 'delimiters' (a nil value) Lua error when pressing a key that doesn't map to any delimiter.

Secondly, this makes the indentation of the inner line in multi-line mode (<c-g>S by default) consistent with native auto-indent. Before this patch, if shiftwidth was 0 the inner line would have the same indent as the lines next to it.

This plugin would also falsely assume all tabs have a width equal to shiftwidth. Tab width is always defined by tabstop (unless vartabstop is set) and shiftwidth doesn't even have to be a multiple of tabstop, regardless of whether expandtab is set. Any configuration with noexpandtab and a shiftwidth that isn't identical to tabstop (or 0, which has the same meaning) or at least a multiple of tabstop is, of course, awful, but that doesn't mean nvim-surround can't do what native indent behaviour does.

vartabstop is even more cursed, but if there's anyone who actually uses both it and this plugin I'm sure they'll be pleased to see this fix. :stuck_out_tongue:

NoahTheDuke commented 2 years ago

Hah the fail message was intentional after #59, as seen in #75. I think there's a config option to set that disable the message but I'm forgetting at the moment.

Diomendius commented 2 years ago

@NoahTheDuke This message (caused by the default function attached to invalid_key_behavior) is definitely intentional:

Error: Invalid character! Configure this message in require("nvim-surround").setup()

But this message definitely isn't:

E5108: Error executing lua: …/nvim-surround/lua/nvim-surround/init.lua:49: attempt to index local 'delimiters' (a nil value)

This PR doesn't do anything about the former; the message still prints by default and it's still configurable. The Lua error only occurs when using the insert mode surround feature; yslw from normal mode only prints the "Invalid character!" message, for instance.

NoahTheDuke commented 2 years ago

Ope, my apologies, I misunderstood your issue! Good catch.