Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/gopls: extract func/method arg(s) to struct #65552

Open
danp opened this issue Feb 6, 2024 · 5 comments
Open

x/tools/gopls: extract func/method arg(s) to struct #65552

danp opened this issue Feb 6, 2024 · 5 comments
Labels
FeatureRequest gopls Issues related to the Go language server, gopls. help wanted Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@danp
Copy link
Contributor

danp commented Feb 6, 2024

Given something like this, where timeout is an option:

func search(query string, timeout time.Duration) {}

When it's time to add another option, one might extract timeout to an options struct so it's then easy to add more things:

type searchOptions struct {
  timeout time.Duration
}

func search(query string, opts searchOptions) {}

A code action for this kind of extraction would be very helpful!

@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Feb 6, 2024
@gopherbot gopherbot added this to the Unreleased milestone Feb 6, 2024
@mvdan
Copy link
Member

mvdan commented Feb 6, 2024

There should be parameters that this code action doesn't move to a struct, such as those with types context.Context, *testing.T, or testing.TB. To play it safe, one might want to also skip any parameter with name ctx.

@findleyr
Copy link
Contributor

findleyr commented Feb 6, 2024

We all think this is a great idea. And unlike other refactorings, this is unlikely to suffer from dropped comments, so need not wait for us to fix AST comments :)

This would be a reasonable area for a contribution, albeit a challenging one.

There should be parameters that this code action doesn't move to a struct, such as those with types context.Context, *testing.T, or testing.TB. To play it safe, one might want to also skip any parameter with name ctx.

Code actions operate on a range, so it could include any selected parameters.

@findleyr findleyr added help wanted Refactoring Issues related to refactoring tools labels Feb 6, 2024
@findleyr findleyr modified the milestones: Unreleased, gopls/backlog Feb 6, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/563235 mentions this issue: gopls/internal/golang: add extract argument code action

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/620995 mentions this issue: gopls/codeaction: add function parameters extraction feature.

@tttoad
Copy link

tttoad commented Oct 18, 2024

We should also modify the parameter references within the func.body.

func search(ctx context.Context, aa int, bb int) int {
	return aa + bb
}

====>

type searchOpts struct{
	aa int
	bb int
}

func search(ctx context.Context, opts searchOpts) int {
	return opts.aa + opts.bb
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest gopls Issues related to the Go language server, gopls. help wanted Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

5 participants