Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Surround code block with curly braces?

Is there a shortcut to surround a given (selected) code block with curly braces?

VS 2015 + R# 2016.1

like image 227
D.R. Avatar asked May 04 '16 13:05

D.R.


People also ask

Why do we use block of statements with braces?

Braces improve the uniformity and readability of code. More important, when inserting an additional statement into a body containing only a single statement, it is easy to forget to add braces because the indentation gives strong (but misleading) guidance to the structure.

How do I add braces to Visual Studio?

This is an in-built option in Visual Studio 2017. Go to Tools -> Options -> C / C++ -> Advanced , then navigate within the options dialog as shown on the screenshot below. Set the Enable Surround with Parentheses option to True .

What do braces do in coding?

What Does Bracket Mean? Brackets, or braces, are a syntactic construct in many programming languages. They take the forms of "[]", "()", "{}" or "<>." They are typically used to denote programming language constructs such as blocks, function calls or array subscripts. Brackets are also known as braces.

What is enclosed in curly braces?

How are curly brackets used? Curly brackets are commonly used in programming languages such as C, Java, Perl, and PHP to enclose groups of statements or blocks of code.


3 Answers

You do not need Resharper for this. You can use the "surround with" with a custom snippet.

Then you can select your text with Ctrl + K, Ctrl + S and select the snippet {} in the custom snippet. In order to create such a snippet :

  1. Create a ".snippet" file containing the content below
  2. Go to the snippet manager (Tools > Code Snippets Manager)
  3. Click import and select the file you just created

Use the following snippet :

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
 <CodeSnippet Format="1.0.0">
  <Header>
   <Title>{}</Title>
   <Shortcut>{}</Shortcut>
   <Description>Code snippet for {}</Description>
   <Author>Alexandre</Author>
   <SnippetTypes>
    <SnippetType>Expansion</SnippetType>
    <SnippetType>SurroundsWith</SnippetType>
   </SnippetTypes>
  </Header>
  <Snippet>
   <Code Language="csharp">
    <![CDATA[{ 
    $selected$ $end$ 
    }]]>
  </Code>
  </Snippet>
 </CodeSnippet>
</CodeSnippets>
like image 159
AlexandreG Avatar answered Oct 13 '22 04:10

AlexandreG


As an as an alternative to Patrick's answer (Ctrl+E, U, 7) you could also use the extended Alt+Enter -menu of ReSharper 9+.

Alt+Enter, UP, UP, Enter, or

Alt+Enter, "bl", Enter

Screen

Unfortunately, I don't know a way to bind this to a shorter hotkey.

But if this is really important to you, you could try AutoHotkey.

like image 22
ulrichb Avatar answered Oct 13 '22 03:10

ulrichb


The quickest built-in shortcut in the Visual Studio scheme I can find is Ctrl+E+U, 7 (surround with..., then choose option 7 which is curly braces).

I don't see a shortcut for it in Visual Studio's keyboard options, so that may be the best you can get.

like image 14
Patrick Quirk Avatar answered Oct 13 '22 04:10

Patrick Quirk