TextMate 语法——规则的优先级 [英] TextMate Grammar -- precedence of rules

查看:30
本文介绍了TextMate 语法——规则的优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改 CSharp 语言的语法突出显示,因此我将在 C# 字符串中获得 SQL 的语法突出显示.TextMate 支持嵌入语言,所以这似乎是可能的.

我基于

否则,我的规则会被 csharp 规则覆盖,例如

  • punctuation.definition.comment.cs
  • string.quoted.double.cs
  • comment.block

问题是,我的规则适用于多种语言元素,而 csharp 定义在针对这些元素时胜出.

元素标记的依据是什么?如何编写我的规则,以便在其他语言规则之前赢得并标记该语法?有没有计算规则权重的算法?

<小时>

解决方案

如果你不能在 csharp 中劫持注释语法,让我们在 SQL 中使用注释.我创建了一个由 -- SQL 注释启用的规则,并将其应用于逐字字符串.现在它可以工作了,但样式有时会与字符串混合.需要一些额外的改进,但看起来很有希望.

证明有效的规则是这样的

 "embeded-sql": {"contentName": "source.sql","begin": "--\\s*SQL","end": "(?:\\b|^)(?=\"\\s*;)",模式":[{"include": "source.sql"}]},

现在我想

I'm trying modify syntax highlighting for CSharp language, so I will get syntax highlighting for SQL in C# string. TextMate has support for embeded languages, so this seems possible.

I build on csharp.tmLanguage.json and I would like to be able to enable embeded SQL with special comment before string like

string query = /*SQL*/ $@"SELECT something FROM ..."

Thanks to TextMate's Language Grammars and Introduction to scopes I came up with this JSON rule

"repository": {
    "embeded-sql": {
        "contentName": "source.sql",            
        "begin": "/\\*\\s*SQL\\s*\\*/\\s*\\$?@?\"",
        "end": "\";",
        "patterns": [
            {
                "include": "source.sql"
            }
        ]
    },
    ...
}

And thanks to VSCode's Themes, Snippets and Colorizers and Running and Debugging Your Extension I was able to test, that this rule works.

But I have one problem, which I'm unable to solve.

My grammar rule works only if signifficant portion of csharp rules are disabled, If I disable all #declarations and #script-top-level, embeded SQL works:

Otherwise, my rule is overridden by csharp rules like

  • punctuation.definition.comment.cs
  • string.quoted.double.cs
  • comment.block
  • etc.

The problem is, that my rule works on several language elements and the csharp definition wins on targeting these elements.

On which basis are the elements tagged? How to write my rule, so it will win and tag that syntax before other language rules? Is there any algorithm for calculating weight of rules?


Solution

If you cannot hijack comment syntax in csharp, lets us work with comment in SQL. I made a rule enabled by -- SQL comment and I applied this to verbatim string. Now it works but the styles are sometimes mixed with string. Needs some additional improvements, but looks promising.

The rule that proves to work goes like this

    "embeded-sql": {
        "contentName": "source.sql",
        "begin": "--\\s*SQL",
        "end": "(?:\\b|^)(?=\"\\s*;)",
        "patterns": [
            {
                "include": "source.sql"
            }
        ]
    },

Now I would like to enable Intellisense and error checking in such embedded language.

解决方案

The rules in the patterns list are matched in order.

Your rule appears like a specialisation of comment, so you can put it just before the comment.block.cs

    "comment": {
        "patterns": [
            {
                "contentName": "source.sql",
                "begin": "(/\\*\\s*SQL\\s*\\*/)\\s*\\$?@?\"",
                "beginCaptures": {
                    "1": {
                        "patterns": [
                            {
                                "include": "#comment"
                            }
                        ]
                    }
                },
                "end": "\";",
                "patterns": [
                    {
                        "include": "source.sql"
                    }
                ]
            },
            {
                "name": "comment.block.cs",
                "begin": "/\\*",
                "beginCaptures": {
                    "0": {
                        "name": "punctuation.definition.comment.cs"
                    }
                },
                "end": "\\*/",
                "endCaptures": {
                    "0": {
                        "name": "punctuation.definition.comment.cs"
                    }
                }
            },
            ...

The snapshot is done on a language my which is just a copy of c# json plus your sql embedding.

这篇关于TextMate 语法——规则的优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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