-
Notifications
You must be signed in to change notification settings - Fork 5
Description
Here are examples of powershell code that when passed to Snippet Generator create invalid snippets.
Example 1
$1
Is converted to this snippet body:
"body": [
"$1"
]
The extension doesn't escape the dollar sign and instead creates a snippet with a tabstop. ($1
)
The correct snippet body should be:
"body": [
"\\$1"
]
Similarly:
"$1"
is converted to"\"$1\""
instead of"\"\\$1\""
."`$1"
is converted to"\"`$1\""
instead of"\"`\\$1\""
"${suffix}`$1"
is converted to"\"${suffix}`$1\""
instead of"\"\\${suffix}`\\$1\""
- Etc.
Example 2
The extension gets it right when the variable does not begin with a digit.
For example, "$Test"
correctly becomes "\\$Test"
.
But unfortunately, it again fails when the variable is formatted with curly braces I.E: "${Test}"
. The extension fails to escape the dollar signs again and are instead treated as variables:
${Test}
is converted to:
"body": [
"${Test}"
]
Instead of:
"body": [
"\\${Test}"
]
"${suffix}`$1"
is converted to:
"body": [
"\"${suffix}`$1\""
],
Instead of:
"body": [
"\"\\${suffix}`\\$1\""
]
Example 3 (Real world example)
I originally ran into this bug when creating regex snippets that use $1-$9
for capture group replacements.
# This code adds a suffix to a filename before the extension
$suffix = "_mod"
'DataAPI.svg' -replace '(\.\w+$)', "${suffix}`$1"
After converting the above to a snippet, the incorrectly generated body is:
"body": [
"\\$suffix = \"_mod\"",
"'DataAPI.svg' -replace '(\\.\\w+\\$)', \"${suffix}`$1\""
],
Since the dollar signs in the replacement string are not escaped, the snippet is broken when inserting.
The correct body should be:
"body": [
"\\$suffix = \"_mod\"",
"'DataAPI.svg' -replace '(\\.\\w+\\$)', \"\\${suffix}`\\$1\""
],
Summary
Can you possibly look into patching the escaping logic so that dollar signs in all instances are escaped correctly with double backslashes when creating Powershell snippets? If the user wants to insert a special tabstop or variable entity, it should be done manually after the snippet is created.
Right now Snippet Generator seems to assume that $1
-$9
or ${variableName}
should be Tabstops/Placeholders/Choice Entities/Variables/Etc.
Thanks for any consideration and I appreciate all your hard work put into this plugin.