简体   繁体   中英

VSCode language extension by filename pattern

I am trying to set up custom language highlighting in VSCode extension. So far I was successful at making it depend on file extension. But due to some specifics of structure I need to apply language by a string in the file path. This is the example from VSC API docs:

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

It seems there is a filenames parameter. But from my testing it seems that it support only full name of the file, doesn't accept regex or file path.

Is there a way to enable language by a part of file path. For ex.: we have a file \someFolder\Important\file.file , apply our custom language to all file in that have Important in their path.

Use filenamePatterns , which allows glob matching. So, in your package.json:

{
  "contributes": {
    "languages": [
      {
        "id": "python",
        "aliases": ["Python", "py"],
        "filenamePatterns": [
          "*.py",
          "*Important*"
        ],
        "firstLine": "^#!/.*\\bpython[0-9.-]*\\b",
        "configuration": "./language-configuration.json"
      }
    ]
  }
}

Note: the filenamePatterns feature is not documented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM