Skip to content

Commit

Permalink
Expose filenames in Positions only as relative paths under their resp…
Browse files Browse the repository at this point in the history
…ective corpora
  • Loading branch information
korfuri committed Jul 12, 2017
1 parent 9345b6c commit 87cb030
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
6 changes: 5 additions & 1 deletion package.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type Package struct {

// Path is the package's load path
Path string

// Corpus is the corpus that contains this package
Corpus
}

// String implements the Stringer interface
Expand Down Expand Up @@ -78,7 +81,7 @@ func (p Package) MarshalJSON() ([]byte, error) {
})
}

func newPackage(pi *loader.PackageInfo, fset *token.FileSet, version int64) *Package {
func newPackage(pi *loader.PackageInfo, fset *token.FileSet, version int64, corpus Corpus) *Package {
return &Package{
//PackageInfo: pi,
Name: pi.Pkg.Name(),
Expand All @@ -89,5 +92,6 @@ func newPackage(pi *loader.PackageInfo, fset *token.FileSet, version int64) *Pac
Fset: fset,
Version: version,
Path: pi.Pkg.Path(),
Corpus: corpus,
}
}
36 changes: 25 additions & 11 deletions packagegraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type PackageGraph struct {

// Map of file path to File objects.

// Slice of corpora that files may be loaded from
corpora []Corpus

// versionF is a function that returns the version of the
// provided Go package as an int64.
//
Expand Down Expand Up @@ -151,12 +154,24 @@ func (pg *PackageGraph) loadPackage(prog *loader.Program, loadpath string, pi *l
return nil
}

// Find what corpus this package was loaded from.
var corpus Corpus
if len(pi.Files) > 0 {
fpath := prog.Fset.File(pi.Files[0].Package).Name()
for _, c := range pg.corpora {
if c.Contains(fpath) {
corpus = c
break
}
}
}

// If pkg was a hardcoded package, return it now. Otherwise,
// create it.
if pkg != nil {
return pkg
}
pkg = newPackage(pi, prog.Fset, version)
pkg = newPackage(pi, prog.Fset, version, corpus)
pg.Packages[loadpath] = pkg

// Iterate over all files in that package.
Expand Down Expand Up @@ -207,8 +222,8 @@ func (pg *PackageGraph) loadPackage(prog *loader.Program, loadpath string, pi *l
if f.Name != nil {
r := &Ref{
RefType: Import,
FromPosition: NewPosition(prog.Fset, imported.Pos(), imported.End()),
ToPosition: NewPosition(prog.Fset, f.Name.Pos(), f.Name.End()),
FromPosition: NewPosition(corpus, prog.Fset, imported.Pos(), imported.End()),
ToPosition: NewPosition(corpus, prog.Fset, f.Name.Pos(), f.Name.End()),
FromIdent: importAs,
ToIdent: i.Pkg.Name(),
FromPackage: pkg,
Expand All @@ -234,10 +249,10 @@ func (pg *PackageGraph) loadPackage(prog *loader.Program, loadpath string, pi *l
RefType: refTypeForIdent(prog, id),
ToIdent: obj.Name(),
ToPackage: foreignPkg,
ToPosition: NewPosition(prog.Fset, obj.Pos(), NoPos),
ToPosition: NewPosition(corpus, prog.Fset, obj.Pos(), NoPos),
FromIdent: id.Name,
FromPackage: pkg,
FromPosition: NewPosition(prog.Fset, id.Pos(), id.End()),
FromPosition: NewPosition(corpus, prog.Fset, id.Pos(), id.End()),
}

foreignPkg.InRefs = append(foreignPkg.InRefs, ref)
Expand Down Expand Up @@ -302,15 +317,14 @@ func (pg *PackageGraph) ComputeInterfaceImplementationMatrix() {
continue
}
if types.AssignableTo(typ, iface) {
fset := pb.Fset
r := &Ref{
RefType: Implementation,
ToIdent: iface.Obj().Name(),
ToPackage: pa,
ToPosition: NewPosition(fset, iface.Obj().Pos(), NoPos),
ToPosition: NewPosition(pa.Corpus, pa.Fset, iface.Obj().Pos(), NoPos),
FromIdent: typ.Obj().Name(),
FromPackage: pb,
FromPosition: NewPosition(fset, typ.Obj().Pos(), NoPos),
FromPosition: NewPosition(pb.Corpus, pb.Fset, typ.Obj().Pos(), NoPos),
}
pa.InRefs = append(pa.InRefs, r)
pb.OutRefs = append(pb.OutRefs, r)
Expand All @@ -321,16 +335,15 @@ func (pg *PackageGraph) ComputeInterfaceImplementationMatrix() {
continue
}
if types.AssignableTo(ifaceb, iface) {
fset := pb.Fset
r := &Ref{
RefType: Extension,
ToIdent: iface.Obj().Name(),
ToPackage: pa,
ToPosition: NewPosition(fset, ifaceb.Obj().Pos(), NoPos),
ToPosition: NewPosition(pa.Corpus, pa.Fset, ifaceb.Obj().Pos(), NoPos),

FromIdent: ifaceb.Obj().Name(),
FromPackage: pb,
FromPosition: NewPosition(fset, ifaceb.Obj().Pos(), NoPos),
FromPosition: NewPosition(pb.Corpus, pb.Fset, ifaceb.Obj().Pos(), NoPos),
}
pa.InRefs = append(pa.InRefs, r)
pb.OutRefs = append(pb.OutRefs, r)
Expand All @@ -347,6 +360,7 @@ func NewPackageGraph(versionF func(loader.Program, loader.PackageInfo) (int64, e
Packages: make(map[string]*Package),
versionF: versionF,
filterF: FilterPass,
corpora: DefaultCorpora(),
}
return p
}
Expand Down
6 changes: 3 additions & 3 deletions position.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ func (p Position) String() string {
// NewPosition creates a Position from a token.FileSet and a pair of
// Pos in that FileSet. It will panic if both Pos are not from the
// same Filename.
func NewPosition(fset *token.FileSet, pos, end token.Pos) Position {
func NewPosition(corpus Corpus, fset *token.FileSet, pos, end token.Pos) Position {
ppos := fset.Position(pos)
if end == token.NoPos {
return Position{
File: ppos.Filename,
File: corpus.Rel(ppos.Filename),
PosL: ppos.Line,
PosC: ppos.Column,
EndL: -1,
Expand All @@ -58,7 +58,7 @@ func NewPosition(fset *token.FileSet, pos, end token.Pos) Position {
panic("Invalid pair of {pos,end} for NewPosition: pos and end come from different files!")
}
return Position{
File: ppos.Filename,
File: corpus.Rel(ppos.Filename),
PosL: ppos.Line,
PosC: ppos.Column,
EndL: pend.Line,
Expand Down
4 changes: 2 additions & 2 deletions simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestSimplePackage(t *testing.T) {
const (
pkgpath = "github.com/korfuri/goref/testprograms/simple"
filepath = "testprograms/simple/main.go"
filepath = "github.com/korfuri/goref/testprograms/simple/main.go"
)

pg := goref.NewPackageGraph(goref.ConstantVersion(0))
Expand All @@ -30,5 +30,5 @@ func TestSimplePackage(t *testing.T) {
assert.Equal(t, 6, p.PosC)
assert.Equal(t, 6, p.EndL)
assert.Equal(t, 13, p.EndC)
assert.Contains(t, p.File, filepath)
assert.Equal(t, filepath, p.File)
}

0 comments on commit 87cb030

Please sign in to comment.