Skip to content

Commit a586d0d

Browse files
committed
go/types/internal/play: show more types.Scope detail
Change-Id: Idc02ca548f4412af95bf08d5799a0ce286b1595c Reviewed-on: https://go-review.googlesource.com/c/tools/+/543956 Reviewed-by: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 53ad329 commit a586d0d

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

go/types/internal/play/play.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,39 @@ func handleSelectJSON(w http.ResponseWriter, req *http.Request) {
125125
startOffset, endOffset, exact)
126126
var innermostExpr ast.Expr
127127
for i, n := range path {
128+
// Show set of names defined in each scope.
129+
scopeNames := ""
130+
{
131+
node := n
132+
prefix := ""
133+
134+
// A function (Func{Decl.Lit}) doesn't have a scope of its
135+
// own, nor does its Body: only nested BlockStmts do.
136+
// The type parameters, parameters, and locals are all
137+
// in the scope associated with the FuncType; show it.
138+
switch n := n.(type) {
139+
case *ast.FuncDecl:
140+
node = n.Type
141+
prefix = "Type."
142+
case *ast.FuncLit:
143+
node = n.Type
144+
prefix = "Type."
145+
}
146+
147+
if scope := pkg.TypesInfo.Scopes[node]; scope != nil {
148+
scopeNames = fmt.Sprintf(" %sScope={%s}",
149+
prefix,
150+
strings.Join(scope.Names(), ", "))
151+
}
152+
}
153+
128154
// TODO(adonovan): turn these into links to highlight the source.
129155
start, end := fset.Position(n.Pos()), fset.Position(n.End())
130-
fmt.Fprintf(out, "[%d] %T @ %d:%d-%d:%d (#%d-%d)\n",
156+
fmt.Fprintf(out, "[%d] %T @ %d:%d-%d:%d (#%d-%d)%s\n",
131157
i, n,
132158
start.Line, start.Column, end.Line,
133-
end.Column, start.Offset, end.Offset)
159+
end.Column, start.Offset, end.Offset,
160+
scopeNames)
134161
if e, ok := n.(ast.Expr); ok && innermostExpr == nil {
135162
innermostExpr = e
136163
}
@@ -282,7 +309,12 @@ func formatObj(out *strings.Builder, fset *token.FileSet, ref string, obj types.
282309
// scope tree
283310
fmt.Fprintf(out, "Scopes:\n")
284311
for scope := obj.Parent(); scope != nil; scope = scope.Parent() {
285-
fmt.Fprintln(out, scope)
312+
var (
313+
start = fset.Position(scope.Pos())
314+
end = fset.Position(scope.End())
315+
)
316+
fmt.Fprintf(out, "%d:%d-%d:%d: %s\n",
317+
start.Line, start.Column, end.Line, end.Column, scope)
286318
}
287319
}
288320

0 commit comments

Comments
 (0)