Joe White’s Blog

Life, .NET, and Cats


ReSharper, day 4: Auto closing delimiters

Welcome to day 4 of my 31 Days of ReSharper.

Here’s a feature you’ll notice as soon as you start typing: when you type an opening delimiter, ReSharper will type the closing one for you.

It’s a simple feature, but not a simple one to do well, and except for a bug that they introduced in 2.5 (and then fixed in 2.5.1), they do it well. Very seldom does it do the Wrong Thing.

This works for the following delimiters: {, (, [, ", and '. It doesn’t autocomplete generic syntax, though: < is not on the list.

Here’s a sample of it at work.

Starting out with an empty statement.
You type a method name…
and type “(”.
You type a subexpression…
and type “(”.
Oops. You meant “[”, not “(”. Backspace…
and type “[”.
Once you’ve typed the parameter…
you type “]”…
then you type “)”…
and then you type “;”.

They do the things right that need doing right: when you backspace over an opening delimiter, they remove the closing delimiter (assuming you didn’t type anything inside the parens yet); and when your cursor is before the closing delimiter and you type the closing delimiter, you don’t end up with two. This also applies to semicolons; when the cursor is before a semicolon, you can type a semicolon, and you won’t get two. You can just type normally, without having to hit the right arrow all the time.

They’re even smart enough to make it work inside arbitrary nesting: if you type the opening delimiter and the closing delimiter is already there, they don’t add it. There’s got to be some guesswork involved at some point, but I don’t remember it being wrong enough to notice.

Note: When you type {, you automatically get the matching } — meaning, if your intent is to put braces around a block of code, the matching delimiter gets in your way. Instead, you can use Surround With to add the braces around the current selection. The shortcut for adding braces is Ctrl+Alt+J 8. (Ctrl+Alt+J pops up a menu, so you don’t need to memorize the “8″ part.)

(Update: Ilya, one of the ReSharper developers, notes that you can have ReSharper add the closing brace when you hit Enter, instead of right away, which would make it easier to simply type braces around a block of existing code. Thanks, Ilya!)

4 Responses to “ReSharper, day 4: Auto closing delimiters”

  1. Ilya Ryzhenkov Says:

    Thank you very much for you articles - they are very interesting and useful, even for us, ReSharper developers! We will put a link to your blog on JetBrains .NET tools blog.

    Conerning braces "{}", did you try configuring it to autoinsert closing braces on Enter after opening brace, instead of when opening brace is typed? See ReSharper/Options/Editor, "Auto-insert closing brace", "On Enter after opening brace". I use this style and it doesn’t get in the way when I want to surround existing block, and I anyway start statement at the line following the opening brace. This works great for me.

  2. Adam Says:

    Hey, very nice article! But Surround with is Ctrl+Alt+J 7 and not 8. It is actually in my IDE.

  3. Joe White Says:

    Interesting. Adam, are you running ReSharper 2.5.1, or a different version?

    Also, I know you can customize the Surround With templates, and can add and remove them, which would probably affect which ones got which shortcut key. I don’t think I customized mine before I wrote this, but it’s possible…

  4. Brian Says:

    It’s Ctrl+Alt+J 7 for me also…

    Great series!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Joe White's Blog copyright © 2004-2008. Portions of the site layout use Yahoo! YUI Reset, Fonts, and Grids.
Proudly powered by WordPress. Entries (RSS) and Comments (RSS).