如何为VS Code创建简单的自定义语言着色 [英] How to create a simple custom language colorization to VS Code

查看:145
本文介绍了如何为VS Code创建简单的自定义语言着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为日志文件创建简单的着色,现在可以在Code中包含自定义语言(我在0.9.2上).我创建了一个简单的.tmLanguage文件,用于为字母'q'着色,仅用于启动,但未成功.

I'm trying to create a simple colorization for log files, now that it's possible include custom languages in Code (I'm on 0.9.2). I have created a simple .tmLanguage file for colorizing the letter 'q', just for starting up, but have been unsuccessful.

我的新语言log与文件扩展名正确关联,我也可以从Code内部手动选择它,但不会发生着色.我觉得这与我将模式关联到的范围"有关,但是我不确定.是否有可供选择的有效范围列表?最初,我以为我会使用一些通用的东西,例如"comment"来获得某种颜色,但这似乎不起作用.

My new language, log, is associated correctly with the file extension and I can also select it manually from inside Code, but no coloring takes places. I have a feeling it has to do with what "scope" I associate my pattern with, but I'm not sure. Is there a list of valid scope to choose from? Initially I thought I'd use something general, such as "comment" to get some color, but it doesn't seem to work.

这是我的.tmLanguage文件:

Here's my .tmLanguage file:

<?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>scopeName</key>
        <string>text.log</string>

        <key>fileTypes</key>
        <array>
            <string>log</string>
        </array>

        <key>name</key>
        <string>Log file</string>

        <key>patterns</key>
        <array>
            <dict>
                <key>match</key>
                <string>q</string>
                <key>name</key>
                <string>comment</string>
            </dict>
        </array>
    </dict>
</plist>

我可能在这里误会了一些东西,所以非常感谢您的帮助:-)

I'm probably misunderstanding something here, so any help is very appreciated :-)

推荐答案

您需要使用正则表达式而不是静态字符串来描述模式:

You need to use regular expressions instead of static strings to describe the pattern:

<key>match</key>
<string>q</string>  <- This needs to be a regular expression
<key>name</key>
<string>comment</string>

我为日志文件突出显示提供了一个更有用的示例.它用不同的颜色给数字,提示,警告和错误涂上颜色.识别这些关键字和数字的规则是基于正则表达式的.

I provide a more useful example for a log file highlighter. It colors numbers, hints, warnings and errors in different colors. The rules to identify these keywords and numbers are based on regular expression.

<?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>scopeName</key>
        <string>text.log</string>

        <key>fileTypes</key>
        <array>
            <string>log</string>
        </array>

        <key>name</key>
        <string>Log file</string>

        <key>patterns</key>
        <array>
            <dict>
                <key>match</key>
                <string>\b(?i:(hint|info|information))\b</string>
                <key>name</key>

                <string>info-token</string>
            </dict>                
            <dict>
                <key>match</key>
                <string>\b(?i:(warning|warn))\b</string>
                <key>name</key>
                <string>warn-token</string>
            </dict>
            <dict>
                <key>match</key>
                <string>\b(?i:(Error|Failure|Fail))\b</string>
                <key>name</key>
                <string>error-token</string>
            </dict>
            <dict>
                <key>match</key>
                <string>\b((0(x|X)[0-9a-fA-F]*)|(([0-9]+\.?[0-9]*)|(\.[0-9]+))((e|E)(\+|-)?[0-9]+)?)(L|l|UL|ul|u|U|F|f|ll|LL|ull|ULL)?\b</string>
                <key>name</key>
                <string>constant.numeric</string>
            </dict>                                
        </array>
        <key>uuid</key>
        <string>FF0550E0-3A29-11E3-AA6E-0800200C9A77</string>
    </dict>
</plist>

荧光笔给出这样的结果(使用默认主题):

The highlighter gives a result like this (using the default theme):

我没有找到有关可用令牌的官方文档(例如error-tokenconstant.numeric等).但是%VSCODE_INSTALLATION%\resources\app\out\vs\languages\markdown\common\tokens.css中有一个文件.它似乎列出了所有可用的令牌等.创建.tmLanguage文件时将其用作参考.

I didn't find an official documentation about the available tokens (like error-token, constant.numeric etc). But there is a file located in %VSCODE_INSTALLATION%\resources\app\out\vs\languages\markdown\common\tokens.css. It seems to list all available tokens etc. Use it as a reference when you create the .tmLanguage file.

但是请注意:某些主题仅使用 basic 标记.还有一些其他主题将相同的颜色用于许多不同的标记.因此,您应该经常针对最常见的主题测试荧光笔,以查看结果是否看起来不错.

But pay attention: Some themes are using only the basic tokens. And some other themes are using the same color for many different tokens. So you should test the highlighter frequently against the most common themes to see whether the result looks good or not.

您绝对应该访问有关语言语法的页面,以了解更多信息.

You should definitely visit this page about Language Grammars to learn more.

这篇关于如何为VS Code创建简单的自定义语言着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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