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 8c85d43
Show file tree
Hide file tree
Showing 10 changed files with 844 additions and 6 deletions.
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 8c85d43

Please sign in to comment.