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

Change doesn't work with multiline quotes (both single and double) #285

Closed telemachus closed 10 months ago

telemachus commented 10 months ago

Checklist

Neovim Version

NVIM v0.10.0-dev-1744+g0d885247b

Plugin Version

Nightly (Beta)

Minimal Configuration

require("nvim-surround").setup()

Sample Buffer

(This is a test.
This is only a test.)

"This is a test.
This is only a test."

'This is a test.
This is only a test.'

[This is a test.
This is only a test.]

Keystroke Sequence

With the cursor inside of the two-line double quote:

cs"'

With the cursor inside of the two-line single quote:

cs'"

Expected behavior

I expect the change command to find the quotes on both lines and change them. (This is what happens if you run cs)* or cs]* on the other multi-line pairs.)

Actual behavior

A search is triggered. For example, if you put the cursor on the first "This" inside one of the two-line quotes, and try the change command, neovim runs a search for /\<This\> and highlights all the items found.

Additional context

I doubt it matters, but I'm on macOS 14.1.2.

In the docs for jumps (section 2.5), there's this note:

Note: |nvim-surround| jumps across lines to find the next/previous surrounding pair, except for quote characters. This is done to match existing behavior for the a" text-object.

Maybe my problem is related to this?

kylechui commented 10 months ago

Yes, you are right that that is your error. Basically the behavior for nvim-surround tries to closely follow the default behavior in Neovim itself. If you do va(, Neovim will start from the cursor and select any surrounding parenthesis pair, or jump to the next one if none surrounds the cursor (including across lines). By default, Neovim does not recognize quotes on multiple lines as a "surrounding pair" (e.g. try va"), so neither does nvim-surround. You can get the desired behavior by using Lua patterns to match for double/single quotes (see the default config/help docs for more info), but this will cause issues with escaped quotes etc.