Customizing syntax highlighting in Visual Studio Code - visual-studio-code

I'm currently trying to write an extension for a new file type (ANTLR) and wonder how to change the colors used for syntax highlighting in Visual Studio Code. To me it looks as if that is not defined in the extension, but somewhere else. There is no preferences entry for colors nor did I find a CSS file which defines that (which I'd expect since vscode is using Electron). I also looked through the settings file vscode generated and files that came with it, but no clue either. Neither did a web search help. So, I'm kinda lost now.
Can someone give me some hints or point me to the relevant docs?

There are two concepts at play here:
language grammars, which turn a text file into tokens with different scopes, and
themes, which colorize those scopes in a (hopefully) eye-pleasing way.
If you're writing your own grammar, or converting from TextMate etc., there's a chance you're using different scopes than the ones defined by the theme. In that case, there won't be a clear distinction between the tokens you define, even if they are actually defined.
There are two ways out of this. First one is, extend a theme with your custom scopes and colour them however you want. Not really a good way to go, unless everyone using your language also likes your colour scheme. The other is, use the (limited set of) scopes already defined and colorized by VSCode and the theme authors. Chances are, your language is going to look good in your theme of choice, and good enough in others.
To give you an example, here's the comment scope as defined by the default dark VSCode theme.
"name": "Dark Visual Studio",
"settings": [
{
"scope": "comment",
"settings": {
"foreground": "#608b4e"
}
},
and here's the equivalent language snippet from the C++ grammar:
"comments": {
"patterns": [
{
"captures": {
"0": {
"name": "punctuation.definition.comment.java"
}
},
"match": "/\\*\\*/",
"name": "comment.block.empty.java"
},
Basically, the language defines multiple tokens under comment, as needed, and since the theme says that comment.* will be green, they all get colorized the same.

There is no need to patch the theme, from the official documentation:
To tune the editor's syntax highlighting colors, use editor.tokenColorCustomizations in your user settings settings.json file
Besides simple token customization you can fully override the TextMate rules with a slightly more complex setting, for example:
"editor.tokenColorCustomizations": {
"textMateRules": [
{
"scope": "keyword.control.ref.latex",
"settings": {
"foreground": "#FF0000"
}
}
]
}

Syntax highlighting rules are stored in .plist files (or alternatively in .tmLanguage files). In those files different token types are declared for syntax highlighting:
What is a keyword?
What is a string literal?
What is a comment?
etc.
Take a look here to get more information about it: https://code.visualstudio.com/Docs/customization/colorizer
You do not define colors in .plist files!
The relation between token types and colors is done in color theme declarations.
Learn more about it here
https://code.visualstudio.com/Docs/customization/themes and here How to add theme in Visual Studio Code?
In general this document is also helpful when you try to extend VSCode: https://code.visualstudio.com/docs/extensionAPI/overview

You might consider using a color theme
Since VSCode 1.44 (March 2020), you now have:
Theme Support for Semantic Tokens
Color themes can now write rules to color semantic tokens reported by language extensions like TypeScript.
"semanticHighlighting": true,
"semanticTokenColors": {
"variable.declaration.readonly:java": { "foreground": "#00ff00" "fontStyle": "bold" }
}
The rule above defines that all declarations of readonly variables in Java should be colored green and bold
See the Semantic Highlighting Wiki Page for more information.

Related

How to edit the color theme for mathematical operators (+, -, =, .etc) in Visual Studio Code?

I'm trying to personalize the color theme on Visual Studio Code because I'm used to a certain theme on a another software. So I want to change the color of the mathematical operators (+, -, =, ...).
I tried scrolling through the different options under workbench.colorCustomizations and editor.tokenColorCustomizations but I can't seem to find anything that changes the color of the operators.
Does anyone know which keyword covers the operators?
The first things you need to do is to find the token name/scopes of the operatos. You can do this by open vscode command palette Ctrl + Shift + P then search for Developer: Inspect Editor Tokens and Scopes, check the scopes by moving you caret.
After finding the scope name, open your vscode JSON settings, and create new textMateRules, it's looks like this:
"editor.tokenColorCustomizations": {
"textMateRules": [
{
"scope": ["bold.inline.source.post"],
"settings": {
"foreground": "#D55"
}
}
]
}
Reference: vscode syntax highlighting

Change style of VS Code Markdown `code` syntax highlighting

I just installed VS Code 1.70.0 on a new machine, and imported settings by copying settings.json from the old machine. I opened a Markdown (.md) file, and oddly the code segments marked with backtick characters are very hard to see. For example, both "foo" and "bar" below appear to be the same style.
foo `bar`
I zoomed in with the magnifier, but even then the style looks almost identical to the other text—the red color is very faint. This is the same monitor as before, so maybe it is related to the video driver on the new computer.
Is there some way to tweak the syntax highlighting just for inline code formatting for Markdown in VS Code?
Yes, you can tweak the color by:
Go to Command Palette and find "Preferences: Open User Settings (JSON)"
In the settings.json that it opened, add the following field:
"editor.tokenColorCustomizations": {
"[YOUR COLOR SCHEME]": {
"textMateRules": [
{
"scope": "markup.inline.raw.string.markdown",
"settings": {
"foreground": "#aaff00", // <-- Change your color here
}
}
]
}
}
In the 2nd line of the above code, change YOUR COLOR SCHEME to the name of the color scheme you are using. For example, my color scheme is called "Monokai Pro" so the 2nd line would be:
"[Monokai Pro]" : {
Change the color to whatever you like in the "foreground" field. For example, I'm using #aaff00 (lime color) and this is how it looks like:
Tip
You may wonder how do I know that the "scope" field should be "markup.inline.raw.string.markdown". To see which TextMate scope that the markup inline code belongs to:
Open Command Palette and search for "Developer: Inspect Editor Tokens and Scopes"
Place your mouse caret inside the markdown inline code

VSCode create custom colorized language syntax highlighter extension for a custom language

Where and how do we change the colors for the syntax highlighters for specific languages?
GOAL: Ideally, I'd like to:
1. Understand how to create an extension that modifies the colors of code
2. Branch and modify this extension to do what I want (but I can't find out which is the file to add colors to) Freemarker Highlighting Extension
All I've found was information on editor.tokenColorCustomizations for the editor in general, but this is apparently not allowed in settings.json for individual languages: "This setting does not support per-language configuration." **
I have found plenty of information on how to create your own language extensions, etc., but none of them deal with the code colorization details.
The search term "visual studio how to create an extension to color code a language" comes up with pretty much everything but what I'm trying to figure out.
It seems that Yo Code may be part of the solution, but again, no specifics about colorization.
Things I've researched / read:
TextMate language grammars (there is one for the language I'm trying to re-color (https://github.com/bburbach/textmate-freemarker-bundle/blob/master/FreeMarker.tmbundle/Syntaxes/FreeMarker.tmLanguage) and even an extension for Freemarker, but no way to change the color scheme
Gone through the repo for the extension I mention above but see nowhere that the colors are declared (https://github.com/dcortes92/vs-freemarker)
Various VSCode support pages on syntax highlighting (https://code.visualstudio.com/api/extension-guides/color-theme, https://code.visualstudio.com/docs/getstarted/settings, https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide, https://code.visualstudio.com/api/references/theme-color
And lots of other random pages about syntax highlighting
Maybe it's obvious to some of you, but I am unable to find the answers I need. So if the SO community can help point me in the right direction without giving me a hard time, I'd appreciate it.
The TextMate language grammar gives a scope to certain parts of the file: keyword, string, number.
The Theme file has a section where it assigns colors to the TextMate scopes. From the generic to maybe the detailed scopes for a particular language. And most likely the semantic colors (unused variable, unreachable code, ...).
If your particular language already has a TextMate language you modify editor.tokenColorCustomizations in settings.json (VSC Configuration), and specific the textMateRule. Better to do it for a particular theme.
"editor.tokenColorCustomizations": {
"[One Dark Pro]": {
"textMateRules": [
{ "scope":"punctuation.definition.comment",
"settings": {"foreground": "#229977"}
},
{ "scope":"variable.other.property.js",
"settings": {"foreground": "#00ff00"}
}
]
}
}
These are dummy colors

How to highlight current line number in Visual Studio Code / VS Code

I am in the process of migrating from Atom to VS Code, as it seems to be what all the cool kids use these days.
In atom I was able to highlight the current line number as pictured (the blue highlight in the gutter).
Is there a way to do this in VS Code? With a setting or extension? Can't find a way to do it, and really missing that obvious at-a-glance indication of where I'm working.
(I know that I can add a background to the current line itself, but this is too intrusive to the code, especially working with a variety of languages in different colours.)
Thanks!
You could try
"editor.renderLineHighlight": "gutter"
UPDATE
In an ideal world I'd want both the gutter and the line itself highlighted, but in 2 very different colours - sadly that one doesn't seem possible, but this option is better than nothing!
Well, you may try something like settings below, liner number in different color as well as box for the line
"editor.renderLineHighlight": "all",
"workbench.colorCustomizations": {
"editor.lineHighlightBackground": "#00000000",
"editor.lineHighlightBorder": "#0000ff"
}
To address dark and light themes
In the settings.json file add something like:
"workbench.colorCustomizations": {
"[Default Dark+]": {
"editor.lineHighlightBackground": "#00000071",
},
"[Default Light+]": {
"editor.lineHighlightBackground": "#0000003f",
}
},

Custom brace highlighting in Visual Studio Code

Is it possible to customize the brace highlighting in Visual Studio Code? It seems just about everything else is customizable through user and workspace settings, as well as textmate themes. Regardless of the syntax highlighting you employ, the braces always have the same light gray outline/rectangle around them. I don't see an existing user/workspace setting or a textmate scope that addresses this specific feature.
Ultimately I'd like to have a solid color highlight of matching braces, similar to what you would get with the default dark theme in Visual Studio 2013 and 2015.
For future reference, vscode now has the option to change the color of bracket highlighting by adding this to settings.json:
"workbench.colorCustomizations" : {
"editorBracketMatch.background": "#f008",
"editorBracketMatch.border": "#f00"
}
Formats supported are #RGB, #RGBA, #RRGGBB, #RRGGBBAA. The rgba(255,255,255,1) format that seems to work in other places in the settings file appears to not work here.
Only feature that is still missing regarding this subject is bracket highlighting when the cursor is between brackets. They only highlight when one of the brackets is selected. I did not find a current solution searching trough the Settings or workbench.colorCustomizations.
Update 2018.04.21
There is now a plugin called Bracket Pair Colorizer that does shows the current brackets besides the line number no matter where the cursor is placed within the brakes. But on slower hardware (3nd generation i5 laptop) i encountered slowdowns with it when editing large files (3000+ line php file). And alternate plugin that performs well on the same file and highlights the current indentation is Guides.
Update 2019.11.10
VSCode 1.40 now highlights the brackets enclosing the cursor.
Update 2021.08.09
as #desilicius mentioned in the comments Bracket Pair colorizer is no longer maintained by the author. He offers as an alternative Blockman - Highlight Nested Code Blocks which has the same functionality but has a totally different look and Highlight Matching Tag which is a great plugin but only works on html (and jsx) tags as the name implies.
Update 2021.08.13
The VSCode team decided to add bracket pair colorization as a built in feature. It is worked on right now with improvements of 10k x the speed of BPC2. The discussion can be found here
You can try Bracket Pair Colorizer 2 Extension.
Highlighting and changing colors of brackets in VS Code is now possible.
You can try this extension Subtle Brackets by modifying the following the default values in the .json settings of VS Code:
I changed it like this:
"subtleBrackets.bracketPairs": [
"{}",
"[]",
"()",
"<>"
],
"subtleBrackets.styles": {
"global": {
"color": "yellow",
"borderWidth": "1px",
"borderStyle": "none none solid none"
}
}
Don't forget to set to "false" the value of .matchbrackets of VS Code.
"editor.matchBrackets": false
Update September 2021: The "Bracket pair colorization" feature is now available in VS Code as of the August 2021 (version 1.60) update.
Add this to your settings.json file:
"editor.bracketPairColorization.enabled": true
After saving the file, brackets should instantly be colorized.
Also maybe you will like VSCode extension "Blockman", it highlights nested code blocks. (I am the author of Blockman).
The VSCode extension "Blockman" is a new approach of dealing with brackets, well, it does not highlight brackets themselves, but it renders blocks based on the brackets (curly/square/round brackets/braces), and I think it is a better visual helper for our eyes to perceive code structure more easily, to understand code structure more quickly and more comfortably.
If you want to remove brackets color and border, you can use hex colors with 0 opacity in your settings.json:
"workbench.colorCustomizations": {
"editorBracketMatch.border": "#ffffff00",
"editorBracketMatch.background": "#ffffff00",
}
You can overwrite any theme:
"workbench.colorCustomizations": {
"[*]": {
"editorBracketMatch.border": "#ffffff00",
"editorBracketMatch.background": "#ffffff00",
}
}
In order to target a specific theme, replace the glob character, *, with the theme name.
Custom brace highlighting will be even more precise with VSCode 1.76 (Feb. 2022), with issue 170497 and PR 172758: it brings support for coloring a subset of the matched bracket.
Example:
"[plaintext]": {
"editor.language.brackets": [
["{", "}"],
["(", ")"],
["[", "]"]
],
"editor.language.colorizedBracketPairs": [
["{", "}"],
["<", ">"]
]
},
Gives:
This is available with VSCode Insiders today.