Skip to content
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

package: improve import_path by using go list #1321

Merged
merged 1 commit into from
Jun 7, 2017
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
3 changes: 1 addition & 2 deletions autoload/go/guru.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion autoload/go/jobcontrol.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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' : [],
Expand Down
10 changes: 5 additions & 5 deletions autoload/go/lint.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand All @@ -199,14 +199,14 @@ 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
return
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)
Expand Down
28 changes: 12 additions & 16 deletions autoload/go/package.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down