有什么办法使Visual Studio停止缩进命名空间? [英] Is there any way to make Visual Studio stop indenting namespaces?
问题描述
Visual Studio一直尝试在命名空间中缩进代码。
例如:
namespace Foo
{
void Bar();
void Bar()
{
}
}
现在,如果我手动缩进它,它保持那样。但是不幸的是,如果我在 void Bar();
之前添加一些东西 - 比如注释 - VS会继续尝试缩进。
这很烦人,基本上是因为这个唯一的原因,我几乎从来不使用命名空间在C + +。我不明白为什么它试图缩进它们(缩进1或甚至5个标签中整个文件?),或如何让它停止。
有办法阻止这种行为吗?
这里是一个配置选项,加载项,注册表设置,甚至是直接修改devenv.exe的黑客是一个宏可以帮助你。如果检测到您当前正在创建一个命名空间
,它将删除缩进。
Public Sub aftekeypress(ByVal key As String,ByVal sel As TextSelection,ByVal完成作为布尔)_
句柄TextDocumentKeyPressEvents.AfterKeyPress
如果(未完成和键= vbCr)然后
'只有执行这,如果我们使用智能缩进
如果DTE.Properties TextEditor,C / C ++)。Item(IndentStyle)Value = 2 Then
Dim textDocument As TextDocument = DTE.ActiveDocument.Object(TextDocument)
Dim startPoint As EditPoint = sel.ActivePoint.CreateEditPoint()
Dim matchPoint As EditPoint = sel.ActivePoint.CreateEditPoint()
Dim findOptions As Integer = vsFindOptions.vsFindOptionsMatchCase + vsFindOptions.vsFindOptionsMatchWholeWord + vsFindOptions.vsFindOptionsBackwards
如果startPoint。 FindPattern(namespace,findOptions,matchPoint)然后
Dim lines = matchPoint.GetLines(matchPoint.Line,sel.ActivePoint.Line)
'确保我们仍然在命名空间{},已输入
如果System.Text.RegularExpressions.Regex.IsMatch(行,^ [\s] *(namespace [\s\w] +)?[\s\ {] + $ )then
sel.Unindent()
结束如果
结束如果
结束如果
结束如果
结束子
由于它一直运行,因此您需要确保您在 EnvironmentEvents
项目项。您只能在宏资源管理器(工具 - >宏 - >宏资源管理器)中访问此模块。
注意,它目前不支持压缩命名空间
命名空间A {命名空间B {
...
}
}
EDIT b
$ b
支持packed命名空间,例如上面的例子和/或在命名空间之后支持注释,例如 namespace A {/ * Example * /
,您可以尝试改用下面的代码:
如果System.Text.RegularExpressions.Regex.IsMatch ,^ [\ s] *(namespace。+)?[\s\ {] + $)然后
我还没有机会测试它,但它似乎是工作。
Visual Studio keeps trying to indent the code inside namespaces.
For example:
namespace Foo
{
void Bar();
void Bar()
{
}
}
Now, if I un-indent it manually then it stays that way. But unfortunately if I add something right before void Bar();
- such as a comment - VS will keep trying to indent it.
This is so annoying that basically because of this only reason I almost never use namespaces in C++. I can't understand why it tries to indent them (what's the point in indenting 1 or even 5 tabs the whole file?), or how to make it stop.
Is there a way to stop this behavior? A config option, an add-in, a registry setting, hell even a hack that modifies devenv.exe directly.
Here is a macro that could help you. It will remove indentation if it detects that you are currently creating a namespace
. It is not perfect but seems to work so far.
Public Sub aftekeypress(ByVal key As String, ByVal sel As TextSelection, ByVal completion As Boolean) _
Handles TextDocumentKeyPressEvents.AfterKeyPress
If (Not completion And key = vbCr) Then
'Only perform this if we are using smart indent
If DTE.Properties("TextEditor", "C/C++").Item("IndentStyle").Value = 2 Then
Dim textDocument As TextDocument = DTE.ActiveDocument.Object("TextDocument")
Dim startPoint As EditPoint = sel.ActivePoint.CreateEditPoint()
Dim matchPoint As EditPoint = sel.ActivePoint.CreateEditPoint()
Dim findOptions As Integer = vsFindOptions.vsFindOptionsMatchCase + vsFindOptions.vsFindOptionsMatchWholeWord + vsFindOptions.vsFindOptionsBackwards
If startPoint.FindPattern("namespace", findOptions, matchPoint) Then
Dim lines = matchPoint.GetLines(matchPoint.Line, sel.ActivePoint.Line)
' Make sure we are still in the namespace {} but nothing has been typed
If System.Text.RegularExpressions.Regex.IsMatch(lines, "^[\s]*(namespace[\s\w]+)?[\s\{]+$") Then
sel.Unindent()
End If
End If
End If
End If
End Sub
Since it is running all the time, you need to make sure you are installing the macro inside in your EnvironmentEvents
project item inside MyMacros. You can only access this module in the Macro Explorer (Tools->Macros->Macro Explorer).
One note, it does not currently support "packed" namespaces such as
namespace A { namespace B {
...
}
}
EDIT
To support "packed" namespaces such as the example above and/or support comments after the namespace, such as namespace A { /* Example */
, you can try to use the following line instead:
If System.Text.RegularExpressions.Regex.IsMatch(lines, "^[\s]*(namespace.+)?[\s\{]+$") Then
I haven't had the chance to test it a lot yet, but it seems to be working.
这篇关于有什么办法使Visual Studio停止缩进命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!