Wrap TryCatch in Coffeescript

Sublime Text snippet: try/catch wrapping in CoffeeScript

Davide Barranca —  — Leave a comment

One of the editor’s cool features are Snippets (bits of code you use very often and want to keep handy). The CoffeeScript package that I use, Better-CoffeeScript, already has a snippet for try/catch block which creates some empty code to be filled:

Yet, I’d like to select some text, then have a shortcut-bound Snippet that acts on selected code and wraps it in a try/catch block. From this:

To this:

(Don’t worry if the code is weird, it’s what ExtendScript, a Javascript superset made by Adobe for scripting purposes, looks like). It seems trivial – yet I’ve had issues with proper indentation unless I’ve eventually turned to Regular Expressions.

There are plenty of tutorial about Sublime Text Snippet creation, so I’ll keep it as short as possible.

1. Create a New Snippet

In Sublime, go to the Tools > New Snippet… menu and replace the boilerplate XML-like code with the following:

The RegEx basically acts on the selected text ${SELECTION (mind you, there’s no tab in the snippet before it) and adds tabs to each line. Everything is wrapped in the try/catch block, and the caret is set in the last catch line, a comment ${1: #...}. Feel free to customize the error handling to your needs.

The <scope> tag instruct Sublime not to use this snippet unless the file is CoffeeScript, while the <description> is the label that will appear in the Snippets menu. You can follow the Sublime Text Documentation to learn more about Snippet creation, and this summary for Regular Expressions in Perl.

2. Save the Snippet

Save to Sublime Text > Packages > User folder, with a .sublime-snippet extension. I’ve used TryCatch_Wrap.sublime-snippet.

3. Bind to a Shortcut

Go to the Sublime Text > Preferences > Key-Bindings – User menu. A .sublime-keymap file will open, mine was:

In case (like me) you already have a line, add a comma and the following:

As I’ve written it, the Snippet is bound to Command+Control+t, but you can use the combination you like the most (have a look at Key-Bindings – Default to check whether you’re overriding some shortcut already in use). If you’re wondering, accepted modifiers are:

  • shift
  • ctrl
  • alt
  • super (Windows key, Command key)

Be sure to refer to the same .sublime-snippet filename, save and close the keymap.

4. Enjoy!

You’ve now built a Sublime Text Snippet for CoffeeScript that wraps and indent selected code in a try/catch block. You can select a portion of your code, hit ctrl+command+t and voilà! Hope this helps, you coffee fellows out there.

Print Friendly
Share

No Comments

Be the first to start the conversation.

Leave a Reply

Text formatting is available via select .

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url=""> 

*
*