Skip to content

Commit

Permalink
gopls/internal: add code action "move to a new file"
Browse files Browse the repository at this point in the history
This code action moves selected code sections to a newly created file
within the same package. The created filename is chosen as the first
{function, type, const, var} name encountered. In addition, import
declarations are added or removed as needed.

Fixes golang/go#65707
  • Loading branch information
golopot committed Feb 22, 2024
1 parent c111c4d commit 2cfb2f0
Show file tree
Hide file tree
Showing 11 changed files with 844 additions and 11 deletions.
5 changes: 0 additions & 5 deletions gopls/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDf
golang.org/x/exp/typeparams v0.0.0-20221212164502-fae10dda9338 h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
golang.org/x/exp/typeparams v0.0.0-20221212164502-fae10dda9338/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
Expand All @@ -27,12 +26,8 @@ golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/telemetry v0.0.0-20240201224847-0a1d30dda509 h1:Nr7eTQpQZ/ytesxDJpQgaf0t4sdLnnDtAbmtViTrSUo=
golang.org/x/telemetry v0.0.0-20240201224847-0a1d30dda509/go.mod h1:ZthVHHkOi8rlMEsfFr3Ie42Ym1NonbFNNRKW3ci0UrU=
golang.org/x/telemetry v0.0.0-20240208230135-b75ee8823808 h1:+Kc94D8UVEVxJnLXp/+FMfqQARZtWHfVrcRtcG8aT3g=
golang.org/x/telemetry v0.0.0-20240208230135-b75ee8823808/go.mod h1:KG1lNk5ZFNssSZLrpVb4sMXKMpGwGXOxSG3rnu2gZQQ=
golang.org/x/telemetry v0.0.0-20240209200032-7b892fcb8a78 h1:vcVnuftN4J4UKLRcgetjzfU9FjjgXUUYUc3JhFplgV4=
golang.org/x/telemetry v0.0.0-20240209200032-7b892fcb8a78/go.mod h1:KG1lNk5ZFNssSZLrpVb4sMXKMpGwGXOxSG3rnu2gZQQ=
Expand Down
16 changes: 16 additions & 0 deletions gopls/internal/cache/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,22 @@ func inVendor(uri protocol.DocumentURI) bool {
return found && strings.Contains(after, "/")
}

// Sandbox clones the reciever, applying given file modfications as overlays.
func (s *Snapshot) Sandbox(ctx, bgCtx context.Context, modifications []file.Modification) *Snapshot {
updatedFiles := make(map[protocol.DocumentURI]file.Handle)
for _, m := range modifications {
updatedFiles[m.URI] = &overlay{
uri: m.URI,
content: m.Text,
}
}
cloned, _ := s.clone(ctx, bgCtx, StateChange{
Modifications: modifications,
Files: updatedFiles,
}, func() {})
return cloned
}

// clone copies state from the receiver into a new Snapshot, applying the given
// state changes.
//
Expand Down
5 changes: 5 additions & 0 deletions gopls/internal/golang/codeaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ func CodeActions(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
return nil, err
}
actions = append(actions, extractions...)
moves, err := getMoveToNewFileCodeAction(pgf, rng, snapshot.Options())
if err != nil {
return nil, err
}
actions = append(actions, moves)
}
}

Expand Down
32 changes: 32 additions & 0 deletions gopls/internal/golang/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,38 @@ func Format(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) ([]pr
return computeTextEdits(ctx, pgf, formatted)
}

func formatImportsBytes(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) ([]byte, error) {
_, done := event.Start(ctx, "golang.formatImportsBytes")
defer done()

errorPrefix := "formatImportsBytes"

text, err := fh.Content()
if err != nil {
return nil, fmt.Errorf("%s: %w", errorPrefix, err)
}

var out []byte
if err := snapshot.RunProcessEnvFunc(ctx, func(ctx context.Context, opts *imports.Options) error {
fixes, err := imports.FixImports(ctx, fh.URI().Path(), text, opts)
if err != nil {
return fmt.Errorf("%s: %w", errorPrefix, err)
}
out, err = imports.ApplyFixes(fixes, fh.URI().Path(), text, opts, parsego.Full)
if err != nil {
return fmt.Errorf("%s: %w", errorPrefix, err)
}
return nil
}); err != nil {
return nil, fmt.Errorf("%s: %w", errorPrefix, err)
}
out, err = format.Source(out)
if err != nil {
return nil, fmt.Errorf("%s: %w", errorPrefix, err)
}
return out, nil
}

func formatSource(ctx context.Context, fh file.Handle) ([]byte, error) {
_, done := event.Start(ctx, "golang.formatSource")
defer done()
Expand Down

0 comments on commit 2cfb2f0

Please sign in to comment.