C++0x 枚举类的 Emacs cc 模式缩进问题

发布于 2024-11-17 07:21:10 字数 407 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙女 2024-11-24 07:21:10

这就是问题所在:

cc-mode 在某种程度上依赖于关键字是单个单词的假设。添加对 enum_class 而不是 enum class 的支持只需更改一些正则表达式即可。

相反,Emacs 将其视为一个类。解决这个问题的正确方法是告诉 Emacs 这是一个枚举。但这超出了答案的范围。

这就是黑客:

所以我们将修改现有的缩进,以在这种情况下表现不同。 (可在此 gist 中修改代码。)

(defun inside-class-enum-p (pos)
  "Checks if POS is within the braces of a C++ \"enum class\"."
  (ignore-errors
    (save-excursion
      (goto-char pos)
      (up-list -1)
      (backward-sexp 1)
      (looking-back "enum[ \t]+class[ \t]+[^}]+"))))

(defun align-enum-class (langelem)
  (if (inside-class-enum-p (c-langelem-pos langelem))
      0
    (c-lineup-topmost-intro-cont langelem)))

(defun align-enum-class-closing-brace (langelem)
  (if (inside-class-enum-p (c-langelem-pos langelem))
      '-
    '+))

(defun fix-enum-class ()
  "Setup `c++-mode' to better handle \"class enum\"."
  (add-to-list 'c-offsets-alist '(topmost-intro-cont . align-enum-class))
  (add-to-list 'c-offsets-alist
               '(statement-cont . align-enum-class-closing-brace)))

(add-hook 'c++-mode-hook 'fix-enum-class)

这尚未经过严格测试。 ;)

工作原理:

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 of enum 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.)

(defun inside-class-enum-p (pos)
  "Checks if POS is within the braces of a C++ \"enum class\"."
  (ignore-errors
    (save-excursion
      (goto-char pos)
      (up-list -1)
      (backward-sexp 1)
      (looking-back "enum[ \t]+class[ \t]+[^}]+"))))

(defun align-enum-class (langelem)
  (if (inside-class-enum-p (c-langelem-pos langelem))
      0
    (c-lineup-topmost-intro-cont langelem)))

(defun align-enum-class-closing-brace (langelem)
  (if (inside-class-enum-p (c-langelem-pos langelem))
      '-
    '+))

(defun fix-enum-class ()
  "Setup `c++-mode' to better handle \"class enum\"."
  (add-to-list 'c-offsets-alist '(topmost-intro-cont . align-enum-class))
  (add-to-list 'c-offsets-alist
               '(statement-cont . align-enum-class-closing-brace)))

(add-hook 'c++-mode-hook 'fix-enum-class)

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文