Any way to surround code block with Curly Braces {} in VS2008?

后端 未结 8 2079
-上瘾入骨i
-上瘾入骨i 2020-12-13 12:42

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

相关标签:
8条回答
  • 2020-12-13 12:59

    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">
      <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>
    
    0 讨论(0)
  • 2020-12-13 13:01

    You can wrap a code block with braces by

    1. Highlight the code block
    2. Ctrl e -> Ctrl u
    3. select option 7

    I know this is an old question but I hope it helps someone

    Ref: Wrapping multiple statements in braces

    0 讨论(0)
  • 2020-12-13 13:05

    In VS2015 there is an experimental feature that supports it by selecting the text and typing in }.

    See here how to enable.

    0 讨论(0)
  • 2020-12-13 13:10

    How about:

    Ctrl-X, {, Ctrl-V, }
    

    You could even bind that to a macro.

    0 讨论(0)
  • 2020-12-13 13:12

    To complete Ray Vega's answer, for those using Resharper, I figured out you can associate a shortcut to Resharper commands.

    Just do the following (I am using VS 2010):

    go to Tools->Options

    In the listbox, extend Environment and click on Keyboard.

    In the field under "Show commands containing:" enter "resharper.resharper_surroundwith"

    In the field under "Press shortcut keys" enter your shortcut (eg: I choose Ctrl+R,Ctrl+S) and click Assign and then Ok.

    That's it. you can select your code, and type that shorcut to view all Resharper SurroundWith commands. Just enter 7 to put braces.

    0 讨论(0)
  • 2020-12-13 13:12

    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?

    0 讨论(0)
提交回复
热议问题