[tools] internal/astutil: return enclosing node when Select finds no enclosing node

0 views
Skip to first unread message

Gopher Robot (Gerrit)

unread,
Dec 18, 2025, 10:44:04 PM (2 days ago) Dec 18
to Hongxiang Jiang, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Go LUCI, Alan Donovan, Madeline Kalil, golang-co...@googlegroups.com

Gopher Robot submitted the change with unreviewed changes

Unreviewed changes

5 is the latest approved patch-set.
The change was submitted with unreviewed changes in the following files:

```
The name of the file: gopls/internal/golang/type_definition.go
Insertions: 1, Deletions: 1.

@@ -31,7 +31,7 @@
if err != nil {
return nil, err
}
- cur, _, _, _ := astutil.Select(pgf.Cursor, start, end) // can't happen
+ cur, _, _, _ := astutil.Select(pgf.Cursor(), start, end) // can't fail (start, end are within File)

// Find innermost enclosing expression that has a type.
// It needn't be an identifier.
@@ -66,8 +66,6 @@
if t == nil {
return nil, fmt.Errorf("no enclosing expression has a type")
}
- // TODO(hxjiang, adnonvan): check the ergonomics in language clients (VSCode,
- // vim, emacs) and support basic types.
tnames := typeToObjects(t)
if len(tnames) == 0 {
return nil, fmt.Errorf("cannot find type name(s) from type %s", t)
```
```
The name of the file: internal/astutil/util.go
Insertions: 3, Deletions: 2.

@@ -179,7 +179,7 @@
// Select returns the enclosing BlockStmt, the f() CallExpr, and the g() CallExpr.
//
// If the selection does not wholly enclose any nodes, Select returns an error
-// and invalid stsrt/end nodes, but it may return a valid enclosing node.
+// and invalid start/end nodes, but it may return a valid enclosing node.
//
// Callers that require exactly one syntax tree (e.g. just f() or just
// g()) should check that the returned start and end nodes are
@@ -219,7 +219,8 @@
if !CursorValid(curStart) {
// The selection is valid (inside curEnclosing) but contains no
// complete nodes. This happens for point selections (start == end),
- // or selections covering only tokens/whitespace.
+ // or selections covering only only spaces, comments, and punctuation
+ // tokens.
// Return the enclosing node so the caller can still use the context.
return curEnclosing, noCursor, noCursor, fmt.Errorf("invalid selection")
}
```

Change information

Commit message:
internal/astutil: return enclosing node when selection contains no nodes

Previously, if a selection range did not wholly enclose any syntax nodes
(e.g., point selections or ranges covering only whitespace), Select
returned an error with all noCursor return values.

Select now returns the valid enclosing cursor alongside the error in
these cases, while keeping start and end as noCursor.

This allows callers to inspect the surrounding syntax tree even when the
selection does not map to specific sub-nodes.
Change-Id: I623cd5bc1b4173970ad1dc3e7baae79aa74cc5de
Reviewed-by: Alan Donovan <adon...@google.com>
Auto-Submit: Hongxiang Jiang <hxj...@golang.org>
Files:
  • M gopls/internal/golang/type_definition.go
  • M internal/astutil/util.go
Change size: S
Delta: 2 files changed, 10 insertions(+), 5 deletions(-)
Branch: refs/heads/master
Submit Requirements:
  • requirement satisfiedCode-Review: +2 by Alan Donovan
  • requirement satisfiedTryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI
Open in Gerrit
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: merged
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I623cd5bc1b4173970ad1dc3e7baae79aa74cc5de
Gerrit-Change-Number: 730320
Gerrit-PatchSet: 7
Gerrit-Owner: Hongxiang Jiang <hxj...@golang.org>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Hongxiang Jiang <hxj...@golang.org>
Gerrit-Reviewer: Madeline Kalil <mka...@google.com>
open
diffy
satisfied_requirement
Reply all
Reply to author
Forward
0 new messages