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 11 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
26 changes: 26 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
10 changes: 10 additions & 0 deletions declcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,16 @@ func find_global_file(imp string, context *package_lookup_context) (string, bool
}
}

if g_daemon != nil && g_daemon.modList != nil {
pkg, _, _ := g_daemon.modList.LookupModule(imp)
if pkg != nil {
if *g_debug {
log.Println("lookup module", pkg.Path, pkg.Dir)
}
return imp, true
}
}

if p, err := context.Import(imp, "", build.AllowBinary|build.FindOnly); err == nil {
try_autobuild(p)
if file_exists(p.PkgObj) {
Expand Down
15 changes: 15 additions & 0 deletions gocode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"go/build"
"testing"
)

func TestGocode(t *testing.T) {
var ctx package_lookup_context
ctx.Context = build.Default
*g_debug = true
resolveKnownPackageIdent("fmt", "gocode.go", &ctx)
resolveKnownPackageIdent("os", "gocode.go", &ctx)
resolveKnownPackageIdent("http", "gocode.go", &ctx)
}
75 changes: 56 additions & 19 deletions package.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"go/ast"
"log"
"os"
"strings"
)
Expand Down Expand Up @@ -78,6 +79,10 @@ func (m *package_file_cache) update_cache() {
fname := m.find_file()
stat, err := os.Stat(fname)
if err != nil {
if *g_debug {
log.Println("update cache source", m.import_name)
}
m.process_package_data(nil, true)
return
}

Expand All @@ -89,43 +94,75 @@ func (m *package_file_cache) update_cache() {
if err != nil {
return
}
m.process_package_data(data)
m.process_package_data(data, false)
}
}

func (m *package_file_cache) process_package_data(data []byte) {
func (m *package_file_cache) process_package_data(data []byte, source bool) {
m.scope = new_named_scope(g_universe_scope, m.name)

// find import section
i := bytes.Index(data, []byte{'\n', '$', '$'})
if i == -1 {
panic(fmt.Sprintf("Can't find the import section in the package file %s", m.name))
if !source {
i := bytes.Index(data, []byte{'\n', '$', '$'})
if i == -1 {
panic(fmt.Sprintf("Can't find the import section in the package file %s", m.name))
}
data = data[i+len("\n$$"):]
}
data = data[i+len("\n$$"):]

// main package
m.main = new_decl(m.name, decl_package, nil)
// create map for other packages
m.others = make(map[string]*decl)

var pp package_parser
if data[0] == 'B' {
// binary format, skip 'B\n'
data = data[2:]
if source {
var tp types_parser
var srcDir string
if g_daemon.modList != nil {
pkg, _, dir := g_daemon.modList.LookupModule(m.import_name)
if pkg != nil {
srcDir = dir
if *g_debug {
log.Println(m.import_name, srcDir)
}
}
}
tp.init(m.import_name, srcDir, m, true)
data = tp.exportData()
if data == nil {
return
}
var p gc_bin_parser
p.init(data, m)
pp = &p
} else {
// textual format, find the beginning of the package clause
i = bytes.Index(data, []byte{'p', 'a', 'c', 'k', 'a', 'g', 'e'})
if i == -1 {
panic("Can't find the package clause")
}
data = data[i:]
if data[0] == 'B' {
// binary format, skip 'B\n'
data = data[2:]
if data[0] == 'i' {
var tp types_parser
tp.init(m.import_name, "", m, false)
data = tp.exportData()
if data == nil {
return
}
}
var p gc_bin_parser
p.init(data, m)
pp = &p
} else {
// textual format, find the beginning of the package clause
i := bytes.Index(data, []byte{'p', 'a', 'c', 'k', 'a', 'g', 'e'})
if i == -1 {
panic("Can't find the package clause")
}
data = data[i:]

var p gc_parser
p.init(data, m)
pp = &p
var p gc_parser
p.init(data, m)
pp = &p
}
}

prefix := "!" + m.name + "!"
Expand Down Expand Up @@ -249,6 +286,6 @@ $$

func (c package_cache) add_builtin_unsafe_package() {
pkg := new_package_file_cache_forever("unsafe", "unsafe")
pkg.process_package_data(g_builtin_unsafe_package)
pkg.process_package_data(g_builtin_unsafe_package, false)
c["unsafe"] = pkg
}
24 changes: 22 additions & 2 deletions package_bin.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ func (p *gc_bin_parser) parse_export(callback func(string, ast.Decl)) {

// read version specific flags - extend as necessary
switch p.version {
// case 6:
// case 7:
// ...
// fallthrough
case 5, 4, 3, 2, 1:
case 6, 5, 4, 3, 2, 1:
p.debugFormat = p.rawStringln(p.rawByte()) == "debug"
p.trackAllTypes = p.int() != 0
p.posInfoFormat = p.int() != 0
Expand Down Expand Up @@ -152,6 +152,9 @@ func (p *gc_bin_parser) parse_export(callback func(string, ast.Decl)) {
}
}

// MaxPkgHeight is a height greater than any likely package height.
const MaxPkgHeight = 1e9

func (p *gc_bin_parser) pkg() string {
// if the package was seen before, i is its index (>= 0)
i := p.tagOrIndex()
Expand All @@ -172,6 +175,10 @@ func (p *gc_bin_parser) pkg() string {
} else {
path = p.string()
}
var height int
if p.version >= 6 {
height = p.int()
}

// we should never see an empty package name
if name == "" {
Expand All @@ -184,6 +191,18 @@ func (p *gc_bin_parser) pkg() string {
panic(fmt.Sprintf("package path %q for pkg index %d", path, len(p.pkgList)))
}

if p.version >= 6 {
if height < 0 || height >= MaxPkgHeight {
panic(fmt.Sprintf("bad package height %v for package %s", height, name))
}

// reexported packages should always have a lower height than
// the main package
// if len(p.pkgList) != 0 && height >= p.imp.Height {
// p.formatErrorf("package %q (height %d) reexports package %q (height %d)", p.imp.Path, p.imp.Height, path, height)
// }
}

var fullName string
if path != "" {
fullName = "!" + path + "!" + name
Expand All @@ -193,6 +212,7 @@ func (p *gc_bin_parser) pkg() string {
}

// if the package was imported before, use that one; otherwise create a new one
// pkg.Height = height
p.pkgList = append(p.pkgList, fullName)
return p.pkgList[len(p.pkgList)-1]
}
Expand Down
41 changes: 41 additions & 0 deletions package_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"bytes"
"go/build"
"go/importer"
"go/token"
"go/types"

"github.com/visualfc/gotools/pkg/srcimporter"
"golang.org/x/tools/go/gcexportdata"
)

type types_parser struct {
pfc *package_file_cache
pkg *types.Package
}

func (p *types_parser) init(path string, dir string, pfc *package_file_cache, source bool) {
if source {
im := srcimporter.New(&build.Default, token.NewFileSet(), make(map[string]*types.Package))
if dir != "" {
p.pkg, _ = im.ImportFrom(path, dir, 0)
} else {
p.pkg, _ = im.Import(path)
}
} else {
p.pkg, _ = importer.Default().Import(path)
}
p.pfc = pfc
}

func (p *types_parser) exportData() []byte {
if p.pkg == nil {
return nil
}
fset := token.NewFileSet()
var buf bytes.Buffer
gcexportdata.Write(&buf, fset, p.pkg)
return buf.Bytes()
}
4 changes: 4 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"reflect"
"runtime"
"time"

"github.com/visualfc/gotools/pkg/gomod"
)

func do_server() int {
Expand Down Expand Up @@ -58,6 +60,7 @@ type daemon struct {
pkgcache package_cache
declcache *decl_cache
context package_lookup_context
modList *gomod.ModuleList
}

func new_daemon(network, address string) *daemon {
Expand Down Expand Up @@ -181,6 +184,7 @@ func server_auto_complete(file []byte, filename string, cursor int, context_pack
} else if *g_debug {
log.Printf("Go project path not found: %s", err)
}
g_daemon.modList = gomod.LooupModList(filepath.Dir(filename))
}
if *g_debug {
var buf bytes.Buffer
Expand Down
28 changes: 28 additions & 0 deletions vendor/github.com/visualfc/gotools/LICENSE

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

Loading