Skip to content

TruncatedDinosour/vim-codefmt

main
Switch branches/tags
Code
This branch is 35 commits ahead, 8 commits behind google:master.
Contribute

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 

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.

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Vim Script 100.0%