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

Posted

技术标签:

【中文标题】如何让 Visual Studio 为功能块自动生成大括号?【英文标题】:How do I make Visual Studio auto generate braces for a function block? 【发布时间】:2010-09-05 18:53:06 【问题描述】:

我敢发誓我见过有人输入函数标题,然后按一些组合键来自动创建函数括号并在它们之间插入光标,如下所示:

void foo()_

void foo()

    _

这是内置功能吗?

【问题讨论】:

【参考方案1】:

这些工具看起来不错(尤其是 Resharper,但要 200-350 美元哦!)但我最终只是录制了一个宏并将其分配给 ctrl+alt+[

宏是这样出来的:

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

编辑:我用宏记录器来做这个,还不错

【讨论】:

【参考方案2】:

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

另见C# Completer,另一个附加组件。

如果您想自己动手,请查看this article。太疯狂了,但应该这样做。

【讨论】:

【参考方案3】:

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

网上有很多关于创建这些的信息:

Jeff did a post himself here

有一个谷歌!我经常使用它们! :D

【讨论】:

【参考方案4】:

也可以看看visual assist。

【讨论】:

【参考方案5】:

我刚刚根据上面的@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

【讨论】:

以上是关于如何让 Visual Studio 为功能块自动生成大括号?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Visual Studio 中自动将预处理器和注释块插入到新的 C++ 头文件中?

如何在 Visual Studio 2010 中自动折叠某些评论?

如何让 Visual Studio 自动升级项目?

如何让 Visual Studio ToolBox 自动填充项目组件?

Visual Studio 2013 - 自动插入“->”运算符

Visual Studio 2022 有哪些新功能?