alfredodeza / pytest.vim

Runs your UnitTests with py.test displaying red/green bars and errors
274 stars 40 forks source link

Add documentation for tweaking jump behaviour #64

Closed JakobGM closed 6 years ago

JakobGM commented 6 years ago

Hi, thanks for the great plugin!

Glancing over the vimscript of this plugin, it is not instantly apparent to me how to tweak how pytest.vim jumps after executing :Pytest Class|Method|.... I would like to keep my cursor position after invoking pytest.vim.

I wondered if you could point me in the right direction for configuring pytest.vim for this use case. Naively, it could be done with a remap. Is this the recommended way, or are there any additional hooks I could use?

After knowing the best way to do this, I could write a PR to the documentation explaining how to tweak pytest.vim to behave in this manner, if you'd like.

Thanks!

alfredodeza commented 6 years ago

Do you mean when the red/green/yellow bar is displayed? There are other kinds of jumps like when trying to go to the origin of the failed test.

I think we try to keep the cursor in the same place, and I haven't noticed in what situation the cursor would not comply:

function! s:ThisClass(verbose, ...)
    let extra_flags = ''
    let save_cursor = getpos('.')
    call s:ClearAll()
    let c_name      = s:NameOfCurrentClass()
    let abspath     = s:CurrentPath()
    if (strlen(c_name) == 1)
        call setpos('.', save_cursor)
        call s:Echo("Unable to find a matching class for testing")
        return
    endif
    ...    
JakobGM commented 6 years ago

Do you mean when the red/green/yellow bar is displayed?

Yes, exactly!

I think we try to keep the cursor in the same place, and I haven't noticed in what situation the cursor would not comply.

Hmmm, might be something weird with my init.vim then, I will check if some other plugin/map is the cause for the jump. On my setup, the cursor jumps to the function definition.

If this proves to be caused by something external to pytest.vim, I'm sorry to have posted the issue!

JakobGM commented 6 years ago

Okay, I have now confirmed that I see the same behaviour on a minimal init.vim configuration:

set nocompatible
filetype off

call plug#begin('$XDG_CONFIG_HOME/nvim/plugged')
Plug 'alfredodeza/pytest.vim'
call plug#end()

let maplocalleader = ","
nmap <silent><LocalLeader>tf <Esc>:Pytest function<CR>

Before pressing ,tf or executing :Pytest function:

def test_foo():
    assert 1 + 1 == 2
           ^
           |

Movement after having pressed Enter to confirm the passing/failing test:

def test_foo():
^   assert 1 + 1 == 2
|

Here is the output of nvim --version:

NVIM v0.2.2
Build type: Release
LuaJIT 2.0.5
Compilation: /usr/bin/cc -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fno-plt -Wconversion -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -O2 -DNDEBUG -DMIN_LOG_LEVEL=3 -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wimplicit-fallthrough -Wvla -fstack-protector-strong -fdiagnostics-color=auto -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -I/build/neovim/src/build/config -I/build/neovim/src/neovim-0.2.2/src -I/usr/include -I/usr/include -I/usr/include -I/usr/include -I/usr/include -I/usr/include -I/usr/include -I/build/neovim/src/build/src/nvim/auto -I/build/neovim/src/build/include
Compiled by builduser

Features: +acl +iconv +jemalloc +tui 
alfredodeza commented 6 years ago

Aha, I've confirmed this annoying behavior in functions. I didn't noticed before because it doesn't happen on test methods. Will try and find why that is. Thanks for reporting it!

JakobGM commented 6 years ago

Great, thanks for your time! :smiley:

alfredodeza commented 6 years ago

Latest changes to master (including the tip at https://github.com/alfredodeza/pytest.vim/commit/f885af726b1398438710d02288925ab01027a8e1) should fix this problem for you. Please confirm so I can close this issue. Thanks again!

JakobGM commented 6 years ago

That has fixed the issue, you're awesome :tada:

Thanks!