Reputation: 38703
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!
Upvotes: 46
Views: 18370
Reputation: 957
For new comers in 2022,
Until this extension is available in the market, you have to clone the repo, build it and install it.
Upvotes: 0
Reputation: 1067
In VS2015 there is an experimental feature that supports it by selecting the text and typing in }.
See here how to enable.
Upvotes: 3
Reputation: 2283
You can wrap a code block with braces by
I know this is an old question but I hope it helps someone
Ref: Wrapping multiple statements in braces
Upvotes: -1
Reputation: 91432
How about:
Ctrl-X, {, Ctrl-V, }
You could even bind that to a macro.
Upvotes: 22
Reputation: 451
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.
Upvotes: 1
Reputation: 192186
In ReSharper 4.5, curly braces are included as one of the built-in 'Surround Templates':
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)
Upvotes: 38
Reputation: 8674
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".
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?
Upvotes: 0
Reputation: 76500
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>
Upvotes: 52
Reputation: 1644
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
Upvotes: 1