How can I change the color of C++ function parameters in VS Code?
Asked Answered
T

3

9

Environment

  • Visual Studio Code 1.25 for linux (ubuntu 16.04).

  • Theme VisualStudio Dark.

I'm currently using visual studio code 1.25 in Ubuntu 16.04 for writing c++ code. I want to change the color of function parameters like in the visual studio 2015 where they are greyed out both inside function declaration and in function body. I tried to achieve that by using the following user setting:

"editor.tokenColorCustomizations": {
    "textMateRules": [
      {
        "scope": "variable.parameter",
        "settings": {
          "fontStyle": "",
          "foreground":"#413f39"
        }
      }
    ]
  }

but with no success. Am I doing something wrong or this feature is not supported in vs code yet ?

Terce answered 28/7, 2018 at 20:52 Comment(6)
I am assuming your code above works for the function declaration but not in the body of the function? The parameter within the body has a different scope, which you can examine with "Inspect TM Scopes" in the command palette, which is probably something generic like "variable" and so isn't all that helpful. All variables within the function body would be similarly styles, not just those representing parameters. But maybe in a C++ file you will be luckier.Domiciliary
@Domiciliary The code doesn't work neither for the functon declaration nor in the body of the functionTerce
Hmmm, I just tried your code and it works just fine. You don't have any other conflicting textMateRules? Your code is correct otherwise.Domiciliary
@Domiciliary That's quite weird. I have no other textmate rule. I also put in my user settings json only the above code and the function parameters still have the same white color as the variables.Terce
Is there a page I can reference to see more than just variable.parameter that I can edit? I'd like to edit the color of the function name, etc.Lingo
worked for me in vscode for a .js file (I put your json in the settings.json file)Naucratis
S
1

I was looking for the same thing, using Monokai Pro theme and losing highlightin option.

I find out this solution, which must maybe interrest you :

"editor.semanticTokenColorCustomizations": {
  "enabled": true,
}

Try it in your settings.json file !

Skillful answered 1/12, 2022 at 9:12 Comment(0)
Z
1

I was also searching sulotions to change the color of parameters as it always in a kind of light color when using light theme

I tried these two solutions:

"editor.semanticTokenColorCustomizations": {
    "enabled": true,
  "rules": {
    "parameter": {
        "bold": true,
        "foreground": "#ff0000"
    }
  }
},

and

"editor.tokenColorCustomizations": {
    "textMateRules": [
      {
        "scope": "variable.parameter",
        "settings": {
          "fontStyle": "bold strikethrough",
          "foreground":"#413f39"
        }
      }
    ]
}

I found font style works fine in these two solutions,but the foreground color setting always fails.

I later discovered that the color setting would take effect when I closed the minimap, but then failed again when I switched tabs.It looks like there is some setting that overrides the color settings.

Zenaidazenana answered 21/10, 2023 at 6:39 Comment(0)
T
1

If you're using a language support extension that provides semantic highlighting for C++ (such as vscode-cpptools with IntelliSense properly configured), then you can use the following:

"editor.semanticTokenColorCustomizations": {
    "[Colour Theme Name Goes Here]": { // remove to apply regardless of colour theme selection
        "rules": {
            "parameter:cpp": { // remove the ":cpp" modifier to apply to all languages
                "foreground": "#FF0000", // TODO
                // "fontStyle": "italic bold underline strikethrough"
            }
        }
    },
},

Otherwise, you can use the following for the builtin TextMate grammar provided by VS Code:

"editor.tokenColorCustomizations": {
    "[Colour Theme Name Goes Here]": { // remove to apply regardless of colour theme selection
        "textMateRules": [{
            // "scope": "variable.parameter", // use this instead to apply to any language
            "scope": "variable.parameter.cpp",
            // "scope": "meta.parameter.cpp", // use this for entire parameter including its type
            "settings": {
                "foreground": "#FF0000", // TODO
                // "fontStyle": "italic bold underline strikethrough"
            }
        }]
    }
},

If those don't work as described, something else might be interfering, in which case you can debug using the scope inspector: Developer: Inspect Editor Tokens and Scopes in the command palette. I'm not familiar with other C++ VS Code extensions, but I'd wonder if another extension (Ex. jeff-hykin.better-cpp-syntax) could be contributing tokens/scopes that could be interfering.


As for why the attempt in the question post didn't work, note that the asker was on VS Code 1.25. Going through the release tags, I found that variable.parameter.cpp doesn't show up in extensions/cpp/syntaxes/cpp.tmLanguage.json until 1.34.

Tuberculate answered 21/10, 2023 at 7:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.