google / vim-codefmt

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

Please include instructions on how to use keep-sorted #222

Open andrewpollock opened 1 year ago

andrewpollock commented 1 year ago

I'd like to have https://github.com/google/keep-sorted get invoked whenever I save a file, rather adopt https://pre-commit.com/

Could you include some instructions on how to do so?

dbarnett commented 1 year ago

Do you mean to add support for it? I don't see that we have that as a supported formatter.

andrewpollock commented 1 year ago

@dbarnett I guess so? I figured it was more a case of configuring things to "just do it"?

malcolmr commented 1 year ago

One issue here is that vim-codefmt is largely based on the assumption that a particular filetype has a single formatter that we can invoke to format (on save, or on :FormatCode, etc), while keep-sorted is an addition to (almost) any filetype.

While you can register a keep-sorted formatter yourself (it's not well-documented, but it is supported: you can get the codefmt registry from your own code via maktaba#extension#GetRegistry('codefmt'), and then add your own extension just as in plugin/register.vim), the problem you'll run into is that you'd always need to invoke it manually (i.e. :FormatCode keep-sorted), because :AutoFormatBuffer (to format-on-save) sets the default formatter as 'the' formatter for the buffer, as well as enabling format-on-save.

(Perhaps you could use something like autocmd BufWritePre cpp FormatCode keep-sorted? That's basically what :AutoFormatBuffer does, but it's a bit indirect.)

I think adding good support for something as general as keep-sorted probably requires solving #44 first, so that you configure a chain of formatters for a filetype to run (say) clang-format and then keep-sorted.