Maria Ines Parnisari
Maria Ines Parnisari

Reputation: 17496

How to add "show preview" button to VS Code extension?

I have a VS Code extension that analyses custom JSON and YAML files. So in the project's package.json, there is this:

    "activationEvents": [
        "onLanguage:yaml",
        "onLanguage:json",
        "onCommand:extension.sidePreview"
    ],

Whenever someone opens one of these files, I'd like to add a "show preview" icon in the top right corner of the editor:

preview

So I added the corresponding icon resources to the project, and:

"contributes": {
        "commands": [
            {
                "command": "extension.sidePreview",
                "title": "Preview file",
                "icon": {
                    "dark": "./resources/open-preview-dark.svg",
                    "light": "./resources/open-preview-light.svg"
                }
            }
        ],
        "menus": {
            "editor/title": [
                {
                    "command": "extension.sidePreview",
                    "when": "true"
                }
            ]
        },

But this doesn't work... I don't see any icon.

I'd also like to ensure that this button and command are only available when my function isCustomFile in server.ts returns true. Is there a way of doing this?

Upvotes: 0

Views: 1688

Answers (1)

Lex Li
Lex Li

Reputation: 63203

That's because you added the wrong section under menus.

You are supposed to add editor/title instead.

Reference

Upvotes: 3

Related Questions