Theming API
Relevant source files
The following files were used as context for generating this wiki page:
- extensions/theme-defaults/package.json
- extensions/theme-abyss/package.json
- extensions/theme-monokai/package.json
- extensions/yaml/syntaxes/yaml.tmLanguage.json
- extensions/vscode-colorize-tests/test/colorize-results/issue-6303_yaml.json
- extensions/markdown-language-features/package.json
- extensions/theme-solarized-dark/package.json
The Visual Studio Code Theming API allows extensions to customize the visual appearance of the editor. This includes both the overall UI (color themes) and the file icons displayed in the explorer (icon themes). Theming is primarily declarative, with extensions contributing themes through their package.json manifest file. These themes define colors for UI elements and for syntax highlighting in the editor.
Syntax highlighting is achieved by mapping token scopes, defined by language grammars (TextMate .tmLanguage.json files), to specific colors and styles within a theme's JSON definition file. This creates a decoupled but powerful system where language support and visual appearance are managed independently but work together to provide a rich user experience.
Theme Contribution
Themes are contributed by extensions via the contributes section in their package.json file. An extension can contribute multiple themes, which can be either color themes or icon themes.
The following diagram illustrates the contribution points within an extension's manifest for defining themes.
This diagram shows how package.json uses the contributes field to register both themes (color themes) and iconThemes.
Sources: extensions/theme-defaults/package.json:13-53, extensions/theme-abyss/package.json:11-19
Color Themes
Color themes define the colors for UI components and syntax highlighting. Each color theme is an object in the themes array.
| Property | Type | Description |
|---|---|---|
id | string | A unique identifier for the theme. |
label | string | The display name of the theme shown in the UI. Often uses a localization key like %themeLabel%. |
uiTheme | string | The base theme for the UI. Determines whether the theme is light or dark. (e.g., vs, vs-dark, hc-light, hc-black). |
path | string | The path to the JSON file that defines the theme's colors and styles. |
Sources: extensions/theme-defaults/package.json:15-19, extensions/theme-abyss/package.json:13-18, extensions/theme-monokai/package.json:13-18
Here is an example of a color theme contribution from the Abyss theme extension:
"contributes": {
"themes": [
{
"id": "Abyss",
"label": "%themeLabel%",
"uiTheme": "vs-dark",
"path": "./themes/abyss-color-theme.json"
}
]
}
Sources: extensions/theme-abyss/package.json:11-19
The uiTheme property is crucial as it classifies the theme, allowing VS Code to apply appropriate base styles.
uiTheme Value | Base Experience | Example Themes |
|---|---|---|
vs-dark | Dark | Dark+, Dark Modern, Monokai, Abyss, Solarized Dark |
vs | Light | Light+, Light Modern, Solarized Light, Quiet Light |
hc-black | High Contrast Dark | Default High Contrast |
hc-light | High Contrast Light | Default High Contrast Light |
Sources: extensions/theme-defaults/package.json:15-50, extensions/theme-monokai/package.json:16, extensions/theme-solarized-dark/package.json:16
Icon Themes
Icon themes provide file-specific icons in the File Explorer and other UI areas. They are contributed under the iconThemes array.
| Property | Type | Description |
|---|---|---|
id | string | A unique identifier for the icon theme. |
label | string | The display name of the icon theme. |
path | string | The path to the JSON file that defines the icon mappings. |
Sources: extensions/theme-defaults/package.json:51-56
Example of an icon theme contribution:
"iconThemes": [
{
"id": "vs-minimal",
"label": "%minimalIconThemeLabel%",
"path": "./fileicons/vs_minimal-icon-theme.json"
}
]
Sources: extensions/theme-defaults/package.json:51-56
Syntax Highlighting and Scopes
Syntax highlighting is a core function of color themes. It works by associating "scopes" defined by a language's grammar with colors defined in the theme file.
Language Grammars and Scopes
VS Code uses TextMate grammars (.tmLanguage.json files) to parse source code and assign scope names to tokens. Each language extension typically contributes a grammar for the language it supports.
A grammar file defines a scopeName (e.g., source.yaml) and a set of patterns that match parts of the code and assign them more specific scopes.
The following diagram shows the process from source code to colored text on screen.
This flow shows that a language grammar parses code to produce token scopes, and the active theme file provides the color for those scopes, which the editor then renders.
Sources: extensions/yaml/syntaxes/yaml.tmLanguage.json:7, extensions/vscode-colorize-tests/test/colorize-results/issue-6303_yaml.json:1-12
For example, yaml.tmLanguage.json defines the main scope for YAML files:
{
"name": "YAML Ain't Markup Language",
"scopeName": "source.yaml",
"patterns": [
{
"include": "source.yaml.1.2"
}
]
}
Sources: extensions/yaml/syntaxes/yaml.tmLanguage.json:6-15
Theme-based Colorization
A theme's JSON file contains rules that map these scopes to colors. Test files, such as issue-6303_yaml.json, demonstrate how a specific piece of code is tokenized and what color it receives under different themes.
For a YAML file, the key swagger is assigned the scope entity.name.tag.yaml. The test result shows how different themes color this scope:
[
{
"c": "swagger",
"t": "source.yaml meta.stream.yaml meta.document.yaml meta.mapping.yaml meta.map.key.yaml string.unquoted.plain.yaml entity.name.tag.yaml",
"r": {
"dark_plus": "entity.name.tag: #569CD6",
"light_plus": "entity.name.tag: #800000",
"dark_vs": "entity.name.tag: #569CD6",
"light_vs": "entity.name.tag: #800000",
"hc_black": "entity.name.tag: #569CD6",
"dark_modern": "entity.name.tag: #569CD6",
"hc_light": "entity.name.tag: #0F4A85",
"light_modern": "entity.name.tag: #800000"
}
}
]
Sources: extensions/vscode-colorize-tests/test/colorize-results/issue-6303_yaml.json:2-19
This shows that the dark_plus theme assigns the color #569CD6 to tokens with the entity.name.tag scope.
Customization and Integration
Other extensions can integrate with the theming system. For example, the Markdown extension contributes its own preview styles and can embed other languages, which are then highlighted by the active theme.
Preview Styling
Extensions that provide webview-based previews, like the Markdown preview, can contribute their own CSS files. These styles are loaded alongside the theme's styles to ensure a consistent look and feel.
"contributes": {
"markdown.previewStyles": [
"./media/markdown.css",
"./media/highlight.css"
]
}
Sources: extensions/markdown-language-features/package.json:548-552
Embedded Languages
Language grammars can embed other languages. For instance, the Markdown Math extension injects a grammar for LaTeX into Markdown code blocks. The active color theme is then responsible for styling the LaTeX syntax within the Markdown file.
"grammars": [
{
"scopeName": "markdown.math.block",
"path": "./syntaxes/md-math-block.tmLanguage.json",
"injectTo": [
"text.html.markdown"
],
"embeddedLanguages": {
"meta.embedded.math.markdown": "latex"
}
}
]
Sources: extensions/markdown-math/package.json:34-44
This mechanism allows themes to provide a consistent highlighting experience across different file types and embedded contexts without needing to be aware of every possible language combination.