.sublime-syntax 文件类型的 sublimetext 3 中的多个语法突出显示

multiple syntax highlighting in sublimetext 3 with .sublime-syntax file type

我正在使用 zk 框架,我需要做一个在 XML 语法中突出显示的 sublime-syntax 文件,但在 zscript 片段中使用 java 语法突出显示,在样式上下文中使用 CSS语法。 这是zk代码的例子:

<zk>
    <style>
        .myClass{
            width=300px;
        }
    </style>

    <div id="panel1" class="myClass" visible="true" >   
        <hlayout width="100px">
            <image id="icon1" src="/Desktop/image1.png" width="32px"></image>   
            <image id="icon2" src="/Desktop/image1.png" width="50px"></image>   
        </hlayout>
    </div>  

    <zscript><![CDATA[
        try{
            if (panel1.isVisible()) {
                //do something
            }
        }catch(Exception e){
            //catch exception
        }
    ]]></zscript>
</zk>

最近看到有改动,目前网上的解决方案不是很清楚,比如在this link我发现了下面的注释:

As of Sublime Text Build 3084, a new syntax definition format has been added, with the .sublime-syntax extension.

It is highly encouraged to be used in favor of the legacy format described in this document, unless compatibility with older versions is desired.

Documentation is available here: http://www.sublimetext.com/docs/3/syntax.html

所以我需要一些类似教程的东西,关于如何使用 SublimeText3 构建一个新的多语法文件。

好的,我通过安装 PackageDev (Ctrl+Shift+P, select Package Control: Install Package, type and select PackageDev 以便安装它),然后我 selected: Tools -> Packages -> Package Developement -> New Syntax Definition

这里我写了这段代码:

%YAML 1.2
---
# See http://www.sublimetext.com/docs/3/syntax.html

name: zul
file_extensions:
 - zul
scope: text.zul

contexts:
  main:
    - match: ""
      push: "Packages/XML/XML.sublime-syntax"
      with_prototype:

        - match: '< *zscript *>'
          push: Packages/Java/Java.sublime-syntax
          with_prototype:
            - match: '(?=</ *zscript *>)'
              pop: true

        - match: '< *script *>'
          push: Packages/JavaScript/JavaScript.sublime-syntax
          with_prototype:
            - match: '(?=</ *script *>)'
              pop: true

        - match: '< *style *>'
          push: Packages/CSS/CSS.sublime-syntax
          with_prototype:
            - match: '(?=</ *style *>)'
              pop: true
  • File_extensions 是使用此语法的扩展列表,
  • scope 对于编程语言是 source,对于标记和其他一切都是 text
  • match 是传递给 push 元素的正则表达式。
  • with_prototype 类似于语法突出显示中的异常,您可以在其中为不同的上下文定义具有不同语法的片段

这个例子一般使用xml语法,在标签<zscript> ... </zscript>之间使用java 语法高亮,在 <style> ... </style> 上下文中使用 css 语法。

我在 C:\Users\username\AppData\Roaming\Sublime Text 3\Packages\User 中保存了这个文件,然后在 View -> Syntax -> User -> zul 中创建了这个语法文件(文件名)。