克隆一个崇高的文本 3 高亮语法定义 [英] Cloning a sublime text 3 highlighting syntax definition

查看:26
本文介绍了克隆一个崇高的文本 3 高亮语法定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个简单的程序可以从现有的定义中导出您自己的自定义 sublime text 3 突出显示定义?

Is there a simple procedure for deriving your own custom sublime text 3 highlighting definition, from an existing one?

我已经按照有序且略显乏味的 教程的建议安装了 AAAPackageDev一个新的高亮语法定义,但我发现克隆一个会让我走得更快.至少,我想了解如何浏览 sublime 附带的默认语法定义.

I've installed AAAPackageDev as recommended by the orderly and slightly tedious tutorial for creating a new highlighting syntax definition, but I find that cloning one would get me going much faster. In the very least, I'd like to learn how can I browse the default syntax definitions shipping with sublime.

如果你真的想要完全具体,我希望完成的只是一个原始的突出显示方案,在我自己定义的一对给定的开始和结束标记之间的任何字符串都会被着色.并且我自己定义的另一对不同标记之间的任何字符串都会用第二种颜色着色.一个好办法是也让令牌本身变灰.

If you really want to be entirely specific, all I wish to accomplish is a primitive highlighting scheme where any string between a given pair of opening and closing tokens, of my own definition, would get colored. And any string between another different pair of tokens of my own definition, would get colored with a second color. A nicity would be to also have the tokens themselves grayed out.

(Ubuntu 14.04)

谢谢!

推荐答案

UPDATE 2015-05-10:Sublime Text 3 build 3084 引入了一个全新的 sublime-syntax 格式,用于编写语法定义.它比 Sublime 从 TextMate 继承的旧系统要好得多.新系统应该很快就会在公开的 ST3 测试版中登陆.由于 ST3 是 推荐的 Sublime 版本,我建议使用 新系统而不是所描述的系统编写任何新的荧光笔下面.

UPDATE 2015-05-10: Sublime Text 3 build 3084 introduces a totally new sublime-syntax format for writing syntax definitions. It's much better than the old system, which Sublime inherited from TextMate. The new system should land in the public ST3 beta builds soon. Since ST3 is the recommended version of Sublime, I'd recommend writing any new highlighters using the new system instead of the system described below.


这是一个关于 Sublime Text 语法高亮的速成课程.

Here's a crash course in Sublime Text syntax highlighting.

首先,正如@lthreed 指出的,您可以使用 PackageResourceViewer 来查看 Sublime Text 附带的默认包.那些 .tmLanguage 文件都是 plist 格式,非常难读懂.PackageDev 可以将 plist 文件转换为更易读的 JSON 或 YAML 格式.当您通过查看默认包进行学习时,请务必先将其转换为 YAML.请注意,PackageDev 可能无法完美地转换它.没关系.您只是将代码用作参考.

First, as @lthreed pointed out, you can use PackageResourceViewer to look at the default packages that come with Sublime Text. Those .tmLanguage files are all in plist format, which is extremely difficult to read and understand. PackageDev can convert plist files into the far more readable JSON or YAML formats. When you're learning by looking at the default packages, make sure to convert it to YAML first. Be warned, PackageDev may not convert it perfectly. It doesn't matter. You're just using the code as a reference.

plist 是 Sublime 理解的原生格式,但这并不意味着你应该这样写.我强烈建议您使用 YAML 编写荧光笔,并使用 PackageDev 将其转换为 plist.不要用 JSON 编写它.JSON 不支持原始字符串.所有正则表达式都必须进行双重转义.这绝对是一场噩梦.只需使用 YAML.

plist is the native format that Sublime understands, but that doesn't mean you should write it like that. I strongly recommend writing your highlighter in YAML and converting it to a plist with PackageDev. Don't write it in JSON. JSON does not support raw strings. All the regexes will have to be double-escaped. This is an absolute nightmare. Just use YAML.

您可以通过打开命令面板(Mac 上为 cmd+shift+p)并选择 PackageDev: New YAML Syntax Definition 来开始新的语法定义.当您准备好测试它时,打开命令面板并选择 PackageDev: Convert (YAML, JSON, PList) to...,PackageDev 会发现您有一个 YAML 文件并且想要转换为 plist.转换需要你的 .YAML-tmLanguage 文件并输出一个 Sublime 理解的 .tmLanguage 文件.将该文件放在/Packages/User 目录中,Sublime 将加载并应用它(您可能需要重新启动).

You can start a new syntax definition by opening the command palette (cmd+shift+p on a Mac) and selecting PackageDev: New YAML Syntax Definition. When you're ready to test it, open the command palette and select PackageDev: Convert (YAML, JSON, PList) to... and PackageDev will figure out that you have a YAML file and want to convert to plist. The conversion takes your .YAML-tmLanguage files and spits out a .tmLanguage file that Sublime understand. Put that file inside the /Packages/User directory and Sublime will load it and apply it (you may have to restart).

您正在编写的语法定义不会直接为文本着色.它将范围名称应用于文本.然后编写像 Monokai 和 Solarized 这样的主题的人来制作将范围名称与颜色相关联的文件.您可以创建自己的范围名称,但应坚持使用 官方 TextMate 范围名称.这些范围名称对于您匹配的代码可能根本没有任何意义.没关系.尽你所能.如果您必须组成一个范围名称,请使用 TextMate 范围名称作为起点.例如,代替 string.quoted.double.xxx(其中 xxx 是您匹配的语言的文件扩展名),您可以创建一个名为 string.quoted 的范围名称.三重.xxx.

The syntax definition you are writing doesn't color the text directly. It applies scope names to the text. Then the people who write themes like Monokai and Solarized come along and make files that associate the scope names to colors. You can make up your own scope names, but you should stick to the official TextMate scope names. These scope names may not make any sense at all for the code you're matching. That's ok. Just do the best you can. If you have to make up a scope name, use the TextMate scope names as a starting point. For example, instead of string.quoted.double.xxx (where xxx is the file extension of the language you're matching), you could make up a scope name called string.quoted.triple.xxx.

这里是文件扩展名为 .matt 的组合 matt 语言的语法定义.它只有两条规则:一条用于匹配以竖线分隔的字符串,一条用于匹配具有更复杂分隔符的字符串.

Here's a syntax definition for a made up matt language with file extension .matt. It only has two rules: one for matching a pipe-delimited string, and one for matching a string with more complicated delimiters.

# [PackageDev] target_format: plist, ext: tmLanguage
---
name: Mattlang
scopeName: source.matt
fileTypes: [matt]

patterns:
- include: '#pipe-string'
- include: '#complex-string'

# Rules defined in the repository can reference each other. You can include
# one rule inside another.
repository:
  # This is a rule of the begin-end form. The rule matches a string bounded by
  # pipes, such as |hello there|
  pipe-string:
    # The optional 'name' field lets you apply a single scope to everything,
    # including the begin-end pipes. All the scope names must end with .matt
    name: everything.matt
    # We have to escape the pipe character, because it's a special character in
    # the Oniguruma regex syntax (and most other regex engines).
    begin: \|
    # 'beginCaptures' is required if you want the pipes to be colored differently
    beginCaptures:
      # In regex jargon, the begin pipe is 'captured'. Capture group 0 means the
      # entire match, which in this case is just the pipe.
      '0': {name: entire.begin.match.matt}
    # The optional 'contentName' field lets you apply a scope to all the text
    # between (but not including) the begin-end pipes.
    contentName: stuff.between.the.pipes.matt
    patterns:
    # These rules will only be applied to the text *BETWEEN* the pipes. Sublime
    # will go through the rules from top to bottom and try to match the text, so
    # higher rules have a higher "precedence" and will get matched first.
    # Given the text |hello there|, Sublime will see an 'h' character and move
    # through the rules from top to bottom trying to find a rule that starts
    # with 'h'. The #hell rule will match the 'h' and the rest of the
    # characters. The #hell scope name will be applied to the 'hell' text and 
    # Sublime will resume trying to find the next match at the 'o' character.
    # The 'o' character WILL NOT match #hello. You can think of the matched text
    # as being removed from the stream entirely. The point is: order matters.
    - include: '#hell'
    - include: '#hello'
    - end: \|
    endCaptures:
      '0': {name: entire.end.match.matt}

  # This is the other form of rule you can define. It's extremely simple --
  # just a scope name and a regex pattern to match. Note that these rules will
  # only match text on the same line, unlike begin-end rules, which can cover
  # multiple lines.
  hell:
    name: some.other.scope.matt
    match: hell

  hello:
    name: some.scope.matt
    match: hello

  # This rule matches a string that starts with $!! and ends with !!$,
  # e.g. !!$hello there!!$
  complex-string:
    # I've labeled the capture groups.
    #      |---0---|
    #      |--1-||3|
    begin: (!(!))($)
    #        |2|
    beginCaptures:
      '0': {name: full.match.matt}
      '1': {name: both.exclamation.marks.matt}
      '2': {name: second.exclamation.mark.matt}
      '3': {name: dollar.sign.matt}
    # It's ok to leave out the 'patterns' field. Technically, all you really
    # need is a 'begin' field and an 'end' field.
    end: ((!)!)($)
    endCaptures:
      '0': {name: everything.matt}
      '1': {name: both.exclamation.marks.matt}
      '2': {name: first.exclamation.mark.matt}
      '3': {name: dollar.sign.matt}

这篇关于克隆一个崇高的文本 3 高亮语法定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆