Skip to content

Commit 61da5cd

Browse files
h9jianggopherbot
authored andcommitted
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-on: https://go-review.googlesource.com/c/tools/+/730320 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Alan Donovan <adonovan@google.com> Auto-Submit: Hongxiang Jiang <hxjiang@golang.org>
1 parent 454a5c0 commit 61da5cd

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

‎gopls/internal/golang/type_definition.go‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ func TypeDefinition(ctx context.Context, snapshot *cache.Snapshot, fh file.Handl
3131
if err != nil {
3232
return nil, err
3333
}
34-
cur, ok := pgf.Cursor().FindByPos(start, end)
35-
if !ok {
36-
return nil, fmt.Errorf("no enclosing syntax") // can't happen
37-
}
34+
cur, _, _, _ := astutil.Select(pgf.Cursor(), start, end) // can't fail (start, end are within File)
3835

3936
// Find innermost enclosing expression that has a type.
4037
// It needn't be an identifier.

‎internal/astutil/util.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ func (r Range) IsValid() bool { return r.Start.IsValid() && r.Start <= r.EndPos
178178
//
179179
// Select returns the enclosing BlockStmt, the f() CallExpr, and the g() CallExpr.
180180
//
181+
// If the selection does not wholly enclose any nodes, Select returns an error
182+
// and invalid start/end nodes, but it may return a valid enclosing node.
183+
//
181184
// Callers that require exactly one syntax tree (e.g. just f() or just
182185
// g()) should check that the returned start and end nodes are
183186
// identical.
@@ -214,7 +217,12 @@ func Select(curFile inspector.Cursor, start, end token.Pos) (_enclosing, _start,
214217
}
215218
}
216219
if !CursorValid(curStart) {
217-
return noCursor, noCursor, noCursor, fmt.Errorf("no syntax selected")
220+
// The selection is valid (inside curEnclosing) but contains no
221+
// complete nodes. This happens for point selections (start == end),
222+
// or selections covering only only spaces, comments, and punctuation
223+
// tokens.
224+
// Return the enclosing node so the caller can still use the context.
225+
return curEnclosing, noCursor, noCursor, fmt.Errorf("invalid selection")
218226
}
219227
return curEnclosing, curStart, curEnd, nil
220228
}

0 commit comments

Comments
 (0)