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

感情迁移 提交于 2020-01-13 08:49:33

问题


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.


回答1:


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()


来源:https://stackoverflow.com/questions/21461930/vim-w-python-make-make-take-me-to-the-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!