Merge pull request #255 from Chiel92/dev
Inspect returncode of formatter process
This commit is contained in:
commit
db17ccbacb
@ -351,9 +351,11 @@ contact me by creating an issue in this repository.
|
|||||||
|
|
||||||
## Major Change Log
|
## 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
|
### March 2016
|
||||||
* We don't use the option formatprg internally anymore, to always have the possible of using the default `gq`
|
* We don't use the option formatprg internally anymore, to always have the possible of using the default `gq` command.
|
||||||
command.
|
|
||||||
* More fallback options have been added.
|
* More fallback options have been added.
|
||||||
|
|
||||||
### June 2015
|
### June 2015
|
||||||
|
@ -185,7 +185,7 @@ function! s:TryFormatterPython()
|
|||||||
let verbose = &verbose || g:autoformat_verbosemode == 1
|
let verbose = &verbose || g:autoformat_verbosemode == 1
|
||||||
|
|
||||||
python << EOF
|
python << EOF
|
||||||
import vim, subprocess, os, platform
|
import vim, subprocess, os
|
||||||
from subprocess import Popen, PIPE
|
from subprocess import Popen, PIPE
|
||||||
text = os.linesep.join(vim.current.buffer[:]) + os.linesep
|
text = os.linesep.join(vim.current.buffer[:]) + os.linesep
|
||||||
formatprg = vim.eval('b:formatprg')
|
formatprg = vim.eval('b:formatprg')
|
||||||
@ -194,8 +194,7 @@ verbose = bool(int(vim.eval('verbose')))
|
|||||||
env = os.environ.copy()
|
env = os.environ.copy()
|
||||||
if int(vim.eval('exists("g:formatterpath")')):
|
if int(vim.eval('exists("g:formatterpath")')):
|
||||||
extra_path = vim.eval('g:formatterpath')
|
extra_path = vim.eval('g:formatterpath')
|
||||||
pathsep = ';' if platform.system() == 'Windows' else ':'
|
env['PATH'] = os.pathsep.join(extra_path) + os.pathsep + env['PATH']
|
||||||
env['PATH'] = pathsep.join(extra_path) + pathsep + env['PATH']
|
|
||||||
|
|
||||||
# When an entry is unicode, Popen can't deal with it in Python 2.
|
# When an entry is unicode, Popen can't deal with it in Python 2.
|
||||||
# As a pragmatic fix, we'll omit that entry.
|
# 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)
|
p = subprocess.Popen(formatprg, env=env, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
|
||||||
stdoutdata, stderrdata = p.communicate(text)
|
stdoutdata, stderrdata = p.communicate(text)
|
||||||
|
|
||||||
|
formattername = vim.eval('b:formatters[s:index]')
|
||||||
if stderrdata:
|
if stderrdata:
|
||||||
if verbose:
|
if verbose:
|
||||||
formattername = vim.eval('b:formatters[s:index]')
|
|
||||||
print('Formatter {} has errors: {}'.format(formattername, stderrdata))
|
print('Formatter {} has errors: {}'.format(formattername, stderrdata))
|
||||||
vim.command('return 1')
|
vim.command('return 1')
|
||||||
|
elif p.returncode > 0:
|
||||||
|
if verbose:
|
||||||
|
print('Formatter {} gives nonzero returncode: {}'.format(formattername, p.returncode))
|
||||||
else:
|
else:
|
||||||
# It is not certain what kind of line endings are being used by the format program.
|
# 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.
|
# Therefore we simply split on all possible eol characters.
|
||||||
@ -242,7 +244,7 @@ function! s:TryFormatterPython3()
|
|||||||
let verbose = &verbose || g:autoformat_verbosemode == 1
|
let verbose = &verbose || g:autoformat_verbosemode == 1
|
||||||
|
|
||||||
python3 << EOF
|
python3 << EOF
|
||||||
import vim, subprocess, os, platform
|
import vim, subprocess, os
|
||||||
from subprocess import Popen, PIPE
|
from subprocess import Popen, PIPE
|
||||||
|
|
||||||
# The return code is `failure`, unless otherwise specified
|
# The return code is `failure`, unless otherwise specified
|
||||||
@ -254,8 +256,7 @@ verbose = bool(int(vim.eval('verbose')))
|
|||||||
env = os.environ.copy()
|
env = os.environ.copy()
|
||||||
if int(vim.eval('exists("g:formatterpath")')):
|
if int(vim.eval('exists("g:formatterpath")')):
|
||||||
extra_path = vim.eval('g:formatterpath')
|
extra_path = vim.eval('g:formatterpath')
|
||||||
pathsep = ';' if platform.system() == 'Windows' else ':'
|
env['PATH'] = os.pathsep.join(extra_path) + os.pathsep + env['PATH']
|
||||||
env['PATH'] = pathsep.join(extra_path) + pathsep + env['PATH']
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
p = subprocess.Popen(formatprg, env=env, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
|
p = subprocess.Popen(formatprg, env=env, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
|
||||||
@ -264,10 +265,13 @@ except (BrokenPipeError, IOError):
|
|||||||
if verbose:
|
if verbose:
|
||||||
raise
|
raise
|
||||||
else:
|
else:
|
||||||
|
formattername = vim.eval('b:formatters[s:index]')
|
||||||
if stderrdata:
|
if stderrdata:
|
||||||
if verbose:
|
if verbose:
|
||||||
formattername = vim.eval('b:formatters[s:index]')
|
|
||||||
print('Formatter {} has errors: {}'.format(formattername, stderrdata))
|
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:
|
elif not stdoutdata:
|
||||||
if verbose:
|
if verbose:
|
||||||
print('Formatter {} gives empty result: {}'.format(formattername, stderrdata))
|
print('Formatter {} gives empty result: {}'.format(formattername, stderrdata))
|
||||||
@ -299,6 +303,7 @@ endfunction
|
|||||||
|
|
||||||
" Create a command for formatting the entire buffer
|
" Create a command for formatting the entire buffer
|
||||||
" Save and recall window state to prevent vim from jumping to line 1
|
" 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
|
command! -nargs=? -range=% -complete=filetype -bar Autoformat let winview=winsaveview()|wviminfo|<line1>,<line2>call s:TryAllFormatters(<f-args>)|call winrestview(winview)|rviminfo
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user