-
Notifications
You must be signed in to change notification settings - Fork 18k
x/tools/gopls: exclude unexported fields from hover information for structs #42654
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
Comments
The hover information for structs is actually just the AST node of the declaration. I guess we could filter out the unexported fields, but that would require cloning the AST node. |
@stamblerre I'd like to work on this, would that be fine? |
Will we still show unexported fields if the struct is defined in the current package? |
Yes, I think it would make sense to still show unexported fields in such cases.
Sure, though this may be a little bit complicated because of the cloning of AST nodes required. |
@stamblerre thanks, I'll start with this, will try to get some help from #gopls-dev |
@stamblerre Sorry for the multi-month delay. I've a very crude implementation https://github.com/golang/tools/compare/master...danishprakash:hover-struct-exported?expand=1 if you could take a look whenever you've time. I'm not very confident on using the filtering logic from https://golang.org/src/go/ast/filter.go here albeit with some changes. |
@danishprakash: Thank you for trying that out. As I mentioned above, we cannot modify the AST nodes directly, since the same ASTs are also used for other types of requests. I think you will want some logic like the |
@stamblerre my bad, I was just trying to get a draft implementation working. But that made complete sense. I've made a few changes here. But I still don't feel right about copying over the cloning logic albeit a little stripped down for this use case. Could you suggest something here? Maybe we could have a generic cloneAST method in utils/ or maybe propose something like this to astutil for that matter? What do you think? |
Thank you for spending time looking at this, @danishprakash! Seeing how involved and complicated this change would be is making me wonder if this is even worth having. I think in some cases it might be useful to see the unexported fields, even if you can't access them from the current package. What do you think? |
Now that you've mentioned, I realize I'd much rather have all the members of the struct shown on hover while I'm working on something than have them filtered. On the other hand, and I think is what @myitcv(correct me If I'm wrong) also had in mind when he opened this issue, is the fact that If I'm hovering over a struct from a package that I haven't authored, I would like to see the fields that are exported for me to use. In this case, it's currently implied that the individual should figure out that there are unexported fields in there as well along with the exported ones. But all said and done, I feel that showing all the fields on hover makes more sense. |
Yes, I agree with your assessment--it really varies by your usage and intentions. I don't think it makes sense to offer configuration for this, so I think it's probably best to close this issue and leave it as is. Thank you for your work on this, @danishprakash--I'm sorry we weren't able to merge your changes. |
Unless I am missing something, if I am looking at a struct defined in a package outside of the main module, I can have no interest in the non-exported fields (because unless I resort to reflection I can't get/set their values). The ambiguity comes for structs defined by packages inside by the main module: in that case I agree it does make sense to show all fields. This issue was created for the former category; as such I don't agree with the conclusion. |
In our discussion, we thought that there can still be value in seeing these fields, though it's fairly rare. But more importantly, the logic required to get this right is nontrivial, but it's not totally clear to me what the benefits would be. It also could be confusing to users to see no fields if all of the fields of a struct are unexported. |
Please can you share more details?
The main benefits to my mind are that as the user of a struct defined outside of the main module you are not confronted with lots of detail that is irrelevant. Indeed during completion we filter out non-exported fields for exactly this reason.
In this case I suggest we simply do what
I can appreciate this point, and it might be that the costs outweigh the benefits. I'm just keen to understand if I'm missing the point regarding the reasons against doing this (ignoring the complexity for one second), given the obvious consistency it would seem to offer with respect to |
Didn't think of this earlier, it sounds much more intuitive, better than not showing any fields or showing all the fields.
I think this was part of the reason for not implementing it but I'll be glad to look into a more reasonable implementation if there's one given that we go ahead with how |
I don't have a concrete example of a case where you would benefit from checking an exported field, but at the very least, it might save you a "jump to definition" if you do want to look at a struct's fields or implementation. I'll try to think of a more concrete case.
There might be a way to do it by adjusting the way that we write out AST nodes (currently we simply call |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Hover over
Path
What did you expect to see?
Only the exported fields of
cue.Path
What did you see instead?
Log file: bad.log
cc @stamblerre
FYI @leitzler
The text was updated successfully, but these errors were encountered: