“clang-format: trailing comment after #endif is formatted incorrectly.”

huangapple go评论74阅读模式
英文:

clang-format: trailing comment after #endif is formatted incorrectly

问题

在我的程序中,我有以下代码:

#ifdef __UCOSII__

#define SCANCODE_HKEY1  SCANCODE_F1  ///< 水平按键1
#define SCANCODE_HKEY2  SCANCODE_F2  ///< 水平按键2
#define SCANCODE_HKEY3  SCANCODE_F3  ///< 水平按键4

#define SCANCODE_VKEY1  SCANCODE_1   ///< 垂直按键1
#define SCANCODE_VKEY2  SCANCODE_2   ///< 垂直按键2
#define SCANCODE_VKEY3  SCANCODE_3   ///< 垂直按键3

#endif                               // __UCOSII__

我正在使用 clang-format 来格式化代码,在我的 .clang-format 配置文件中,我添加了以下选项:

...
MaxEmptyLinesToKeep: 2
SpacesBeforeTrailingComments: 2
AlignTrailingComments:
  Kind: Always
  OverEmptyLines: 1
...

生成的格式化代码如下:

#ifdef __UCOSII__

#define SCANCODE_HKEY1  SCANCODE_F1  ///< 水平按键1
#define SCANCODE_HKEY2  SCANCODE_F2  ///< 水平按键2
#define SCANCODE_HKEY3  SCANCODE_F3  ///< 水平按键4

#define SCANCODE_VKEY1  SCANCODE_1   ///< 垂直按键1
#define SCANCODE_VKEY2  SCANCODE_2   ///< 垂直按键2
#define SCANCODE_VKEY3  SCANCODE_3   ///< 垂直按键3

#endif                               // __UCOSII__

正如您所看到的,#ifdef#endif 之间的注释已经正确格式化,但在 #endif 后的注释未按我所期望的方式格式化。我希望它在 #endif 之后缩进两个空格,但它并没有这样做。如何才能实现期望的结果?是否有可用的选项来实现这一点?

英文:

I have the following code in my program:

#ifdef __UCOSII__

#define SCANCODE_HKEY1  SCANCODE_F1 ///< Horizontal key 1
#define SCANCODE_HKEY2  SCANCODE_F2 ///< Horizontal key 2
#define SCANCODE_HKEY3  SCANCODE_F3 ///< Horizontal key 4

#define SCANCODE_VKEY1  SCANCODE_1 ///< Vertical key 1
#define SCANCODE_VKEY2  SCANCODE_2 ///< Vertical key 2
#define SCANCODE_VKEY3  SCANCODE_3 ///< Vertical key 3

#endif  // __UCOSII__

I'm using clang-format to format the code, and in my .clang-format configuration file, I added the following options:

...
MaxEmptyLinesToKeep: 2
SpacesBeforeTrailingComments: 2
AlignTrailingComments:
  Kind: Always
  OverEmptyLines: 1
...

The resulting formatted code looks like this:

#ifdef __UCOSII__

#define SCANCODE_HKEY1  SCANCODE_F1  ///< Horizontal key 1
#define SCANCODE_HKEY2  SCANCODE_F2  ///< Horizontal key 2
#define SCANCODE_HKEY3  SCANCODE_F3  ///< Horizontal key 4

#define SCANCODE_VKEY1  SCANCODE_1   ///< Vertical key 1
#define SCANCODE_VKEY2  SCANCODE_2   ///< Vertical key 2
#define SCANCODE_VKEY3  SCANCODE_3   ///< Vertical key 3

#endif                               // __UCOSII__

As you can see, the comment between #ifdef and #endif is formatted correctly, but the comment after #endif is not formatted the way I want it to be. I want it to be indented two spaces after #endif, but it is not doing that. What can I do to achieve the desired result? Are there any options available to achieve this?

答案1

得分: 1

这是由 OverEmptyLines: 1 引起的。

clang-format 不区分"endif trailing comment"和"other trailing comment"。它对它们一视同仁。您要么降低OverEmptyLines:的值,要么在 #endif 前添加更多的行。

英文:

This is caused by the OverEmptyLines: 1.

clang-format does not distinguish between "endif trailing comment" and "other trailing comment". It treats them the same. You either need to lower the OverEmptyLines: or put more lines before #endif.

huangapple
  • 本文由 发表于 2023年4月13日 23:13:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定