Skip to content

Commit

Permalink
Merge pull request #1321 from fatih/improve-import-path
Browse files Browse the repository at this point in the history
package: improve import_path by using go list
  • Loading branch information
fatih authored Jun 7, 2017
2 parents 61b897f + 15d3b3c commit 913c181
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
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

0 comments on commit 913c181

Please sign in to comment.