Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to escape $ and { characters in snippets #1670

Closed
wmaurer opened this issue Dec 27, 2015 · 8 comments
Closed

Ability to escape $ and { characters in snippets #1670

wmaurer opened this issue Dec 27, 2015 · 8 comments
Assignees
Labels
*question Issue represents a question, should be posted to StackOverflow (VS Code) verified Verification succeeded
Milestone

Comments

@wmaurer
Copy link

wmaurer commented Dec 27, 2015

Snippets use the ${} syntax, but this is also valid Javascript/Typescript syntax for string interpolation, and this can create a conflict.

For example, I've defined a snippet as follows:

"rxjs .do(x => console.log(`${JSON.stringify()}`))": {
    "prefix": "rxdoj",
    "body": [
        ".do(x => console.log(`${JSON.stringify($1)}`))"
    ],
    "description": "rxjs .do(x => console.log(`${JSON.stringify()}`))"
}

I would like initial cursor position to be where the $1 is, but due to the conflicting syntax between snippets and Typescript, I get:

 .do(x => console.log(`JSON.stringify()`))

... with the entire backticked string as the selection.

My suggestion would be to able to escape a $ symbol using $$, which would signal to the snippet logic that this is not a placeholder (I tried this but it doesn't work correctly - the curly braces are still missing).

@bpasero bpasero added the *question Issue represents a question, should be posted to StackOverflow (VS Code) label Dec 28, 2015
@bpasero bpasero added this to the Backlog milestone Dec 28, 2015
@bpasero
Copy link
Member

bpasero commented Dec 28, 2015

I think this is possible @alexandrudima ?

@alexdima
Copy link
Member

@wmaurer You can escape the { and }:

"rxjs .do(x => console.log(`${JSON.stringify()}`))": {
    "prefix": "rxdoj",
    "body": [
        ".do(x => console.log(`$\\{JSON.stringify($1)\\}`))"
    ],
    "description": "rxjs .do(x => console.log(`${JSON.stringify()}`))"
}

@wmaurer
Copy link
Author

wmaurer commented Jan 22, 2016

@alexandrudima Thanks a lot! In the documentation, I haven't been able to find the fact that you can escape { and } ...

alexdima added a commit to microsoft/vscode-docs that referenced this issue Jan 22, 2016
@alexdima
Copy link
Member

@wmaurer Good point. I have pushed microsoft/vscode-docs@55ba3bb to document how to escape { and }.

@wmaurer
Copy link
Author

wmaurer commented Jan 22, 2016

@alexandrudima Great, thanks.

@alexdima alexdima added the verified Verification succeeded label Feb 26, 2016
@cbstodd
Copy link

cbstodd commented Jan 27, 2017

So I'm trying to create a simple string interpolation snippet as @alexandrudima suggested, but I'm getting an extra \ with my snippet:

"Interpolation": { "prefix": "int", "body": [ "$\\{$1\\}" ] }

so my output is:
$\{} when I want it to be ${}

@Neophius
Copy link

Neophius commented Feb 3, 2017

Move the first backslashes to before the $.

@MattGoldwater
Copy link

@Neophius Why does it work this way? Based on what I know of escape characters I would've expected to see 1 backslash before and after the { and }. Is this a JSON thing?

@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 18, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
*question Issue represents a question, should be posted to StackOverflow (VS Code) verified Verification succeeded
Projects
None yet
Development

No branches or pull requests

6 participants