Skip to content

Commit 5a815de

Browse files
adonovangopherbot
authored andcommitted
go/types/internal/play: show astutil.Select results too
Change-Id: Ie9b6ba33f5e8e6aeaa43147d2004782953afc664 Reviewed-on: https://go-review.googlesource.com/c/tools/+/730640 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Alan Donovan <adonovan@google.com> Reviewed-by: Hongxiang Jiang <hxjiang@golang.org>
1 parent 7f7199c commit 5a815de

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

go/types/internal/play/play.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ import (
3030
"strconv"
3131
"strings"
3232

33-
"golang.org/x/tools/go/ast/astutil"
33+
goastutil "golang.org/x/tools/go/ast/astutil"
3434
"golang.org/x/tools/go/ast/inspector"
3535
"golang.org/x/tools/go/packages"
3636
"golang.org/x/tools/go/types/typeutil"
37+
"golang.org/x/tools/internal/astutil"
3738
"golang.org/x/tools/internal/typeparams"
3839
)
3940

@@ -121,7 +122,7 @@ func handleSelectJSON(w http.ResponseWriter, req *http.Request) {
121122
endPos := tokFile.Pos(endOffset)
122123

123124
// Syntax information
124-
path, exact := astutil.PathEnclosingInterval(file, startPos, endPos)
125+
path, exact := goastutil.PathEnclosingInterval(file, startPos, endPos)
125126
fmt.Fprintf(out, "Path enclosing interval #%d-%d [exact=%t]:\n",
126127
startOffset, endOffset, exact)
127128
var innermostExpr ast.Expr
@@ -163,16 +164,26 @@ func handleSelectJSON(w http.ResponseWriter, req *http.Request) {
163164
innermostExpr = e
164165
}
165166
}
166-
// Show the cursor stack too.
167+
// Show the Cursor.Enclosing stack too.
167168
// It's usually the same, but may differ in edge
168169
// cases (e.g. around FuncType.Func).
169-
inspect := inspector.New([]*ast.File{file})
170-
if cur, ok := inspect.Root().FindByPos(startPos, endPos); ok {
171-
fmt.Fprintf(out, "Cursor.FindPos().Enclosing() = %v\n",
170+
curFile, _ := inspector.New([]*ast.File{file}).Root().FirstChild()
171+
if cur, ok := curFile.FindByPos(startPos, endPos); ok {
172+
fmt.Fprintf(out, "Cursor.FindByPos().Enclosing() = %v\n",
172173
slices.Collect(cur.Enclosing()))
173174
} else {
174175
fmt.Fprintf(out, "Cursor.FindPos() failed\n")
175176
}
177+
// And show the astutil.Select result (enclosing, leftmost & rightmost enclosed).
178+
if curEnclosing, curStart, curEnd, err := astutil.Select(curFile, startPos, endPos); err == nil {
179+
format := func(cur inspector.Cursor) string {
180+
return fmt.Sprintf("%T@%v", cur.Node(), fset.Position(cur.Node().Pos()))
181+
}
182+
fmt.Fprintf(out, "astutil.Select() = (%s, %s, %s)\n",
183+
format(curEnclosing), format(curStart), format(curEnd))
184+
} else {
185+
fmt.Fprintf(out, "astutil.Select() = %v", err)
186+
}
176187
fmt.Fprintf(out, "\n")
177188

178189
// Expression type information
@@ -266,7 +277,7 @@ func handleSelectJSON(w http.ResponseWriter, req *http.Request) {
266277

267278
// Show inventory of all objects, including addresses to disambiguate.
268279
fmt.Fprintf(out, "Objects:\n")
269-
for curId := range inspect.Root().Preorder((*ast.Ident)(nil)) {
280+
for curId := range curFile.Preorder((*ast.Ident)(nil)) {
270281
id := curId.Node().(*ast.Ident)
271282
if obj := pkg.TypesInfo.Defs[id]; obj != nil {
272283
fmt.Fprintf(out, "%s: def %v (%p)\n", fset.Position(id.Pos()), obj, obj)

0 commit comments

Comments
 (0)