views:

274

answers:

5

I always find myself needing to enclose a block of code in curly braces { }, but unfortunately that isn't included in the C# surround code snippets, which seems to be an oversight. I couldn't find anything on building your own surround snippets either (just other kinds of snippets).

I am actually running Resharper too, but it doesn't seem to have this functionality either (or I haven't figured how to activate it).

We have a coding standard of including even a single line of code after an if or else in curly braces, so if I could just make Resharper do that refactoring automatically that would be even better!

A: 

Make your own custom code snippet for doing that. You can use snippy to create your own http://blogs.msdn.com/gusperez/articles/93681.aspx or just use an XML editor to create one.

Put the file in My Documents\Visual Studio XXXX\Code Snippets\C#\My Code Snippets

helios456
A: 

Edit: This turns out to be part of DxCore, from DevExpress. Leaving here so others notice, but basically I was wrong wrong wrong. To make this particular menu go away you disable it in the 'add ins' dialog; unloading devexpress from their own menu just unloads CodeRush/Refactor, not the base support libraries.

The is (not!) a built in way to do it. I don't know if you can bind a key to it or not. Also, this embed doesn't do anything if you only select one line, so it probably won't work right if your stuff is on one line after the "if".

  1. Select the block
  2. Right Click
  3. Choose "Embed Selection"
  4. Choose "Block {}"

Note: I have DexExpress installed, but this menu is there even when it isn't loaded, and I could swear it is there even when it isn't installed. However, if I am mistaken...

This honestly seems like something that would be best to ask r# for, a user contrib perhaps?

Andrew Backer
This option does not seem to be in my version of VS2008, but maybe I have something configured differently. It does have a "Surround with..." option, but no curly brace in there.
Mark Wilkins
It turns out it is part of DxCore, the support framework for DevExpress. Noticed it when I went to see if there was some other mystery addon that I forgot about. Even when you disable the main product this still stays, which explains why I can't find it in the menu.
Andrew Backer
@Mark that is the same thing I am seeing. Seems like I remember seeing this when I was using DevExpress' CodeRush.
Jim McKeeth
+4  A: 

Here is a quick and dirty snippet to do just that.

To Install:

Save the code as SurroundWithBraces.snippet into "<my documents>\Visual Studio Version\Code Snippets\Visual C#\My Code Snippets"

To use:

Select block of text.
Press Ctrl+K, Ctrl+S
Chose My Code Snippets, braces

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"&gt;
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>braces</Title>
      <Shortcut>braces</Shortcut>
      <Description>Code snippet to surround a block of code with braces</Description>
      <Author>Igor Zevaka</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="csharp">
        <![CDATA[{
        $selected$ $end$
     }]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
Igor Zevaka
+2  A: 

In ReSharper 4.5, curly braces are included as one of the built-in 'Surround Templates':

  1. Select the text that you want curly braces around.
  2. ReSharper -> Code -> Surround With... -> {}

    or

    ALT + R -> C -> S -> 7

    or

    Ctrl+E, U -> 7 (Visual Studio scheme)

    or

    Ctrl+Alt+J -> 7 (ReSharper 2.x/IDEA scheme)

Ray Vega
A: 
how about the "Ctrl-X, {, Ctrl-V, }"? you could even bind that to a macro. 
Jimmy
Reasonable, but doing this puts the selected text on the clipboard.
devgeezer