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: code action for “add field names to struct literal” #43198

Closed
bcmills opened this issue Dec 15, 2020 · 10 comments
Closed

x/tools/gopls: code action for “add field names to struct literal” #43198

bcmills opened this issue Dec 15, 2020 · 10 comments
Labels
FeatureRequest FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.

Comments

@bcmills
Copy link
Contributor

bcmills commented Dec 15, 2020

gopls version
golang.org/x/tools/gopls v0.6.0
    golang.org/x/tools/gopls@v0.6.0-pre.1 h1:i+eJsCS+4N+3YSxLbuq0SrfWim3V8eG4WgdK8xQdzwI=

What did you do?

  1. In an emacs buffer, open $GOROOT/src/html/template/template.go:229:
    ret := &Template{
    nil,
    text,
    text.Tree,
    t.nameSpace,
    }
  2. M-x eglot-code-actions

What did you expect to see?

An LSP code action that can add field names to the Go struct literal at the current location.

(@stamblerre informs me that VSCode currently provides this functionality using gomodifytags; cc @fatih.)

What did you see instead?

[eglot] No code actions here

@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 Dec 15, 2020
@gopherbot gopherbot added this to the Unreleased milestone Dec 15, 2020
@fatih
Copy link
Member

fatih commented Dec 16, 2020

What is the expected output for the assignment on line template.go/229 ? I'm not sure how we can add tags here (but maybe I'm missing something). For the record, here are some examples that we support recently: https://github.com/fatih/gomodifytags/releases/tag/v1.13.0

@hyangah
Copy link
Contributor

hyangah commented Dec 16, 2020

Based on the title, it sounds like the feature is about transformation to use named fields instead of positional args for struct literals. gomodifytags is to add structure tags, so unrelated.

@bcmills
Copy link
Contributor Author

bcmills commented Dec 16, 2020

Yes, @hyangah has the right intent.

I want the code action to transform the code from

	 ret := &Template{ 
	 	nil, 
	 	text, 
	 	text.Tree, 
	 	t.nameSpace, 
	 } 

to

	 ret := &Template{ 
	 	escapeErr: nil, 
	 	text:      text, 
	 	Tree:      text.Tree, 
	 	nameSpace: t.nameSpace, 
	 } 

@fatih
Copy link
Member

fatih commented Dec 16, 2020

Aha I see, thanks for the clarification. I think keyify does this for you (it also has an emacs plugin). In vim-go you can use :GoKeyify

https://github.com/dominikh/go-tools/tree/master/cmd/keyify

@dominikh
Copy link
Member

dominikh commented Jan 8, 2021

Integrating keyify with gopls is on my todo list. At the same time, gopls already has fillstruct, and I wouldn't be surprised if most of the code required for keyify and fillstruct is identical.

@Frederick888
Copy link

It seems that it's not easy to get keyify to work with Go modules these days: dominikh/go-tools#834

And although golang/vscode-go#1631 has been resolved, which seems to have made omitting field names a better option, I think having field names still has its advantages:

  • Easier when adding/removing fields. Especially helpful in tests where test structs often have consecutive fields of the same type
  • Still in tests, sometimes we write a bunch of new tests that have field names and now we'd like to add field names to the existing ones
  • As a neovim user I'm still waiting for anticonceal (virtual text occupying horizontal space) neovim/neovim#9496

So it'd be great if gopls can offer this functionality.

@dnwe
Copy link

dnwe commented Jul 19, 2022

This capability has been available since gopls@v0.9.0 and works in neovim lsp too

image

@dnwe
Copy link

dnwe commented Jul 19, 2022

This issue can probably be closed as a dupe of #53062 which has the attached PR that was merged in v0.9.0 to support this capability

@mvdan
Copy link
Member

mvdan commented Jul 19, 2022

Thank you for spotting that, @dnwe!

@mvdan mvdan closed this as not planned Won't fix, can't repro, duplicate, stale Jul 19, 2022
@Frederick888
Copy link

@dnwe Looks like this only works if the struct is in a different package?

No code actions on Bar (or govet warning)
image

Foo on the other hand:
image
image

Our test matrices usually involve some (anonymous) structs in the same package. Any way to make this case work? Ty

@golang golang locked and limited conversation to collaborators Jul 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FeatureRequest FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

9 participants