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

Add new semantic token modifiers in gopls 0.17 #3632

Closed
xzbdmw opened this issue Dec 20, 2024 · 24 comments
Closed

Add new semantic token modifiers in gopls 0.17 #3632

xzbdmw opened this issue Dec 20, 2024 · 24 comments
Assignees
Milestone

Comments

@xzbdmw
Copy link

xzbdmw commented Dec 20, 2024

Is your feature request related to a problem? Please describe.
https://github.com/golang/tools/blob/master/gopls/doc/release/v0.17.0.md#semantic-token-modifiers-of-top-level-constructor-of-types

Describe the solution you'd like
Add those as default capabilities for vscode

interface, struct, signature, pointer, array, map, slice, chan, string, number, bool, and invalid

@gopherbot gopherbot added this to the Untriaged milestone Dec 20, 2024
@findleyr
Copy link
Member

CC @h9jiang

@hyangah
Copy link
Contributor

hyangah commented Dec 27, 2024

@h9jiang h9jiang self-assigned this Dec 30, 2024
@h9jiang
Copy link
Member

h9jiang commented Jan 9, 2025

image

A CL will be sent out for review so the user will be able to customize what kind to modifiers and types they want gopls to report. The result will looks like screenshot above.

@xzbdmw
Copy link
Author

xzbdmw commented Jan 9, 2025

customize what kind to modifiers and types they want gopls to report

I feel this is not needed, because vscode users need to define the color of those "not in spec" modifiers, otherwise there are no visual differences.

@findleyr
Copy link
Member

@xzbdmw that may the case, but there is no guarantee that other clients behave well in the presence of unexpected semantic tokens. The spec is (IMO) unclear about how servers are supposed to behave if clients do not advertise a particular token capability.

Furthermore, we already have some ad-hoc configuration to customize the behavior of semantic tokens, to address https://go.dev/issue/45753
https://github.com/golang/tools/blob/master/gopls/doc/settings.md#nosemanticstring-bool

I think it makes sense to deprecate the somewhat ad-hoc "noSemanticString" and "noSemanticNumber" settings, and replace them with a map[string]bool that turns token types on or off. Same for modifiers.

@gopherbot
Copy link
Collaborator

Change https://go.dev/cl/642077 mentions this issue: gopls/internal/golang: customize semantic token types and modifiers

@xzbdmw
Copy link
Author

xzbdmw commented Jan 10, 2025

The spec is (IMO) unclear about how servers are supposed to behave if clients do not advertise a particular token capability

I do hope gopls sends them all by default ( I think that's what rust-analyzer does, I get tons of modifiers with a default setup), because right now, unless people take attention to release notes, and manually advertise these token capabilities, they never know those "out-of-spec" modifiers even exists.

@findleyr
Copy link
Member

@xzbdmw @h9jiang OK, let's do this: let's add the new configuration map, but send all by default. If any clients have trouble with the new token types or modifiers, they can use the configuration map to disable the unexpected types.

Sound good?

@h9jiang
Copy link
Member

h9jiang commented Jan 10, 2025

By default, with only semanticTokens enabled in vscode, gopls will report all token types including string and number ..., will report all token modifiers including struct and pointer ....
image
image

Users can add additional configuration under ui.semanticTokenModifiers and ui.semanticTokenTypes to disable some of them without restarting the gopls or reload the vscode window.

Disable struct and pointer modifiers, then move the cursor to a different place in the same file and move back. The struct modifier will be taken out.
image

Disable number and string types, then move the cursor around.. The number and string will be taken out.
image

@h9jiang
Copy link
Member

h9jiang commented Jan 10, 2025

But there is a small issue with noSemanticNumber & noSemanticString. I feel like that is a more general question and should not be considered as blocker for this issue. See golang/go#71227.

I will proceed with my CL and discussing further in golang/go#71227.

@gopherbot
Copy link
Collaborator

Change https://go.dev/cl/642416 mentions this issue: DNR extension/package.json: introduce new options for semantic legend

@gopherbot
Copy link
Collaborator

Change https://go.dev/cl/642423 mentions this issue: docs/docs.go: add empty go file under docs module

gopherbot pushed a commit to golang/tools that referenced this issue Jan 14, 2025
We agreed to return full set of token types and modifiers from gopls
by default (full means token types and modifiers that gopls understand)
and provide configuraton options for users to disable some of them.

- Two fields of type map[string]bool are introduced to gopls UIOptions
(workspace/configuration) to customize semantic token types and
modifiers. For now, only value of "false" is effective. Choose type of
map over array to keep future compatibility in case we want to introduce
enable capabilities.
- VSCode-Go populate these options from user settings to gopls.
- Gopls "initialize" protocol returns a pre-defined fixed legend
including subset of standard legend defined LSP that gopls understand
with additional customize modifiers gopls recoganized.
- Gopls "textDocument/semanticTokens" protocol returns token types and
modifiers based on configuration defined in workspace/configuration.

Tested with vscode-go changes CL 642416, screenshot is at
golang/vscode-go#3632 (comment)

For golang/vscode-go#3632

Change-Id: Ie8220e12a4c8d6c84c54992d84277767e61ec023
Reviewed-on: https://go-review.googlesource.com/c/tools/+/642077
Auto-Submit: Hongxiang Jiang <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
@h9jiang
Copy link
Member

h9jiang commented Jan 15, 2025

This FR is now implemented and will be shipped with Gopls in v0.18.0 and configuration will be available in vscode-go v0.46.0.

@h9jiang h9jiang closed this as completed Jan 15, 2025
@findleyr
Copy link
Member

@h9jiang let's also be sure to deprecate noSemanticNumber and noSemanticString. We can associate that change with this CL. We should also document this configuration change in gopls/doc/release/v0.18.0.md.

@h9jiang h9jiang reopened this Jan 15, 2025
@gopherbot
Copy link
Collaborator

Change https://go.dev/cl/642998 mentions this issue: gopls/internal/settins: include deprecation message in api-json

@h9jiang
Copy link
Member

h9jiang commented Jan 16, 2025

As mentioned in the CL, noSemanticString & noSemanticNumber will be deprecated in favor of semanticTokenType["string"] = false, semanticTokenType["number"] = false. Users can use semanticTokenType map to disable more types based on their preference. (Same rule applies to semantic token modifiers)

Gopls will still be able to interpret and understand noSemanticString & noSemanticNumber for the next release.

@xzbdmw
Copy link
Author

xzbdmw commented Jan 16, 2025

Regarding noSemanticString, should we report semantic tokens for format string directives now since we already done this for DocumentHighlight?

@gopherbot
Copy link
Collaborator

Change https://go.dev/cl/643056 mentions this issue: extension/tools/goplssetting: interpret deprecation message from apijson

@gopherbot
Copy link
Collaborator

Change https://go.dev/cl/643057 mentions this issue: DNR: extension/package.json: update based on gopls api-json

@h9jiang
Copy link
Member

h9jiang commented Jan 16, 2025

xzbdmw@ you mean, return type: directives, modifiers: [] or return type: string, modifiers: [directive]

I would be very careful when choosing a new type for string directives since vscode theme does not understand this type and have no idea how to render them.

I'm personally ok with having an extra modifiers.

@adonovan might be able to offer a decision here.

@findleyr findleyr modified the milestones: Untriaged, v0.46.0 Jan 16, 2025
@adonovan
Copy link
Member

Regarding noSemanticString, should we report semantic tokens for format string directives now since we already done this for DocumentHighlight?

I'm not sure how format strings relate to noSemanticString. I think you must be asking: when the server returns "string" token types, should it also return a "formatString" modifier for the subset that appear to be format strings?

I don't know the answer (I don't use semantic tokens). What would a client do with this information? Would it not be redundant with the information provided by DocumentHighlight? And let's not forget that we're only using a heuristic to classify format strings.

@xzbdmw
Copy link
Author

xzbdmw commented Jan 16, 2025

What would a client do with this information?

It will look something like this (nvim + treesitter-printf):

Image

It has limitations that you have to specify the names of these methods in advance for it to parse.

Would it not be redundant with the information provided by DocumentHighlight

SemanticHighlight helps to understand the directives without cursor being on it.

let's not forget that we're only using a heuristic to classify format strings

For vscode users, they can set noSemanticString: true and use textmate rule to highlight those %s (that's why this setting exists), and it's a more simple heuristic, as it will highlight any string which has a %, for example, in this image it interprets double % as placeholders for a string literal:

Image

@findleyr
Copy link
Member

Elsewhere, we break up strings into multiple tokens, for example to highlight the imported package name in import specs:
https://cs.opensource.google/go/x/tools/+/master:gopls/internal/golang/semtok.go;l=822;drc=4403100389e1e2f337e097f0b7b27293c6f05c91

I'm doubtful that we should be doing that here, as it is less precise than imports (and I'm not entirely sold on sub-string highlighting in imports as well). However, @xzbdmw please feel free to open that as a separate gopls issue, which we can consider independently.

gopherbot pushed a commit that referenced this issue Jan 16, 2025
When generating package.json based on gopls api-json output,
vscode-go release tool will interpret deprecation message and
write the message to package.json and settings.md.

In settings.md, deprecation message is put above description.

Gopls side CL 642998, Test CL 643057.

For #3632

Change-Id: Ica21fcc446dd1c5a5d4ef9b66101b3a9b0a47e80
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/643056
kokoro-CI: kokoro <[email protected]>
Reviewed-by: Alan Donovan <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
Auto-Submit: Hongxiang Jiang <[email protected]>
Commit-Queue: Hongxiang Jiang <[email protected]>
gopherbot pushed a commit to golang/tools that referenced this issue Jan 21, 2025
- gopls api-json will return deprecation message as additional
property of a configuration.
- go generate ./... will parse the comment of a given field as
doc(entire doc comment) and deprecation message(introduced by
prefix "Deprecated: "). Follow pattern defined in
https://go.dev/wiki/Deprecated.

VSCode-Go side CL 643056.

For golang/vscode-go#3632

Change-Id: Ia6a67948c75dd51b5cb76bbd6d7385b95ea979e4
Reviewed-on: https://go-review.googlesource.com/c/tools/+/642998
Auto-Submit: Hongxiang Jiang <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
@gopherbot
Copy link
Collaborator

Change https://go.dev/cl/643497 mentions this issue: gopls/doc/release: add semantic token config change

gopherbot pushed a commit to golang/tools that referenced this issue Jan 21, 2025
For golang/vscode-go#3632

Change-Id: I8d8a219c380ac8ac07a1baaef3bc89701894b985
Reviewed-on: https://go-review.googlesource.com/c/tools/+/643497
Reviewed-by: Alan Donovan <[email protected]>
Auto-Submit: Hongxiang Jiang <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
@h9jiang h9jiang closed this as completed Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants