Skip to content

x/tools/gopls: code action "Extract interface for type" #65721

Open
golang/tools
#478
@martskins

Description

@martskins

gopls version

Irrelevant

go env

Irrelevant

What did you do?

Given the following code:

type Server struct {
    api *api.Client
}

func (s *Server) CreateUser(ctx context.Context, userID string) {
    err := s.api.CreateUser(ctx, userID)
    // ...
}

func (s *Server) DeleteUser(ctx context.Context, userID string) {
    err := s.api.DeleteUser(ctx, userID)
    // ...
}

What did you see happen?

It would be nice if gopls offered an action to extract an interface out of the used methods on the api field. So for the example given above, it would offer an edit to do something like this:

type Client interface {
    CreateUser(context.Context, string) error
    DeleteUser(context.Context, string) error
}

type Server struct {
    api Client
}

Where the method set of the generated interface is the methods that are called on the field that the action was initiated on, and if there are uses that are not method calls on that field then it would either error or just not offer the action.

What did you expect to see?

Nothing

Editor and settings

No response

Logs

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureRequestIssues asking for a new feature that does not need a proposal.RefactoringIssues related to refactoring toolsToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions