google / vim-codefmt

Vim plugin for syntax-aware code formatting
Apache License 2.0
1.11k stars 113 forks source link

Add mechanism for chaining formatters #44

Open dbarnett opened 9 years ago

dbarnett commented 9 years ago

For some types of files you might want to run more than one formatter, for instance sort C++ includes (which clang-format doesn't handle) and then format with clang-format. Could codefmt support configuring a list of formatters instead of a single formatter? E.g.

AutoFormatBuffer sortincludes,clang-format
let b:codefmt_formatter = ['sortincludes', 'clang-format']
dbarnett commented 3 years ago

Our hacky solution when this came up (for imports+formatting in java) was to define a little one-off formatter with its own formatter name that delegated and called formatter A then formatter B.

As noted in #178, you can't just invoke :AutoFormatBuffer twice and its docs don't explicitly explain it'll replace any previous autoformatting each time you invoke it.

TamaMcGlinn commented 3 years ago

I think my hacky solution is a bit easier than what you describe, since I only needed to add a function to do the delegation, instead of a whole formatter.

dbarnett commented 3 years ago

Yes, that's probably close to what I would've done as a one-off workaround. For our purposes on the java thing, we were solving a common issue and couldn't plop arbitrary autocmds into everyone's vimrc.