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

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

void foo()_ 

 void foo() { _ } 

这是内置function吗?

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

另请参阅C#Completer ,另一个附加组件。

如果您想自己动手,请查看此文章 。 但是,一个人应该这样做是疯了。

这些工具看起来很不错(特别是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 

编辑:我使用宏录制器来制作它并没有太糟糕

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

在创建这些内容时,网上有大量信息:

杰夫在这里做了一个post

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

看看视觉辅助 。

我刚刚在@ 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