如何让Visual Studio自动为功能块生成大括号?

时间:2008-08-13 05:09:33

标签: c# visual-studio

我可以发誓我见过人们输入函数头然后点击一些组合键来自动创建函数括号并将光标插入它们之间,如下所示:

void foo()_

void foo()
{
    _
}

这是内置功能吗?

5 个答案:

答案 0 :(得分:6)

这些工具看起来很不错(特别是Resharper,但价格在200-350美元!)但我最后只是录制一个宏并将其分配给ctrl + alt + [

Macro就是这样出来的:

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

编辑:我使用宏录制器来制作它并且它不是太糟糕

答案 1 :(得分:5)

查看Resharper - 它是一个具有此功能的Visual Studio插件,以及许多其他开发帮助。

另见C# Completer,另一个插件。

如果您想自己推出,请查看this article。然而,疯狂的人应该这样做。

答案 2 :(得分:2)

可以通过使用代码片段来实现,有些已经内置(尝试输入“svm”并点击TAB-TAB)..

网上有大量关于创建这些内容的信息:

Jeff did a post himself here

有一个谷歌!我用它们很多! :d

答案 3 :(得分:2)

同时查看visual assist

答案 4 :(得分:0)

我刚刚创建了一个基于@Luke的上面的内容。这一个,你想按Enter然后点击你的组合键,它将插入:

if ()
{

}
else
{

}

它会将你的光标放在if语句的括号中。

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