Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode. How to reload theme after editing its style

Hello I'm trying to make custom theme for VS Code and want to see style changes right after editing, currently i need to restart editor to see changes which is not very productive.

Is there is any way to enable something like "hot reload" for theme/extension development?

like image 624
Winns Avatar asked Jun 06 '17 13:06

Winns


3 Answers

"hot reload" for theme/extension,

not specifically, but there is a Reload Window command, which is quite fast.
It is a suggested solution by VSCode.
You can Press Ctrl+Shift+P for Command Palette and type Reload Window,

or define a key binding: Open Keyboard Shortcuts from menu or Ctrl+K,Ctrl+S,
find Reload Window command using Search, add your desired key combination

As already suggested by Alex and also VSCode's themes article,
a flexible way to edit a theme, is to transfer lines with values you want to tweak into the Settings. Or create new ones, it's easier there, as you can take advantage of IntelliSense and "live" update, which is not really live, but upon Settings file Save (not all settings are live, e.g. editor.foreground will not auto update)
Once you are satisfied with the changes, or with your new entries, transfer lines containing them from Setting to the Theme file and reload.
Settings in Setting file have priority, will override values from Theme file, if they exist there.

There are usually two sections in a Theme file, one defining Workbench colors,
in Theme file, these settings are under colors Key. e.g.:

"colors": {
    "editor.background": "#000000",
    "editor.something": "#ffffff"
}

but to tweak them in the User Settings, they must go in workbench.colorCustomizations Key. e.g.:

"workbench.colorCustomizations": {
    "editor.background": "#000000",
}

then there are Syntax highlighting colors,
these might be in Theme file in an Array under tokenColors Key, or referenced to external file.
To tweak e.g. String color and style:

{
    "name": "String",
    "scope": "string",
    "settings": {
        "fontStyle": "",
        "foreground": "#E6DB74"
    }
},

place it in User Setting under editor.tokenColorCustomizations inside textMateRules:

"editor.tokenColorCustomizations": {
    "textMateRules": [
        {
            "name": "String",
            "scope": "string",
            "settings": {
                "fontStyle": "",
                "foreground": "#E6DB74"
            }
        },
    ]
},

(*) Trailing commas
Watch out for the trailing commas. They can be a nuisance. The last entry must not have it and when you are copying items up and down, it's easy to miss one.
As a little help you can take advantage of the fact that Settings will forgive you if you'll leave it there. From some point of view, considering that the setting will reside in Settings only temporarily, it might be less error prone to not remove the comma from Settings, if it was already there in Theme file and you're going to put the line/block back to Theme file after tweaking. This is why I included the comma, against JSON rules in examples above.

(*) Error in JSON
If you make an error in JSON and then save and reload VSCode still with the error in JSON, the modifications will not show and theme will look the same as from before the reload. It looks like the VSCode is still using a cached version of the Theme file from before the error was made.
This is quite confusing. Expected behavior would be to fall back to default Theme.
If you then open the Theme selector, your theme will still be listed as selected, but if you select another one and then try to select yours again (one with the error), VSCode will ignore this action, keeping the another theme. This behavior might confuse further, expected would be to show error, or not allow to make the selection.

like image 197
papo Avatar answered Oct 19 '22 05:10

papo


Edit: from vscode 1.31.0 hot-reload is working for theme in [Extension Development Host (when launching F5 extensionHost].

Hot reload works in settings.json Ctrl+,. After theme is ready simply move the content.

"workbench.colorCustomizations": {},
"editor.tokenColorCustomizations": {
    "textMateRules": []
},
like image 31
Alex Avatar answered Oct 19 '22 07:10

Alex


The best I came up with, was to make a shortcut to:

code -n c:\projectdir

and give it a keyboard shortcut like Ctrl + F1.

Then, after editing, save your file, hit Ctrl + F1 and a new VS Code will open up with the theme freshly loaded. If you open up files with several different languages, you get a good preview. Then you can just close that window again, and continue editing your theme. Rinse, repeat.

like image 2
Jacob Bruinsma Avatar answered Oct 19 '22 07:10

Jacob Bruinsma