@@ -502,15 +502,16 @@ func refactorExtractMethod(ctx context.Context, req *codeActionsRequest) error {
502502// See [extractVariable] for command implementation.
503503func refactorExtractVariable (ctx context.Context , req * codeActionsRequest ) error {
504504 info := req .pkg .TypesInfo ()
505- if exprs , err := canExtractVariable (info , req .pgf .Cursor , req .start , req .end , false ); err == nil {
505+ if curExprs , err := canExtractVariable (info , req .pgf .Cursor , req .start , req .end , false ); err == nil && len ( curExprs ) > 0 {
506506 // Offer one of refactor.extract.{constant,variable}
507507 // based on the constness of the expression; this is a
508508 // limitation of the codeActionProducers mechanism.
509509 // Beware that future evolutions of the refactorings
510510 // may make them diverge to become non-complementary,
511511 // for example because "if const x = ...; y {" is illegal.
512512 // Same as [refactorExtractVariableAll].
513- constant := info .Types [exprs [0 ]].Value != nil
513+ expr0 := curExprs [0 ].Node ().(ast.Expr )
514+ constant := info .Types [expr0 ].Value != nil
514515 if (req .kind == settings .RefactorExtractConstant ) == constant {
515516 title := "Extract variable"
516517 if constant {
@@ -528,22 +529,23 @@ func refactorExtractVariableAll(ctx context.Context, req *codeActionsRequest) er
528529 info := req .pkg .TypesInfo ()
529530 // Don't suggest if only one expr is found,
530531 // otherwise it will duplicate with [refactorExtractVariable]
531- if exprs , err := canExtractVariable (info , req .pgf .Cursor , req .start , req .end , true ); err == nil && len (exprs ) > 1 {
532- text , err := req .pgf .NodeText (exprs [0 ])
532+ if curExprs , err := canExtractVariable (info , req .pgf .Cursor , req .start , req .end , true ); err == nil && len (curExprs ) > 1 {
533+ expr0 := curExprs [0 ].Node ().(ast.Expr )
534+ text , err := req .pgf .NodeText (expr0 )
533535 if err != nil {
534536 return err
535537 }
536538 desc := string (text )
537539 if len (desc ) >= 40 || strings .Contains (desc , "\n " ) {
538- desc = goastutil .NodeDescription (exprs [ 0 ] )
540+ desc = goastutil .NodeDescription (expr0 )
539541 }
540- constant := info .Types [exprs [ 0 ] ].Value != nil
542+ constant := info .Types [expr0 ].Value != nil
541543 if (req .kind == settings .RefactorExtractConstantAll ) == constant {
542544 var title string
543545 if constant {
544- title = fmt .Sprintf ("Extract %d occurrences of const expression: %s" , len (exprs ), desc )
546+ title = fmt .Sprintf ("Extract %d occurrences of const expression: %s" , len (curExprs ), desc )
545547 } else {
546- title = fmt .Sprintf ("Extract %d occurrences of %s" , len (exprs ), desc )
548+ title = fmt .Sprintf ("Extract %d occurrences of %s" , len (curExprs ), desc )
547549 }
548550 req .addApplyFixAction (title , fixExtractVariableAll , req .loc )
549551 }
0 commit comments