2016-05-14 07:57:54 -04:00
|
|
|
" guru.vim -- Vim integration for the Go guru.
|
|
|
|
|
2017-02-11 08:01:38 -05:00
|
|
|
" guru_cmd returns a dict that contains the command to execute guru. args
|
2016-12-27 09:46:49 -05:00
|
|
|
" is dict with following options:
|
|
|
|
" mode : guru mode, such as 'implements'
|
|
|
|
" format : output format, either 'plain' or 'json'
|
|
|
|
" needs_scope : if 1, adds the current package to the scope
|
|
|
|
" selected : if 1, means it's a range of selection, otherwise it picks up the
|
|
|
|
" offset under the cursor
|
|
|
|
" example output:
|
|
|
|
" {'cmd' : ['guru', '-json', 'implements', 'demo/demo.go:#66']}
|
|
|
|
function! s:guru_cmd(args) range abort
|
|
|
|
let mode = a:args.mode
|
|
|
|
let format = a:args.format
|
|
|
|
let needs_scope = a:args.needs_scope
|
|
|
|
let selected = a:args.selected
|
|
|
|
|
|
|
|
let result = {}
|
2016-06-26 07:12:36 -04:00
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
"return with a warning if the binary doesn't exist
|
2017-03-07 12:04:28 -05:00
|
|
|
let bin_path = go#path#CheckBinPath("guru")
|
2016-12-27 09:46:49 -05:00
|
|
|
if empty(bin_path)
|
|
|
|
return {'err': "bin path not found"}
|
|
|
|
endif
|
|
|
|
|
|
|
|
" start constructing the command
|
2018-06-14 06:31:12 -04:00
|
|
|
let cmd = [bin_path, '-tags', go#config#BuildTags()]
|
2016-06-26 07:12:36 -04:00
|
|
|
|
2016-08-02 08:48:32 -04:00
|
|
|
if &modified
|
2017-11-24 08:54:40 -05:00
|
|
|
let result.stdin_content = go#util#archive()
|
2016-12-27 09:46:49 -05:00
|
|
|
call add(cmd, "-modified")
|
2016-08-02 08:48:32 -04:00
|
|
|
endif
|
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" enable outputting in json format
|
2017-03-07 12:04:28 -05:00
|
|
|
if format == "json"
|
2016-12-27 09:46:49 -05:00
|
|
|
call add(cmd, "-json")
|
2016-06-26 07:12:36 -04:00
|
|
|
endif
|
|
|
|
|
2018-06-14 06:31:12 -04:00
|
|
|
let scopes = go#config#GuruScope()
|
|
|
|
if empty(scopes)
|
|
|
|
" some modes require scope to be defined (such as callers). For these we
|
|
|
|
" choose a sensible setting, which is using the current file's package
|
|
|
|
if needs_scope
|
|
|
|
let pkg = go#package#ImportPath()
|
|
|
|
if pkg == -1
|
|
|
|
return {'err': "current directory is not inside of a valid GOPATH"}
|
|
|
|
endif
|
|
|
|
let scopes = [pkg]
|
2016-05-14 07:57:54 -04:00
|
|
|
endif
|
2016-06-26 07:12:36 -04:00
|
|
|
endif
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2018-06-14 06:31:12 -04:00
|
|
|
" Add the scope.
|
2016-06-26 07:12:36 -04:00
|
|
|
if !empty(scopes)
|
2018-06-14 06:31:12 -04:00
|
|
|
" guru expect a comma-separated list of patterns.
|
2016-08-02 08:48:32 -04:00
|
|
|
let l:scope = join(scopes, ",")
|
2016-12-27 09:46:49 -05:00
|
|
|
let result.scope = l:scope
|
|
|
|
call extend(cmd, ["-scope", l:scope])
|
2016-06-26 07:12:36 -04:00
|
|
|
endif
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
let pos = printf("#%s", go#util#OffsetCursor())
|
2016-12-27 09:46:49 -05:00
|
|
|
if selected != -1
|
2016-06-26 07:12:36 -04:00
|
|
|
" means we have a range, get it
|
|
|
|
let pos1 = go#util#Offset(line("'<"), col("'<"))
|
|
|
|
let pos2 = go#util#Offset(line("'>"), col("'>"))
|
|
|
|
let pos = printf("#%s,#%s", pos1, pos2)
|
|
|
|
endif
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2018-06-14 06:31:12 -04:00
|
|
|
let l:filename = fnamemodify(expand("%"), ':p:gs?\\?/?') . ':' . pos
|
|
|
|
call extend(cmd, [mode, l:filename])
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
let result.cmd = cmd
|
|
|
|
return result
|
|
|
|
endfunction
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
" sync_guru runs guru in sync mode with the given arguments
|
|
|
|
function! s:sync_guru(args) abort
|
|
|
|
let result = s:guru_cmd(a:args)
|
|
|
|
if has_key(result, 'err')
|
|
|
|
call go#util#EchoError(result.err)
|
|
|
|
return -1
|
2016-07-16 14:30:35 -04:00
|
|
|
endif
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
if !has_key(a:args, 'disable_progress')
|
|
|
|
if a:args.needs_scope
|
2017-11-24 08:54:40 -05:00
|
|
|
call go#util#EchoProgress("analysing with scope ". result.scope .
|
|
|
|
\ " (see ':help go-guru-scope' if this doesn't work)...")
|
2016-12-27 09:46:49 -05:00
|
|
|
elseif a:args.mode !=# 'what'
|
|
|
|
" the query might take time, let us give some feedback
|
|
|
|
call go#util#EchoProgress("analysing ...")
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" run, forrest run!!!
|
2018-06-14 06:31:12 -04:00
|
|
|
if has_key(l:result, 'stdin_content')
|
|
|
|
let [l:out, l:err] = go#util#Exec(l:result.cmd, l:result.stdin_content)
|
2016-08-02 08:48:32 -04:00
|
|
|
else
|
2018-06-14 06:31:12 -04:00
|
|
|
let [l:out, l:err] = go#util#Exec(l:result.cmd)
|
2016-07-16 14:30:35 -04:00
|
|
|
endif
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
if has_key(a:args, 'custom_parse')
|
2018-06-14 06:31:12 -04:00
|
|
|
call a:args.custom_parse(l:err, l:out, a:args.mode)
|
2016-12-27 09:46:49 -05:00
|
|
|
else
|
2018-06-14 06:31:12 -04:00
|
|
|
call s:parse_guru_output(l:err, l:out, a:args.mode)
|
2016-06-26 07:12:36 -04:00
|
|
|
endif
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2018-06-14 06:31:12 -04:00
|
|
|
return l:out
|
2016-05-14 07:57:54 -04:00
|
|
|
endfunc
|
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
" use vim or neovim job api as appropriate
|
|
|
|
function! s:job_start(cmd, start_options) abort
|
|
|
|
if go#util#has_job()
|
|
|
|
return job_start(a:cmd, a:start_options)
|
|
|
|
endif
|
|
|
|
|
|
|
|
let opts = {'stdout_buffered': v:true, 'stderr_buffered': v:true}
|
2018-06-14 06:31:12 -04:00
|
|
|
|
|
|
|
let stdout_buf = ""
|
2018-03-31 10:56:26 -04:00
|
|
|
function opts.on_stdout(job_id, data, event) closure
|
2018-06-14 06:31:12 -04:00
|
|
|
let l:data = a:data
|
|
|
|
let l:data[0] = stdout_buf . l:data[0]
|
|
|
|
let stdout_buf = ""
|
|
|
|
|
|
|
|
if l:data[-1] != ""
|
|
|
|
let stdout_buf = l:data[-1]
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:data = l:data[:-2]
|
|
|
|
if len(l:data) == 0
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
call a:start_options.callback(a:job_id, join(l:data, "\n"))
|
2018-03-31 10:56:26 -04:00
|
|
|
endfunction
|
2018-06-14 06:31:12 -04:00
|
|
|
|
|
|
|
let stderr_buf = ""
|
2018-03-31 10:56:26 -04:00
|
|
|
function opts.on_stderr(job_id, data, event) closure
|
2018-06-14 06:31:12 -04:00
|
|
|
let l:data = a:data
|
|
|
|
let l:data[0] = stderr_buf . l:data[0]
|
|
|
|
let stderr_buf = ""
|
|
|
|
|
|
|
|
if l:data[-1] != ""
|
|
|
|
let stderr_buf = l:data[-1]
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:data = l:data[:-2]
|
|
|
|
if len(l:data) == 0
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
call a:start_options.callback(a:job_id, join(l:data, "\n"))
|
2018-03-31 10:56:26 -04:00
|
|
|
endfunction
|
2018-06-14 06:31:12 -04:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
function opts.on_exit(job_id, exit_code, event) closure
|
|
|
|
call a:start_options.exit_cb(a:job_id, a:exit_code)
|
|
|
|
call a:start_options.close_cb(a:job_id)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" use a shell for input redirection if needed
|
|
|
|
let cmd = a:cmd
|
|
|
|
if has_key(a:start_options, 'in_io') && a:start_options.in_io ==# 'file' && !empty(a:start_options.in_name)
|
2018-06-14 06:31:12 -04:00
|
|
|
let cmd = ['/bin/sh', '-c', go#util#Shelljoin(a:cmd) . ' <' . a:start_options.in_name]
|
2018-03-31 10:56:26 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
return jobstart(cmd, opts)
|
|
|
|
endfunction
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
" async_guru runs guru in async mode with the given arguments
|
|
|
|
function! s:async_guru(args) abort
|
|
|
|
let result = s:guru_cmd(a:args)
|
|
|
|
if has_key(result, 'err')
|
|
|
|
call go#util#EchoError(result.err)
|
|
|
|
return
|
|
|
|
endif
|
2016-06-26 07:12:36 -04:00
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
if !has_key(a:args, 'disable_progress')
|
|
|
|
if a:args.needs_scope
|
2017-11-24 08:54:40 -05:00
|
|
|
call go#util#EchoProgress("analysing with scope " . result.scope .
|
|
|
|
\ " (see ':help go-guru-scope' if this doesn't work)...")
|
2016-12-27 09:46:49 -05:00
|
|
|
endif
|
|
|
|
endif
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
let state = {
|
|
|
|
\ 'status_dir': expand('%:p:h'),
|
|
|
|
\ 'statusline_type': printf("%s", a:args.mode),
|
|
|
|
\ 'mode': a:args.mode,
|
|
|
|
\ 'status': {},
|
|
|
|
\ 'exitval': 0,
|
|
|
|
\ 'closed': 0,
|
|
|
|
\ 'exited': 0,
|
|
|
|
\ 'messages': [],
|
|
|
|
\ 'parse' : get(a:args, 'custom_parse', funcref("s:parse_guru_output"))
|
|
|
|
\ }
|
|
|
|
|
|
|
|
function! s:callback(chan, msg) dict
|
|
|
|
call add(self.messages, a:msg)
|
2017-05-02 08:42:08 -04:00
|
|
|
endfunction
|
2016-06-26 07:12:36 -04:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
function! s:exit_cb(job, exitval) dict
|
|
|
|
let self.exited = 1
|
2018-02-04 06:35:08 -05:00
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
let status = {
|
|
|
|
\ 'desc': 'last status',
|
2018-03-31 10:56:26 -04:00
|
|
|
\ 'type': self.statusline_type,
|
2016-12-27 09:46:49 -05:00
|
|
|
\ 'state': "finished",
|
|
|
|
\ }
|
|
|
|
|
2017-05-02 08:42:08 -04:00
|
|
|
if a:exitval
|
2018-03-31 10:56:26 -04:00
|
|
|
let self.exitval = a:exitval
|
2016-12-27 09:46:49 -05:00
|
|
|
let status.state = "failed"
|
|
|
|
endif
|
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
call go#statusline#Update(self.status_dir, status)
|
2018-02-04 06:35:08 -05:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
if self.closed
|
|
|
|
call self.complete()
|
2018-02-04 06:35:08 -05:00
|
|
|
endif
|
2017-11-24 08:54:40 -05:00
|
|
|
endfunction
|
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
function! s:close_cb(ch) dict
|
|
|
|
let self.closed = 1
|
2018-02-04 06:35:08 -05:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
if self.exited
|
|
|
|
call self.complete()
|
2018-02-04 06:35:08 -05:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
function state.complete() dict
|
|
|
|
let out = join(self.messages, "\n")
|
2016-12-27 09:46:49 -05:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
call self.parse(self.exitval, out, self.mode)
|
2016-12-27 09:46:49 -05:00
|
|
|
endfunction
|
2016-06-26 07:12:36 -04:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
" explicitly bind the callbacks to state so that self within them always
|
|
|
|
" refers to state. See :help Partial for more information.
|
2016-12-27 09:46:49 -05:00
|
|
|
let start_options = {
|
2018-03-31 10:56:26 -04:00
|
|
|
\ 'callback': function('s:callback', [], state),
|
|
|
|
\ 'exit_cb': function('s:exit_cb', [], state),
|
|
|
|
\ 'close_cb': function('s:close_cb', [], state)
|
|
|
|
\ }
|
2016-06-26 07:12:36 -04:00
|
|
|
|
2017-02-11 08:01:38 -05:00
|
|
|
if has_key(result, 'stdin_content')
|
2016-12-27 09:46:49 -05:00
|
|
|
let l:tmpname = tempname()
|
|
|
|
call writefile(split(result.stdin_content, "\n"), l:tmpname, "b")
|
|
|
|
let l:start_options.in_io = "file"
|
|
|
|
let l:start_options.in_name = l:tmpname
|
2016-06-26 07:12:36 -04:00
|
|
|
endif
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
call go#statusline#Update(state.status_dir, {
|
2016-12-27 09:46:49 -05:00
|
|
|
\ 'desc': "current status",
|
2018-03-31 10:56:26 -04:00
|
|
|
\ 'type': state.statusline_type,
|
2016-12-27 09:46:49 -05:00
|
|
|
\ 'state': "analysing",
|
|
|
|
\})
|
2016-08-02 08:48:32 -04:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
return s:job_start(result.cmd, start_options)
|
2016-12-27 09:46:49 -05:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
" run_guru runs the given guru argument
|
|
|
|
function! s:run_guru(args) abort
|
2018-03-31 10:56:26 -04:00
|
|
|
if has('nvim') || go#util#has_job()
|
2017-03-07 12:04:28 -05:00
|
|
|
let res = s:async_guru(a:args)
|
|
|
|
else
|
|
|
|
let res = s:sync_guru(a:args)
|
2016-08-02 08:48:32 -04:00
|
|
|
endif
|
|
|
|
|
2017-03-07 12:04:28 -05:00
|
|
|
return res
|
2016-08-02 08:48:32 -04:00
|
|
|
endfunction
|
|
|
|
|
2016-05-14 07:57:54 -04:00
|
|
|
" Show 'implements' relation for selected package
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#guru#Implements(selected) abort
|
|
|
|
let args = {
|
|
|
|
\ 'mode': 'implements',
|
|
|
|
\ 'format': 'plain',
|
|
|
|
\ 'selected': a:selected,
|
|
|
|
\ 'needs_scope': 1,
|
|
|
|
\ }
|
|
|
|
|
|
|
|
call s:run_guru(args)
|
|
|
|
endfunction
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2018-06-14 06:31:12 -04:00
|
|
|
" Shows the set of possible objects to which a pointer may point.
|
|
|
|
function! go#guru#PointsTo(selected) abort
|
|
|
|
let l:args = {
|
|
|
|
\ 'mode': 'pointsto',
|
|
|
|
\ 'format': 'plain',
|
|
|
|
\ 'selected': a:selected,
|
|
|
|
\ 'needs_scope': 1,
|
|
|
|
\ }
|
|
|
|
|
|
|
|
call s:run_guru(l:args)
|
|
|
|
endfunction
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
" Report the possible constants, global variables, and concrete types that may
|
|
|
|
" appear in a value of type error
|
|
|
|
function! go#guru#Whicherrs(selected) abort
|
|
|
|
let args = {
|
|
|
|
\ 'mode': 'whicherrs',
|
|
|
|
\ 'format': 'plain',
|
|
|
|
\ 'selected': a:selected,
|
|
|
|
\ 'needs_scope': 1,
|
|
|
|
\ }
|
|
|
|
|
|
|
|
|
|
|
|
" TODO(arslan): handle empty case for both sync/async
|
|
|
|
" if empty(out.out)
|
|
|
|
" call go#util#EchoSuccess("no error variables found. Try to change the scope with :GoGuruScope")
|
|
|
|
" return
|
|
|
|
" endif
|
|
|
|
call s:run_guru(args)
|
2016-05-14 07:57:54 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Describe selected syntax: definition, methods, etc
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#guru#Describe(selected) abort
|
|
|
|
let args = {
|
|
|
|
\ 'mode': 'describe',
|
|
|
|
\ 'format': 'plain',
|
|
|
|
\ 'selected': a:selected,
|
|
|
|
\ 'needs_scope': 1,
|
|
|
|
\ }
|
|
|
|
|
|
|
|
call s:run_guru(args)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! go#guru#DescribeInfo() abort
|
|
|
|
" json_encode() and friends are introduced with this patch (7.4.1304)
|
|
|
|
" vim: https://groups.google.com/d/msg/vim_dev/vLupTNhQhZ8/cDGIk0JEDgAJ
|
2017-03-07 12:04:28 -05:00
|
|
|
" nvim: https://github.com/neovim/neovim/pull/4131
|
2016-12-27 09:46:49 -05:00
|
|
|
if !exists("*json_decode")
|
|
|
|
call go#util#EchoError("requires 'json_decode'. Update your Vim/Neovim version.")
|
2016-06-26 07:12:36 -04:00
|
|
|
return
|
|
|
|
endif
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
function! s:info(exit_val, output, mode)
|
2016-12-27 09:46:49 -05:00
|
|
|
if a:exit_val != 0
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
if a:output[0] !=# '{'
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
if empty(a:output) || type(a:output) != type("")
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let result = json_decode(a:output)
|
|
|
|
if type(result) != type({})
|
|
|
|
call go#util#EchoError(printf("malformed output from guru: %s", a:output))
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
if !has_key(result, 'detail')
|
|
|
|
" if there is no detail check if there is a description and print it
|
|
|
|
if has_key(result, "desc")
|
|
|
|
call go#util#EchoInfo(result["desc"])
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
call go#util#EchoError("detail key is missing. Please open a bug report on vim-go repo.")
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let detail = result['detail']
|
|
|
|
let info = ""
|
|
|
|
|
|
|
|
" guru gives different information based on the detail mode. Let try to
|
|
|
|
" extract the most useful information
|
|
|
|
|
|
|
|
if detail == "value"
|
|
|
|
if !has_key(result, 'value')
|
|
|
|
call go#util#EchoError("value key is missing. Please open a bug report on vim-go repo.")
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let val = result["value"]
|
|
|
|
if !has_key(val, 'type')
|
|
|
|
call go#util#EchoError("type key is missing (value.type). Please open a bug report on vim-go repo.")
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let info = val["type"]
|
|
|
|
elseif detail == "type"
|
|
|
|
if !has_key(result, 'type')
|
|
|
|
call go#util#EchoError("type key is missing. Please open a bug report on vim-go repo.")
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let type = result["type"]
|
|
|
|
if !has_key(type, 'type')
|
|
|
|
call go#util#EchoError("type key is missing (type.type). Please open a bug report on vim-go repo.")
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let info = type["type"]
|
|
|
|
elseif detail == "package"
|
|
|
|
if !has_key(result, 'package')
|
|
|
|
call go#util#EchoError("package key is missing. Please open a bug report on vim-go repo.")
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let package = result["package"]
|
|
|
|
if !has_key(package, 'path')
|
|
|
|
call go#util#EchoError("path key is missing (package.path). Please open a bug report on vim-go repo.")
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let info = printf("package %s", package["path"])
|
|
|
|
elseif detail == "unknown"
|
|
|
|
let info = result["desc"]
|
|
|
|
else
|
|
|
|
call go#util#EchoError(printf("unknown detail mode found '%s'. Please open a bug report on vim-go repo", detail))
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
call go#util#EchoInfo(info)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
let args = {
|
|
|
|
\ 'mode': 'describe',
|
|
|
|
\ 'format': 'json',
|
|
|
|
\ 'selected': -1,
|
2017-12-13 09:05:24 -05:00
|
|
|
\ 'needs_scope': 0,
|
2016-12-27 09:46:49 -05:00
|
|
|
\ 'custom_parse': function('s:info'),
|
|
|
|
\ 'disable_progress': 1,
|
|
|
|
\ }
|
|
|
|
|
|
|
|
call s:run_guru(args)
|
2016-05-14 07:57:54 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Show possible targets of selected function call
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#guru#Callees(selected) abort
|
|
|
|
let args = {
|
|
|
|
\ 'mode': 'callees',
|
|
|
|
\ 'format': 'plain',
|
|
|
|
\ 'selected': a:selected,
|
|
|
|
\ 'needs_scope': 1,
|
|
|
|
\ }
|
|
|
|
|
|
|
|
call s:run_guru(args)
|
2016-05-14 07:57:54 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Show possible callers of selected function
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#guru#Callers(selected) abort
|
|
|
|
let args = {
|
|
|
|
\ 'mode': 'callers',
|
|
|
|
\ 'format': 'plain',
|
|
|
|
\ 'selected': a:selected,
|
|
|
|
\ 'needs_scope': 1,
|
|
|
|
\ }
|
|
|
|
|
|
|
|
call s:run_guru(args)
|
2016-05-14 07:57:54 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Show path from callgraph root to selected function
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#guru#Callstack(selected) abort
|
|
|
|
let args = {
|
|
|
|
\ 'mode': 'callstack',
|
|
|
|
\ 'format': 'plain',
|
|
|
|
\ 'selected': a:selected,
|
|
|
|
\ 'needs_scope': 1,
|
|
|
|
\ }
|
|
|
|
|
|
|
|
call s:run_guru(args)
|
2016-05-14 07:57:54 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Show free variables of selection
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#guru#Freevars(selected) abort
|
2016-06-26 07:12:36 -04:00
|
|
|
" Freevars requires a selection
|
|
|
|
if a:selected == -1
|
|
|
|
call go#util#EchoError("GoFreevars requires a selection (range) of code")
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
let args = {
|
|
|
|
\ 'mode': 'freevars',
|
|
|
|
\ 'format': 'plain',
|
|
|
|
\ 'selected': 1,
|
|
|
|
\ 'needs_scope': 0,
|
|
|
|
\ }
|
2016-06-26 07:12:36 -04:00
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
call s:run_guru(args)
|
2016-05-14 07:57:54 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Show send/receive corresponding to selected channel op
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#guru#ChannelPeers(selected) abort
|
|
|
|
let args = {
|
|
|
|
\ 'mode': 'peers',
|
|
|
|
\ 'format': 'plain',
|
|
|
|
\ 'selected': a:selected,
|
|
|
|
\ 'needs_scope': 1,
|
|
|
|
\ }
|
|
|
|
|
|
|
|
call s:run_guru(args)
|
2016-05-14 07:57:54 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Show all refs to entity denoted by selected identifier
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#guru#Referrers(selected) abort
|
|
|
|
let args = {
|
|
|
|
\ 'mode': 'referrers',
|
|
|
|
\ 'format': 'plain',
|
|
|
|
\ 'selected': a:selected,
|
|
|
|
\ 'needs_scope': 0,
|
|
|
|
\ }
|
|
|
|
|
|
|
|
call s:run_guru(args)
|
|
|
|
endfunction
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#guru#SameIds() abort
|
|
|
|
" we use matchaddpos() which was introduce with 7.4.330, be sure we have
|
|
|
|
" it: http://ftp.vim.org/vim/patches/7.4/7.4.330
|
|
|
|
if !exists("*matchaddpos")
|
|
|
|
call go#util#EchoError("GoSameIds requires 'matchaddpos'. Update your Vim/Neovim version.")
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2016-08-02 08:48:32 -04:00
|
|
|
" json_encode() and friends are introduced with this patch (7.4.1304)
|
|
|
|
" vim: https://groups.google.com/d/msg/vim_dev/vLupTNhQhZ8/cDGIk0JEDgAJ
|
2017-03-07 12:04:28 -05:00
|
|
|
" nvim: https://github.com/neovim/neovim/pull/4131
|
2016-08-02 08:48:32 -04:00
|
|
|
if !exists("*json_decode")
|
2016-12-27 09:46:49 -05:00
|
|
|
call go#util#EchoError("GoSameIds requires 'json_decode'. Update your Vim/Neovim version.")
|
|
|
|
return
|
2016-06-26 07:12:36 -04:00
|
|
|
endif
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
let args = {
|
|
|
|
\ 'mode': 'what',
|
|
|
|
\ 'format': 'json',
|
|
|
|
\ 'selected': -1,
|
|
|
|
\ 'needs_scope': 0,
|
|
|
|
\ 'custom_parse': function('s:same_ids_highlight'),
|
|
|
|
\ }
|
2016-06-26 07:12:36 -04:00
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
call s:run_guru(args)
|
2016-05-14 07:57:54 -04:00
|
|
|
endfunction
|
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
function! s:same_ids_highlight(exit_val, output, mode) abort
|
2016-12-27 09:46:49 -05:00
|
|
|
call go#guru#ClearSameIds() " run after calling guru to reduce flicker.
|
2016-08-02 08:48:32 -04:00
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
if a:output[0] !=# '{'
|
2018-06-14 06:31:12 -04:00
|
|
|
if !go#config#AutoSameids()
|
2016-12-27 09:46:49 -05:00
|
|
|
call go#util#EchoError(a:output)
|
|
|
|
endif
|
2016-08-02 08:48:32 -04:00
|
|
|
return
|
|
|
|
endif
|
2016-07-16 14:30:35 -04:00
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
let result = json_decode(a:output)
|
2018-06-14 06:31:12 -04:00
|
|
|
if type(result) != type({}) && !go#config#AutoSameids()
|
2016-12-27 09:46:49 -05:00
|
|
|
call go#util#EchoError("malformed output from guru")
|
2016-06-26 07:12:36 -04:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2016-07-16 14:30:35 -04:00
|
|
|
if !has_key(result, 'sameids')
|
2018-06-14 06:31:12 -04:00
|
|
|
if !go#config#AutoSameids()
|
2016-07-16 14:30:35 -04:00
|
|
|
call go#util#EchoError("no same_ids founds for the given identifier")
|
|
|
|
endif
|
|
|
|
return
|
2016-06-26 07:12:36 -04:00
|
|
|
endif
|
|
|
|
|
2016-07-16 14:30:35 -04:00
|
|
|
let poslen = 0
|
|
|
|
for enclosing in result['enclosing']
|
|
|
|
if enclosing['desc'] == 'identifier'
|
|
|
|
let poslen = enclosing['end'] - enclosing['start']
|
|
|
|
break
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
" return when there's no identifier to highlight.
|
|
|
|
if poslen == 0
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let same_ids = result['sameids']
|
|
|
|
" highlight the lines
|
|
|
|
for item in same_ids
|
|
|
|
let pos = split(item, ':')
|
|
|
|
call matchaddpos('goSameId', [[str2nr(pos[-2]), str2nr(pos[-1]), str2nr(poslen)]])
|
|
|
|
endfor
|
2016-08-20 07:23:52 -04:00
|
|
|
|
2018-06-14 06:31:12 -04:00
|
|
|
if go#config#AutoSameids()
|
2016-08-20 07:23:52 -04:00
|
|
|
" re-apply SameIds at the current cursor position at the time the buffer
|
|
|
|
" is redisplayed: e.g. :edit, :GoRename, etc.
|
2017-11-24 08:54:40 -05:00
|
|
|
augroup vim-go-sameids
|
|
|
|
autocmd!
|
|
|
|
autocmd BufWinEnter <buffer> nested call go#guru#SameIds()
|
|
|
|
augroup end
|
2016-08-20 07:23:52 -04:00
|
|
|
endif
|
2016-07-16 14:30:35 -04:00
|
|
|
endfunction
|
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
" ClearSameIds returns 0 when it removes goSameId groups and non-zero if no
|
|
|
|
" goSameId groups are found.
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#guru#ClearSameIds() abort
|
2017-11-24 08:54:40 -05:00
|
|
|
let l:cleared = 0
|
|
|
|
|
2016-07-16 14:30:35 -04:00
|
|
|
let m = getmatches()
|
|
|
|
for item in m
|
|
|
|
if item['group'] == 'goSameId'
|
|
|
|
call matchdelete(item['id'])
|
2017-11-24 08:54:40 -05:00
|
|
|
let l:cleared = 1
|
2016-07-16 14:30:35 -04:00
|
|
|
endif
|
|
|
|
endfor
|
2016-08-20 07:23:52 -04:00
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
if !l:cleared
|
|
|
|
return 1
|
2016-08-20 07:23:52 -04:00
|
|
|
endif
|
2017-11-24 08:54:40 -05:00
|
|
|
|
|
|
|
" remove the autocmds we defined
|
|
|
|
augroup vim-go-sameids
|
|
|
|
autocmd!
|
|
|
|
augroup end
|
|
|
|
|
|
|
|
return 0
|
2016-05-14 07:57:54 -04:00
|
|
|
endfunction
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#guru#ToggleSameIds() abort
|
2017-11-24 08:54:40 -05:00
|
|
|
if go#guru#ClearSameIds() != 0
|
2016-12-27 09:46:49 -05:00
|
|
|
call go#guru#SameIds()
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! go#guru#AutoToogleSameIds() abort
|
2018-06-14 06:31:12 -04:00
|
|
|
if go#config#AutoSameids()
|
2016-12-27 09:46:49 -05:00
|
|
|
call go#util#EchoProgress("sameids auto highlighting disabled")
|
|
|
|
call go#guru#ClearSameIds()
|
2018-06-14 06:31:12 -04:00
|
|
|
call go#config#SetAutoSameids(0)
|
2016-12-27 09:46:49 -05:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
call go#util#EchoSuccess("sameids auto highlighting enabled")
|
2018-06-14 06:31:12 -04:00
|
|
|
call go#config#SetAutoSameids(1)
|
2016-12-27 09:46:49 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
""""""""""""""""""""""""""""""""""""""""
|
|
|
|
"" HELPER FUNCTIONS
|
|
|
|
""""""""""""""""""""""""""""""""""""""""
|
|
|
|
|
|
|
|
" This uses Vim's errorformat to parse the output from Guru's 'plain output
|
|
|
|
" and put it into location list. I believe using errorformat is much more
|
|
|
|
" easier to use. If we need more power we can always switch back to parse it
|
|
|
|
" via regex. 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
|
|
|
|
" useful and location only has the ability to show one line and column
|
|
|
|
" number
|
|
|
|
function! s:parse_guru_output(exit_val, output, title) abort
|
|
|
|
if a:exit_val
|
|
|
|
call go#util#EchoError(a:output)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let errformat = "%f:%l.%c-%[%^:]%#:\ %m,%f:%l:%c:\ %m"
|
2017-11-24 08:54:40 -05:00
|
|
|
let l:listtype = go#list#Type("_guru")
|
|
|
|
call go#list#ParseFormat(l:listtype, errformat, a:output, a:title)
|
2016-12-27 09:46:49 -05:00
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
let errors = go#list#Get(l:listtype)
|
|
|
|
call go#list#Window(l:listtype, len(errors))
|
2016-12-27 09:46:49 -05:00
|
|
|
endfun
|
|
|
|
|
|
|
|
function! go#guru#Scope(...) abort
|
|
|
|
if a:0
|
2018-06-14 06:31:12 -04:00
|
|
|
let scope = a:000
|
2016-12-27 09:46:49 -05:00
|
|
|
if a:0 == 1 && a:1 == '""'
|
2018-06-14 06:31:12 -04:00
|
|
|
let scope = []
|
|
|
|
endif
|
|
|
|
|
|
|
|
call go#config#SetGuruScope(scope)
|
|
|
|
if empty(scope)
|
2016-12-27 09:46:49 -05:00
|
|
|
call go#util#EchoSuccess("guru scope is cleared")
|
|
|
|
else
|
|
|
|
call go#util#EchoSuccess("guru scope changed to: ". join(a:000, ","))
|
|
|
|
endif
|
|
|
|
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2018-06-14 06:31:12 -04:00
|
|
|
let scope = go#config#GuruScope()
|
|
|
|
if empty(scope)
|
2016-12-27 09:46:49 -05:00
|
|
|
call go#util#EchoError("guru scope is not set")
|
|
|
|
else
|
2018-06-14 06:31:12 -04:00
|
|
|
call go#util#EchoSuccess("current guru scope: ". join(scope, ","))
|
2016-12-27 09:46:49 -05:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" vim: sw=2 ts=2 et
|