Wrapping multiple statements in braces

僤鯓⒐⒋嵵緔 提交于 2019-12-05 13:07:21

问题


Is there a keyboard shortcut in Visual Studio 2010 (I'm using ReSharper 6.1 also) that will allow me to surround a selected block of text with curly braces? I tried "Surround With..." (Ctrl+K, Ctrl+S), but I didn't see an option in the list to choose curly braces as the surrounding element. The common use case for this is that I'll have an if-statement like the following:

if (conditional)
    statement1;
// the rest of the program

I'll realize that there are some additional tasks that need to be performed inside the if-statement and I add them:

if (conditional)
    statement1;
    statement2;
    statement3;
// the rest of the program

Then, I remember that I need to wrap all the statements in curly braces and the code should really look like this:

if (conditional)
{
    statement1;
    statement2;
    statement3;
}
// the rest of the program

What I'd like to do is just select the three statements and then hit a shortcut key to wrap them in curly braces. What I actually end up doing is moving the cursor to the beginning of the line after the conditional, then typing a { character, then deleting the } character that ReSharper (unhelpfully) automatically inserts immediately after the {, then moving the cursor down to end of the last statement of the block and entering } to complete the block.


回答1:


Select rows of code.

Press Ctrl E-U (Surround with template) (or Ctrl Alt J for Intelli J).

Select option 7: { }.

Works for me.



来源:https://stackoverflow.com/questions/10036450/wrapping-multiple-statements-in-braces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!