Skip to content

Commit

Permalink
Redirect stderr to log file by default (#174)
Browse files Browse the repository at this point in the history
* Redirect stderr to log file by default, fixes #172

* Echom stderr output of formatter if verbose logging is set on

* Document new ‘stderr’ flag

* Remove stderr redirections from definitions, as they are added automatically now
  • Loading branch information
fenuks authored and sbdchd committed Sep 4, 2018
1 parent 1c27419 commit 8988987
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Options:
| `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 |
| `stderr` | capture stderr output from 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 |
| `env` | list of environment variable definitions to be prepended to the formatter command | \[] | optional |
| `valid_exit_codes` | list of valid exit codes for formatters who do not respect common unix practices | \[0] | optional |
Expand Down
17 changes: 17 additions & 0 deletions autoload/neoformat.vim
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ function! s:neoformat(bang, user_input, start_line, end_line) abort
call neoformat#utils#log(v:shell_error)

let process_ran_succesfully = index(cmd.valid_exit_codes, v:shell_error) != -1

if cmd.stderr_log != ''
call neoformat#utils#log('stderr output redirected to file' . cmd.stderr_log)
call neoformat#utils#log_file_content(cmd.stderr_log)
endif
if process_ran_succesfully
" 1. append the lines that are before and after the formatterd content
let lines_after = getbufline(bufnr('%'), a:end_line + 1, '$')
Expand Down Expand Up @@ -229,6 +234,8 @@ function! s:generate_cmd(definition, filetype) abort

let no_append = get(a:definition, 'no_append', 0)
let using_stdin = get(a:definition, 'stdin', 0)
let using_stderr = get(a:definition, 'stderr', 0)
let stderr_log = ''

let filename = expand('%:t')

Expand All @@ -250,10 +257,20 @@ function! s:generate_cmd(definition, filetype) abort
let _fullcmd = join(inline_environment, ' ') . ' ' . executable . ' ' . join(args_expanded) . ' ' . (no_append ? '' : path)
" make sure there aren't any double spaces in the cmd
let fullcmd = join(split(_fullcmd))
if !using_stderr
if neoformat#utils#should_be_verbose()
let stderr_log = expand(tmp_dir . '/stderr.log')
let fullcmd = fullcmd . ' 2> ' . stderr_log
else
let stderr_log = ''
let fullcmd = fullcmd . ' 2> ' . '/dev/null'
endif
endif

return {
\ 'exe': fullcmd,
\ 'stdin': using_stdin,
\ 'stderr_log': stderr_log,
\ 'name': a:definition.exe,
\ 'replace': get(a:definition, 'replace', 0),
\ 'tmp_file_path': path,
Expand Down
1 change: 0 additions & 1 deletion autoload/neoformat/formatters/haskell.vim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ endfunction
function! neoformat#formatters#haskell#stylishhaskell() abort
return {
\ 'exe': 'stylish-haskell',
\ 'args': ['2>/dev/null'],
\ 'stdin': 1,
\ }
endfunction
Expand Down
2 changes: 1 addition & 1 deletion autoload/neoformat/formatters/python.vim
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function! neoformat#formatters#python#black() abort
return {
\ 'exe': 'black',
\ 'stdin': 1,
\ 'args': ['-', '2>/dev/null'],
\ 'args': ['-'],
\ }
endfunction

Expand Down
1 change: 1 addition & 0 deletions autoload/neoformat/formatters/ruby.vim
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ function! neoformat#formatters#ruby#rubocop() abort
\ 'exe': 'rubocop',
\ 'args': ['--auto-correct', '--stdin', '%:p', '2>/dev/null', '|', 'sed "1,/^====================$/d"'],
\ 'stdin': 1,
\ 'stderr': 1
\ }
endfunction
18 changes: 14 additions & 4 deletions autoload/neoformat/utils.vim
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
function! neoformat#utils#log(msg) abort
if !exists('g:neoformat_verbose')
let g:neoformat_verbose = 0
endif
if &verbose || g:neoformat_verbose
if neoformat#utils#should_be_verbose()
return s:better_echo(a:msg)
endif
endfunction

function! neoformat#utils#log_file_content(path) abort
if neoformat#utils#should_be_verbose()
return s:better_echo(readfile(a:path))
endif
endfunction

function! neoformat#utils#warn(msg) abort
echohl WarningMsg | call s:better_echo(a:msg) | echohl NONE
endfunction
Expand All @@ -18,6 +21,13 @@ function! neoformat#utils#msg(msg) abort
return s:better_echo(a:msg)
endfunction

function! neoformat#utils#should_be_verbose() abort
if !exists('g:neoformat_verbose')
let g:neoformat_verbose = 0
endif
return &verbose || g:neoformat_verbose
endfunction

function! s:better_echo(msg) abort
if type(a:msg) != type('')
echom 'Neoformat: ' . string(a:msg)
Expand Down
3 changes: 3 additions & 0 deletions doc/neoformat.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ Options:
| `args` | list of arguments | default: [] | optional
| `replace` | overwrite the file, instead of updating the buffer | default: 0 | optional
| `stdin` | send data to the stdin of the formatter | default 0 | optional
| `stderr` | used to specify whether stderr output should be read along with
the stdin, otherwise redirects stderr to `stderr.log` file in neoformat's
temporary directory | default 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 | default: 0 |
optional
Expand Down

0 comments on commit 8988987

Please sign in to comment.