I've been trying to write my own syntax highlighting extension for the rules we use in a document composition tool.
I've been able to get most of it working okay but I just can't get the comments to be recognised by the scope inspector.
I removed all the other working code from the tmLanguage file to rule out conflicts and am left with the following
ote-rules.tmLanguage.json
{
"scopeName": "source.ote-rules",
"patterns": [{
"include": "#expression"
}],
"repository": {
"expression": {
"patterns": [
{
"include": "#comments-ote"
}
]
},
"comments-ote": {
"patterns": [{
"include": "#comments-block"
},
{
"include": "#comments-line"
}
],
"comments-line": {
"match": "\\/\\/.*?$",
"name": "comment.line.double-slash.ote-rules"
},
"comments-block": {
"begin": "\\/\\*",
"end": "\\*\\/",
"beginCaptures":{
"0":{"name":"punctuation.definition.comment.ote-rules"}
},
"endCaptures":{
"0":{"name":"punctuation.definition.comment.ote-rules"}
}
}
}
}
}
The file I'm trying to match on is a plain text file (*.txt
extension) and comments are either line comments with a double forward slash or block comments which begin and end with /*
and */
respectively
test text file.txt
// just some comment text
// indented comment
//
// left the above line empty apart from slashes
/*
inline block comment
*/
When I look at the text above with the scope inspector it's recoginising that it's from source.ote-rules but is showing the token type as 'other'
I've checked the regex's on rubular.com and they seem to work on the samples I've shown so what have I missed?
The problem is that patterns included with #
references have to be directly in the "repository"
, but you nested them into "comments-ote"
. It simply doesn't find them.
Also, you probably want to give your "comments-block"
a scope name so it's highlighted as a comment, such as "name": "comment.block.ote-rules"
.
{
"scopeName": "source.ote-rules",
"patterns": [
{
"include": "#expression"
}
],
"repository": {
"expression": {
"patterns": [
{
"include": "#comments-ote"
}
]
},
"comments-ote": {
"patterns": [
{
"include": "#comments-block"
},
{
"include": "#comments-line"
}
]
},
"comments-line": {
"match": "\\/\\/.*?$",
"name": "comment.line.double-slash.ote-rules"
},
"comments-block": {
"begin": "\\/\\*",
"end": "\\*\\/",
"beginCaptures": {
"0": {
"name": "punctuation.definition.comment.ote-rules"
}
},
"endCaptures": {
"0": {
"name": "punctuation.definition.comment.ote-rules"
}
},
"name": "comment.block.ote-rules"
}
}
}
Note: you don't need to use "include"
, you can also specify the patterns directly within "patterns"
:
"comments-ote": {
"patterns": [
{
"match": "\\/\\/.*?$",
"name": "comment.line.double-slash.ote-rules"
},
{
"begin": "\\/\\*",
"end": "\\*\\/",
"beginCaptures": {
"0": {
"name": "punctuation.definition.comment.ote-rules"
}
},
"endCaptures": {
"0": {
"name": "punctuation.definition.comment.ote-rules"
}
},
"name": "comment.block.ote-rules"
}
]
}