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: semantic tokenizing of string escape/format characters #45753

Closed
soypat opened this issue Apr 24, 2021 · 12 comments
Closed

x/tools/gopls: semantic tokenizing of string escape/format characters #45753

soypat opened this issue Apr 24, 2021 · 12 comments
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@soypat
Copy link

soypat commented Apr 24, 2021

What version of Go are you using (go version)?

$ gopls version
golang.org/x/tools/gopls v0.6.10
    golang.org/x/tools/gopls@v0.6.10 h1:8Ebz8PymS2umcuCFhoz67unyJfWsipjTIrkBUF9kypg=

$ VsCode settings:
 "gopls": {	
        "ui.semanticTokens": true,
	},
    "editor.semanticHighlighting.enabled": true,

What did you do?

Write following code in VSCode with semanticTokens enabled

const httpResponse = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n<h2>..::TinyGo Rocks::..</h2>"
fmt.Sprintf("%v", httpResponse)

What did you expect to see?

Semantic tokenization of \n\r\t escaped characters and format characters like %v in strings inside a Format function.

What did you see instead?

Monocolor string like the one github displays.

@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 Apr 24, 2021
@gopherbot gopherbot added this to the Unreleased milestone Apr 24, 2021
@suzmue suzmue modified the milestones: Unreleased, gopls/unplanned Apr 26, 2021
@pjweinb
Copy link

pjweinb commented Apr 26, 2021

What should semantic tokens return in this case? (the possibilities are in the section on semantic tokens in https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/)

@soypat
Copy link
Author

soypat commented Apr 26, 2021

Maybe an unused token, such as class could be used for such end. I'm not sure how to check what VScode uses for other languages, is there a way?

From here the following semantic tokens are available:

Semantic Tokens

  • type
  • class
  • enum
  • struct
  • typeParameter
  • parameter
  • variable
  • property
  • enumMember
  • event
  • function
  • method
  • macro
  • keyword
  • modifier
  • comment
  • string
  • number
  • regexp
  • operator

Semantic Token Modifiers

  • declaration
  • definition
  • readonly
  • static
  • deprecated
  • abstract
  • async
  • modification
  • documentation
  • defaultLibrary

@soypat
Copy link
Author

soypat commented Apr 27, 2021

Looks like the default coloring for these characters before enabling semantic tokenizing is property.

@pjweinb
Copy link

pjweinb commented Apr 28, 2021

This is more a vscode issue than a gopls issue, but here's the vscode story. Based on experiments I believe what happens is the following, to decide the foreground color:

  1. If there is a semantic token, use that color.
  2. If not, if there is a textmate label, use that color.
  3. Otherwise, use the default color.

In this case the semantic token is a string, so vscode uses the color for strings. If gopls didn't return string semantic tokens, then vscode would show the colors from the textmate grammar, which would show different colors for \n etc. In semantic tokens I know of no way to get different colors in the string (i tried all 10 of the SemanticTokenModifiers, and they all use the same string color.)

So there are two possiblities for gopls:

  1. Leave things the way they are. Then the vscode user's choice would be semantic tokens and drab strings, or turn off semantic tokens and get colorful strings.
  2. Don't return string semantic tokens. Then vscode users would get colorful strings, but other LSP clients would get incomplete semantic tokens. I do not know if there are any LSP clients using semantic tokens, other than vscode.

@pjweinb
Copy link

pjweinb commented Apr 28, 2021

See the comment in #45753.

There are only two choices, as I see it:

  1. Leave things the way they are. Then the vscode user's choice would be semantic tokens and drab numbers, or turn off semantic tokens and get colorful numbers.
  2. Don't return number semantic tokens. Then vscode users would get colorful numbers, but other LSP clients would get incomplete semantic tokens. I do not know if there are any LSP clients using semantic tokens, other than vscode.

@soypat
Copy link
Author

soypat commented Apr 28, 2021

See the comment in #45753.

There are only two choices, as I see it:

1. Leave things the way they are. Then the vscode user's choice would be semantic tokens and drab numbers, or turn off semantic tokens and get colorful numbers.

2. Don't return number semantic tokens. Then vscode users would get colorful numbers, but other LSP clients would get incomplete semantic tokens. I do not know if there are any LSP clients using semantic tokens, other than vscode.

@pjweinb I think this comment was maybe meant for #45792?

@pjweinb
Copy link

pjweinb commented Apr 28, 2021 via email

@soypat
Copy link
Author

soypat commented Apr 29, 2021

Well I don't believe it is reasonable to be picky about what is implemented from the LSP. Option 1 seems like the better of two options. That said, is there any chance this could be brought in as an optional parameter to gopls? Something that can be modified in settings.json in VSCode for example.

@ian-h-chamberlain
Copy link

For an example of another LSP provider faced with a similar problem, see rust-lang/rust-analyzer#7111

Their solution was to simply add a config that enables/disables semantic tokens for strings only (as well as one for all semantic tokens), which I think seems like a good option in this case. Personally I'd love to have a way to disable it just for strings, since in my opinion the TM highlighting does look better than semantic tokens for strings (with escapes and format tokens), but isn't as good for the rest of the code.

I suppose a separate config for numbers or other literals might also make sense, in case a user wanted different behavior for different semantic types.

@pjweinb
Copy link

pjweinb commented Aug 3, 2022 via email

@gopherbot
Copy link

Change https://go.dev/cl/421256 mentions this issue: internal/lsp: new options to disable certain kinds of semantic tokens

@usersina
Copy link

Change https://go.dev/cl/421256 mentions this issue: internal/lsp: new options to disable certain kinds of semantic tokens

"gopls": {
    "ui.semanticTokens": true,
    "ui.noSemanticString": true
 }

@golang golang locked and limited conversation to collaborators Jan 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
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
No open projects
Development

No branches or pull requests

7 participants