5
5
package golang
6
6
7
7
import (
8
- "bytes"
9
8
"context"
10
9
"encoding/json"
11
10
"fmt"
@@ -42,7 +41,6 @@ import (
42
41
//
43
42
// See ../protocol/codeactionkind.go for some code action theory.
44
43
func CodeActions (ctx context.Context , snapshot * cache.Snapshot , fh file.Handle , rng protocol.Range , diagnostics []protocol.Diagnostic , enabled func (protocol.CodeActionKind ) bool , trigger protocol.CodeActionTriggerKind ) (actions []protocol.CodeAction , _ error ) {
45
-
46
44
loc := protocol.Location {URI : fh .URI (), Range : rng }
47
45
48
46
pgf , err := snapshot .ParseGo (ctx , fh , parsego .Full )
@@ -341,7 +339,7 @@ func quickFix(ctx context.Context, req *codeActionsRequest) error {
341
339
} else {
342
340
// Offer a "Declare missing field T.f" code action.
343
341
// See [stubMissingStructFieldFixer] for command implementation.
344
- fi := GetFieldStubInfo (req .pkg .FileSet (), info , path )
342
+ fi := stubmethods . GetFieldStubInfo (req .pkg .FileSet (), info , path )
345
343
if fi != nil {
346
344
msg := fmt .Sprintf ("Declare missing struct field %s.%s" , fi .Named .Obj ().Name (), fi .Expr .Sel .Name )
347
345
req .addApplyFixAction (msg , fixMissingStructField , req .loc )
@@ -364,75 +362,6 @@ func quickFix(ctx context.Context, req *codeActionsRequest) error {
364
362
return nil
365
363
}
366
364
367
- func GetFieldStubInfo (fset * token.FileSet , info * types.Info , path []ast.Node ) * StructFieldInfo {
368
- for _ , node := range path {
369
- n , ok := node .(* ast.SelectorExpr )
370
- if ! ok {
371
- continue
372
- }
373
- tv , ok := info .Types [n .X ]
374
- if ! ok {
375
- break
376
- }
377
-
378
- named , ok := tv .Type .(* types.Named )
379
- if ! ok {
380
- break
381
- }
382
-
383
- structType , ok := named .Underlying ().(* types.Struct )
384
- if ! ok {
385
- break
386
- }
387
-
388
- return & StructFieldInfo {
389
- Fset : fset ,
390
- Expr : n ,
391
- Struct : structType ,
392
- Named : named ,
393
- Info : info ,
394
- Path : path ,
395
- }
396
- }
397
-
398
- return nil
399
- }
400
-
401
- type StructFieldInfo struct {
402
- Fset * token.FileSet
403
- Expr * ast.SelectorExpr
404
- Struct * types.Struct
405
- Named * types.Named
406
- Info * types.Info
407
- Path []ast.Node
408
- }
409
-
410
- // Emit writes to out the missing field based on type info.
411
- func (si * StructFieldInfo ) Emit (out * bytes.Buffer , qual types.Qualifier ) error {
412
- if si .Expr == nil || si .Expr .Sel == nil {
413
- return fmt .Errorf ("invalid selector expression" )
414
- }
415
-
416
- // Get types from context at the selector expression position
417
- typesFromContext := typesutil .TypesFromContext (si .Info , si .Path , si .Expr .Pos ())
418
-
419
- // Default to interface{} if we couldn't determine the type from context
420
- var fieldType types.Type
421
- if len (typesFromContext ) > 0 && typesFromContext [0 ] != nil {
422
- fieldType = typesFromContext [0 ]
423
- } else {
424
- // Create a new interface{} type
425
- fieldType = types .NewInterfaceType (nil , nil )
426
- }
427
-
428
- tpl := "\n \t %s %s"
429
- if si .Struct .NumFields () == 0 {
430
- tpl += "\n "
431
- }
432
- fmt .Fprintf (out , tpl , si .Expr .Sel .Name , types .TypeString (fieldType , qual ))
433
- return nil
434
- }
435
-
436
365
// allImportsFixesResult is the result of a lazy call to allImportsFixes.
437
366
// It implements the codeActionsRequest lazyInit interface.
438
367
type allImportsFixesResult struct {
0 commit comments