Skip to content

debug: add :GoDebugTestFunc #3011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions autoload/go/debug.vim
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ function! go#debug#Stop() abort
endfor
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>)

Expand Down Expand Up @@ -689,6 +690,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:test)], 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 +780,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
22 changes: 5 additions & 17 deletions autoload/go/test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,12 @@ 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]
let args = [a:bang, 0, "-run", name . "$"]
endif
let args = [a:bang, 0, "-run", l:test . "$"]

if a:0
call extend(args, a:000)
Expand Down
21 changes: 21 additions & 0 deletions autoload/go/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,27 @@ function! go#util#Chdir(dir) abort
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 ''
endif

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

" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
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