Vscode-ruby: Add snippet for #{} within a string

Created on 21 Mar 2018  路  4Comments  路  Source: rubyide/vscode-ruby

Environment

  • vscode-ruby version: 0.17.0
  • Ruby version: ruby 2.3.1p112
  • VS Code version: 1.21.1
  • Operating System: Windows 7 enterprise
  • Hardware (optional):

Expected behavior
In SublimeText 3 there is a nice snippet that within a string you type # and you get #{}

Actual behavior
At the moment I don't see this behavior in VSCode, I need to manually type # and { and } and move back the cursor to write the variable within the curly brackets.

Can somebody give me a hint where/how to implement this. I would like to add it to this excellent extension.

stale

Most helpful comment

Hi, I created a solution specifically for this issue. Feel free to try it out.

https://marketplace.visualstudio.com/items?itemName=aliariff.auto-add-brackets

All 4 comments

Great idea! I'm not an expert, but I think this can't be done as a standard snippet because snippets have no sense of context. You won't be able to figure out if the cursor is inside a string when using the VS Code Snippets feature. You also can't (yet) access the grammar/scopes within an extension (https://github.com/Microsoft/vscode/issues/580). Bummer.

You still might be able to hack it together, though. You'll have to add a custom command that runs when # is pressed. This part is easy - see the extension docs and vscode-ruby itself for hints. See if you can just great a function that calls vscode.window.showWarningMessage or something. Then you can try to use regex magic to see if you are within a string and insert the #{}.

Matching quotes is notoriously hard for regular expressions, though. If I were in your shoes I'd probably only attempt to handle the simplest possible case - /^[^"]+"[^"]+$/. That will still fail in many use cases but users may not mind.

I switched to VS code from Atom a few months ago. And one of the few features of Atoms Ruby support that I am really missing in VS code is this one.
Too bad that it looks like this simple wish might be somewhat complicated in VS Code...

Hi, I created a solution specifically for this issue. Feel free to try it out.

https://marketplace.visualstudio.com/items?itemName=aliariff.auto-add-brackets

This issue has not had activity for 30 days. It will be automatically closed in 7 days.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

webmastak picture webmastak  路  4Comments

wingrunr21 picture wingrunr21  路  4Comments

pelletencate picture pelletencate  路  5Comments

rebornix picture rebornix  路  3Comments

SerkanOruc picture SerkanOruc  路  5Comments