Skip to content

Commit

Permalink
Fix quickfix error formatting
Browse files Browse the repository at this point in the history
A proof of concept that fixes a few issues within the vim-flow type
checking output to the quickfix list.

- Each error spans over one line instead of four.
- Don't open the quickfix window unless there is at least one error. The
  previous behavior was to add a message called 'No errors!' in the
  quickfix list if there were no errors.
- We output json instead of a strange vim format from the flow compiler
  to easily assemble error messages ourselves. Requires vim8/neovim.
  • Loading branch information
danihodovic committed Dec 6, 2016
1 parent 610be19 commit 0d6870e
Showing 1 changed file with 43 additions and 35 deletions.
78 changes: 43 additions & 35 deletions plugin/flow.vim
Original file line number Diff line number Diff line change
Expand Up @@ -74,44 +74,52 @@ function! <SID>FlowClientCall(cmd, suffix)
return flow_result
endfunction

let s:errorformat_line = '%f:%l:%c%m'
function! Format_json_error_msg(error)
" Extract the file, line and column from the first message
let first_msg = a:error.message[0]
let str = printf('%s:%d:%d',
\first_msg.loc.source,
\first_msg.loc.start.line,
\first_msg.loc.start.column
\)
" The error message is most informative when all the message.descr are concatenated
for message in a:error.message
let str .= message.descr . ' '
endfor
return str
endfunction

" Main interface functions.
function! flow#typecheck()
let res = system('flow --from vim')
let arr = split(res, "\n")
echom arr[0]

" let &errorformat = 'File "%f"\, line %l\, characters %c'

let &errorformat = 'File "%f"\, line %l\, characters %c-%m'
cgetexpr arr[0] . "\n"


" let &errorformat = 'File "%f"\, %l %c'
" cgetexpr 'File "index.js", 2 5'
botright copen

return

" Flow current outputs errors to stderr and gets fancy with single character
" files
let flow_result = <SID>FlowClientCall('--timeout '.g:flow#timeout.' --retry-if-init false'.expand('%:p'), '2> /dev/null')
let old_fmt = &errorformat
let &errorformat = s:flow_errorformat


let flow_result = substitute(flow_result, "\n", " ", "g")
if g:flow#errjmp
cexpr flow_result
else
cgetexpr flow_result
let output = system('flow --json')
let dict = json_decode(output)

let lines = []
for error in dict.errors
let str = Format_json_error_msg(error)
let lines = add(lines, str)
endfor

" If there are no lines that means we have no errors :)
if len(lines) > 0
let old_errorformat = &errorformat
let &errorformat = s:errorformat_line

if g:flow#errjmp
cexpr lines
else
cgetexpr lines
endif

if g:flow#autoclose
botright cwindow
else
botright copen
endif

let &errorformat = old_errorformat
endif

if g:flow#autoclose
botright cwindow
else
botright copen
endif
let &errorformat = old_fmt
endfunction

" Get the Flow type at the current cursor position.
Expand Down

0 comments on commit 0d6870e

Please sign in to comment.