From 3be20a1cb7dc8954b5e5939d6aca51cd779996ec Mon Sep 17 00:00:00 2001 From: Steve Dignam Date: Mon, 9 Jan 2017 12:52:31 -0500 Subject: [PATCH] Refactored main neoformat function Before, Neoformat was using a messy multi function control flow hodge podge. (I think it was warranted by job control) Now, just using a simple for loop Minor change to test vimrc so that swap files don't mess with testing Tiny change to README: vim8 --> vim --- README.md | 2 +- autoload/neoformat.vim | 139 ++++++++++++++++++----------------------- plugin/neoformat.vim | 2 +- test/vimrc | 1 + 4 files changed, 65 insertions(+), 79 deletions(-) diff --git a/README.md b/README.md index ea385f2c..2c5384b1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Neoformat [![Build Status](https://travis-ci.org/sbdchd/neoformat.svg?branch=master)](https://travis-ci.org/sbdchd/neoformat) -A [Neovim](https://neovim.io) and Vim8 plugin for formatting code. +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 diff --git a/autoload/neoformat.vim b/autoload/neoformat.vim index 8fa0de2b..88418914 100644 --- a/autoload/neoformat.vim +++ b/autoload/neoformat.vim @@ -1,97 +1,88 @@ -function! neoformat#Start(user_formatter) abort - let s:current_formatter_index = 0 - call neoformat#Neoformat(a:user_formatter) -endfunction - function! neoformat#Neoformat(user_formatter) abort - if !&modifiable return neoformat#utils#warn('buffer not modifiable') endif let filetype = s:split_filetypes(&filetype) + if !empty(a:user_formatter) - let formatter = a:user_formatter + let formatters = [a:user_formatter] else let formatters = s:get_enabled_formatters(filetype) if formatters == [] call neoformat#utils#msg('formatter not defined for ' . filetype . ' filetype') return s:basic_format() endif - - if s:current_formatter_index >= len(formatters) - call neoformat#utils#msg('attempted all formatters available for ' . filetype) - return s:basic_format() - endif - - let formatter = formatters[s:current_formatter_index] endif - if exists('g:neoformat_' . filetype . '_' . formatter) - let definition = g:neoformat_{filetype}_{formatter} - elseif s:autoload_func_exists('neoformat#formatters#' . filetype . '#' . formatter) - let definition = neoformat#formatters#{filetype}#{formatter}() - else - call neoformat#utils#log('definition not found for formatter: ' . formatter) - if !empty(a:user_formatter) - call neoformat#utils#msg('formatter definition for ' . a:user_formatter . ' not found') - return s:basic_format() + for formatter in formatters + + if exists('g:neoformat_' . filetype . '_' . formatter) + let definition = g:neoformat_{filetype}_{formatter} + elseif s:autoload_func_exists('neoformat#formatters#' . filetype . '#' . formatter) + let definition = neoformat#formatters#{filetype}#{formatter}() + else + call neoformat#utils#log('definition not found for formatter: ' . formatter) + if !empty(a:user_formatter) + call neoformat#utils#msg('formatter definition for ' . a:user_formatter . ' not found') + return s:basic_format() + endif + continue endif - return neoformat#NextNeoformat() - endif - let cmd = s:generate_cmd(definition, filetype) - if cmd == {} - if !empty(a:user_formatter) - return neoformat#utils#warn('formatter ' . a:user_formatter . ' failed') + let cmd = s:generate_cmd(definition, filetype) + if cmd == {} + if !empty(a:user_formatter) + return neoformat#utils#warn('formatter ' . a:user_formatter . ' failed') + endif + continue endif - return neoformat#NextNeoformat() - endif - let stdin = getbufline(bufnr('%'), 1, '$') - if cmd.stdin == 1 - let stdout = systemlist(cmd.exe, stdin) - else - let stdout = systemlist(cmd.exe) - endif + let stdin = getbufline(bufnr('%'), 1, '$') + if cmd.stdin + let stdout = systemlist(cmd.exe, stdin) + else + let stdout = systemlist(cmd.exe) + endif - " read from /tmp file if formatter replaces file on format - if cmd.replace == 1 - let stdout = readfile(cmd.tmp_file_path) - endif + " read from /tmp file if formatter replaces file on format + if cmd.replace + let stdout = readfile(cmd.tmp_file_path) + endif - call neoformat#utils#log(stdin) - call neoformat#utils#log(stdout) - if v:shell_error == 0 - if stdout != stdin - " 1. set lines to '' aka \n from end of file when new data < old data - let datalen = len(stdout) - - while datalen <= line('$') - call setline(datalen, '') - let datalen += 1 - endwhile - - " 2. remove extra newlines at the end of the file - let search = @/ - let view = winsaveview() - " http://stackoverflow.com/a/7496112/3720597 - " vint: -ProhibitCommandRelyOnUser -ProhibitCommandWithUnintendedSideEffect - silent! %s#\($\n\)\+\%$## - " vint: +ProhibitCommandRelyOnUser +ProhibitCommandWithUnintendedSideEffect - let @/=search - call winrestview(view) - - " 3. write new data to buffer - call setline(1, stdout) - call neoformat#utils#msg(cmd.name . ' formatted buffer') + call neoformat#utils#log(stdin) + call neoformat#utils#log(stdout) + if !v:shell_error + if stdout != stdin + " 1. set lines to '' aka \n from end of file when new data < old data + let datalen = len(stdout) + + while datalen <= line('$') + call setline(datalen, '') + let datalen += 1 + endwhile + + " 2. remove extra newlines at the end of the file + let search = @/ + let view = winsaveview() + " http://stackoverflow.com/a/7496112/3720597 + " vint: -ProhibitCommandRelyOnUser -ProhibitCommandWithUnintendedSideEffect + silent! %s#\($\n\)\+\%$## + " vint: +ProhibitCommandRelyOnUser +ProhibitCommandWithUnintendedSideEffect + let @/=search + call winrestview(view) + + " 3. write new data to buffer + call setline(1, stdout) + call neoformat#utils#msg(cmd.name . ' formatted buffer') + else + call neoformat#utils#msg('no change necessary with ' . cmd.name) + endif else - call neoformat#utils#msg('no change necessary with ' . cmd.name) + call neoformat#utils#log(v:shell_error) + continue endif - else - call neoformat#utils#log(v:shell_error) - return neoformat#NextNeoformat() - endif + endfor endfunction function! s:get_enabled_formatters(filetype) abort @@ -112,12 +103,6 @@ function! neoformat#CompleteFormatters(ArgLead, CmdLine, CursorPos) abort \ "v:val =~? '^" . a:ArgLead ."'") endfunction -function! neoformat#NextNeoformat() abort - call neoformat#utils#log('trying next formatter') - let s:current_formatter_index += 1 - return neoformat#Neoformat('') -endfunction - function! s:autoload_func_exists(func_name) abort try call eval(a:func_name . '()') diff --git a/plugin/neoformat.vim b/plugin/neoformat.vim index 5466fb4b..4adf9835 100644 --- a/plugin/neoformat.vim +++ b/plugin/neoformat.vim @@ -1,2 +1,2 @@ command! -nargs=? -bar -complete=customlist,neoformat#CompleteFormatters Neoformat - \ call neoformat#Start() + \ call neoformat#Neoformat() diff --git a/test/vimrc b/test/vimrc index e6e64275..39432fe2 100644 --- a/test/vimrc +++ b/test/vimrc @@ -6,3 +6,4 @@ syntax enable autocmd! filetype css set shiftwidth=2 autocmd! filetype py set shiftwidth=4 set nocompatible +set noswapfile