Skip to content

Support multiple formatters on a single buffer #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TamaMcGlinn opened this issue Apr 15, 2021 · 4 comments
Closed

Support multiple formatters on a single buffer #178

TamaMcGlinn opened this issue Apr 15, 2021 · 4 comments

Comments

@TamaMcGlinn
Copy link
Contributor

I'm trying to run several different formatters on a single buffer when I save it. Something like:

  autocmd FileType cpp AutoFormatBuffer clang-format
  autocmd FileType cpp AutoFormatBuffer copyright

(which in fact only applies the last formatter given) I also tried:

  autocmd FileType cpp AutoFormatBuffer clang-format,copyright

But that doesn't apply either. :help AutoFormatBuffer didn't help me, but surely I'm not the only person who wants to apply two or more formatters?

@TamaMcGlinn
Copy link
Contributor Author

TamaMcGlinn commented Apr 15, 2021

The workaround is bit ugly, but hopefully it helps anyone coming to this issue in the meantime:

function! ApplyCppFormatters() abort
  execute 'FormatCode copyright'
  if expand('%:e') ==# 'h'
    execute 'FormatCode header_guards'
  endif
  execute 'FormatCode clang-format'
endfunction

augroup autoformat
  autocmd!
  autocmd BufWritePre *.cpp,*.h call ApplyCppFormatters()
augroup END

@dbarnett
Copy link
Contributor

Yes, you're not alone! See #44. I'll merge these and post an update with some more details there.

@dbarnett
Copy link
Contributor

  autocmd BufWritePre *.cpp,*.h call ApplyCppFormatters() | noautocmd write

BTW, isn't noautocmd write redundant since vim's already going to write the file after BufWritePre?

@TamaMcGlinn
Copy link
Contributor Author

Correct, thank you! I had this because I had a different formatter which wasn't able to accept stdin, so it works on a post-write hook and then needs to write the file again:

autocmd BufWritePost *.ad[sb] Autoformat | noautocmd write

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants