Search code examples
visual-studio-codevscode-extensionscode-snippetsvscode-snippets

VSCode snippets as extension


I created a code snippet like this in the snippets/snippet.json file:

{
  "Add subscription": {
    "scope": "typescript",
    "prefix": "!subscription",
    "body": "subscription: Subscription$0;",
    "description": "Add subscription property to a class"
  }
}

And I created a package.json file too:

{
  // ...
  "contributes": {
    "languages": [
      {
        "id": "typescript",
        "aliases": ["typescript", "ts"],
        "extensions": [".ts"]
      }
    ],
    "snippets": [
      {
        "language": "typescript",
        "path": "./snippets/snippets.json"
      }
    ]
  }
}

If I put this snippet JSON into any project's .vscode/mysnippets.code-snippet file, then it's working fine. But if I publish as an extension and then install it, it's not working.

Any idea?


Solution

  • The main problem is that the extension is installed as a disabled extension.

    Using debug mode a message tells the possible cause.

    you have a "main" property in package.json but not an "activation" property.

    Because there is no index.js in this snippet extension, remove the line

    "main": "index.js",
    

    To get a cleaner package also define a file .vscodeignore file with content

    .vscode/**
    .gitignore
    

    There is still a warning left

    [netdjw.ddata-snippets]: One or more snippets from the extension 'ddata-snippets' very likely confuse snippet-variables and snippet-placeholders (see https://code.visualstudio.com/docs/editor/userdefinedsnippets#_snippet-syntax for more details)

    You have to add them one at a time to see which snippet is causing this warning.