Adding ruby and elixir mappings for vim-sandwich

In my earlier quick review of vim-sandwich I noted that it didn’t have integration with ragtag.vim‘s useful bindings.

@gvaughn and I worked through the elixir ones, the current version looks like this:

" vim-sandwich
let g:sandwich#recipes = deepcopy(g:sandwich#default_recipes)
let g:sandwich#recipes += [
      \   {
      \     'buns'    : ['%{', '}'],
      \     'filetype': ['elixir'],
      \     'input'   : ['m'],
      \     'nesting' : 1,
      \   },
      \   {
      \     'buns'    : 'StructInput()',
      \     'filetype': ['elixir'],
      \     'kind'    : ['add', 'replace'],
      \     'action'  : ['add'],
      \     'input'   : ['M'],
      \     'listexpr'    : 1,
      \     'nesting' : 1,
      \   },
      \   {
      \     'buns'    : ['%\w\+{', '}'],
      \     'filetype': ['elixir'],
      \     'input'   : ['M'],
      \     'nesting' : 1,
      \     'regex'   : 1,
      \   }
      \ ]

function! StructInput() abort
  let s:StructLast = input('Struct: ')
  if s:StructLast !=# ''
    let struct = printf('%%%s{', s:StructLast)
  else
    throw 'OperatorSandwichCancel'
  endif
  return [struct, '}']
endfunction

Most of it is pretty straightforward, but the Struct mapping needs a function to take an input to name the struct.

In the below gif, I use the commands targeting inside the parentheses to change the argument of this elixir function. Because this action is repeatable through the repeat operator (.), you could imagine this would make refactoring some random arguments to take a map or struct instead to be very quick.

gif showing the working

It works really well!

While working on some areas of this blog, I realized that I did miss having sandwich buns that matched the vim-ragtag mappings, so I referenced the ragtag help docs and whipped up a few more lines of viml.

let g:sandwich#recipes += [
      \   {
      \     'buns':     ['<%= ', ' %>'],
      \     'filetype': ['eruby'],
      \     'input':    ['='],
      \     'nesting':  1
      \   },
      \   {
      \     'buns':     ['<% ', ' %>'],
      \     'filetype': ['eruby'],
      \     'input':    ['-'],
      \     'nesting':  1
      \   },
      \   {
      \     'buns':     ['<%# ', ' %>'],
      \     'filetype': ['eruby'],
      \     'input':    ['#'],
      \     'nesting':  1
      \   }
      \ ]

Even more simple than the elixir options, but just works. If you want both, you can check my full config below.

References:


Changelog
  • 2019-08-27 10:40:10 -0500

    Remove runaway `!`

    Thanks @megalithic!

  • 2019-08-27 10:19:43 -0500

    New post: adding ruby and elixir to vim-sandwich