Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Semantic Highlighting #3134

Closed
appleton opened this issue Mar 27, 2020 · 5 comments
Closed

Semantic Highlighting #3134

appleton opened this issue Mar 27, 2020 · 5 comments
Labels
upstream-grammar Issue is related to the grammar used for Go which is controlled upstream in VS Code project

Comments

@appleton
Copy link

Is your feature request related to a problem? Please describe.
The syntax highlighting for Go is rather sparse by comparison to other languages (e.g. TypeScript) and other editors (e.g. GoLand).

VSCode:
Screenshot 2020-03-27 at 11 07 51 am

GoLand:
Screenshot 2020-03-27 at 11 08 42 am

VSCode (TypeScript):
Screenshot 2020-03-27 at 11 09 14 am

At a glance I can see that compared to GoLand, VSCode is missing tokens for:

  • Method receiver
  • Function parameter
  • Function parameter type annotation
  • References to a type (e.g. a struct definition).
  • References to an imported package.
  • Built in function call.

I'm sure there's more that I'm missing.

Describe the solution you'd like
I'd love to see VSCode's Go syntax highlighting get up to parity with other editors. I wonder if Go can make use of the new Semantic Highlighting feature added in v1.43?

Describe alternatives you've considered
I've followed a number of other issues similar to this which tend to reference other issues which tend to end up closed without resolution. I have not been able to find a currently active issue tracking this, hence opening this.

@hyangah
Copy link
Contributor

hyangah commented Mar 27, 2020

The syntax highlighting is done outside of this extension.
According to microsoft/vscode#82549, the upstream currently used by vscode is in https://github.com/jeff-hykin/better-go-syntax/issues. There are a couple of other issues open in the microsoft/vscode about tokenization and syntax highlighting.

cc: @jeff-hykin

@stamblerre For LSP side, maybe we need to keep an eye on the progress of microsoft/vscode-languageserver-node#367 and
microsoft/vscode#77140

@jeff-hykin
Copy link

@appleton
So this is a common complaint because it's true the Go Lang TextMate syntax is minimal at best. It's going to take some work to improve it though (PR's are welcome).

TextMate (the language-agnostic engine VS Code uses) is a very primitive parser, it will never be capable of matching GoLand. Language servers however (while slower and language-specific) can match GoLand, but building a language server is beyond my knowledge.

Typescript has a somewhat unfair advantage because it is the language the VS Code team uses for everything. Naturally it is going to be prioritized over everything else, and the language server for it has had a lot of work and optimizations put into it.

Here's what can be done right now with the TextMate grammar

  • the float64() is an error, that should be fixed on the better-go-syntax and highlighted as a type casting
  • the p and other can be highlighted in the p *Point and other *Point but can't be highlighted correctly in the body without a language server
  • *Point can be highlighted it just takes work to add the ability
  • math, p, and other can be highlighted as the same color (the color of an unknown variable), again though it just takes work to add the ability

Also be aware your theme can make a notable difference. Here's (basically) the same code on a different theme.
code

@appleton
Copy link
Author

Thank you for the thorough response @jeff-hykin, I appreciate it!

I've cloned your syntax repo and will try to add support for the tokens you've listed as possible. I understand the language server restriction for the others.

Aside: what theme is that in your screengrab? I like it!

@stamblerre stamblerre added the upstream-grammar Issue is related to the grammar used for Go which is controlled upstream in VS Code project label Mar 27, 2020
@jeff-hykin
Copy link

@appleton No problem 👍

As for cloning the repo. It might be kinda tough to contribute at the moment just because TextMate grammars are notoriously messy and hard to contribute to. However, it is much easier with a guide, and I'm happy to help be that guide. Another guy and I have been working on a library for over a year now that makes TextMate grammars manageable. He finished the inline documentation about a month ago and I have been working on a tutorial posts titled "Make a TextMate grammar (without wanting to kill yourself)". I'll probably be posting it on Medium in the next month and that should be able to jumpstart any contributions and explanations of the codebase.

Well, the exact theme actually doesn't exist haha. There is a very close theme: XD Theme made by yours truly. For the screenshot I edited the theme to show what could be colored.

If you want to you can add the following to your settings to recreate the screenshot. In general you can customize whatever you'd like following this brief guide.

{
"editor.tokenColorCustomizations" : 
    {
        "[XD Theme]": {
            "textMateRules": [
                {
                    "name": "green",
                    "scope": [
                            "source.go storage.type",
                            "source.go storage.type.string",
                        ],
                    "settings": {
                        "foreground": "#4EC9B0",
                        "fontStyle": "none"
                    }
                },
            ]
        }
    }
}

@ramya-rao-a
Copy link
Contributor

Closing this issue as there are no next steps from the extension's side to solve the problem here. For further discussions, please log an issue at https://github.com/jeff-hykin/better-go-syntax/issues

Thanks all!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
upstream-grammar Issue is related to the grammar used for Go which is controlled upstream in VS Code project
Projects
None yet
Development

No branches or pull requests

5 participants