jobel-aqua / neoformat

:sparkles: A (Neo)vim plugin for formatting code.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Neoformat Build Status

A (Neo)vim plugin for formatting code.

Neoformat uses a variety of formatters for many filetypes. Currently, Neoformat will run a formatter using the current buffer data, and on success it will update the current buffer with the formatted text. On a formatter failure, Neoformat will try the next formatter defined for the filetype.

By using getbufline() to read from the current buffer instead of file, Neoformat is able to format your buffer without you having to :w your file first. Also, by using setline(), marks, jumps, etc. are all maintained after formatting.

Neoformat supports both sending buffer data to formatters via stdin, and also writing buffer data to /tmp/ for formatters to read that do not support input via stdin.

Basic Usage

Format the entire buffer, or visual selection of the buffer

:Neoformat

Or specify a certain formatter (must be defined for the current filetype)

:Neoformat jsbeautify

Or format a visual selection of code in a different filetype

Note: you must use a ! and pass the filetype of the selection

:Neoformat! python

You can also pass a formatter to use

:Neoformat! python yapf

Or perhaps run a formatter on save

augroup fmt
  autocmd!
  autocmd BufWritePre * Neoformat
augroup END

Install

vim-plug

Plug 'sbdchd/neoformat'

Current Limitation(s)

If a formatter is either not configured to use stdin, or is not able to read from stdin, then buffer data will be written to a file in /tmp/neoformat/, where the formatter will then read from

Config [Optional]

Define custom formatters.

Options:

name description default optional / required
exe the name the formatter executable in the path n/a required
args list of arguments [] optional
replace overwrite the file, instead of updating the buffer 0 optional
stdin send data to the stdin of the formatter 0 optional
no_append do not append the path of the file to the formatter command, used when the path is in the middle of a command 0 optional

Example:

let g:neoformat_python_autopep8 = {
            \ 'exe': 'autopep8',
            \ 'args': ['-s 4', '-E'],
            \ 'replace': 1 " replace the file, instead of updating buffer (default: 0),
            \ 'stdin': 1, " send data to stdin of formatter (default: 0)
            \ 'no_append': 1,
            \ }

let g:neoformat_enabled_python = ['autopep8']

Configure enabled formatters.

let g:neoformat_enabled_python = ['autopep8', 'yapf']

Have Neoformat use &formatprg as a formatter

let g:neoformat_try_formatprg = 1

Enable basic formatting when a filetype is not found. Disabled by default.

" Enable alignment
let g:neoformat_basic_format_align = 1

" Enable tab to spaces conversion
let g:neoformat_basic_format_retab = 1

" Enable trimmming of trailing whitespace
let g:neoformat_basic_format_trim = 1

Have Neoformat only msg when there is an error

let g:neoformat_only_msg_on_error = 1

When debugging, you can enable either of following variables for extra logging.

let g:neoformat_verbose = 1 " only affects the verbosity of Neoformat
" Or
let &verbose            = 1 " also increases verbosity of the editor as a whole

Adding a New Formatter

Note: you should replace everything {{ }} accordingly

  1. Create a file in autoload/neoformat/formatters/{{ filetype }}.vim if it does not already exist for your filetype.

  2. Follow the following format

See Config above for options

function! neoformat#formatters#{{ filetype }}#enabled() abort
    return ['{{ formatter name }}', '{{ other formatter name for filetype }}']
endfunction

function! neoformat#formatters#{{ filetype }}#{{ formatter name }}() abort
    return {
        \ 'exe': '{{ formatter name }}',
        \ 'args': ['-s 4', '-q'],
        \ 'stdin': 1
        \ }
endfunction

function! neoformat#formatters#{{ filetype }}#{{ other formatter name }}() abort
  return {'exe': {{ other formatter name }}
endfunction

Supported Filetypes

About

:sparkles: A (Neo)vim plugin for formatting code.

License:BSD 2-Clause "Simplified" License


Languages

Language:Vim Script 83.5%Language:Python 11.6%Language:Shell 2.9%Language:TypeScript 1.5%Language:CSS 0.5%Language:Makefile 0.1%