1

I have a tricky problem and don't know how to solve it. I use map to declare a shortcut key for the gradle test below:

map <C-S-F10> <Esc>:wa<CR>:let last_test = expand("%")<CR><S-F10>
map <S-F10> <Esc>:wa<CR>:call OpenWindow('tests')<CR>:%d<CR>:call RunTests(last_test)<CR>

fun! RunTests(...)
  if(a:0 == 0)
    silent .!./gradlew test
  else
    execute "silent" ".!./gradlew test --tests '*".a:1."'"
  endif
endfun

fun! OpenWindow(bufname)
  let winid = bufwinid(a:bufname)
  if(winid != -1) "buff window is not closed
    call win_gotoid(winid)
    return
  endif

  let absent = ! bufexists(a:bufname)
  execute "new ".a:bufname
  "move window to bottom
  let n = winnr('$') - winnr()
  execute n.'wincmd r'

  if(absent) "set options & map for new buffer
    set bufhidden=hide buftype=nofile nobuflisted noswapfile
    map <buffer> q <Esc>:q<CR>
  endif

endfun

When I press <C-S-F10> this behavior is what I want:

  • First, if the tests buffer does not exist, then create a new buffer named tests, otherwise open/focus the window for the existing tests buffer.

  • Next, clear the last test result in tests buffer by %d

  • Finally, execute RunTests and fill the result of !./gradlew test into tests buffer.

But when I went to refactor the mapping as below, the behavior is not what I want. All of the steps above will not be done until the bash script is done. How can I make my OpenRunTests execute the behavior like the mapping?

map <C-F10> <Esc>:wa<CR>:silent OpenWinAndRunTests()<CR>

fun! OpenWinAndRunTests()
  call OpenWindow("tests")
  %d  "clear buffer is not done until the shell command finished, how can I fix it?
  call RunTests(g:last_test)
endfun

2 Answers 2

1

I would recommend using a full range with you filter command.

%!./gradlew test

However I have some other thoughts:

  • You may want to look into use :make and the quickfix list (gradle compiler plugin)
  • You could :bwipe the buffer and opening a new one
  • I would recommend supplying a mode with your map's. e.g. nmap (Use noremap if possible)
  • May want to use wincmd J instead of rotating the window down

Personally I would recommend you look into :make and the quickfix list. Using :make allows for you to collect your errors up into a list known as the quickfix list. Open the quickfix list in a window via :copen. Using the quickfix you can navigate between the errors in your files, so you are jumping right to where the compiler/linter/test runner says the error is (handy right?).

For more help see:

:h :range
:h :make
:h quickfix
:h :compiler
:h 'errorformat'
:h 'makeprg'
:h :copen
:h :wincmd
:h CTRL-W_J
:h :bw
1
  • Hi, I want to show it in vim console, because I can use vim features. e.g: search, copy and .etc. and it is quickly return to my workshop when I press down q. and then I can write my own syntax for my tests to highlight the test result.
    – holi-java
    Sep 6, 2017 at 20:09
1

The trick is whenever execute bash command will prevent vim redrawing screen. Actually, it wasn't blocked until the bash command done. So I only need to redraw the screen before execute bash. for example:

"    v--- redraw the screen before run `./gradlew test`
%d | redraw | silent .!./gradlew test

The advantage of clear buffer before execute hash is that I can know exactly that the bash whether is done, since I only can see a blank buffer view when the bash is in progress.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.