1
0
Fork 0
mirror of synced 2024-11-27 11:15:35 -05:00
ultimate-vim/sources_non_forked/vim-go/autoload/go/oracle.vim

239 lines
7.8 KiB
VimL
Raw Normal View History

2014-10-31 17:30:24 -04:00
" oracle.vim -- Vim integration for the Go oracle.
"
" Part of this plugin was taken directly from the oracle repo, however it's
" massively changed for a better integration into vim-go. Thanks Alan Donovan
2015-01-18 07:58:28 -05:00
" for the first iteration based on quickfix! - Fatih Arslan
2014-10-31 17:30:24 -04:00
"
if !exists("g:go_oracle_bin")
let g:go_oracle_bin = "oracle"
endif
2015-02-24 05:45:22 -05:00
" Parses (via regex) Oracle's 'plain' format output and puts them into a
2015-12-08 08:20:04 -05:00
" location list
func! s:loclist(output)
let llist = []
2014-10-31 17:30:24 -04:00
" Parse GNU-style 'file:line.col-line.col: message' format.
let mx = '^\(\a:[\\/][^:]\+\|[^:]\+\):\(\d\+\):\(\d\+\):\(.*\)$'
for line in split(a:output, "\n")
let ml = matchlist(line, mx)
2015-02-24 05:45:22 -05:00
2014-10-31 17:30:24 -04:00
" Ignore non-match lines or warnings
if ml == [] || ml[4] =~ '^ warning:'
continue
endif
2015-02-24 05:45:22 -05:00
2014-10-31 17:30:24 -04:00
let item = {
\ 'filename': ml[1],
\ 'text': ml[4],
\ 'lnum': ml[2],
\ 'col': ml[3],
\}
let bnr = bufnr(fnameescape(ml[1]))
if bnr != -1
let item['bufnr'] = bnr
endif
2015-12-08 08:20:04 -05:00
call add(llist, item)
2014-10-31 17:30:24 -04:00
endfor
2016-02-17 14:02:39 -05:00
call go#list#Populate("locationlist", llist)
call go#list#Window("locationlist", len(llist))
2014-10-31 17:30:24 -04:00
endfun
2015-02-24 05:45:22 -05:00
" This uses Vim's errorformat to parse the output from Oracle's 'plain output
2015-12-08 08:20:04 -05:00
" and put it into location list. I believe using errorformat is much more
2015-02-24 05:45:22 -05:00
" easier to use. If we need more power we can always switch back to parse it
" via regex.
2015-12-08 08:20:04 -05:00
func! s:loclistSecond(output)
2015-02-24 05:45:22 -05:00
" backup users errorformat, will be restored once we are finished
2015-07-13 06:22:46 -04:00
let old_errorformat = &errorformat
2015-02-24 05:45:22 -05:00
" match two possible styles of errorformats:
"
" 'file:line.col-line2.col2: message'
" 'file:line:col: message'
"
" We discard line2 and col2 for the first errorformat, because it's not
2015-12-08 08:20:04 -05:00
" useful and location only has the ability to show one line and column
2015-02-24 05:45:22 -05:00
" number
2015-12-08 08:20:04 -05:00
let errformat = "%f:%l.%c-%[%^:]%#:\ %m,%f:%l:%c:\ %m"
2016-02-17 14:02:39 -05:00
call go#list#ParseFormat("locationlist", errformat, split(a:output, "\n"))
2015-02-24 05:45:22 -05:00
2016-02-17 14:02:39 -05:00
let errors = go#list#Get("locationlist")
call go#list#Window("locationlist", len(errors))
2015-02-24 05:45:22 -05:00
endfun
2014-10-31 17:30:24 -04:00
func! s:getpos(l, c)
if &encoding != 'utf-8'
let buf = a:l == 1 ? '' : (join(getline(1, a:l-1), "\n") . "\n")
let buf .= a:c == 1 ? '' : getline('.')[:a:c-2]
return len(iconv(buf, &encoding, 'utf-8'))
endif
return line2byte(a:l) + (a:c-2)
endfun
2015-12-08 08:20:04 -05:00
func! s:RunOracle(mode, selected, needs_package) range abort
2014-10-31 17:30:24 -04:00
let fname = expand('%:p')
let dname = expand('%:p:h')
let pkg = go#package#ImportPath(dname)
2015-02-24 05:45:22 -05:00
if exists('g:go_oracle_scope')
" let the user defines the scope, must be a space separated string,
" example: 'fmt math net/http'
2015-12-08 08:20:04 -05:00
let scopes = split(get(g:, 'go_oracle_scope'))
elseif a:needs_package || exists('g:go_oracle_include_tests') && pkg != -1
2014-10-31 17:30:24 -04:00
" give import path so it includes all _test.go files too
2015-12-08 08:20:04 -05:00
let scopes = [pkg]
2014-10-31 17:30:24 -04:00
else
" best usable way, only pass the package itself, without the test
" files
2015-02-24 05:45:22 -05:00
let scopes = go#tool#Files()
2014-10-31 17:30:24 -04:00
endif
"return with a warning if the bin doesn't exist
2015-07-13 06:22:46 -04:00
let bin_path = go#path#CheckBinPath(g:go_oracle_bin)
2014-10-31 17:30:24 -04:00
if empty(bin_path)
return
endif
2015-12-08 08:20:04 -05:00
if exists('g:go_oracle_tags')
let tags = get(g:, 'go_oracle_tags')
else
let tags = ""
endif
2014-10-31 17:30:24 -04:00
if a:selected != -1
let pos1 = s:getpos(line("'<"), col("'<"))
let pos2 = s:getpos(line("'>"), col("'>"))
2015-12-08 08:20:04 -05:00
let cmd = printf('%s -format plain -pos=%s:#%d,#%d -tags=%s %s',
2014-10-31 17:30:24 -04:00
\ bin_path,
2015-12-08 08:20:04 -05:00
\ shellescape(fname), pos1, pos2, tags, a:mode)
2014-10-31 17:30:24 -04:00
else
let pos = s:getpos(line('.'), col('.'))
2015-12-08 08:20:04 -05:00
let cmd = printf('%s -format plain -pos=%s:#%d -tags=%s %s',
2014-10-31 17:30:24 -04:00
\ bin_path,
2015-12-08 08:20:04 -05:00
\ shellescape(fname), pos, tags, a:mode)
2014-10-31 17:30:24 -04:00
endif
2015-02-24 05:45:22 -05:00
" now append each scope to the end as Oracle's scope parameter. It can be
" a packages or go files, dependent on the User's own choice. For more
" info check Oracle's User Manual section about scopes:
" https://docs.google.com/document/d/1SLk36YRjjMgKqe490mSRzOPYEDe0Y_WQNRv-EiFYUyw/view#heading=h.nwso96pj07q8
2015-12-08 08:20:04 -05:00
let cmd .= ' ' . go#util#Shelljoin(scopes)
2015-02-24 05:45:22 -05:00
2014-10-31 17:30:24 -04:00
echon "vim-go: " | echohl Identifier | echon "analysing ..." | echohl None
2015-07-13 06:22:46 -04:00
let old_gopath = $GOPATH
let $GOPATH = go#path#Detect()
2014-10-31 17:30:24 -04:00
let out = system(cmd)
2015-07-13 06:22:46 -04:00
let $GOPATH = old_gopath
2014-10-31 17:30:24 -04:00
if v:shell_error
" unfortunaly oracle outputs a very long stack trace that is not
" parsable to show the real error. But the main issue is usually the
" package which doesn't build.
redraw | echon "vim-go: " | echohl Statement | echon out | echohl None
2015-02-24 05:45:22 -05:00
return ""
2015-07-13 06:22:46 -04:00
endif
2015-02-24 05:45:22 -05:00
return out
2015-07-13 06:22:46 -04:00
endfunc
2014-10-31 17:30:24 -04:00
2015-07-13 06:22:46 -04:00
function! go#oracle#Scope(...)
2015-12-08 08:20:04 -05:00
if a:0
if a:0 == 1 && a:1 == '""'
unlet g:go_oracle_scope
2015-07-13 06:22:46 -04:00
echon "vim-go: " | echohl Function | echon "oracle scope is cleared"| echohl None
else
let g:go_oracle_scope = join(a:000, ' ')
echon "vim-go: " | echohl Function | echon "oracle scope changed to: '". g:go_oracle_scope ."'" | echohl None
endif
return
endif
2015-12-08 08:20:04 -05:00
if !exists('g:go_oracle_scope')
2015-07-13 06:22:46 -04:00
echon "vim-go: " | echohl Function | echon "oracle scope is not set"| echohl None
else
echon "vim-go: " | echohl Function | echon "current oracle scope: '". g:go_oracle_scope ."'" | echohl None
endif
endfunction
2014-10-31 17:30:24 -04:00
2015-12-08 08:20:04 -05:00
function! go#oracle#Tags(...)
if a:0
if a:0 == 1 && a:1 == '""'
unlet g:go_oracle_tags
echon "vim-go: " | echohl Function | echon "oracle tags is cleared"| echohl None
else
let g:go_oracle_tags = a:1
echon "vim-go: " | echohl Function | echon "oracle tags changed to: '". g:go_oracle_tags ."'" | echohl None
endif
return
endif
if !exists('g:go_oracle_tags')
echon "vim-go: " | echohl Function | echon "oracle tags is not set"| echohl None
else
echon "vim-go: " | echohl Function | echon "current oracle tags: '". g:go_oracle_tags ."'" | echohl None
endif
endfunction
2014-10-31 17:30:24 -04:00
" Show 'implements' relation for selected package
function! go#oracle#Implements(selected)
2015-12-08 08:20:04 -05:00
let out = s:RunOracle('implements', a:selected, 0)
call s:loclistSecond(out)
2014-10-31 17:30:24 -04:00
endfunction
" Describe selected syntax: definition, methods, etc
function! go#oracle#Describe(selected)
2015-12-08 08:20:04 -05:00
let out = s:RunOracle('describe', a:selected, 0)
call s:loclistSecond(out)
2014-10-31 17:30:24 -04:00
endfunction
" Show possible targets of selected function call
function! go#oracle#Callees(selected)
2015-12-08 08:20:04 -05:00
let out = s:RunOracle('callees', a:selected, 1)
call s:loclistSecond(out)
2014-10-31 17:30:24 -04:00
endfunction
" Show possible callers of selected function
function! go#oracle#Callers(selected)
2015-12-08 08:20:04 -05:00
let out = s:RunOracle('callers', a:selected, 1)
call s:loclistSecond(out)
2014-10-31 17:30:24 -04:00
endfunction
" Show path from callgraph root to selected function
function! go#oracle#Callstack(selected)
2015-12-08 08:20:04 -05:00
let out = s:RunOracle('callstack', a:selected, 1)
call s:loclistSecond(out)
2014-10-31 17:30:24 -04:00
endfunction
" Show free variables of selection
function! go#oracle#Freevars(selected)
2015-12-08 08:20:04 -05:00
" Freevars requires a selection
if a:selected == -1
echon "vim-go: " | echohl Statement | echon "GoFreevars requires a selection (range) of code "| echohl None
return
endif
let out = s:RunOracle('freevars', a:selected, 0)
call s:loclistSecond(out)
2014-10-31 17:30:24 -04:00
endfunction
" Show send/receive corresponding to selected channel op
2015-02-24 05:45:22 -05:00
function! go#oracle#ChannelPeers(selected)
2015-12-08 08:20:04 -05:00
let out = s:RunOracle('peers', a:selected, 1)
call s:loclistSecond(out)
2014-10-31 17:30:24 -04:00
endfunction
" Show all refs to entity denoted by selected identifier
function! go#oracle#Referrers(selected)
2015-12-08 08:20:04 -05:00
let out = s:RunOracle('referrers', a:selected, 0)
call s:loclistSecond(out)
2014-10-31 17:30:24 -04:00
endfunction
" vim:ts=4:sw=4:et
2015-07-13 06:22:46 -04:00
"