visual-studio 有没有办法让 Visual Studio 停止缩进命名空间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3727862/
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
Is there any way to make Visual Studio stop indenting namespaces?
提问by Thomas Bonini
Visual Studio keeps trying to indent the code inside namespaces.
Visual Studio 不断尝试缩进命名空间内的代码。
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.
现在,如果我手动取消缩进,那么它会保持这种状态。但不幸的是,如果我之前添加了一些东西void Bar();- 例如评论 - VS 将继续尝试缩进它。
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.
这太烦人了,基本上是因为这个唯一的原因,我几乎从不使用 C++ 中的命名空间。我不明白为什么它试图缩进它们(在整个文件中缩进 1 个甚至 5 个标签有什么意义?),或者如何让它停止。
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.
有没有办法阻止这种行为?一个配置选项、一个加载项、一个注册表设置,甚至是直接修改 devenv.exe 的黑客。
采纳答案by Rod
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.
这是一个可以帮助您的宏。如果它检测到您当前正在创建一个namespace. 它并不完美,但到目前为止似乎有效。
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 EnvironmentEventsproject iteminside MyMacros. You can only access this module in the Macro Explorer (Tools->Macros->Macro Explorer).
由于它一直在运行,因此您需要确保在 MyMacros内的EnvironmentEvents项目项中安装宏。您只能在宏资源管理器(工具->宏->宏资源管理器)中访问该模块。
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:
要支持“打包”命名空间(例如上面的示例)和/或支持命名空间后的注释(例如 )namespace A { /* Example */,您可以尝试使用以下行:
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.
我还没有机会对其进行大量测试,但它似乎奏效了。
回答by user64953
As KindDragon points out, Visual Studio 2013 Update 2 has an option to stop indenting.
正如 KindDragon 指出的那样,Visual Studio 2013 Update 2 有一个停止缩进的选项。
You can uncheck TOOLS -> Options -> Text Editor -> C/C++ -> Formatting -> Indentation -> Indent namespace contents.
您可以取消选中工具 -> 选项 -> 文本编辑器 -> C/C++ -> 格式 -> 缩进 -> 缩进命名空间内容。
回答by bacar
Just don't insert anything before the first line of code. You could try the following approach to insert a null line of code (it seems to work in VS2005):
只是不要在第一行代码之前插入任何内容。您可以尝试使用以下方法插入空代码行(它似乎在 VS2005 中有效):
namespace foo
{; // !<---
void Test();
}
This seems to suppress the indentation, but compilers may issue warnings and code reviewers/maintainers may be surprised! (And quite rightly, in the usual case!)
这似乎抑制了缩进,但编译器可能会发出警告,代码者/维护者可能会感到惊讶!(而且非常正确,在通常情况下!)
回答by Ken Simon
Probably not what you wanted to hear, but a lot of people work around this by using macros:
可能不是你想听到的,但很多人通过使用宏来解决这个问题:
#define BEGIN_NAMESPACE(x) namespace x {
#define END_NAMESPACE }
Sounds dumb, but you'd be surprised how many system headers use this. (glibc's stl implentation, for instance, has _GLIBCXX_BEGIN_NAMESPACE()for this.)
听起来很愚蠢,但您会惊讶于有多少系统标头使用它。(例如,glibc 的 stl 实现_GLIBCXX_BEGIN_NAMESPACE()就是为此。)
I actually prefer this way, because I always tend to cringe when I see un-indented lines following a {. That's just me though.
我实际上更喜欢这种方式,因为当我看到{. 那只是我。
回答by metator
You could also forward declare your types (or whatever) inside the namespace then implement outside like this:
您还可以在命名空间内转发声明您的类型(或其他类型),然后在外部实现如下:
namespace test {
class MyClass;
}
class test::MyClass {
//...
};
回答by Michael Hall
回答by David Rodríguez - dribeas
I understand the problem when there are nested namespaces. I used to pack all the namespaces in a single line to avoid the multiple indentation. It will leave one level, but that's not as bad as many levels. It's been so long since I have used VS that I hardly remember those days.
我理解嵌套命名空间时的问题。我曾经将所有namespaces打包在一行中以避免多个缩进。它会留下一个级别,但这并不像许多级别那样糟糕。我已经很久没有使用 VS 了,以至于我几乎不记得那些日子了。
namespace outer { namespace middle { namespace inner {
void Test();
.....
}}}


