vscode/sublime 语法高亮定义和代码段的区别

vscode插件数据格式基于json,sublime插件数据格式基于xml。sublime插件的官方文档说的不清楚,相关教程也很难找,遇到的一些坑记录一下

语法定义文件对比

同样使用TextMate定义(tmLanguage),sublime可读取的是xml格式,使用plist结构,极其坑爹

vscode: test.tmLanguage.json

{
    "name": "Test",
    "scopeName": "source.test",
    "fileTypes": [
        "test"
    ],
    "patterns": [
        {"include": "#comments"},
    ],
    "repository": {
        "comments": {
            "match": "//.*",
            "name": "comment.line.double-slash"
        }
    }
}

sublime: test.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>uuid</key>
    <string>XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX</string>

    <key>name</key>
    <string>Test</string>
    <key>scopeName</key>
    <string>source.test</string>
    <key>fileTypes</key>
    <array>
        <string>test</string>
    </array>
    <key>patterns</key>
    <array>
        <dict>
            <key>include</key>
            <string>#comments</string>
        </dict>
    </array>
    <key>repository</key>
    <dict>
        <key>comments</key>
        <dict>
            <key>match</key>
            <string>//.*</string>
            <key>name</key>
            <string>comment.line.double-slash</string>
        </dict>
    </dict>
</dict>
</plist>
  • 格式区别: sublime提供一种单独的sublime-syntax格式,使用YAML格式编写,没试过。sublime提供了一种安装PackageDev后,使用YAML编写"*.YAML-tmLanguage"文件,再生成XML文件的格式,但在有一定限制需要手写的时候不太好用。因为使用xml,支持多行文本(vscode的json中使用字符串列表),字符串不需要使用双引号包围,也不需要''转义
  • 生效区别: vscode的语法文件需要在package.json中正确配置,sublime的语法定义文件直接塞到Packages文件夹就可以生效。但有一点需要注意的是sublime需要uuid字段来识别文件...因为这个问题纠结了一段比较长的时间

代码段定义对比

vscode

test.json 或 test.code-snippets

{
    "snippet1": {
        "scope": "source.test",
        "prefix": "header",
        "body": [
            "this is a header.",
            "this is the 2nd line."
        ]
    },
    "snippet2": {
        "scope": "source.test",
        "prefix": "header2",
        "body": [
            "this is a header 2.",
            "this is the 2nd line."
        ]
    }
}

sublime

header.sublime-snippets

<snippet>
    <scope>source.proto</scope>
    <tabTrigger>header</tabTrigger>
    <content>this is a header.
this is the 2nd line.</content>
</snippet>

header2.sublime-snippets

<snippet>
    <scope>source.proto</scope>
    <tabTrigger>header2</tabTrigger>
    <content>this is a header 2.
this is the 2nd line.</content>
</snippet>

二者的文件后缀名似乎都没什么限制。
sublime的代码段定义文件默认好像是只会读取第一个<snippet>片段,有多个代码段需要建立多个文件,没有深入研究是否有一个文件定义多个代码段的形式