C++0x 枚举类的 Emacs cc 模式缩进问题
Emacs cc-mode 似乎尚未识别 C++0x 中引入的类型安全enum class
。我得到的结果是第二个、第三个等枚举的双缩进:
enum class Color {
Blue,
Red,
Orange,
Green
};
我想要的是:
enum class Color {
Blue,
Red,
Orange,
Green
};
您能否推荐一个好的命令来添加到 .emacs
中,这将使 cc-mode 处理 枚举类
与处理普通旧枚举
的方式相同吗?
Emacs cc-mode does not appear to yet recognize the type-safe enum class
introduced in C++0x. The result I get is a double indentation for second, third, etc enums:
enum class Color {
Blue,
Red,
Orange,
Green
};
What I would like is:
enum class Color {
Blue,
Red,
Orange,
Green
};
Can you recommend a good command to add to .emacs
which will make cc-mode treat enum class
the same way it treats the plain old enum
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是问题所在:
cc-mode 在某种程度上依赖于关键字是单个单词的假设。添加对
enum_class
而不是enum class
的支持只需更改一些正则表达式即可。相反,Emacs 将其视为一个类。解决这个问题的正确方法是告诉 Emacs 这是一个枚举。但这超出了答案的范围。
这就是黑客:
所以我们将修改现有的缩进,以在这种情况下表现不同。 (可在此 gist 中修改代码。)
这尚未经过严格测试。 ;)
工作原理:
c-offsets-alist
确定语法树中不同位置的缩进。可以为其分配常量或函数。这两个函数确定当前位置是否在
enum class {...}
内部。如果是这种情况,它们将返回 0 或 '-,cc-mode 将其解释为缩进深度。如果不是,它们将返回默认值。inside-class-enum-p 只需向上移动到前一个大括号,并检查其前面的文本是否是“枚举类”。
This is the problem:
cc-mode relies somewhat on the assumption that keywords are single words. Adding support for
enum_class
instead ofenum class
would just be a matter of changing a few regexps.Instead Emacs treats this as a class. The correct way of solving this would be teaching Emacs that this is an enum. But that's beyond the scope of an answer.
This is the hack:
So we'll modify the existing indentation to behave differently in this one case. (Code available for tinkering in this gist.)
This is not heavily tested. ;)
How it works:
c-offsets-alist
determines the indentation for different positions in the syntax tree. It can be assigned constants or functions.These two functions find out whether the current position is inside the
enum class {...}
. If that's the case, they return 0 or '-, which cc-mode interprets as an indentation depth. If it isn't, they return the default value.inside-class-enum-p
simply moves up to the previous brace and checks if the text before it is "enum class".