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

Solution for #42 #43

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions go/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1967,6 +1967,9 @@ func parseImportSpec(p *parser, doc *ast.CommentGroup, decl *ast.GenDecl, _ int)
if declIdent == nil {
filename := p.fset.Position(path.Pos()).Filename
name, err := p.pathToName(litToString(path), filepath.Dir(filename))
if litToString(path) == "C" {
name = "C"
}
if name == "" {
p.error(path.Pos(), fmt.Sprintf("cannot find identifier for package %q: %v", litToString(path), err))
} else {
Expand Down
69 changes: 69 additions & 0 deletions godef.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var tflag = flag.Bool("t", false, "print type information")
var aflag = flag.Bool("a", false, "print public type and member information")
var Aflag = flag.Bool("A", false, "print all type and members information")
var fflag = flag.String("f", "", "Go source filename")
var pflag = flag.String("p", "", "Go file path (used with -i, otherwise ignored)")
var acmeFlag = flag.Bool("acme", false, "use current acme window")
var jsonFlag = flag.Bool("json", false, "output location in JSON format (-t flag is ignored)")

Expand Down Expand Up @@ -61,6 +62,7 @@ func main() {
}
filename, src, searchpos = afile.name, afile.body, afile.offset
} else if *readStdin {
filename = *pflag
src, _ = ioutil.ReadAll(os.Stdin)
} else {
// TODO if there's no filename, look in the current
Expand Down Expand Up @@ -208,6 +210,73 @@ func (o orderedObjects) Len() int { return len(o) }
func (o orderedObjects) Swap(i, j int) { o[i], o[j] = o[j], o[i] }

func done(obj *ast.Object, typ types.Type) {
defer os.Exit(0)
pos := types.FileSet.Position(types.DeclPos(obj))
if *jsonFlag && *Aflag {
type field struct {
Name string `json:"name,omitempty"`
Def string `json:"def,omitempty"`
Type string `json:"type,omitempty"`
Filename string `json:"filename,omitempty"`
Line int `json:"line,omitempty"`
Column int `json:"column,omitempty"`
}
var JSONOutput = struct {
Filename string `json:"filename,omitempty"`
Line int `json:"line,omitempty"`
Column int `json:"column,omitempty"`
Definition string `json:"definition,omitempty"`
Fields []*field `json:"fields,omitmepty"`
}{
Filename: pos.Filename,
Line: pos.Line,
Column: pos.Column,
}
if typ.Kind == ast.Bad || !*tflag {
jsonStr, err := json.Marshal(JSONOutput)
if err != nil {
fail("JSON marshal error: %v", err)
}
fmt.Println(string(jsonStr))
return
}

JSONOutput.Definition = typeStr(obj, typ)
var m orderedObjects
for obj := range typ.Iter() {
m = append(m, obj)
}
// sort.Sort(m)
JSONOutput.Fields = make([]*field, 0, len(m))
ts := map[ast.ObjKind]string{ast.Fun: "function", ast.Var: "variable"}
for _, obj := range m {
id := ast.NewIdent(obj.Name)
id.Obj = obj
if obj.Kind == ast.Fun || obj.Kind == ast.Var {
_, mt := types.ExprType(id, types.DefaultImporter, types.FileSet)
var innerPos = types.FileSet.Position(types.DeclPos(obj))
f := &field{
Name: obj.Name,
Def: fmt.Sprint(prettyType{mt}),
Type: ts[obj.Kind],
Filename: innerPos.Filename,
Line: innerPos.Line,
Column: innerPos.Column,
}
JSONOutput.Fields = append(JSONOutput.Fields, f)
}
}
jsonStr, err := json.MarshalIndent(JSONOutput, "", "\t")
if err != nil {
fail("JSON marshal error: %v", err)
}
fmt.Println(string(jsonStr))
} else {
doneSimple(obj, typ)
}
}

func doneSimple(obj *ast.Object, typ types.Type) {
defer os.Exit(0)
pos := types.FileSet.Position(types.DeclPos(obj))
if *jsonFlag {
Expand Down