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


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

void foo()_

void foo()
{
    _
}

这是内置功能吗?


9544
2017-08-13 05:09


起源



答案:


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

另见 C#完成者,另一个附加组件。

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


5
2017-08-13 05:20





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

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


6
2017-08-13 05:30





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

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

杰夫在这里做了一个帖子

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


2
2017-08-13 05:14





看一眼 视觉辅助 同样。


2
2017-08-13 05:15





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

0
2017-09-05 15:47