vscode icon indicating copy to clipboard operation
vscode copied to clipboard

Go syntax highlighting incorrectly identifies multiplication operator as address operator

Open ChristiannYa opened this issue 8 months ago • 4 comments

When using the multiplication operator (*) in Go code, the syntax highlighter incorrectly identifies it as "keyword.operator.address.go" instead of "keyword.operator.arithmetic.go".

Example code

func Abs(v Vertex) float64 {
  return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

When inspecting the token at the * between v.X and v.X, it shows "keyword.operator.address.go" when it should be an arithmetic operator.

ChristiannYa avatar May 21 '25 02:05 ChristiannYa

Are you using the Go extension? If so, have you followed the advice at https://code.visualstudio.com/docs/languages/go#_semantic-syntax-highlighting?

gjsjohnmurray avatar May 21 '25 03:05 gjsjohnmurray

That did not help much at least visually. I am using a custom textMateRules color palette in vscode's settings.json

{
  "scope": [
    // Operators
    "keyword.operator.go",
    "keyword.operator.arithmetic.go",
    "keyword.operator.comparison.go"
  ],
  "settings": {
    "foreground": "#f56e6e"
  }
},
{
  "scope": [
    // Struct field names, address operator
    "variable.other.member.go",
    "variable.other.property.go",
    "keyword.operator.address.go"
  ],
  "settings": {
    "foreground": "#ffe87c"
  }
},

So, to clarify, in the code i provided earlier, the line return math.Sqrt(v.X*v.X + v.Y*v.Y), the (*) operator was highlighted with the yellow (#ffe87c) color. It is being counted as an address operator, but the purpose of the operator in the line is to be an arithmetic one (highlighted in the red color, #f56e6e)

But i noticed that separating the operator like this

func Abs(v Vertex) float64 {
	return math.Sqrt(v.X * v.X + v.Y * v.Y)
}

makes turns it to the "keyword.operator.arithmetic.go" scope as it is supposed to be and it now gets the appropriate red color (#f56e6e)

ChristiannYa avatar May 21 '25 03:05 ChristiannYa

should move report to upstream grammar repo https://github.com/worlpaker/go-syntax CC @worlpaker

RedCMD avatar May 21 '25 06:05 RedCMD

Thanks for reporting @ChristiannYa, and thanks for the ping @RedCMD.

I've published a new version (v0.8.2) to fix this issue. You can install go-syntax to access the quick fix.

Let me know if the bug persists.

worlpaker avatar May 21 '25 13:05 worlpaker

@RedCMD thanks for triaging this, and @worlpaker thanks for jumping on it so fast! As a small thank-you for this, and the other work you've done to make VS Code better, you can both go to https://swag.code.visualstudio.com/ to get some free VS Code swag. The swag is tied to your GitHub accounts, so you'll just need to do the GitHub OAuth dance.

I've pulled the grammar change in and the fix will be in the next insiders.

alexr00 avatar May 22 '25 08:05 alexr00