how to enable bilingual language syntax highlighting for vscode using textmate grammar - Stack Overflow

We are working on a vscode language extension which is bilingual language. Let’s say - primary language

We are working on a vscode language extension which is bilingual language. Let’s say - primary language is A and Secondary Language is B. and using text mate we are trying to enable syntax highlighting for both language.

file.A

<start of file>
    // some code with syntax highlighting of language A

    // following statement is language A syntax which start with <Block> keyword
    Block parameters; 


    // following is the snippet which start with <Block> keyword and end with <@endBlock>
    Block parameters; 
        // some code with syntax highlighting of secondary language B
    @endblock;

    // some code with syntax highlighting of language A
<end of file>

Following is TM grammar to match a code snippet which start with “block” and ends with “@endblock” .

"patterns": [
        {
          "name" : "meta.block,
          "begin": “(?i)(block)\\W.*$",
          "end": "(?i)^\\s*(@endblock)\\W”,
        }

Now problem is - textmate match with start pattern of “block” statement, but keep checking for “@endBlock”, if it(@endBlock) is not available, then textmate continue to checks same till end of file, because of this - syntax of language A highlighted like language B.

Scenario -1

<Block>
.
. This should not be secondary language B syntax highlighting 
.
<Block>
.
.  This is correct secondary language B syntax highlighting 
.
<@endblock>

Scenario - 2

<Block>
.
. following code should not be secondary language B syntax highlighting, it must be primary language A.
.
.
.
.
<end of file>

Can’t we prevent textmate to match both begin and end together then only it should highlight particular grammar, else ignore? Or is there any other way to solve this issue?

We are working on a vscode language extension which is bilingual language. Let’s say - primary language is A and Secondary Language is B. and using text mate we are trying to enable syntax highlighting for both language.

file.A

<start of file>
    // some code with syntax highlighting of language A

    // following statement is language A syntax which start with <Block> keyword
    Block parameters; 


    // following is the snippet which start with <Block> keyword and end with <@endBlock>
    Block parameters; 
        // some code with syntax highlighting of secondary language B
    @endblock;

    // some code with syntax highlighting of language A
<end of file>

Following is TM grammar to match a code snippet which start with “block” and ends with “@endblock” .

"patterns": [
        {
          "name" : "meta.block,
          "begin": “(?i)(block)\\W.*$",
          "end": "(?i)^\\s*(@endblock)\\W”,
        }

Now problem is - textmate match with start pattern of “block” statement, but keep checking for “@endBlock”, if it(@endBlock) is not available, then textmate continue to checks same till end of file, because of this - syntax of language A highlighted like language B.

Scenario -1

<Block>
.
. This should not be secondary language B syntax highlighting 
.
<Block>
.
.  This is correct secondary language B syntax highlighting 
.
<@endblock>

Scenario - 2

<Block>
.
. following code should not be secondary language B syntax highlighting, it must be primary language A.
.
.
.
.
<end of file>

Can’t we prevent textmate to match both begin and end together then only it should highlight particular grammar, else ignore? Or is there any other way to solve this issue?

Share Improve this question asked Mar 13 at 12:12 jitenjiten 5,2213 gold badges14 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

sadly it is impossible

TextMate is a top down parser
parsing line by line
eg. you cannot lookahead multiple lines to see if something exists or not

the best you could do is have it stop Language B when Block is found

"end": "(?i)^\\s*(@endblock)\\b|(?=block\\b.*$)”

(also I think you want \\b word-boundary instead of \\W non-word-character)

OR
if you're able to add a comment after Block that states that the next section should be Language B

Block // Language B

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744701417a4588800.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信