codefmt is a utility for syntax-aware code formatting. It contains several built-in formatters, and allows new formatters to be registered by other plugins. https://git.ari.lt/ari/vim-codefmt
Find a file
Ari Archer a4a9049b58
remove prettier's filetype inheretence
Signed-off-by: Ari Archer <ari.web.xyz@gmail.com>
2022-12-31 09:05:46 +02:00
.github/ISSUE_TEMPLATE Add issue template for "Request new formatter" 2020-03-28 09:38:54 -07:00
autoload remove prettier's filetype inheretence 2022-12-31 09:05:46 +02:00
instant Add lua formatter flags and fix indenting 2022-05-27 20:09:24 +03:00
plugin Merge https://github.com/google/vim-codefmt/pull/154 2022-01-15 21:01:11 +02:00
addon-info.json Formatted all code and fixed addon-info.json 2022-01-15 22:48:31 +02:00
bootstrap.vim Merge https://github.com/google/vim-codefmt/pull/121 and reformat all files 2022-01-15 19:35:56 +02:00
CONTRIBUTING.md Formatted all code and fixed addon-info.json 2022-01-15 22:48:31 +02:00
LICENSE Improved README and fixed license 2022-01-15 22:29:40 +02:00
README.md Made README more portable 2022-01-15 23:18:34 +02:00

codefmt is a utility for syntax-aware code formatting. It contains several built-in formatters, and allows new formatters to be registered by other plugins.

Supported file types

Commands

Use :FormatLines to format a range of lines or use :FormatCode to format the entire buffer. Use :NoAutoFormatBuffer to disable current buffer formatting.

Usage example

Before:

int foo(int * x) { return * x** x ; }

After running :FormatCode:

int foo(int* x) { return *x * *x; }

Installation

These are only few examples

Vundle

call vundle#begin()
" ...

" Add maktaba and codefmt to the runtimepath.
" (The latter must be installed before it can be used.)
Plugin 'google/vim-maktaba'
Plugin 'TruncatedDinosour/vim-codefmt'

" ...
call vundle#end()

NeoBundle

call neobundle#begin(expand('...'))
" ...

" Add maktaba and codefmt to the runtimepath.
" (The latter must be installed before it can be used.)
NeoBundle 'google/vim-maktaba'
NeoBundle 'TruncatedDinosour/vim-codefmt'

" ...
call neobundle#end()

VimPlug

" Add maktaba and codefmt to the runtimepath.
" (The latter must be installed before it can be used.)
call plug#begin("...")
" ...

Plug 'google/vim-maktaba'
Plug 'TruncatedDinosour/vim-codefmt'

" ...
call plug#end()

Pathogen

$ cd ~/.vim/bundle
$ git clone https://github.com/google/vim-maktaba
$ git clone https://github.com/TruncatedDinosour/vim-codefmt

Make sure you have updated maktaba recently. Codefmt depends upon maktaba to register formatters.

Autoformatting

Want to just sit back and let autoformat happen automatically? Add this to your vimrc (or any subset):

autocmd FileType * silent FormatCode

Configuring formatters

Most formatters have some options available that can be configured with variables but you can also use Glaive

You can get a quick view of all codefmt flags by executing :Glaive codefmt, or start typing flag names and use tab completion. See :help Glaive for usage details.

Installing formatters

Codefmt defines several built-in formatters. The easiest way to see the list of available formatters is via tab completion: Type :FormatCode <TAB> in vim. Formatters that apply to the current filetype will be listed first.

To use a particular formatter, type :FormatCode FORMATTER-NAME. This will either format the current buffer using the selected formatter or show an error message with basic setup instructions for this formatter. Normally you will trigger formatters via key mappings and/or autocommand hooks. See vroom/main.vroom to learn more about formatting features, and see vroom/FORMATTER-NAME.vroom to learn more about usage for individual formatters.

Creating a New Formatter

Assume a filetype myft and a formatter called MyFormatter.

  • Create a new file in autoload/codefmt/myformatter.vim See `autoload/codefmt/buildifier.vim for an example. This is where all the logic for formatting goes.

  • Register the formatter in plugin/register.vim with:

    call s:registry.AddExtension(codefmt#myformatter#GetFormatter())
    
  • Create a flag in instant/flags.vim

    ""
    " The path to my formatter executable.
    call s:plugin.Flag('myformatter_executable', 'myformatter')
    
  • Update the README.md to mention your new filetype

Thats it! Now make a pull request.