I am creating a custom language extension to colorize syntax for an oddball, little-used language.
In this particular language, keywords and commands are not case sensitive - but the language extension I've created seems to be case sensitive.
Here is an example of a group I called "commands":
"commands": {
"patterns": [
{
"name": "keyword.control.wbt",
"match": "\\b(if|IF|If|iF|then|else|elseif|endif|goto|gosub|exit|return|for|next|while|endwhile|break|drop|errormode|continue|switch|case)\\b"
}
]
},
You'll note that I created entries for every possible capitalization of "if", which allowed IF and If to be colorized in the source code I am using to test.
Is there a way to tell VSCode to ignore capitalization?
Have you tried to use a RegEX mode to make it case insensitive? For your example, try:
"commands": {
"patterns": [
{
"name": "keyword.control.wbt",
"match": "(?i)\\b(if|then|else|elseif|endif|goto|gosub|exit|return|for|next|while|endwhile|break|drop|errormode|continue|switch|case)\\b"
}
]
},
Note that I removed the other variants of if
Related
I am trying to embed c# syntax highlighting in a VSC Extension.
My tmLanguange.json is as follows
{
"$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json",
"name": "CS Asm",
"patterns": [
{
"include":"#csasm"
}
],
"repository": {
"csasm" :{
"patterns": [{
"name" : "meta.embedded.block.bmasm",
"begin": "#{",
"end": "}",
"patterns" : [{
"include" : "source.csharp"
}]
}]
}
},
"scopeName": "source.csasm"
}
However this doesn't produce any highlighting within VSC. If I change source.csharp to something else it does work.
Normal .cs files are highlighted properly, so I assume there is a 'csharp' extension loaded to provide the syntax rules.
Is there something special about C#?
Because Microsoft defines source.cs not source.csharp,
https://github.com/microsoft/vscode/blob/main/extensions/csharp/package.json#L33
I want to select [ ] for syntax highlighting within a markdown file in VS Code.
I can target [ ] using the following regex: (?<=\s)\]|\[(?=\s).
However [ ] doesn't get matched when it is part of a heading.
As a minimal example, in the package.json I have the following:
"contributes": {
"grammars": [
{
"scopeName": "markdown.todo",
"path": "./syntaxes/todo.markdown.json",
"injectTo": ["text.html.markdown"]
}
]
}
Then, in the todo.markdown.json I have:
{
"scopeName": "markdown.todo",
"injectionSelector": "L:text.html.markdown",
"patterns": [
{ "include": "#item-todo" }
],
"repository": {
"item-todo": {
"name": "item.todo",
"match": "(?<=\\s)\\]|\\[(?=\\s)"
}
}
}
Finally, in the VS Code settings.json I apply a color using:
"editor.tokenColorCustomizations": {
"textMateRules": [
{
"scope": "item.todo",
"settings": {
"foreground": "#FF0000"
}
}
]
}
I can see below that [ ] gets selected, but not when it is within a heading.
When I inspect the tokens and scopes I see several textmate scopes.
I am not sure if this is related, but it seems that VS Code is highlighting the markdown headings based on the markup.heading scope. However, markup.heading is not present in the textmate scopes array.
I tried changing to "injectionSelector": "L:heading.1.markdown" and/ or "injectTo": ["heading.1.markdown"], but no matter what selectors I specify I cannot seem to be able to match [ ] within a heading.
I am trying to create a language extension for VS Code. The comments in this language are single-line comments and start with a semicolon, like this
command ;comment
for this, I put the following into the repository section of my tmLanguage.json:
"comments": {
"name": "comment.lang",
"begin": ";",
"end": "\n"
}
and include it in the patterns section
{
"include": "#comments"
}
this works, so far. Now on top of that, the language also features special blocks, which start with ";!" and with ";;" respectively. Those I want to be treated differently:
"magicString": {
"name": "magicString.lang",
"begin": ";!",
"end": "\n"
},
"commentHeader": {
"name": "commentHeader.lang",
"begin": ";;",
"end": "\n"
},
Again, I include them in the patterns section
{
"include": "#magicString"
},
{
"include": "#commentHeader"
}
Now the obvious problem is that those two start exactly like a comment. As a consequence, they seem to be recognized and treated as comments. The scope inspector confirms that the tokens are indeed handled as "comment.lang".
How can I get around this? Is there a way to prioritize one rule above another? I looked up the topic in the TextMate documentation, but I don't get it. I tried specifying in the begin regex the number of semicolon repetitions -- I thought this should work but it does not.
"magicString": {
"name": "magicString.lang",
"begin": ";!",
"end": "\n"
},
"commentHeader": {
"name": "commentHeader.lang",
"begin": ";{2}",
"end": "\n"
},
"comments": {
"name": "comment.lang",
"begin": ";{1}",
"end": "\n"
}
A simple solution in your case would be to make sure the three begin regular expressions are mutually exclusive. For example, you could change your repository patterns as follows:
"magicString": {
"name": "comment.magic.lang",
"begin": ";!",
"end": "\n"
},
"commentHeader": {
"name": "comment.header.lang",
"begin": ";;",
"end": "\n"
},
"comments": {
"name": "comment.lang",
"begin": ";[^;!]",
"end": "\n"
}
Note the regex for comments: ;[^;!] means "a ; character that is not followed by a ; or ! character".
(The change in scope names in the above snippet is not directly related to your question. It is just my impression of what would be considered better practice, although I caution that I am a complete beginner on TextMate.)
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.
I was able to successfully add basic language support for ColdFusion in visual studio code and for the most part it seems to work properly.
The only issue is that the syntax highlighting does not work at all, specifically the colorization. Otherwise the file extension association works and bracket highlight works and even the commenting rules work.
I used the yo code generator to create the new language.
I got the .tmLanguage file from here:
https://github.com/textmate/coldfusion.tmbundle
I feel like there must be a step I'm missing in order to get the new language to work with the theme being used.
Thanks in advance!
EDIT: added package.json
{
"name": "cf",
"displayName": "ColdFusion",
"description": "ColdFusion VSCode Support",
"version": "0.0.1",
"publisher": "epetti",
"engines": {
"vscode": "^0.10.1"
},
"categories": [
"Languages"
],
"contributes": {
"languages": [{
"id": "coldfusion",
"aliases": ["ColdFusion", "coldfusion"],
"extensions": [".cfm",".cfml",".cfc"],
"configuration": "./coldfusion.configuration.json"
}],
"grammars": [{
"language": "coldfusion",
"scopeName": "text.html.cfm",
"path": "./syntaxes/coldfusion.tmLanguage"
}]
}
}