Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim w/Python: Make ":make" take me to the error

Tags:

python

vim

If I have a python file like:

  def Bar():
      raise NotImplementedError

  def Foo():
      Bar()

  if __name__ == '__main__':
      Foo()

And I type :make in vim, it nicely builds me a :cwindow filled with the relevant areas to move up the traceback.

However, it defaults my cursor to the first frame of the call (in name == 'main'). Can I somehow change the default behaviour, so it takes me to the actual call of the exception?

-- Update -- Answering Ingo's question:

:makeprg/errorformat are set to default for the gentoo install. That is:

makeprg=python %
errorformat=%A  File "%f"\, line %l%.%#,%Z%[%^ ]%\@=%m 

The stacktrace in the quickfix window looks like such:

    main.py
     1 || Traceback (most recent call last):
     2 main.py|8|
     3 ||     Foo()
     4 main.py|5|
     5 ||     Bar()
     6 main.py|2|
     7 ||     raise NotImplementedError
     8 || NotImplementedError

Spoiled brat that I am, I'd love it if I started at the 'raise' (line 7) and could :cp 'backwards' as needed.

like image 837
Eddie Parker Avatar asked Jan 30 '14 16:01

Eddie Parker


1 Answers

This code will navigate to innermost exception.

function! s:InnermostExceptionInQFList()
  let s:num = 0
  for item in getqflist()
    if item.lnum > 0
      let s:num += 1
    endif
  endfor
  if s:num > 0
    try
      silent execute(s:num . 'cnext')
    catch /E553:/
      " E553: No more elements
    endtry
  endif
  silent execute('wincmd w')
endfunction

autocmd! QuickfixCmdPost * call s:InnermostExceptionInQFList()
like image 163
P̲̳x͓L̳ Avatar answered Sep 20 '22 23:09

P̲̳x͓L̳