machakann/vim-sandwich

Allow user to customize function name to delete

Closed this issue ยท 6 comments

Hello!

I often find myself in need to delete or change a surrounding function which name includes special characters, something like this: Foo::Bar.baz("hello").

I'd love to use magiccharacter f for that, but the problem is, it respects the iskeyword setting, so whenever I run sdf, it just transforms Foo::Bar.baz("hello") into Foo::Bar."hello".

A solution here might be a buffer local setting, like b:sandwich_function_name_includes = ['.', ':'], with a pseudo algorithm on sdf like this:

  1. Check if b:sandwich_function_name_includes exists.
  2. If no, just delete the function as usual.
  3. If yes:
    3.1. Remember the old &iskeyword.
    3.2. Append the custom chars to iskeyword+=<char>.
    3.3. Delete the function.
    3.4. Restore the previous &iskeyword back.

That will allow to delete the function completely: Foo::Bar.baz("hello") -> "hello".

What do you think?

It seems, there is a more simple way to do that, defining our own pattern like this:
https://github.com/machakann/vim-sandwich/blob/master/autoload/sandwich/magicchar/f.vim#L40,L47

Adding this piece to the file above made it possible to define custom patterns depending on filetype:

if exists('b:sandwich_function_header_pattern')
  let s:patterns[&filetype] = b:sandwich_function_header_pattern
endif

Thank you for the nicely organized code, it was really easy to find out. ๐Ÿ˜„

Nice catch, thanks.

Probably, I forget to document it but you can use g:sandwich_function_patterns or b:sandwich_function_patterns to customize the behavior of sdf.

However, the current system is a little strange. I will fix problems and then drop a news here. Thank you for using!

Oops, I made a mistake in issue numbering for the commit. Please ignore it.

OK, I pushed fixes and documents update. Now you can use g:sandwich#magicchar#f#patterns for general rules or b:sandwich_magicchar_f_patterns for buffer-local (filetype-specific) rules.

For example,

let g:sandwich#magicchar#f#patterns = [
  \   {
  \     'header' : '\<\h\k*::\h\k*\.\h\k*',
  \     'bra'    : '(',
  \     'ket'    : ')',
  \     'footer' : '',
  \   },
  \ ]

(Probably this regex pattern should be polished for the practical use.)

It's perfect, thank you very much!

Thanks, this is very useful!