克隆一个突出显示语法定义的sublime文本3

时间:2014-08-07 14:03:36

标签: sublimetext sublimetext3

是否有一个简单的过程来从现有的自定义sublime文本3突出显示定义?

我按照有序且略显乏味tutorial for creating a new highlighting syntax definition的建议安装了AAAPackageDev,但我发现克隆一个会让我走得更快。至少,我想学习如何浏览sublime附带的默认语法定义。

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

(Ubuntu 14.04)

谢谢!

2 个答案:

答案 0 :(得分:20)

更新2015-05-10 :Sublime Text 3 build 3084引入了一种全新的sublime-syntax格式,用于编写语法定义。它比Sublime继承自TextMate的旧系统要好得多。新系统应尽快登陆公众ST3测试版。由于ST3是recommended version of Sublime,我建议使用new system而不是下面描述的系统来编写任何新的荧光笔。


这是Sublime Text语法高亮显示的速成课程。

设置

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

plist是Sublime理解的原生格式,但这并不意味着你应该这样写。我强烈建议您在YAML中编写荧光笔并将其转换为PackageDev的plist。不要用JSON写它。 JSON不支持原始字符串。所有正则表达式都必须进行双重转义。这绝对是一场噩梦。只需使用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将加载并应用它(您可能必须重新启动)。

Sublime语法高亮显示如何工作

您正在编写的语法定义不会直接为文本着色。它将范围名称应用于文本。然后,编写像Monokai和Solarized这样的主题的人会出现并创建将范围名称与颜色相关联的文件。您可以编写自己的范围名称,但应该坚持the official TextMate scope names。对于您匹配的代码,这些范围名称可能毫无意义。没关系。尽你所能。如果必须组成范围名称,请使用TextMate范围名称作为起点。例如,代替string.quoted.double.xxx(其中xxx是您匹配的语言的文件扩展名),您可以组成名为string.quoted.triple.xxx的范围名称。

代码示例

这是一个带有文件扩展名.matt的哑光语言的语法定义。它只有两个规则:一个用于匹配管道分隔的字符串,另一个用于匹配字符串和更复杂的分隔符。

# [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}

答案 1 :(得分:6)

如果您安装PackageResourceViewer,则可以打开任何内置的压缩​​资源。 Do PackageResourceViewer:打开资源,然后选择您熟悉的语言。然后选择扩展名为.tmLanguage的文件。