kylechui / nvim-surround

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

Ability to disable auto-indent changes when deleting surrounds on current line #201

Closed bew closed 1 year ago

bew commented 1 year ago

Checklist

Is your feature request related to a problem? Please describe.

Happened today, in a Lua file, I pasted something and wanted to change it a bit before integrating it somewhere else.

{
'pasted-text-here'      -- cursor here
}

I go on the pasted text and do ds' to remove the quotes, and I surprisingly got:

{
  pasted-text-here      -- cursor here
}

It seems to only happen when I delete the quotes, not when I add or change them.

Describe the solution you'd like

I want to delete surrounds without changing indentation.

In a more general way, I think that surround changes that only impact the current line shouldn't change the indentation. So:

{
'foobar'      -- cursor here
}

Then do ds' on the foobar line, should give:

{
foobar      -- cursor here
}

Because the surround only impacted the line with foobar

BUT:

{
  {
    foobar      -- cursor here
  }
}

Then do ds} on the foobar line, should auto-indent and give:

{

  foobar      -- cursor here

}
kylechui commented 1 year ago

All of this should be able to be customized via the indent_lines configuration key, as well as customizing the delete key for surrounds.

See the following:

bew commented 1 year ago

Indeed I managed to stop reindent with:

    local surround_utils = require"nvim-surround.config"
    require"nvim-surround".setup {
      -- ...
      indent_lines = function(start_row, end_row)
        if start_row ~= end_row then
          surround_utils.default_opts.indent_lines(start_row, end_row)
        end
      end,
    }

:+1:

bew commented 1 year ago

For { removing spaces, I also found how to customize the delete pattern!

Would you accept a PR to change the current default to remove as much whitespace as possible instead of just 1 ?

kylechui commented 1 year ago

I would say probably not, as there are probably quite a few people that are used to the current default behavior. Whether or not your implementation is "more correct" is also debatable.

bew commented 1 year ago

Hmm true, maybe add something like it in a wiki or documentation?

Here is my implementation if you want to take a look: https://github.com/bew/dotfiles/commit/b7dc4ee3ffdea87c9d10dbeae3a0f69dbdf354e3

kylechui commented 1 year ago

This should be implemented in the latest commit/version.