@@ -20,6 +20,8 @@ import (
20
20
21
21
"golang.org/x/tools/go/analysis"
22
22
"golang.org/x/tools/go/ast/astutil"
23
+ "golang.org/x/tools/gopls/internal/cache"
24
+ "golang.org/x/tools/gopls/internal/cache/parsego"
23
25
goplsastutil "golang.org/x/tools/gopls/internal/util/astutil"
24
26
"golang.org/x/tools/gopls/internal/util/bug"
25
27
"golang.org/x/tools/gopls/internal/util/safetoken"
@@ -29,13 +31,13 @@ import (
29
31
)
30
32
31
33
// extractVariable implements the refactor.extract.{variable,constant} CodeAction command.
32
- func extractVariable (fset * token. FileSet , start , end token. Pos , src [] byte , curFile cursor. Cursor , _ * types. Package , info * types. Info ) (* token.FileSet , * analysis.SuggestedFix , error ) {
33
- return extractExprs (fset , start , end , src , curFile , info , false )
34
+ func extractVariable (pkg * cache. Package , pgf * parsego. File , start , end token. Pos ) (* token.FileSet , * analysis.SuggestedFix , error ) {
35
+ return extractExprs (pkg , pgf , start , end , false )
34
36
}
35
37
36
38
// extractVariableAll implements the refactor.extract.{variable,constant}-all CodeAction command.
37
- func extractVariableAll (fset * token. FileSet , start , end token. Pos , src [] byte , curFile cursor. Cursor , _ * types. Package , info * types. Info ) (* token.FileSet , * analysis.SuggestedFix , error ) {
38
- return extractExprs (fset , start , end , src , curFile , info , true )
39
+ func extractVariableAll (pkg * cache. Package , pgf * parsego. File , start , end token. Pos ) (* token.FileSet , * analysis.SuggestedFix , error ) {
40
+ return extractExprs (pkg , pgf , start , end , true )
39
41
}
40
42
41
43
// extractExprs replaces occurrence(s) of a specified expression within the same function
@@ -44,11 +46,15 @@ func extractVariableAll(fset *token.FileSet, start, end token.Pos, src []byte, c
44
46
//
45
47
// The new variable/constant is declared as close as possible to the first found expression
46
48
// within the deepest common scope accessible to all candidate occurrences.
47
- func extractExprs (fset * token.FileSet , start , end token.Pos , src []byte , curFile cursor.Cursor , info * types.Info , all bool ) (* token.FileSet , * analysis.SuggestedFix , error ) {
48
- file := curFile .Node ().(* ast.File )
49
+ func extractExprs (pkg * cache.Package , pgf * parsego.File , start , end token.Pos , all bool ) (* token.FileSet , * analysis.SuggestedFix , error ) {
50
+ var (
51
+ fset = pkg .FileSet ()
52
+ info = pkg .TypesInfo ()
53
+ file = pgf .File
54
+ )
49
55
// TODO(adonovan): simplify, using Cursor.
50
56
tokFile := fset .File (file .FileStart )
51
- exprs , err := canExtractVariable (info , curFile , start , end , all )
57
+ exprs , err := canExtractVariable (info , pgf . Cursor , start , end , all )
52
58
if err != nil {
53
59
return nil , nil , fmt .Errorf ("cannot extract: %v" , err )
54
60
}
@@ -157,7 +163,7 @@ Outer:
157
163
return nil , nil , fmt .Errorf ("cannot find location to insert extraction: %v" , err )
158
164
}
159
165
// Within function: compute appropriate statement indentation.
160
- indent , err := calculateIndentation (src , tokFile , before )
166
+ indent , err := calculateIndentation (pgf . Src , tokFile , before )
161
167
if err != nil {
162
168
return nil , nil , err
163
169
}
@@ -576,13 +582,13 @@ type returnVariable struct {
576
582
}
577
583
578
584
// extractMethod refactors the selected block of code into a new method.
579
- func extractMethod (fset * token. FileSet , start , end token. Pos , src [] byte , curFile cursor. Cursor , pkg * types. Package , info * types. Info ) (* token.FileSet , * analysis.SuggestedFix , error ) {
580
- return extractFunctionMethod (fset , start , end , src , curFile , pkg , info , true )
585
+ func extractMethod (pkg * cache. Package , pgf * parsego. File , start , end token. Pos ) (* token.FileSet , * analysis.SuggestedFix , error ) {
586
+ return extractFunctionMethod (pkg , pgf , start , end , true )
581
587
}
582
588
583
589
// extractFunction refactors the selected block of code into a new function.
584
- func extractFunction (fset * token. FileSet , start , end token. Pos , src [] byte , curFile cursor. Cursor , pkg * types. Package , info * types. Info ) (* token.FileSet , * analysis.SuggestedFix , error ) {
585
- return extractFunctionMethod (fset , start , end , src , curFile , pkg , info , false )
590
+ func extractFunction (pkg * cache. Package , pgf * parsego. File , start , end token. Pos ) (* token.FileSet , * analysis.SuggestedFix , error ) {
591
+ return extractFunctionMethod (pkg , pgf , start , end , false )
586
592
}
587
593
588
594
// extractFunctionMethod refactors the selected block of code into a new function/method.
@@ -593,19 +599,26 @@ func extractFunction(fset *token.FileSet, start, end token.Pos, src []byte, curF
593
599
// and return values of the extracted function/method. Lastly, we construct the call
594
600
// of the function/method and insert this call as well as the extracted function/method into
595
601
// their proper locations.
596
- func extractFunctionMethod (fset * token.FileSet , start , end token.Pos , src []byte , curFile cursor.Cursor , pkg * types.Package , info * types.Info , isMethod bool ) (* token.FileSet , * analysis.SuggestedFix , error ) {
602
+ func extractFunctionMethod (cpkg * cache.Package , pgf * parsego.File , start , end token.Pos , isMethod bool ) (* token.FileSet , * analysis.SuggestedFix , error ) {
603
+ var (
604
+ fset = cpkg .FileSet ()
605
+ pkg = cpkg .Types ()
606
+ info = cpkg .TypesInfo ()
607
+ src = pgf .Src
608
+ )
609
+
597
610
errorPrefix := "extractFunction"
598
611
if isMethod {
599
612
errorPrefix = "extractMethod"
600
613
}
601
614
602
- file := curFile .Node ().(* ast.File )
615
+ file := pgf . Cursor .Node ().(* ast.File )
603
616
// TODO(adonovan): simplify, using Cursor.
604
617
tok := fset .File (file .FileStart )
605
618
if tok == nil {
606
619
return nil , nil , bug .Errorf ("no file for position" )
607
620
}
608
- p , ok , methodOk , err := canExtractFunction (tok , start , end , src , curFile )
621
+ p , ok , methodOk , err := canExtractFunction (tok , start , end , src , pgf . Cursor )
609
622
if (! ok && ! isMethod ) || (! methodOk && isMethod ) {
610
623
return nil , nil , fmt .Errorf ("%s: cannot extract %s: %v" , errorPrefix ,
611
624
safetoken .StartPosition (fset , start ), err )
0 commit comments