Skip to content

Commit

Permalink
Fix go#package#FromPath() (#1435)
Browse files Browse the repository at this point in the history
This didn't work at all on my system. Turned out the problem was that on
my system (Linux) some paths didn't have a trailing slash, so
`workspace` would be `/home/martin/gosrc`.

This makes sure that it always works, no matter if a trailing slash is
present or not.

Also `break` from the loop on the first match, so it selects the
*first* package found in GOPATH rather than the *last*. This is also how
imports in Go behave.
  • Loading branch information
arp242 authored Sep 11, 2017
1 parent 2d36a78 commit 5f0c594
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type foo struct{
* Run `:GoMetaLinter` against the package of the open file #1414.
* The `g:go_doc_command` and `g:go_doc_options` to configure the command for
`:GoDoc` were documented but never referenced #1420.
* `go#package#FromPath()` didn't work correctly #1435.

BACKWARDS INCOMPATIBILITIES:

Expand Down
7 changes: 5 additions & 2 deletions autoload/go/package.vim
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,20 @@ function! go#package#FromPath(arg) abort
for dir in dirs
if len(dir) && match(path, dir) == 0
let workspace = dir
break
endif
endfor

if !exists('workspace')
return -1
endif

let path = substitute(path, '/*$', '', '')
let workspace = substitute(workspace . '/src/', '/+', '', '')
if isdirectory(path)
return substitute(path, workspace . 'src/', '', '')
return substitute(path, workspace, '', '')
else
return substitute(substitute(path, workspace . 'src/', '', ''),
return substitute(substitute(path, workspace, '', ''),
\ '/' . fnamemodify(path, ':t'), '', '')
endif
endfunction
Expand Down

0 comments on commit 5f0c594

Please sign in to comment.