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

Add identifier range info #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
52 changes: 36 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ import (
"golang.org/x/tools/go/buildutil"
)

type Range struct {
Start token.Pos `json:"start"`
End token.Pos `json:"end"`
}

type Declaration struct {
Label string `json:"label"`
Type string `json:"type"`
ReceiverType string `json:"receiverType,omitempty"`
Start token.Pos `json:"start"`
End token.Pos `json:"end"`
Children []Declaration `json:"children,omitempty"`
Label string `json:"label"`
Type string `json:"type"`
ReceiverType string `json:"receiverType,omitempty"`
Range
Identifier *Range `json:"identifier,omitempty"`
Children []Declaration `json:"children,omitempty"`
}

var (
Expand All @@ -29,6 +34,21 @@ var (
modified = flag.Bool("modified", false, "read an archive of the modified file from standard input")
)

func nodeRange(n ast.Node) Range {
return Range{
Start: n.Pos(),
End: n.End(),
}
}

func identRange(i *ast.Ident) *Range {
if i == nil {
return nil
}
r := nodeRange(i)
return &r
}

func main() {
flag.Parse()
fset := token.NewFileSet()
Expand Down Expand Up @@ -71,8 +91,8 @@ func main() {
decl.Name.String(),
"function",
receiverType,
decl.Pos(),
decl.End(),
nodeRange(decl),
identRange(decl.Name),
[]Declaration{},
})
case *ast.GenDecl:
Expand All @@ -83,8 +103,8 @@ func main() {
spec.Path.Value,
"import",
"",
spec.Pos(),
spec.End(),
nodeRange(spec),
identRange(spec.Name),
[]Declaration{},
})
case *ast.TypeSpec:
Expand All @@ -93,8 +113,8 @@ func main() {
spec.Name.String(),
"type",
"",
spec.Pos(),
spec.End(),
nodeRange(spec),
identRange(spec.Name),
[]Declaration{},
})
case *ast.ValueSpec:
Expand All @@ -103,8 +123,8 @@ func main() {
id.Name,
"variable",
"",
id.Pos(),
id.End(),
nodeRange(id),
identRange(id),
[]Declaration{},
})
}
Expand All @@ -121,8 +141,8 @@ func main() {
fileAst.Name.String(),
"package",
"",
fileAst.Pos(),
fileAst.End(),
nodeRange(fileAst),
identRange(fileAst.Name),
declarations,
}}

Expand Down