Skip to content
This repository has been archived by the owner on Feb 17, 2021. It is now read-only.

Commit

Permalink
fix import paths in tests, make parse file concurrency safe
Browse files Browse the repository at this point in the history
  • Loading branch information
stamblerre committed Sep 24, 2018
1 parent 042cf91 commit 568f8ec
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion internal/lookdot/lookdot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"sort"
"testing"

"github.com/mdempsky/gocode/internal/lookdot"
"github.com/stamblerre/gocode/internal/lookdot"
)

const src = `
Expand Down
3 changes: 1 addition & 2 deletions internal/suggest/formatters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"bytes"
"testing"

"github.com/mdempsky/gocode/internal/suggest"
"github.com/stamblerre/gocode/internal/suggest"
)

func TestFormatters(t *testing.T) {
// TODO(mdempsky): More comprehensive test.

num := len("client")
candidates := []suggest.Candidate{{
Class: "func",
Expand Down
16 changes: 12 additions & 4 deletions internal/suggest/suggest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"go/token"
"go/types"
"os"
"sync"

"github.com/stamblerre/gocode/internal/lookdot"
"golang.org/x/tools/go/packages"
Expand Down Expand Up @@ -112,6 +113,7 @@ func sameFile(filename1, filename2 string) bool {

func (c *Config) analyzePackage(filename string, data []byte, cursor int) (*token.FileSet, token.Pos, *types.Package) {
var pos token.Pos
var posMu sync.Mutex // guards pos in ParseFile

cfg := &packages.Config{
Mode: packages.LoadSyntax,
Expand All @@ -121,6 +123,7 @@ func (c *Config) analyzePackage(filename string, data []byte, cursor int) (*toke
Tests: true,
ParseFile: func(fset *token.FileSet, parseFilename string) (*ast.File, error) {
var src interface{}
var filePos token.Pos
mode := parser.DeclarationErrors
if sameFile(filename, parseFilename) {
// If we're in trailing white space at the end of a scope,
Expand All @@ -134,14 +137,19 @@ func (c *Config) analyzePackage(filename string, data []byte, cursor int) (*toke
return nil, err
}
if sameFile(filename, parseFilename) {
pos = fset.File(file.Pos()).Pos(cursor)
if pos == token.NoPos {
filePos = fset.File(file.Pos()).Pos(cursor)
if filePos == token.NoPos {
return nil, fmt.Errorf("no position for cursor in %s", parseFilename)
}
posMu.Lock()
if pos == token.NoPos {
pos = filePos
}
posMu.Unlock()
}
for _, decl := range file.Decls {
if fd, ok := decl.(*ast.FuncDecl); ok {
if pos == token.NoPos || (pos < fd.Pos() || pos >= fd.End()) {
if filePos == token.NoPos || (filePos < fd.Pos() || filePos >= fd.End()) {
fd.Body = nil
}
}
Expand All @@ -153,7 +161,7 @@ func (c *Config) analyzePackage(filename string, data []byte, cursor int) (*toke
if len(pkgs) <= 0 { // ignore errors
return nil, token.NoPos, nil
}
pkg := pkgs[len(pkgs)-1]
pkg := pkgs[0]

return pkg.Fset, pos, pkg.Types
}
Expand Down
2 changes: 1 addition & 1 deletion internal/suggest/suggest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
"testing"

"github.com/mdempsky/gocode/internal/suggest"
"github.com/stamblerre/gocode/internal/suggest"
)

func TestRegress(t *testing.T) {
Expand Down

0 comments on commit 568f8ec

Please sign in to comment.