Escape $ in snippet - visual-studio-code

I'm trying to escape the $ character in my snippets but I cannot seem to get it correct.
If I use \$ as it looks like I should be from the documentation, I get the error:
file: path/to/snippets/php.json'
severity: 'Error'
message: 'Invalid escape character in string'
And no $ appear in my snippet.
If I use $$ found from this answer, I get a $ to show up but it thinks that the text immediately following is a tabstop.
If I use $\ it works but I have to have a character that is part of an escape sequence immediately following. So if I wanted $factory, I would need to do $\ffactory. \f seems to be the best as it does not effect the layout of my snippet.
I'm pretty sure that I am missing what needs to be done here.
My snippet for reference:
"factory" :{
"prefix": "factory",
"body": [
"\$factory->define($1, function (Faker\\Generator \$faker){",
"\treturn [",
"\t\t$2,",
"\t];",
"}"
],
"description": "Creates Model factory"
},

After much trial and error, I found that using \\$ will give me the desired results. So if I wanted $test in my snippet output, with test not being a tabstop, I would need \\$test snippet definition body:
"factory" :{
"prefix": "factory",
"body": [
"\\$factory->define($1, function (Faker\\Generator \\$faker){",
"\treturn [",
"\t\t$2,",
"\t];",
"});"
],
"description": "Creates Model factory"
},

Related

How to remove JSON text (including CRLF's) inside delimited records in Powershell

Have a strange issue where I need to remove JSON text in a tilde delimited file (having the JSON breaks the import due to CRLF at the end of each line of the JSON). Example line:
Test Plan Work~Response Status: BadRequest Bad Request,Response Content: {
"trace": "0HM5285F2",
"errors": [
{
"code": "server_error",
"message": "Couldn't access service ",
"moreInfoUrl": null,
"target": {
"type": null,
"name": null
}
}
]
},Request: https://www.test.com Headers: Accept: application/json
SubscriberId:
~87c5de00-5906-4d2d-b65f-4asdfsdfsdfa29~3/17/2020 1:54:08 PM
or ones like these that don't have JSON but still have the same pattern I need:
Test Plan Pay Work~Response Status: InternalServerError Internal Server Error,Response Content: Error,Request: https://api.test.com Headers: Accept: application/json
Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5c
SubscriberId: eb7aee
~9d05b16e-e57b-44be-b028-b6ddsdfsdf62a5~1/20/2021 7:07:53 PM
Need both of these types of CSV text to be in the format:
Test Plan Work~Response Status: BadRequest Bad Request~87c5de00-5906-4d2d-b65f-4asdfsdfsdfa29~3/17/2020 1:54:08 PM
The JSON (including the CRLF's at the end of each line of the JSON) are breaking the import of the data into Powershell. Any help or insight would be appreciated!
PowerShell (or rather, .NET) has two perculiar features in its regex engine that might be perfect for this use case - balancing groups and conditionals!
Balancing groups is a complicated feature to fully explain, but it essentially allows us to "keep count" of occurrences of specific named subexpressions in a regex pattern, and looks like this when applied:
PS ~> $string = 'Here is text { but wait { it has } nested { blocks }} here is more text'
PS ~> $string -replace '\{(?>\{(?<depth>)|[^{}]+|\}(?<-depth>))*(?(depth)(?!))\}'
Here is text here is more text
Let's break down the regex pattern:
\{ # match literal '{'
(?> # begin atomic group*
\{(?<depth>) # match literal '{' and increment counter
| [^{}]+ # OR match any sequence of characters that are NOT '{' or '}'
| \}(?<-depth>) # OR match literal '}' and decrement counter
)* # end atomic group, whole group should match 0 or more times
(? # begin conditional group*
(depth)(?!) # if the 'depth' counter > 0, then FAIL!
) # end conditional group
\} # match literal '}' (corresponding to the initial '{')
*) The (?>...) atomic grouping prevents backtracking - a safeguard against accidentally counting anything more than once.
For the CRLF characters in the remaining fields, we can prefix the pattern with (?s) - this makes the regex engine include new lines when matching the . "any" metacharacter, up until we reach the position just before ~87c5...:
(?s),Response Content:\s*\{(?>\{(?<depth>)|[^{}]+|\}(?<-depth>))*(?(depth)(?!))\}.*?(?=~)
Or we can, perhaps more accurately, describe the fields following the JSON as repeating pairs of , and "not ,":
,Response Content:\s*(?:\{(?>\{(?<depth>)|[^{}]+|\}(?<-depth>))*(?(depth)(?!))\})?\s*(?:,[^,]+?)*(?=~)
Let's give it a try against your multi-line input string:
$string = #'
Test Plan Work~Response Status: BadRequest Bad Request,Response Content: {
"trace": "0HM5285F2",
"errors": [
{
"code": "server_error",
"message": "Couldn't access service ",
"moreInfoUrl": null,
"target": {
"type": null,
"name": null
}
}
]
},Request: https://www.test.com Headers: Accept: application/json
SubscriberId:
~87c5de00-5906-4d2d-b65f-4asdfsdfsdfa29~3/17/2020 1:54:08 PM
'#
$string -replace ',Response Content:\s*(?:\{(?>\{(?<depth>)|[^{}]+|\}(?<-depth>))*(?(depth)(?!))\})?\s*(?:,[^,]+?)*(?=~)'
Output:
Test Plan Work~Response Status: BadRequest Bad Request~87c5de00-5906-4d2d-b65f-4asdfsdfsdfa29~3/17/2020 1:54:08 PM

Capture text between two different char. Between the first{ and the last }

Capture text between two different char using PowerShell. Between the first{ and the last } . Basically there is text with Json in it and I want to capture the json from it. I have looked for examples but so far no luck.
PROJECT Description: Azure Test Project Description
PROJECT ADMINISTRATORS: jjohnson
CONTRIBUTORS: jdoe
BOARD PROCESS: Agile
SPECIAL INSTRUCTIONS:
{
"organization": "https://dev.azure.com/cloudops",
"projectName": "Test Project",
"projectDescription": "Azure Test Project Description",
"projectProcessType": "Agile",
"specialInstructions": "",
"adminMembers": [
{
"userSamAccountName": "jjohnson",
"userEmailAddress": "jjohnson#test.com",
"userPrincipalName": "jjohnson#test.com",
"projectGroupType": "projectAdministrator"
}
],
"contribMembers": [
{
"userSamAccountName": "jdoe",
"userEmailAddress": "jdoe#test.com",
"userPrincipalName": "jdoe#test.com",
"projectGroupType": "projectContributor"
}
]
}
Is this what you were looking for?
[Regex]::Match((Get-Content "sampleinputfile.txt" -Raw),
'^{.+}',
[Text.RegularExpressions.RegexOptions]::Multiline -bor
[Text.RegularExpressions.RegexOptions]::Singleline).Value
Basically this converts the input file to a single (newline-delimited) string (Get-Content -Raw), and then uses the .NET Framework's Match method to do a regular expression match for the lines of text between the { and } characters (inclusive).

How to add dollar signs in Code Snippets of Visual Studio Code?

$ (dollar signs) are removed from Snippets in Visual Studio Code
How to disable placeholders?
My vue-html.json file snippet
"body": ["<ActivityIndicator v-if=\"$apollo.loading\" />"]
does not add $ and produces
<ActivityIndicator v-if="apollo.loading" />
but instead it should produce
<ActivityIndicator v-if="$apollo.loading" />
"description": {
"prefix": "trigger",
"body": [
"<ActivityIndicator v-if=\"\\$apollo.loading\" />"
],
"description": "description"
}
In JSON the backslash itself needs to be encoded with \, which is why you need \\
"body": ["<ActivityIndicator v-if=\"\\$apollo.loading\" />"]
You need to escape the $ symbol individually too

How to escape $ in Visual Studio Code snippet creation

It seems when using PHP variables like $_SERVER the code snippet ignores the $. For example
{
// Example:
"IP Address Test": {
"scope": "php",
"prefix": "iptest",
"body": [
"// Debugging",
"if($_SERVER[\"REMOTE_ADDR\"]=='${1:ipaddress}'){",
"\t//run only my ip",
"\t$0",
"}"
],
"description": "Test only from IP address"
}
}
outputs :
// Debugging
if(_SERVER["REMOTE_ADDR"]=='xxx.xxx.xxx.xxx'){
//run only my ip
}
You can't use \ you have to use double $ ..
eg.
// Debugging
if($$_SERVER["REMOTE_ADDR"]=='xxx.xxx.xxx.xxx'){
//run only my ip
}

Where is the "code" field in the Problems pane?

VS Code supports capturing error codes in custom problem matchers. What use are they? They don't seem to be displayed anywhere.
As described by the documentation:
code the match group index for the problem's code. Can be omitted if no code value is provided by the compiler.
For example, take the following tslint error:
ERROR: (comment-format) C:/Users/Kendall/Source/ncre/src/ncre.ts[84, 5]: comment must start with uppercase letter
Use this problem matcher:
"problemMatcher": {
"owner": "tslint",
"fileLocation": "absolute",
"pattern": {
"regexp": "^(ERROR|WARNING): \\((.+?)\\) (.+?)\\[(\\d+), (\\d+)\\]: (.+)$",
"severity": 1,
"code": 2,
"file": 3,
"line": 4,
"column": 5,
"message": 6
}
}
The error is displayed in the Problems pane like this:
The code comment-format doesn't appear here.
I've verified that it's correctly captured by copying the error and the code does appear in the result:
file: 'file:///c%3A/Users/Kendall/Source/ncre/src/ncre.ts'
severity: 'Error'
message: 'comment must start with uppercase letter'
at: '84,5'
source: ''
code: 'comment-format'
Am I missing something, or is capturing the error code mostly pointless?
Update: it's implemented in VSCode September 2018. Hooray!