vim-endwise icon indicating copy to clipboard operation
vim-endwise copied to clipboard

Adjust elixir addition to handle anonymous functions

Open dewyze opened this issue 7 years ago • 2 comments

I understand this solution is a little bit hacky, taking advantage of the way the endwise_additions are added in, so if it cannot be merged I understand. I was interested in trying to solve it either way and it was some good vimscript practice.

Commit Message: In the case where an elixir file has a single-line anonymous function, then other 'def do' will not have the 'end' automatically added.

Example:

  def foo do*

  def bar do
    fn -> baz end
  end

If the cursor is at the * and you press enter, the 'end' will not be added. However, if you remove the 'fn -> baz end' line, then the 'end' will be added when pressing enter on line 10.

This solution removes the negative match for an 'end' on the same line as a 'do' or 'fn'. Instead, the endwise_addition that was added will check that the line:

  • Has a do or fn.*->
  • Does not have a "#" OR if there is a "#", it is always a part of interpolation "#{".

If there is a commented out end, as in: fn -> foo # end, an end will still be inserted.

To the best of my knowledge, this will not cause any accidental inserts. There are cases where if a "#" is inside of a string, it will not add the extra end. However, this is a smaller problem than the current one where a "def foo do" does not produce an end.

@kassio as the last one to update the elixir syntax, would you mind reviewing this as well?

dewyze avatar Feb 09 '18 03:02 dewyze

Thank you for mention my name, although I'm not contributing to vim-elixir anymore and all my knowledge is very outdated, I'll cc @jbodah who is the main contributor now.

kassio avatar Feb 09 '18 13:02 kassio

Super late on this. Not sure if it's helpful, but here's the regex we use to match do/fn/end in the indent code which works pretty well:

https://github.com/elixir-editors/vim-elixir/blob/882a16fc36db323f4350bd4389c96db8d414cf53/autoload/elixir/indent.vim#L111-L113

And a usage example:

https://github.com/elixir-editors/vim-elixir/blob/882a16fc36db323f4350bd4389c96db8d414cf53/autoload/elixir/indent.vim#L249

jbodah avatar May 11 '18 04:05 jbodah