C# 如何让 Visual Studio 自动为功能块生成大括号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make Visual Studio auto generate braces for a function block?
提问by Luke
I could swear I've seen people typing function headers and then hitting some key combination to auto-create function braces and insert the cursor between them like so:
我可以发誓我见过有人输入函数标题,然后按一些组合键来自动创建函数大括号并在它们之间插入光标,如下所示:
void foo()_
to
到
void foo()
{
_
}
Is this a built-in feature?
这是内置功能吗?
采纳答案by pc1oad1etter
Check out Resharper- it is a Visual Studio add-on with this feature, among many other development helps.
查看Resharper- 它是具有此功能的 Visual Studio 附加组件,以及许多其他开发帮助。
Also see C# Completer, another add-on.
另请参阅C# Completer,另一个附加组件。
If you want to roll your own, check out this article. Insane that one should have to do that, though.
如果您想推出自己的产品,请查看这篇文章。疯狂的是,一个人应该这样做,虽然。
回答by Rob Cooper
It can be achieved by using code snippets, some are already built in (try typing "svm" and hitting TAB-TAB)..
它可以通过使用代码片段来实现,其中一些已经内置(尝试键入“svm”并按 TAB-TAB)。
There's a wealth of info on the net on creating these:
网上有很多关于创建这些的信息:
Have a google! I use them LOTS! :D
有一个谷歌!我经常使用它们!:D
回答by David McGraw
Take a look at visual assistas well.
也看看视觉辅助。
回答by Luke
The tools look nice (especially Resharper but at $200-350 ouch!) but I ended up just recording a macro and assigning it to ctrl+alt+[
这些工具看起来不错(尤其是 Resharper,但价格为 200-350 美元哦!)但我最终只录制了一个宏并将其分配给 ctrl+alt+[
Macro came out like this:
宏是这样出来的:
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
Edit: I used the macro recorder to make this and it wasn't too bad
编辑:我用宏记录器来做这个,还不错
回答by PCPGMR
I just created one based on @Luke's above. This one, you want to hit Enter then hit your key combination and it will insert:
我刚刚根据上面的@Luke 创建了一个。这个,你想按 Enter 然后按你的组合键,它会插入:
if ()
{
}
else
{
}
And it will put your cursor in the parenthesis by the if statement.
它会将您的光标放在 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