Merge pull request #255 from Chiel92/dev

Inspect returncode of formatter process
This commit is contained in:
Chiel ten Brinke 2018-10-22 13:52:44 +02:00 committed by GitHub
commit db17ccbacb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 10 deletions

View File

@ -351,9 +351,11 @@ contact me by creating an issue in this repository.
## Major Change Log
### October 2018
* We also take the returncode of the formatter process into account, not just the presence of output on stderr.
### March 2016
* We don't use the option formatprg internally anymore, to always have the possible of using the default `gq`
command.
* We don't use the option formatprg internally anymore, to always have the possible of using the default `gq` command.
* More fallback options have been added.
### June 2015

View File

@ -185,7 +185,7 @@ function! s:TryFormatterPython()
let verbose = &verbose || g:autoformat_verbosemode == 1
python << EOF
import vim, subprocess, os, platform
import vim, subprocess, os
from subprocess import Popen, PIPE
text = os.linesep.join(vim.current.buffer[:]) + os.linesep
formatprg = vim.eval('b:formatprg')
@ -194,8 +194,7 @@ verbose = bool(int(vim.eval('verbose')))
env = os.environ.copy()
if int(vim.eval('exists("g:formatterpath")')):
extra_path = vim.eval('g:formatterpath')
pathsep = ';' if platform.system() == 'Windows' else ':'
env['PATH'] = pathsep.join(extra_path) + pathsep + env['PATH']
env['PATH'] = os.pathsep.join(extra_path) + os.pathsep + env['PATH']
# When an entry is unicode, Popen can't deal with it in Python 2.
# As a pragmatic fix, we'll omit that entry.
@ -207,11 +206,14 @@ env=newenv
p = subprocess.Popen(formatprg, env=env, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdoutdata, stderrdata = p.communicate(text)
formattername = vim.eval('b:formatters[s:index]')
if stderrdata:
if verbose:
formattername = vim.eval('b:formatters[s:index]')
print('Formatter {} has errors: {}'.format(formattername, stderrdata))
vim.command('return 1')
elif p.returncode > 0:
if verbose:
print('Formatter {} gives nonzero returncode: {}'.format(formattername, p.returncode))
else:
# It is not certain what kind of line endings are being used by the format program.
# Therefore we simply split on all possible eol characters.
@ -242,7 +244,7 @@ function! s:TryFormatterPython3()
let verbose = &verbose || g:autoformat_verbosemode == 1
python3 << EOF
import vim, subprocess, os, platform
import vim, subprocess, os
from subprocess import Popen, PIPE
# The return code is `failure`, unless otherwise specified
@ -254,8 +256,7 @@ verbose = bool(int(vim.eval('verbose')))
env = os.environ.copy()
if int(vim.eval('exists("g:formatterpath")')):
extra_path = vim.eval('g:formatterpath')
pathsep = ';' if platform.system() == 'Windows' else ':'
env['PATH'] = pathsep.join(extra_path) + pathsep + env['PATH']
env['PATH'] = os.pathsep.join(extra_path) + os.pathsep + env['PATH']
try:
p = subprocess.Popen(formatprg, env=env, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
@ -264,10 +265,13 @@ except (BrokenPipeError, IOError):
if verbose:
raise
else:
formattername = vim.eval('b:formatters[s:index]')
if stderrdata:
if verbose:
formattername = vim.eval('b:formatters[s:index]')
print('Formatter {} has errors: {}'.format(formattername, stderrdata))
elif p.returncode > 0:
if verbose:
print('Formatter {} gives nonzero returncode: {}'.format(formattername, p.returncode))
elif not stdoutdata:
if verbose:
print('Formatter {} gives empty result: {}'.format(formattername, stderrdata))
@ -299,6 +303,7 @@ endfunction
" Create a command for formatting the entire buffer
" Save and recall window state to prevent vim from jumping to line 1
" Write and read viminfo to restore marks
command! -nargs=? -range=% -complete=filetype -bar Autoformat let winview=winsaveview()|wviminfo|<line1>,<line2>call s:TryAllFormatters(<f-args>)|call winrestview(winview)|rviminfo