How do I make Visual Studio auto generate braces f

2020-03-11 08:09发布

I could swear I've seen people typing function headers and then hitting some key combination to auto-create function braces and insert the cursor between them like so:

void foo()_

to

void foo()
{
    _
}

Is this a built-in feature?

5条回答
淡お忘
2楼-- · 2020-03-11 08:50

It can be achieved by using code snippets, some are already built in (try typing "svm" and hitting TAB-TAB)..

There's a wealth of info on the net on creating these:

Jeff did a post himself here

Have a google! I use them LOTS! :D

查看更多
太酷不给撩
3楼-- · 2020-03-11 08:50

I just created one based on @Luke's above. This one, you want to hit Enter then hit your key combination and it will insert:

if ()
{

}
else
{

}

And it will put your cursor in the parenthesis by the if statement.

Sub IfStatement()
    DTE.ActiveDocument.Selection.Text = "if ()"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "{"
    DTE.ActiveDocument.Selection.NewLine(2)
    DTE.ActiveDocument.Selection.Text = "}"
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "else"
    DTE.ActiveDocument.Selection.NewLine(2)
    DTE.ActiveDocument.Selection.Text = "{"
    DTE.ActiveDocument.Selection.NewLine(2)
    DTE.ActiveDocument.Selection.Text = "}"
    DTE.ActiveDocument.Selection.LineUp(False, 7)
    DTE.ActiveDocument.Selection.EndOfLine()
    DTE.ActiveDocument.Selection.CharLeft(3)
End Sub
查看更多
ゆ 、 Hurt°
4楼-- · 2020-03-11 08:54

Take a look at visual assist as well.

查看更多
相关推荐>>
5楼-- · 2020-03-11 09:00

Check out Resharper - it is a Visual Studio add-on with this feature, among many other development helps.

Also see C# Completer, another add-on.

If you want to roll your own, check out this article. Insane that one should have to do that, though.

查看更多
干净又极端
6楼-- · 2020-03-11 09:04

The tools look nice (especially Resharper but at $200-350 ouch!) but I ended up just recording a macro and assigning it to ctrl+alt+[

Macro came out like this:

Sub FunctionBraces()
    DTE.ActiveDocument.Selection.NewLine
    DTE.ActiveDocument.Selection.Text = "{}"
    DTE.ActiveDocument.Selection.CharLeft
    DTE.ActiveDocument.Selection.NewLine(2)
    DTE.ActiveDocument.Selection.LineUp
    DTE.ActiveDocument.Selection.Indent
End Sub

Edit: I used the macro recorder to make this and it wasn't too bad

查看更多
登录 后发表回答