1

I'm writing a VSCode custom editor extension. The extension should be activated for component.yaml files. However I realize that there could be files named component.yaml with completely different formats. I want to detect that the file is not in expected format and bail out, skipping my extension, so that the file opens in the default text editor or another registered extension.

How can my extension refuse/skip opening a file?

Ark-kun
  • 6,358
  • 2
  • 34
  • 70

1 Answers1

0

An extension cannot refuse to be opened, but you can specify a regular expression for the first line in the file, which has to match to allow your extension to be activate for that file.

{
  "contributes": {
    "languages": [
      {
        "id": "python",
        "extensions": [".py"],
        "aliases": ["Python", "py"],
        "filenames": [],
        "firstLine": "^#!/.*\\bpython[0-9.-]*\\b",
        "configuration": "./language-configuration.json",
        "icon": {
          "light": "./icons/python-light.png",
          "dark": "./icons/python-dark.png"
        }
      }
    ]
  }
}

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
  • Thank you for the suggestion. Maybe there is a way an extension can conditionally activate a default text editor instead of itself? – Ark-kun Mar 28 '22 at 08:21