diff --git a/autoload/go/guru.vim b/autoload/go/guru.vim index 5cff49d8f6..fbd36110de 100644 --- a/autoload/go/guru.vim +++ b/autoload/go/guru.vim @@ -16,8 +16,7 @@ function! s:guru_cmd(args) range abort let selected = a:args.selected let result = {} - let dirname = expand('%:p:h') - let pkg = go#package#ImportPath(dirname) + let pkg = go#package#ImportPath() " this is important, check it! if pkg == -1 && needs_scope diff --git a/autoload/go/jobcontrol.vim b/autoload/go/jobcontrol.vim index d914cb8cd2..8e2eaca237 100644 --- a/autoload/go/jobcontrol.vim +++ b/autoload/go/jobcontrol.vim @@ -52,7 +52,7 @@ function! s:spawn(bang, desc, args) abort \ 'desc': a:desc, \ 'bang': a:bang, \ 'winnr': winnr(), - \ 'importpath': go#package#ImportPath(expand('%:p:h')), + \ 'importpath': go#package#ImportPath(), \ 'state': "RUNNING", \ 'stderr' : [], \ 'stdout' : [], diff --git a/autoload/go/lint.vim b/autoload/go/lint.vim index e25636efe4..c876af6869 100644 --- a/autoload/go/lint.vim +++ b/autoload/go/lint.vim @@ -171,13 +171,13 @@ endfunction " the location list function! go#lint#Errcheck(...) abort if a:0 == 0 - let goargs = go#package#ImportPath(expand('%:p:h')) - if goargs == -1 + let import_path = go#package#ImportPath() + if import_path == -1 echohl Error | echomsg "vim-go: package is not inside GOPATH src" | echohl None return endif else - let goargs = go#util#Shelljoin(a:000) + let import_path = go#util#Shelljoin(a:000) endif let bin_path = go#path#CheckBinPath(g:go_errcheck_bin) @@ -188,7 +188,7 @@ function! go#lint#Errcheck(...) abort echon "vim-go: " | echohl Identifier | echon "errcheck analysing ..." | echohl None redraw - let command = bin_path . ' -abspath ' . goargs + let command = bin_path . ' -abspath ' . import_path let out = go#tool#ExecuteInDir(command) let l:listtype = "quickfix" @@ -199,7 +199,6 @@ function! go#lint#Errcheck(...) abort call go#list#ParseFormat(l:listtype, errformat, split(out, "\n"), 'Errcheck') let errors = go#list#Get(l:listtype) - if empty(errors) echohl Error | echomsg "GoErrCheck returned error" | echohl None echo out @@ -207,6 +206,7 @@ function! go#lint#Errcheck(...) abort endif if !empty(errors) + echohl Error | echomsg "GoErrCheck found errors" | echohl None call go#list#Populate(l:listtype, errors, 'Errcheck') call go#list#Window(l:listtype, len(errors)) if !empty(errors) diff --git a/autoload/go/package.vim b/autoload/go/package.vim index b12c7b7171..8e6126dd9f 100644 --- a/autoload/go/package.vim +++ b/autoload/go/package.vim @@ -54,29 +54,25 @@ function! go#package#Paths() abort return dirs endfunction -function! go#package#ImportPath(arg) abort - let path = fnamemodify(resolve(a:arg), ':p') - let dirs = go#package#Paths() +" ImportPath returns the import path in the current directory it was executed +function! go#package#ImportPath() abort + let out = go#tool#ExecuteInDir("go list") + if go#util#ShellError() != 0 + return -1 + endif - for dir in dirs - if len(dir) && matchstr(escape(path, '\/'), escape(dir, '\/')) == 0 - let workspace = dir - endif - endfor + let import_path = split(out, '\n')[0] - if !exists('workspace') + " go list returns '_CURRENTDIRECTORY' if the directory is not inside GOPATH. + " Check it and retun an error if that is the case + if import_path[0] ==# '_' return -1 endif - if go#util#IsWin() - let srcdir = substitute(workspace . '\src\', '//', '/', '') - return path[len(srcdir):] - else - let srcdir = substitute(workspace . '/src/', '//', '/', '') - return substitute(path, srcdir, '', '') - endif + return import_path endfunction + function! go#package#FromPath(arg) abort let path = fnamemodify(resolve(a:arg), ':p') let dirs = go#package#Paths()