I'm trying to implement support for a new language in VS Code, using a tmGrammar json file. In this language variables start with !, and there is a special reserved variable called !this in this language.
I want to apply one class to all common variables and other for the !this variable. I'm doing this in my json file:
"repository": {
"keywords": {
"patterns": [
{
"name": "constant.character",
"match": "(?i)\\b(!this)\\b"
},
{
"name": "variable.other",
"match": "(?i)\\![a-z0-9]+"
},
}
}
But this is coloring both !this and !someVar to the same color.
If I change the first rule to (?i)\\b(this)\\b, without the ! the word this get colored correctly.
I also tried do change the order of the rules, but now matter what I do makes !this the same color as common variables.
Is there a problem with the first regular expression?
Related
Can anyone clarify the exact algorithm used for the VS code multi-line problem matcher? Specifically curious what the behavior is when there are more than 2 patterns defined.
The context is that I'm trying to write a multi-line problem matcher in VS Code for a rollup task. A sample of the output would look like:
(!) Plugin typescript: #rollup/plugin-typescript TS2345: Argument of type 'FirstType' is not assignable to parameter of type 'SecondType'.
Types of property 'PropertyOne' are incompatible.
Type 'FirstType' is missing the following properties from type 'PropertyOne': X, Y
src/main/pathToFile/file.ts: (174:37)
I've tried a number of options. Here's the most recent attempt:
"pattern": [
{
"regexp":"^[(][!][)](.*)$",
"message": 1
},
{
"regexp":"^\\s+(.*)$",
},
{
"regexp": "^([^:\\s]+):\\s+\\((\\d+):(\\d+)\\)$",
"file": 1,
"line": 2,
"column":3
}],
I would expect this to match the output described, but it doesn't match at all.
If I remove the first pattern, I'm able to get a match, but this doesn't give me all of the information I need.
In Visual Studio Code, in the "settings.json" file, I am editing a color theme, and need a way to describe the property for triple quoted strings seen in functions as comments:
"editor.tokenColorCustomizations": {
"[Starfall Palenight]": {
"comments": "#82bdd1",
"strings": "#75ec9d"
}
},
The attempt to use "comments" fails, as this property only applies to real comments, and "strings" only affects normal strings.
An example of what my desired comment would look like is shown below:
def func():
'''this is the comment explaining how the function works'''
pass
Thank you
I'm trying to make a custom syntax highlighter for my own markup language. All the examples are complicated, missing steps and are very, very hard to understand.
Is there anything that fully documents how to make a syntax highlighter?
(for VSCode, by the way)
For example, this video https://www.youtube.com/watch?v=5msZv-nKebI which has an extremely large skip in the middle and doesn't really explain much.
My current code, made with Yeoman generator is:
{
"$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json",
"name": "BetterMarkupLanguage",
"patterns": [
{
"include": "#keywords"
},
{
"include": "#strings"
}
],
"repository": {
"keywords": {
"patterns": [{
"name": "entity.other.bml",
"match": "\\b({|}|\\\\|//)\\b"
}]
},
"strings": {
"name": "string.quoted.double.bml",
"begin": "`",
"end": "`"
}
},
"scopeName": "source.bml"
}
Synopsis
I'm not sure at what level you're approaching the problem, but there are basically two kinds of syntax-highlighting:
Just identify little nuggets of canonically identifiable tokens (strings, numbers, maybe operators, reserved words, comments) and highlight those, OR
Do the former, and also add in context awareness.
tmLanguage engines basically have two jobs:
Assign scopes.
Maintain a stack of contexts.
Samples
Lets say you make a definition for integers with this pattern:
"integers": {
"patterns": [{
"name": "constant.numeric.integer.bml",
"match": "[+-]\\d+"
}]
},
When the engine matches an integer like that, it will match to the end of the regex, assign the scope from "name", and then continue matching things in this same context.
Compare that to your "strings" definition:
"strings": {
"name": "string.quoted.double.bml", // should be string.quoted.backtick.bml
"begin": "`",
"end": "`"
},
Those "begin" and "end" markers denote a change in the tmLanguage stack. You have pushed into a new context inside of a string.
Right now, there are no matches configured in this context, but you could do that by adding a "patterns" key with some "match"es or "include"s. "include"s are other sets of matches like "integers" that you've defined elsewhere. You can add it to the "strings" patterns to match integers inside strings. Matching integers might be silly, but think about escaped backticks: You want to scope those and stay in the same context within "strings". You don't want those popping back out prematurely.
Order of operations
You'll eventually notice that the first pattern encountered is matched. Remember the integers set? What happens when you have 45.125? It will decide to match the 45 and the 125 as integers and ignore the . entirely. If you have a "floats" pattern, you want to include that before your naïve integer pattern. Both these "numbers" definitions below are equivalent, but one lets you re-use floats and integers independently (if that's useful for your language):
"numbers": {
"patterns": [
{"include": "#floats"},
{"include": "#integers"}
]
},
"integers": {
"patterns": [{
"name": "constant.numeric.integer.bml",
"match": "[+-]\\d+"
}]
},
"floats": {
"patterns": [{
"name": "constant.numeric.float.bml",
"match": "[+-]\\d+\\.\\d*"
}]
},
"numbers": {
"patterns": [{
"name": "constant.numeric.float.bml",
"match": "[+-]\\d+\\.\\d*"
}, {
"name": "constant.numeric.integer.bml",
"match": "[+-]\\d+"
}]
},
Doing it right
The "numbers"/"integers"/"floats" thing was trivial, but well-designed syntax definitions will define utility groups that "include" equivalent things together for re-usability:
A normal programming language will have things like
A "statements" group of all things that can be directly executed. This then may or may not (language-dependent) include...
An "expressions" group of things you can put on the right-hand-side of an assignment, which will definitely include...
An "atoms" group of strings, numbers, chars, etc. that might also be valid statements, but that also depends on your language.
"function-definitions" probably won't be in "expressions" (unless they are lambdas) but probably would be in "statements." Function definitions might push into a context that lets you return and so on.
A markup language like yours might have
An "inline" group to keep track of all the markup one can have within a block.
A "block" group to hold lists, quotes, paragraphs, headers.
...
Though there is more you could learn (capture groups, injections, scope conventions, etc.), this is hopefully a practical overview for getting started.
Conclusion
When you write your syntax highlighting, think to yourself: Does matching this token put me in a place where things like it can be matched again? Or does it put me in a different place where different things (more or fewer) ought to be matched? If the latter, what returns me to the original set of matches?
I have written a VSCode snippet that makes two substitutions to the clipboard content.
For instance, this snippet changes "a" characters into "x" and "e" characters into "y"
"t2": {
"prefix": "t2",
"body": [
"${CLIPBOARD/(a)|(e)/${1:?x:y}/g}"
],
"description": "Makes two substitutions to the clipboard"
},
Using this snippet, if I cut the text "This snippet changes the clipboard" and I execute the snippet, the pasted text is "This snippyt chxngys thy clipboxrd".
My question is: Is it possible to create a snippet that applies three substitutions? For instance, "a" into "x", "e" into "y" and "i" into "z", obtaining "Thzs snzppyt chxngys thy clzpboxrd" in the example above.
Yes, you can change as many as you want. I made this snippet for another answer:
"color conditional": {
"prefix": "_hex",
"body": [
"let color = '${1};",
"let hex = '${1/(white)|(black)|(red)/${1:+#fff}${2:+#000}${3:+#f00}/}';" //works
],
"description": "conditional color"
},
See vscode if/else conditions in user defined snippet.
for one example but in your case try:
"${CLIPBOARD/(a)|(e)|(i)/${1:+x}${2:+y}${3:+z}/g}"
instead of using the if/else conditional, you can use any number of if's.
When trying to access the serviceM8 API to search for customers I am using the following API request. The endpoint works but I am not sure how to handle special chars like ' or &.
API Documentation: https://developer.servicem8.com/docs/filtering
So if I call the following:
https://api.servicem8.com/api_1.0/company.json?%24filter=name%20eq%20"BENTON%"
Then I get following result (3 results including the ones with a special char.
[
{
"uuid": "60791e9a-8c5a-4e2a-aca5-f9625bef7d8b",
"edit_date": "2018-06-20 15:13:53",
"name": "BENTON'S TEST",
},
{
"uuid": "f722b374-e330-42f1-a09e-333b375af1ab",
"edit_date": "2018-07-05 09:46:30",
"name": "Benton's test",
},
{
"uuid": "df01f8ce-a1c9-438b-8954-297b113689ab",
"edit_date": "2018-07-05 17:02:43",
"name": "BENTONS TEST",
}
]
What I would like to try to filter for is for BENTON'S TEST so I try the following but that does not work.
https://api.servicem8.com/api_1.0/company.json?%24filter=name%20eq%20"BENTON'S%"
{
"uuid": "df01f8ce-a1c9-438b-8954-297b113689ab",
"edit_date": "2018-07-05 17:02:43",
"name": "BENTONS TEST",
}
My question is now, how can I filter in this API so that I can search for these special characters? like BENTON'S. If I use %27 to replace the ' sign that does not change the result as it seems the % sign in a filter is used as search parameter?
UPDATE
Reading this: https://community.dynamics.com/crm/b/mscrmshop/archive/2015/12/21/crm-odata-rest-queries-and-special-characters
I assume that the %27 is just not recognized and I should use two single quotes but that does not work either. If I search in the front end application, searching for ' does not work but when I use \' I can search for the correct names. But using that in my query string does not work.
Please use &$filter=substringof("BENTON'S",name) instead of &$filter=name eq "BENTON'S". This would return all names that contain Benton's.
Otherwise you can also use &$filter=startswith("BENTON'S",name)