Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code Language Extension Inherit Existing

In Visual Studio Code it is relatively easy to add your own Language Extension just by providing a grammar file, e.g. via JSON.

I want to provide a syntax file for a particular script language I use. The script language is embedded in ARM Assembly source code, which there already exists a plugin for. So I basically want to extend the ARM Extension by my script language. Is that currently possible?

This would IMO be the way to go in my case, because if I just copy the existing extension (Which is MIT licenced) I would de-facto create a hard-fork, which I do not intend to do.

like image 404
Karathan Avatar asked Jul 20 '18 01:07

Karathan


1 Answers

1. Injection

You can inject your language extension to the scope of the parent scope by adding the following to your package.json:

"contributes": {
    "grammars": [
        {
            "scopeName": "source.asm.x86_64.your_syntax_extension",
            "path": "./syntaxes/your_syntax_extension.json",
            "injectTo": [ "source.asm.x86_64" ]
        }
    ]
}

2. Include

If your language extension uses a custom file-type or you want to override some of the parent scope's syntax definitions, you can write you own definition and include the parent scope. You can use either of the following formats:

your_syntax_extension.json

{
  "fileTypes": [
    "myExtension"
  ],
  "name": "Your Syntax Extension",
  "patterns": [
    {
      "include": "source.asm.x86_64"
    }
  ],
  "scopeName": "source.asm.x86_64.your_syntax_extension"
}

your_syntax_extension.tmLanguage

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>fileTypes</key>
    <array>
        <string>myExtension</string>
    </array>
    <key>name</key>
    <string>Your Syntax Extension</string>
    <key>patterns</key>
    <array>
        <dict>
            <key>include</key>
            <string>source.asm.x86_64</string>
        </dict>
    </array>
    <key>scopeName</key>
    <string>source.asm.x86_64.your_syntax_extension</string>
</dict>
</plist>

In both cases, you might want to include the extended package as a dependency. To do so, add its unique identifier to your package.json:

"extensionDependencies": [
    "13xforever.language-x86-64-assembly"
]
like image 86
idleberg Avatar answered Nov 15 '22 07:11

idleberg