2017-09-02 06:43:18 -04:00
|
|
|
" From "go list -h".
|
|
|
|
function! go#tool#ValidFiles(...)
|
|
|
|
let l:list = ["GoFiles", "CgoFiles", "IgnoredGoFiles", "CFiles", "CXXFiles",
|
|
|
|
\ "MFiles", "HFiles", "FFiles", "SFiles", "SwigFiles", "SwigCXXFiles",
|
|
|
|
\ "SysoFiles", "TestGoFiles", "XTestGoFiles"]
|
|
|
|
|
|
|
|
" Used as completion
|
|
|
|
if len(a:000) > 0
|
|
|
|
let l:list = filter(l:list, 'strpart(v:val, 0, len(a:1)) == a:1')
|
|
|
|
endif
|
|
|
|
|
|
|
|
return l:list
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! go#tool#Files(...) abort
|
|
|
|
if len(a:000) > 0
|
|
|
|
let source_files = a:000
|
2016-06-26 07:12:36 -04:00
|
|
|
else
|
2017-09-02 06:43:18 -04:00
|
|
|
let source_files = ['GoFiles']
|
2016-06-26 07:12:36 -04:00
|
|
|
endif
|
2017-09-02 06:43:18 -04:00
|
|
|
|
|
|
|
let combined = ''
|
|
|
|
for sf in source_files
|
|
|
|
" Strip dot in case people used ":GoFiles .GoFiles".
|
|
|
|
let sf = substitute(sf, '^\.', '', '')
|
|
|
|
|
|
|
|
" Make sure the passed options are valid.
|
|
|
|
if index(go#tool#ValidFiles(), sf) == -1
|
|
|
|
echoerr "unknown source file variable: " . sf
|
|
|
|
endif
|
|
|
|
|
|
|
|
if go#util#IsWin()
|
|
|
|
let combined .= '{{range $f := .' . sf . '}}{{$.Dir}}\{{$f}}{{printf \"\n\"}}{{end}}{{range $f := .CgoFiles}}{{$.Dir}}\{{$f}}{{printf \"\n\"}}{{end}}'
|
|
|
|
else
|
|
|
|
let combined .= "{{range $f := ." . sf . "}}{{$.Dir}}/{{$f}}{{printf \"\\n\"}}{{end}}{{range $f := .CgoFiles}}{{$.Dir}}/{{$f}}{{printf \"\\n\"}}{{end}}"
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
let out = go#tool#ExecuteInDir('go list -f ' . shellescape(combined))
|
2016-06-26 07:12:36 -04:00
|
|
|
return split(out, '\n')
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#tool#Deps() abort
|
2016-06-26 07:12:36 -04:00
|
|
|
if go#util#IsWin()
|
|
|
|
let format = '{{range $f := .Deps}}{{$f}}{{printf \"\n\"}}{{end}}'
|
|
|
|
else
|
|
|
|
let format = "{{range $f := .Deps}}{{$f}}\n{{end}}"
|
|
|
|
endif
|
|
|
|
let command = 'go list -f '.shellescape(format)
|
|
|
|
let out = go#tool#ExecuteInDir(command)
|
|
|
|
return split(out, '\n')
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#tool#Imports() abort
|
2016-06-26 07:12:36 -04:00
|
|
|
let imports = {}
|
|
|
|
if go#util#IsWin()
|
|
|
|
let format = '{{range $f := .Imports}}{{$f}}{{printf \"\n\"}}{{end}}'
|
|
|
|
else
|
|
|
|
let format = "{{range $f := .Imports}}{{$f}}{{printf \"\\n\"}}{{end}}"
|
|
|
|
endif
|
|
|
|
let command = 'go list -f '.shellescape(format)
|
|
|
|
let out = go#tool#ExecuteInDir(command)
|
|
|
|
if go#util#ShellError() != 0
|
|
|
|
echo out
|
|
|
|
return imports
|
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
for package_path in split(out, '\n')
|
|
|
|
let cmd = "go list -f '{{.Name}}' " . shellescape(package_path)
|
|
|
|
let package_name = substitute(go#tool#ExecuteInDir(cmd), '\n$', '', '')
|
|
|
|
let imports[package_name] = package_path
|
|
|
|
endfor
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
return imports
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#tool#Info(auto) abort
|
|
|
|
let l:mode = get(g:, 'go_info_mode', 'gocode')
|
|
|
|
if l:mode == 'gocode'
|
|
|
|
call go#complete#Info(a:auto)
|
|
|
|
elseif l:mode == 'guru'
|
|
|
|
call go#guru#DescribeInfo()
|
|
|
|
else
|
|
|
|
call go#util#EchoError('go_info_mode value: '. l:mode .' is not valid. Valid values are: [gocode, guru]')
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! go#tool#PackageName() abort
|
2016-08-02 08:48:32 -04:00
|
|
|
let command = "go list -f \"{{.Name}}\""
|
2016-07-03 07:53:59 -04:00
|
|
|
let out = go#tool#ExecuteInDir(command)
|
|
|
|
if go#util#ShellError() != 0
|
|
|
|
return -1
|
|
|
|
endif
|
|
|
|
|
|
|
|
return split(out, '\n')[0]
|
|
|
|
endfunction
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#tool#ParseErrors(lines) abort
|
2016-06-26 07:12:36 -04:00
|
|
|
let errors = []
|
|
|
|
|
|
|
|
for line in a:lines
|
|
|
|
let fatalerrors = matchlist(line, '^\(fatal error:.*\)$')
|
|
|
|
let tokens = matchlist(line, '^\s*\(.\{-}\):\(\d\+\):\s*\(.*\)')
|
|
|
|
|
|
|
|
if !empty(fatalerrors)
|
|
|
|
call add(errors, {"text": fatalerrors[1]})
|
|
|
|
elseif !empty(tokens)
|
|
|
|
" strip endlines of form ^M
|
|
|
|
let out = substitute(tokens[3], '\r$', '', '')
|
|
|
|
|
|
|
|
call add(errors, {
|
|
|
|
\ "filename" : fnamemodify(tokens[1], ':p'),
|
|
|
|
\ "lnum" : tokens[2],
|
|
|
|
\ "text" : out,
|
|
|
|
\ })
|
|
|
|
elseif !empty(errors)
|
|
|
|
" Preserve indented lines.
|
|
|
|
" This comes up especially with multi-line test output.
|
|
|
|
if match(line, '^\s') >= 0
|
|
|
|
call add(errors, {"text": line})
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfor
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
return errors
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
2015-12-16 08:53:53 -05:00
|
|
|
"FilterValids filters the given items with only items that have a valid
|
|
|
|
"filename. Any non valid filename is filtered out.
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#tool#FilterValids(items) abort
|
2016-06-26 07:12:36 -04:00
|
|
|
" Remove any nonvalid filename from the location list to avoid opening an
|
|
|
|
" empty buffer. See https://github.com/fatih/vim-go/issues/287 for
|
|
|
|
" details.
|
|
|
|
let filtered = []
|
|
|
|
let is_readable = {}
|
|
|
|
|
|
|
|
for item in a:items
|
|
|
|
if has_key(item, 'bufnr')
|
|
|
|
let filename = bufname(item.bufnr)
|
|
|
|
elseif has_key(item, 'filename')
|
|
|
|
let filename = item.filename
|
|
|
|
else
|
|
|
|
" nothing to do, add item back to the list
|
|
|
|
call add(filtered, item)
|
|
|
|
continue
|
|
|
|
endif
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
if !has_key(is_readable, filename)
|
|
|
|
let is_readable[filename] = filereadable(filename)
|
|
|
|
endif
|
|
|
|
if is_readable[filename]
|
|
|
|
call add(filtered, item)
|
|
|
|
endif
|
|
|
|
endfor
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
for k in keys(filter(is_readable, '!v:val'))
|
|
|
|
echo "vim-go: " | echohl Identifier | echon "[run] Dropped " | echohl Constant | echon '"' . k . '"'
|
|
|
|
echohl Identifier | echon " from location list (nonvalid filename)" | echohl None
|
|
|
|
endfor
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
return filtered
|
2015-12-16 08:53:53 -05:00
|
|
|
endfunction
|
|
|
|
|
2014-10-31 17:30:24 -04:00
|
|
|
function! go#tool#ExecuteInDir(cmd) abort
|
2017-11-24 08:54:40 -05:00
|
|
|
" Verify that the directory actually exists. If the directory does not
|
|
|
|
" exist, then assume that the a:cmd should not be executed. Callers expect
|
|
|
|
" to check v:shell_error (via go#util#ShellError()), so execute a command
|
|
|
|
" that will return an error as if a:cmd was run and exited with an error.
|
|
|
|
" This helps avoid errors when working with plugins that use virtual files
|
|
|
|
" that don't actually exist on the file system (e.g. vim-fugitive's
|
|
|
|
" GitDiff).
|
|
|
|
if !isdirectory(expand("%:p:h"))
|
|
|
|
let [out, err] = go#util#Exec(["false"])
|
|
|
|
return ''
|
|
|
|
endif
|
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
let old_gopath = $GOPATH
|
2016-11-09 12:22:55 -05:00
|
|
|
let old_goroot = $GOROOT
|
2016-06-26 07:12:36 -04:00
|
|
|
let $GOPATH = go#path#Detect()
|
2016-11-09 12:22:55 -05:00
|
|
|
let $GOROOT = go#util#env("goroot")
|
2016-06-26 07:12:36 -04:00
|
|
|
|
|
|
|
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
|
|
|
|
let dir = getcwd()
|
|
|
|
try
|
|
|
|
execute cd . fnameescape(expand("%:p:h"))
|
|
|
|
let out = go#util#System(a:cmd)
|
|
|
|
finally
|
|
|
|
execute cd . fnameescape(dir)
|
|
|
|
endtry
|
|
|
|
|
2016-11-09 12:22:55 -05:00
|
|
|
let $GOROOT = old_goroot
|
2016-06-26 07:12:36 -04:00
|
|
|
let $GOPATH = old_gopath
|
|
|
|
return out
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Exists checks whether the given importpath exists or not. It returns 0 if
|
|
|
|
" the importpath exists under GOPATH.
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#tool#Exists(importpath) abort
|
2014-10-31 17:30:24 -04:00
|
|
|
let command = "go list ". a:importpath
|
|
|
|
let out = go#tool#ExecuteInDir(command)
|
|
|
|
|
2016-05-14 07:57:54 -04:00
|
|
|
if go#util#ShellError() != 0
|
2014-10-31 17:30:24 -04:00
|
|
|
return -1
|
|
|
|
endif
|
|
|
|
|
|
|
|
return 0
|
|
|
|
endfunction
|
|
|
|
|
2017-03-07 12:04:28 -05:00
|
|
|
" following two functions are from: https://github.com/mattn/gist-vim
|
2014-10-31 17:30:24 -04:00
|
|
|
" thanks @mattn
|
2016-12-27 09:46:49 -05:00
|
|
|
function! s:get_browser_command() abort
|
2014-10-31 17:30:24 -04:00
|
|
|
let go_play_browser_command = get(g:, 'go_play_browser_command', '')
|
|
|
|
if go_play_browser_command == ''
|
2015-07-13 06:22:46 -04:00
|
|
|
if go#util#IsWin()
|
2014-10-31 17:30:24 -04:00
|
|
|
let go_play_browser_command = '!start rundll32 url.dll,FileProtocolHandler %URL%'
|
2017-11-24 08:54:40 -05:00
|
|
|
elseif go#util#IsMac()
|
2014-10-31 17:30:24 -04:00
|
|
|
let go_play_browser_command = 'open %URL%'
|
|
|
|
elseif executable('xdg-open')
|
|
|
|
let go_play_browser_command = 'xdg-open %URL%'
|
|
|
|
elseif executable('firefox')
|
|
|
|
let go_play_browser_command = 'firefox %URL% &'
|
2017-11-24 08:54:40 -05:00
|
|
|
elseif executable('chromium')
|
|
|
|
let go_play_browser_command = 'chromium %URL% &'
|
2014-10-31 17:30:24 -04:00
|
|
|
else
|
|
|
|
let go_play_browser_command = ''
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
return go_play_browser_command
|
|
|
|
endfunction
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#tool#OpenBrowser(url) abort
|
2014-10-31 17:30:24 -04:00
|
|
|
let cmd = s:get_browser_command()
|
|
|
|
if len(cmd) == 0
|
|
|
|
redraw
|
|
|
|
echohl WarningMsg
|
|
|
|
echo "It seems that you don't have general web browser. Open URL below."
|
|
|
|
echohl None
|
|
|
|
echo a:url
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
if cmd =~ '^!'
|
2016-06-11 09:56:50 -04:00
|
|
|
let cmd = substitute(cmd, '%URL%', '\=escape(shellescape(a:url),"#")', 'g')
|
2014-10-31 17:30:24 -04:00
|
|
|
silent! exec cmd
|
|
|
|
elseif cmd =~ '^:[A-Z]'
|
2016-06-11 09:56:50 -04:00
|
|
|
let cmd = substitute(cmd, '%URL%', '\=escape(a:url,"#")', 'g')
|
2014-10-31 17:30:24 -04:00
|
|
|
exec cmd
|
|
|
|
else
|
|
|
|
let cmd = substitute(cmd, '%URL%', '\=shellescape(a:url)', 'g')
|
2016-05-14 07:57:54 -04:00
|
|
|
call go#util#System(cmd)
|
2014-10-31 17:30:24 -04:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" vim: sw=2 ts=2 et
|