Skip to content

Commit 7086838

Browse files
committed
gopls/internal/golang: Extract function: simplify using Cursor
This change deletes adjustRangeForCommentsAndWhiteSpace, which duplicates functionality in PathEnclosingInterval to turn a rough selection (including extraneous whitespace) into a precise selection aligned with syntax nodes. Instead we use a new function, astutil.Select, that does the same thing but on Cursors, and push cursors throughout the rest of the extract logic. In due course it should replace all existing uses of PathEnclosingInterval. Extract Variable will be dealt with in a follow-up. Change-Id: I7c67757ffa4233d5e7a6f02e44e68e14c46faee7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/716340 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Madeline Kalil <mkalil@google.com> Auto-Submit: Alan Donovan <adonovan@google.com>
1 parent 63a7b87 commit 7086838

File tree

5 files changed

+156
-202
lines changed

5 files changed

+156
-202
lines changed

gopls/internal/golang/codeaction.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ func goDoc(ctx context.Context, req *codeActionsRequest) error {
483483
// refactorExtractFunction produces "Extract function" code actions.
484484
// See [extractFunction] for command implementation.
485485
func refactorExtractFunction(ctx context.Context, req *codeActionsRequest) error {
486-
if _, ok, _, _ := canExtractFunction(req.pgf.Tok, req.start, req.end, req.pgf.Src, req.pgf.Cursor); ok {
486+
if _, ok, _, _ := canExtractFunction(req.pgf.Cursor, req.start, req.end); ok {
487487
req.addApplyFixAction("Extract function", fixExtractFunction, req.loc)
488488
}
489489
return nil
@@ -492,7 +492,7 @@ func refactorExtractFunction(ctx context.Context, req *codeActionsRequest) error
492492
// refactorExtractMethod produces "Extract method" code actions.
493493
// See [extractMethod] for command implementation.
494494
func refactorExtractMethod(ctx context.Context, req *codeActionsRequest) error {
495-
if _, ok, methodOK, _ := canExtractFunction(req.pgf.Tok, req.start, req.end, req.pgf.Src, req.pgf.Cursor); ok && methodOK {
495+
if _, ok, methodOK, _ := canExtractFunction(req.pgf.Cursor, req.start, req.end); ok && methodOK {
496496
req.addApplyFixAction("Extract method", fixExtractMethod, req.loc)
497497
}
498498
return nil

0 commit comments

Comments
 (0)