如何在VSCode中修改C++文件类JSDoc标签及Doxygen标记的高亮颜色
Got it, let's break this down so you can get those C++ Doxygen tags looking exactly like your JS JSDoc ones—blue @param, @return, and all. Here's how to make it happen in VSCode:
First, pull up the command palette:
- Windows/Linux: Press
Ctrl+Shift+P - Mac: Press
Cmd+Shift+P
Type in Preferences: Open User Settings (JSON) and hit enter. This opens the raw JSON config file where we'll add our custom highlighting rules.
VSCode uses Textmate syntax scopes to style text, so we'll target the Doxygen tags in C++ comments and force them to use the same blue as JS JSDoc tags. Paste this into your settings.json file (make sure it's inside the top-level curly braces):
"editor.tokenColorCustomizations": { "textMateRules": [ // Target all Doxygen-style @tags in C++ single-line and block comments { "scope": "comment.line.double-slash.cpp entity.name.tag.doxygen.cpp, comment.block.cpp entity.name.tag.doxygen.cpp", "settings": { "foreground": "#569CD6" // This is VSCode's default JS JSDoc tag blue—feel free to tweak the hex code! } }, // Optional: If you also want to style JSDoc-style class tags (like @class) the same way { "scope": "comment.line.double-slash.cpp storage.type.class.jsdoc.cpp, comment.block.cpp storage.type.class.jsdoc.cpp", "settings": { "foreground": "#569CD6" } } ] }
Quick breakdown of what this does:
- The first rule targets every
@param,@return,@brief, etc., in both//single-line comments and/* */block comments in .cpp/.hpp files. - The hex color
#569CD6matches the default blue used for JSDoc tags in JavaScript files. If you want a different shade, just swap in your preferred hex value.
Sometimes syntax scopes can vary based on installed extensions (like the official C/C++ extension). To confirm exactly what scope your Doxygen tags are using:
- Open a .cpp file with a comment containing
@param - Open the command palette again and run
Developer: Inspect Editor Tokens and Scopes - Click directly on the
@paramtext—look in the right-hand panel for the "Scope" section. You'll see something likeentity.name.tag.doxygen.cpp. - If your scope is different, just replace the scope string in the JSON rule above with the one you found.
If you want the C++ tags to mirror JS JSDoc perfectly (like having italic text too), add the fontStyle property to the settings:
"settings": { "foreground": "#569CD6", "fontStyle": "italic" }
Once you save the settings.json file, close and re-open your .cpp/.hpp files—your @param and @return tags should now be bright blue, just like they are in your JavaScript files.
内容的提问来源于stack exchange,提问作者user5203275

