代码之家  ›  专栏  ›  技术社区  ›  Luke

如何使Visual Studio自动为函数块生成大括号?

  •  11
  • Luke  · 技术社区  · 16 年前

    我可以发誓,我见过有人键入函数头,然后点击一些键组合来自动创建函数大括号,并在它们之间插入光标,就像这样:

    void foo()_
    

    void foo()
    {
        _
    }
    

    这是内置功能吗?

    5 回复  |  直到 12 年前
        1
  •  5
  •   pc1oad1etter    16 年前

    退房 Resharper -它是一个具有此功能的Visual Studio附加组件,以及许多其他开发帮助。

    也看到 C# Completer ,另一个附加组件。

    如果你想自己滚,就去看看 this article . 不过,真是太疯狂了。

        2
  •  6
  •   Luke    16 年前

    这些工具看起来不错(尤其是再弹琴,但价格在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
    

    编辑:我用宏录制器录制的,还不错

        3
  •  2
  •   Rob Cooper    16 年前

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

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

    Jeff did a post himself here

    喝一杯谷歌!我经常使用它们!D

        4
  •  2
  •   David McGraw    16 年前

    看一看 visual assist 也。

        5
  •  0
  •   PCPGMR    12 年前

    我刚刚根据上面的@luke创建了一个。这一个,你要按回车键,然后按组合键,它将插入:

    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