neoformat icon indicating copy to clipboard operation
neoformat copied to clipboard

How to formatting ERB files, like html.erb and js.erb

Open nPaul opened this issue 6 years ago • 2 comments

Now i use prettier for html.erb files. like init.vim autocmd BufNewFile,BufRead *.html.erb set filetype=html let g:neoformat_enabled_html = ['prettier']

but it working incorrect for example all tags ERB <%=%> formatting in one line

<% if condition %>
<%= lint_to  :example %>
<%= input :example, "qweqwe" %>
<% end %>

to

<% if condition %>  <%= link_to example %> <%= input
:example, "qweqwe" %><% end %>

etc

nPaul avatar Sep 29 '19 03:09 nPaul

I haven't tried js.erb but for html.erb, I'm using htmlbeautifier with the following in ~/.config/nvim/autoload/neoformat/formatters/eruby.vim

function! neoformat#formatters#eruby#enabled() abort
    return ['htmlbeautifier']
endfunction

function! neoformat#formatters#eruby#htmlbeautifier() abort
    return {
        \ 'exe': 'htmlbeautifier',
        \ 'args': ['--keep-blank-lines', '1'],
        \ 'stdin': 1
        \ }
endfunction

semanticart avatar Sep 25 '22 20:09 semanticart

Using lua config in Neovim, i just set these variables:

 vim.g.neoformat_enabled_eruby = { "erbformat", "htmlbeautifier" }
 vim.g.neoformat_eruby_erbformat = { exe = "erb-format", args = { "--stdin" }, stdin = 1 }
 vim.g.neoformat_eruby_htmlbeautifier = { exe = "htmlbeautifier", args = { "--keep-blank-lines", '1' }, stdin = 1 }

I use erb-format, that'll seems to work.

Or one can create the autoload/neoformat/formatters/eruby.vim like above and prepend other formatters:

function! neoformat#formatters#eruby#enabled() abort
    return ['erbformat', 'htmlbeautifier']
endfunction

function! neoformat#formatters#eruby#erbformat() abort
    return {
        \ 'exe': 'erb-format',
        \ 'args': ['--stdin'],
        \ 'stdin': 1
        \ }
endfunction
function! neoformat#formatters#eruby#htmlbeautifier() abort
    return {
        \ 'exe': 'htmlbeautifier',
        \ 'args': ['--keep-blank-lines', '1'],
        \ 'stdin': 1
        \ }
endfunction

zealot128 avatar Dec 11 '23 14:12 zealot128