Sublime Forum

Improved syntax highlighting for C++ in ST4

#1

Look at the image attached. this is VSCode’s syntax highlighting. Variables (like x or y) is colored in VSCode outside of the declaration. Whereas with ST4, the variables arent colored (it is colored, for example at the definition of x. But, when i use x, it isn’t colored). Same with the shared_ptr and Vector2. It’s colored in VSCode whereas in ST4 it isn’t. Overall, VSCode seems to ‘understand’ c++ better, because of the improved syntax highlighting. How can i do this (or at least replicate) this behaviour in ST4? I’m using the same Monokai Pro theme for both the images, if that helps. I can’t put the image of the one in sublime text 4 because im a new user. Any help would be appreciated. Thanks in advance!

0 Likes

#2

VS Code uses language servers’ intelligence to understand and highlight semantic meaning of tokens based on their context.

A static syntax highlighter such as the one used by ST is limited with regards to understand semantics, especially if they rely on dynamic information.

LSP package provides an option to leverage semantic highlighting provided by language servers. To enable it, open LSP settings and set "semantic_highlighting": true.

1 Like

#3


For some reason it still isn’t working. My LSP is clangd, which the LSP website says it supports semantic highlighting. It’s pretty weird. Here’s the LSP settings file:

    "clients": {
        "clangd": {
            "command": [
                "clangd",
                "--completion-style=detailed",
                "--header-insertion=iwyu",
                "-std=c++23"
            ],
            "enabled": true,
            // "file_extensions": ["cpp", "c", "cc", "h", "hpp", "hh"],
            // "root_patterns": [".clangd", ".git"]
        },
        "myopl-lsp": {
            "command": [
                "python",
                "C:\\Users\\Administrator\\Desktop\\LSP\\main.py"
            ],
            "enabled": true,
            "selector": "source.myopl",
            "auto_complete_selector": "source.myopl",
        },
    },
    "semantic_highlighting": true
}

ignore the ‘myopl’ one, i made an lsp for fun back then and tested it in ST4. Regardless, i’m still not getting the same effect in vscode. After I typed in the new setting, i restarted the lsp server and when that didn’t work, i restarted st4. Still isn’t working. Weird

0 Likes

#4

Maybe your Colour Scheme (theme) requires the tweak mentioned here to get Semantic Highlighting working: https://lsp.sublimetext.io/customization/#semantic-highlighting

1 Like

#5

Thanks for the help! it’s working now.

0 Likes