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

support go1.11 beta2 Go modules #523

Closed
wants to merge 24 commits into from
Closed
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
54 changes: 54 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Godeps/Readme

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## An autocompletion daemon for the Go programming language

**IMPORTANT: consider switching to https://github.com/mdempsky/gocode if you have problems starting with Go version 1.10, due to changes in binary packages architecture (introduction of package cache) I'm not going to adjust gocode for it for quite some time. There is a higher chance that fork under the given link will have some solution to the problem sooner or later.**
Now support Go1.11 Go modules.

Gocode is a helper tool which is intended to be integrated with your source code editor, like vim, neovim and emacs. It provides several advanced capabilities, which currently includes:

Expand Down
20 changes: 14 additions & 6 deletions autocompletecontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
"runtime"
"sort"
"strings"
"sync"
"time"

pkgwalk "github.com/visualfc/gotools/types"
)

//-------------------------------------------------------------------------
Expand Down Expand Up @@ -77,7 +80,6 @@ func (b *out_buffers) append_decl(p, name, pkg string, decl *decl, class decl_cl
if c1 || c2 || c3 || c4 || c5 {
return
}

decl.pretty_print_type(b.tmpbuf, b.canonical_aliases)
b.candidates = append(b.candidates, candidate{
Name: name,
Expand Down Expand Up @@ -153,13 +155,18 @@ type auto_complete_context struct {

pcache package_cache // packages cache
declcache *decl_cache // top-level declarations cache
fset *token.FileSet
walker *pkgwalk.PkgWalker
mutex sync.Mutex
}

func new_auto_complete_context(pcache package_cache, declcache *decl_cache) *auto_complete_context {
func new_auto_complete_context(ctx *package_lookup_context, pcache package_cache, declcache *decl_cache) *auto_complete_context {
c := new(auto_complete_context)
c.current = new_auto_complete_file("", declcache.context)
c.pcache = pcache
c.declcache = declcache
c.fset = token.NewFileSet()
c.walker = pkgwalk.NewPkgWalker(&ctx.Context)
return c
}

Expand All @@ -175,7 +182,7 @@ func (c *auto_complete_context) update_caches() {
c.pcache.append_packages(ps, other.packages)
}

update_packages(ps)
c.update_packages(ps)

// fix imports for all files
fixup_packages(c.current.filescope, c.current.packages, c.pcache)
Expand Down Expand Up @@ -380,7 +387,7 @@ func (c *auto_complete_context) apropos(file []byte, filename string, cursor int
if !ok {
var d *decl
if ident, ok := cc.expr.(*ast.Ident); ok && g_config.UnimportedPackages {
p := resolveKnownPackageIdent(ident.Name, c.current.name, c.current.context)
p := c.resolveKnownPackageIdent(ident.Name, c.current.name, c.current.context)
if p != nil {
c.pcache[p.name] = p
d = p.main
Expand Down Expand Up @@ -441,9 +448,10 @@ func (c *auto_complete_context) apropos(file []byte, filename string, cursor int
return b.candidates, partial
}

func update_packages(ps map[string]*package_file_cache) {
func (c *auto_complete_context) update_packages(ps map[string]*package_file_cache) {
// initiate package cache update
done := make(chan bool)

for _, p := range ps {
go func(p *package_file_cache) {
defer func() {
Expand All @@ -452,7 +460,7 @@ func update_packages(ps map[string]*package_file_cache) {
done <- false
}
}()
p.update_cache()
p.update_cache(c)
done <- true
}(p)
}
Expand Down
8 changes: 4 additions & 4 deletions cursorcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,19 +420,19 @@ func (c *auto_complete_context) deduce_cursor_context(file []byte, cursor int) (
// package name has nothing to do with package file name, that's why we need to
// scan the packages. And many of them will have conflicts. Can we make a smart
// prediction algorithm which will prefer certain packages over another ones?
func resolveKnownPackageIdent(ident string, filename string, context *package_lookup_context) *package_file_cache {
func (c *auto_complete_context) resolveKnownPackageIdent(ident string, filename string, context *package_lookup_context) *package_file_cache {
importPath, ok := knownPackageIdents[ident]
if !ok {
return nil
}

path, ok := abs_path_for_package(filename, importPath, context)
path, vname, ok := abs_path_for_package(filename, importPath, context)
if !ok {
return nil
}

p := new_package_file_cache(path, importPath)
p.update_cache()
p := new_package_file_cache(path, importPath, vname)
p.update_cache(c)
return p
}

Expand Down
9 changes: 8 additions & 1 deletion decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,14 @@ func infer_type(v ast.Expr, scope *scope, index int) (ast.Expr, *scope, bool) {
if d.class == decl_package {
return ast.NewIdent(t.Name), scope, false
}
//check type, fix bug test.0055
if i, ok := d.typ.(*ast.Ident); ok {
if i.Obj != nil && i.Obj.Decl != nil {
if typ, ok := i.Obj.Decl.(*ast.TypeSpec); ok {
return infer_type(typ.Type, scope, -1)
}
}
}
typ, scope := d.infer_type()
return typ, scope, d.class == decl_type
}
Expand Down Expand Up @@ -910,7 +918,6 @@ func infer_type(v ast.Expr, scope *scope, index int) (ast.Expr, *scope, bool) {
if it == nil {
break
}

if d := type_to_decl(it, s); d != nil {
c := d.find_child_and_in_embedded(t.Sel.Name)
if c != nil {
Expand Down
38 changes: 23 additions & 15 deletions declcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type package_import struct {
alias string
abspath string
path string
vpath string
}

// Parses import declarations until the first non-import declaration and fills
Expand All @@ -33,9 +34,9 @@ func collect_package_imports(filename string, decls []ast.Decl, context *package
for _, spec := range gd.Specs {
imp := spec.(*ast.ImportSpec)
path, alias := path_and_alias(imp)
abspath, ok := abs_path_for_package(filename, path, context)
abspath, vpath, ok := abs_path_for_package(filename, path, context)
if ok && alias != "_" {
pi = append(pi, package_import{alias, abspath, path})
pi = append(pi, package_import{alias, abspath, path, vpath})
}
}
} else {
Expand Down Expand Up @@ -149,17 +150,17 @@ func append_to_top_decls(decls map[string]*decl, decl ast.Decl, scope *scope) {
})
}

func abs_path_for_package(filename, p string, context *package_lookup_context) (string, bool) {
func abs_path_for_package(filename, p string, context *package_lookup_context) (string, string, bool) {
dir, _ := filepath.Split(filename)
if len(p) == 0 {
return "", false
return "", "", false
}
if p[0] == '.' {
return fmt.Sprintf("%s.a", filepath.Join(dir, p)), true
return fmt.Sprintf("%s.a", filepath.Join(dir, p)), "", true
}
pkg, ok := find_go_dag_package(p, dir)
if ok {
return pkg, true
return pkg, "", true
}
return find_global_file(p, context)
}
Expand Down Expand Up @@ -282,14 +283,15 @@ func log_build_context(context *package_lookup_context) {
// find_global_file returns the file path of the compiled package corresponding to the specified
// import, and a boolean stating whether such path is valid.
// TODO: Return only one value, possibly empty string if not found.
func find_global_file(imp string, context *package_lookup_context) (string, bool) {
// pkgpath, update importpath, bool
func find_global_file(imp string, context *package_lookup_context) (string, string, bool) {
// gocode synthetically generates the builtin package
// "unsafe", since the "unsafe.a" package doesn't really exist.
// Thus, when the user request for the package "unsafe" we
// would return synthetic global file that would be used
// just as a key name to find this synthetic package
if imp == "unsafe" {
return "unsafe", true
return "unsafe", "", true
}

pkgfile := fmt.Sprintf("%s.a", imp)
Expand All @@ -300,14 +302,14 @@ func find_global_file(imp string, context *package_lookup_context) (string, bool
pkg_path := filepath.Join(p, pkgfile)
if file_exists(pkg_path) {
log_found_package_maybe(imp, pkg_path)
return pkg_path, true
return pkg_path, "", true
}
// Also check the relevant pkg/OS_ARCH dir for the libpath, if provided.
pkgdir := fmt.Sprintf("%s_%s", context.GOOS, context.GOARCH)
pkg_path = filepath.Join(p, "pkg", pkgdir, pkgfile)
if file_exists(pkg_path) {
log_found_package_maybe(imp, pkg_path)
return pkg_path, true
return pkg_path, "", true
}
}
}
Expand All @@ -322,7 +324,7 @@ func find_global_file(imp string, context *package_lookup_context) (string, bool
pkg_path := filepath.Join(pkgdir, pkgfile)
if file_exists(pkg_path) {
log_found_package_maybe(imp, pkg_path)
return pkg_path, true
return pkg_path, "", true
}
}

Expand Down Expand Up @@ -352,7 +354,7 @@ func find_global_file(imp string, context *package_lookup_context) (string, bool
if !fi.IsDir() && filepath.Ext(fi.Name()) == ".a" {
pkg_path := filepath.Join(root, impath, fi.Name())
log_found_package_maybe(imp, pkg_path)
return pkg_path, true
return pkg_path, "", true
}
}
}
Expand All @@ -376,8 +378,8 @@ func find_global_file(imp string, context *package_lookup_context) (string, bool
try_autobuild(p)
if file_exists(p.PkgObj) {
log_found_package_maybe(imp, p.PkgObj)
return p.PkgObj, true
}
return p.PkgObj, p.ImportPath, true
}
if package_path == "" {
break
Expand All @@ -391,20 +393,26 @@ func find_global_file(imp string, context *package_lookup_context) (string, bool
}
}

for _, v := range g_daemon.autocomplete.walker.Imported {
if v.Path() == imp {
return imp, v.Path(), true
}
}

if p, err := context.Import(imp, "", build.AllowBinary|build.FindOnly); err == nil {
try_autobuild(p)
if file_exists(p.PkgObj) {
log_found_package_maybe(imp, p.PkgObj)
return p.PkgObj, true
}
return p.PkgObj, p.ImportPath, true
}

if *g_debug {
log.Printf("Import path %q was not resolved\n", imp)
log.Println("Gocode's build context is:")
log_build_context(context)
}
return "", false
return "", "", false
}

func package_name(file *ast.File) string {
Expand Down
Loading