在 Sublime Text 中设置我自己的语法 [英] Set my own syntax in Sublime Text

查看:52
本文介绍了在 Sublime Text 中设置我自己的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我使用 Sublime Text 来写提醒.我总是为这个任务使用相同的布局,如下所示:

Sometimes I use Sublime Text for writing reminders. I always use the same layout for this task which looks like this :

>Title
>>Subtitle
>>>Comment

> Title
>> ...

其中 > 代表制表符

所以我想知道是否可以为这种类型的文件创建自己的语法突出显示,标题使用一种颜色,副标题使用另一种颜色,评论使用常规颜色.

So I'm wondering if it's possible to create my own syntax highlighting for this kinds of files, with one color for the title, another one for the subtitle and the regular color for the comment.

推荐答案

语法高亮使用 .tmLanguage 语法定义.它们被格式化为 Apple 的基于 XML 的 PLIST 格式,尽管感谢优秀的 Sublime 插件 PackageDev 它们可以用 JSON 或 YAML 编写,我选择它是因为它的紧凑性,而且我使用主题* 使用它突出显示了甜蜜的语法.

Syntax highlighting is performed using .tmLanguage syntax definitions. They are formatted in Apple's XML-based PLIST format, although thanks to the excellent Sublime plugin PackageDev they can be written in JSON or in YAML, which I chose for its compactness, and the fact that I get sweet syntax highlighting with it using my theme*.

所以,您的语法非常简单.您将拥有三个规则:

So, your syntax is very straightforward. You'll have three rules:

  1. 以单个选项卡开头的匹配行作为标题"行
  2. 以两个选项卡开头的匹配行作为副标题"行
  3. 以三个选项卡开头的匹配行作为注释"行.

其他所有内容都将被 Sublime 显示为纯文本.

Everything else will be displayed by Sublime as plain text.

这里是,在 YAML 中:

Here it is, in YAML:

# [PackageDev] target_format: plist, ext: tmLanguage
---
name: Reminders
comment: Written for http://stackoverflow.com/q/25689365/1426065 by @MattDMo
scopeName: text.reminders
fileTypes: [todo]
uuid: 6B548E74-5E01-497A-B030-9D31131B7A70

patterns:
- name: text.title.reminders
  match: ^\t(?!\t+)(.*)

- name: text.subtitle.reminders
  match: ^\t\t(?!\t+)(.*)

- name: text.comment.reminders
  match: ^\t\t\t(.*)

一切都很简单.名字是 Sublime 右下角显示的,是我写的,它的基本作用域名称是 text.reminders,打开一个带有 .todo 的文件扩展将自动应用此语法,并且 UUID 只是一个唯一标识符.正如我上面提到的,有三种模式.要记住的一件事:这只会匹配以文字制表符开头的行,而不是作为制表符插入的空格字符!这意味着您需要选择查看 ->缩进并确保Indent Using SpacesNOT被选中.只是为了更好的衡量,选择 View ->缩进 ->也将缩进转换为制表符.这些设置只能应用于提醒"视图,我稍后会介绍.

Everything is pretty straightforward. The name is what shows up in the bottom right corner of Sublime, it was written by me, its base scope name is text.reminders, opening a file with the .todo extension will automatically apply this syntax, and the UUID is just a unique identifier. As I mentioned above, there are three patterns. One thing to keep in mind: this will only match if the line begins with a literal tab character, not space characters being inserted as a tab! That means you'll need to select View -> Indentation and make sure Indent Using Spaces is NOT checked. Just for good measure, select View -> Indentation -> Convert Indentation to Tabs as well. These settings can be applied just to "Reminders" views, I'll cover that later.

所以我们有我们的 YAML,如果你没有 PackageDev,它就没用了.但是,转换为 PLIST 效果会更好:

So we have our YAML, which is useless if you don't have PackageDev. However, translated to a PLIST it works much better:

<?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>comment</key>
    <string>Written for http://stackoverflow.com/q/25689365/1426065 by @MattDMo</string>
    <key>fileTypes</key>
    <array>
        <string>todo</string>
    </array>
    <key>name</key>
    <string>Reminders</string>
    <key>patterns</key>
    <array>
        <dict>
            <key>match</key>
            <string>^\t(?!\t+)(.*)</string>
            <key>name</key>
            <string>text.title.reminders</string>
        </dict>
        <dict>
            <key>match</key>
            <string>^\t\t(?!\t+)(.*)</string>
            <key>name</key>
            <string>text.subtitle.reminders</string>
        </dict>
        <dict>
            <key>match</key>
            <string>^\t\t\t(.*)</string>
            <key>name</key>
            <string>text.comment.reminders</string>
        </dict>
    </array>
    <key>scopeName</key>
    <string>text.reminders</string>
    <key>uuid</key>
    <string>6B548E74-5E01-497A-B030-9D31131B7A70</string>
</dict>
</plist>

在 Sublime 中,使用 XML 语法创建一个新文件,并将上述 XML 复制到其中.通过选择 Preferences -> 找到您的 Packages 目录;浏览包... 然后将此新文件保存为 Packages/User/Reminders.tmLanguage(确保 tmLanguage 中的 L 大写).Sublime 右下角的语言列表中现在应该有一个Users -> Reminders"选项,或者通过 View ->语法菜单选项.

In Sublime, create a new file with XML syntax and copy the above XML into it. Find your Packages directory by selecting Preferences -> Browse Packages... then save this new file as Packages/User/Reminders.tmLanguage (make sure the L in tmLanguage is capitalized). There should now be a "Users -> Reminders" option in the language list in the bottom right of Sublime, or through the View -> Syntax menu option.

但是,还有一件事要做 - 着色.为此,您需要修改 .tmTheme 配色方案文件.由于您使用的是 Sublime Text 2(我假设),因此非常简单.打开 Preferences ->Settings-User 并检查 "color_scheme" 的值.通过选择 File -> 打开配色方案文件;打开...,导航到您之前使用 Preferences -> 找到的 Packages 目录;浏览包...,然后在它所在的任何子目录中打开文件.例如,如果您的设置文件显示 "color_scheme": "Packages/Color Scheme - Default/Monokai.tmTheme" 您将导航到打开文件对话框中的 Packages/Color Scheme - Default 目录并打开 Monokai.tmTheme.超级简单.

However, there's one more thing to do - get coloring. To do this you'll need to modify your .tmTheme color scheme file. Since you're using Sublime Text 2 (I assume), it's pretty easy. Open Preferences -> Settings-User and check the value of "color_scheme". Open the color scheme file by selecting File -> Open..., navigating to the Packages directory you found earlier with Preferences -> Browse Packages..., and opening the file in whatever subdirectory it's in. For example, if your settings file says "color_scheme": "Packages/Color Scheme - Default/Monokai.tmTheme" you'll navigate to the Packages/Color Scheme - Default directory in the open file dialog and open Monokai.tmTheme. Super easy.

既然您已打开配色方案文件,您可以根据需要将语法设置为 XML,然后一直向下滚动到底部.您需要插入新颜色(我稍后会讲到)上方表示:

Now that you have your color scheme file open, you can set the syntax to XML if you want, then scroll all the way down to the bottom. You'll want to insert your new colors (I'll get to that in a minute) above the lines that say:

    </array>
    <key>uuid</key>
    <string>06CD1FB2-A00A-4F8C-97B2-60E131912345</string>
</dict>
</plist>

UUID 甚至可能不存在,它可能只是说:

The UUID may not even be there, it might just say:

    </array>
</dict>
</plist>

无论如何,无论哪一行都应该是文件中的最后几行,否则事情就会中断.无论如何,在这些行上方插入以下指令:

Regardless, whichever is there should always be the last lines in the file, or things will break. At any rate, above these lines, insert the following dicts:

    <dict>
        <key>name</key>
        <string>Reminders - Title</string>
        <key>scope</key>
        <string>text.reminders text.title.reminders</string>
        <key>settings</key>
        <dict>
            <key>fontStyle</key>
            <string>bold italic</string>
            <key>foreground</key>
            <string>#00FF00</string>
            <key>background</key>
            <string></string>
        </dict>
    </dict>
    <dict>
        <key>name</key>
        <string>Reminders - Subtitle</string>
        <key>scope</key>
        <string>text.reminders text.subtitle.reminders</string>
        <key>settings</key>
        <dict>
            <key>fontStyle</key>
            <string></string>
            <key>foreground</key>
            <string>#FF0080</string>
            <key>background</key>
            <string></string>
        </dict>
    </dict>
    <dict>
        <key>name</key>
        <string>Reminders - Comment</string>
        <key>scope</key>
        <string>text.reminders text.comment.reminders</string>
        <key>settings</key>
        <dict>
            <key>fontStyle</key>
            <string></string>
            <key>foreground</key>
            <string></string>
            <key>background</key>
            <string></string>
        </dict>
    </dict>

随意自定义 foregroundbackground 颜色和 fontStyle 属性(bold"和"斜体"是唯一的工作值),无论您喜欢什么.

Feel free to customize the foreground and background colors and the fontStyle attributes ("bold" and "italic" are the only working values) however you like.

还有一件事,如果您还记得的话 - 将 Sublime 设置为仅在 Reminders 视图中使用选项卡.使用 JSON 语法创建一个新文件,或者如果您安装了 PackageDev,则使用Sublime Settings"语法.添加以下内容:

One more thing, if you'll remember - setting Sublime to use tabs just in Reminders views. Create a new file using JSON syntax, or if you installed PackageDev, with "Sublime Settings" syntax. Add to it the following:

{
    "translate_tabs_to_spaces": false,
    "extensions": 
    [
        "todo"
    ]
}

(是的,我知道todo"已经在语言定义中,我只是想安全一点).您可以在此处添加 Preferences -> 中使用的任何其他选项.设置-默认... ->Settings-User 文件,只需确保该文件是有效的 JSON.将文件另存为 Packages/User/Reminders.sublime-settings.

(Yes, I know "todo" is already in the language definition, I just like to be safe). You can add any other option here that's used in the Preferences -> Settings-Default and ... -> Settings-User files, just make sure the file is valid JSON. Save the file as Packages/User/Reminders.sublime-settings.

就是这样!您可能需要重新启动 Sublime 才能使更改生效,但一切就绪.总结一下,创建新语法定义的步骤如下:

And that's it! You may need to restart Sublime for the changes to kick in, but then you're all set. To recap, the steps to making a new syntax definition are as follows:

  1. 获取PackageDev,你的生活会轻松很多.
  2. 使用 Oniguruma 正则表达式以 JSON 或 YAML 格式编写突出显示的正则表达式语言,使用 Rubular 等工具在线测试(使用 Ruby 1.9.2 选项).
  3. 编译为 PLIST/XML.
  4. 修改新范围的配色方案.
  5. 为特定于语法的设置(如选项卡)创建一个 .sublime-settings 文件.
  6. ???
  7. 利润!
  1. Get PackageDev, your life will be a lot easier.
  2. Write highlighting regexes in JSON or YAML format using the Oniguruma regex language, testing online with tools like Rubular (using Ruby 1.9.2 option).
  3. Compile to PLIST/XML.
  4. Modify color scheme for new scopes.
  5. Create a .sublime-settings file for syntax-specific settings like tabs.
  6. ???
  7. Profit!

<小时>

* 如果您有兴趣,请在我的用户资料中链接...

这篇关于在 Sublime Text 中设置我自己的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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