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

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

void foo()_

void foo()
{
    _
}

这是内置功能吗?

【问题讨论】:

    标签: c# visual-studio


    【解决方案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
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-06-18
              • 1970-01-01
              • 1970-01-01
              • 2012-08-24
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多