Skip to content

Commit

Permalink
debug: add :GoDebugTestFunc
Browse files Browse the repository at this point in the history
Closes fatih#2931
  • Loading branch information
bhcleek committed Sep 7, 2020
1 parent 9b9b527 commit b524372
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 22 deletions.
13 changes: 11 additions & 2 deletions autoload/go/debug.vim
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,15 @@ function! s:handleRPCResult(resp) abort
endtry
endfunction

function! go#debug#TestFunc(...) abort
let l:test = go#util#TestName()
if l:test is ''
call go#util#Warn("vim-go: [debug] no test found immediate to cursor")
return
endif
call call('go#debug#Start', extend(['test', '.', '-test.run', printf('%s$', l:testname)], a:000))
endfunction

" Start the debug mode. The first argument is the package name to compile and
" debug, anything else will be passed to the running program.
function! go#debug#Start(mode, ...) abort
Expand Down Expand Up @@ -770,9 +779,9 @@ endfunction

" s:package returns the import path of package name of a :GoDebug(Start|Test)
" call as a list so that the package can be appended to a command list using
" extend(). args is expected to be a (potentially empty_ list. The first
" extend(). args is expected to be a (potentially empty) list. The first
" element in args (if there are any) is expected to be a package path. An
" emnpty list is returned when either args is an empty list or the import path
" empty list is returned when either args is an empty list or the import path
" cannot be determined.
function! s:package(args)
if len(a:args) == 0
Expand Down
20 changes: 4 additions & 16 deletions autoload/go/test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,11 @@ endfunction
" Testfunc runs a single test that surrounds the current cursor position.
" Arguments are passed to the `go test` command.
function! go#test#Func(bang, ...) abort
" search flags legend (used only)
" 'b' search backward instead of forward
" 'c' accept a match at the cursor position
" 'n' do Not move the cursor
" 'W' don't wrap around the end of the file
"
" for the full list
" :help search
let test = search('func \(Test\|Example\)', "bcnW")

if test == 0
echo "vim-go: [test] no test found immediate to cursor"
let l:test = go#util#TestName()
if l:test is ''
call go#util#EchoWarning("[test] no test found immediate to cursor")
return
end

let line = getline(test)
let name = split(split(line, " ")[1], "(")[0]
endif
let args = [a:bang, 0, "-run", name . "$"]

if a:0
Expand Down
26 changes: 22 additions & 4 deletions autoload/go/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -704,10 +704,28 @@ function! go#util#Chdir(dir) abort
if !exists('*chdir')
let l:olddir = getcwd()
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
execute cd . fnameescape(a:dir)
return l:olddir
endif
return chdir(a:dir)
execute cd . fnameescape(a:dir) return l:olddir endif return chdir(a:dir)
endfunction

" go#util#TestName returns the name of the test function that preceeds the
" cursor.
function go#util#TestName() abort
" search flags legend (used only)
" 'b' search backward instead of forward
" 'c' accept a match at the cursor position
" 'n' do Not move the cursor
" 'W' don't wrap around the end of the file
"
" for the full list
" :help search
let l:line = search('func \(Test\|Example\)', "bcnW")

if l:line == 0
return ''
end

let l:decl = getline(l:line)
return split(split(l:decl, " ")[1], "(")[0]
endfunction

" restore Vi compatibility settings
Expand Down
8 changes: 8 additions & 0 deletions doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2321,6 +2321,14 @@ The rest of the commands and mappings become available after executing
Use `-test.flag` to pass flags to `go test` when debugging a test; for
example `-test.v` or `-test.run TestFoo`

*:GoDebugTestFunc*
:GoDebugTestFunc [expand]

Behaves the same as |:GoDebugTest| and implicitly adds `-test.run` to run
the nearest test or example function (i.e the nearest function declaration
that matches `func Test` or `func Example`) at or previous to the cursor.
Search will not wrap around when at the top of the file.

*:GoDebugRestart*
:GoDebugRestart

Expand Down
1 change: 1 addition & 0 deletions ftplugin/go/commands.vim
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ command! -nargs=0 GoFillStruct call go#fillstruct#FillStruct()
if !exists(':GoDebugStart')
command! -nargs=* -complete=customlist,go#package#Complete GoDebugStart call go#debug#Start('debug', <f-args>)
command! -nargs=* -complete=customlist,go#package#Complete GoDebugTest call go#debug#Start('test', <f-args>)
command! -nargs=* GoDebugTestFunc call go#debug#TestFunc(<f-args>)
command! -nargs=1 GoDebugAttach call go#debug#Start('attach', <f-args>)
command! -nargs=? GoDebugBreakpoint call go#debug#Breakpoint(<f-args>)
endif
Expand Down

0 comments on commit b524372

Please sign in to comment.