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 badges1 Answer
Reset to default 0sadly 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条)