简体   繁体   English

Eclipse如何缩进C ++预处理程序宏

[英]Eclipse how can I indent C++ preprocessor macros

I can't find a setting in eclipse so that I can have it automatically indent my preprocessor macros the same way it indents code. 我在eclipse中找不到设置,因此可以像缩进代码一样使它自动缩进预处理程序宏。 For example eclipse tries to format code like this. 例如,eclipse尝试格式化这样的代码。

int main()
{
#ifdef SOMETHING
     cout << "Something Defined" << endl;
#endif
    return 0;
}

Whereas I want it to look like... 而我希望它看起来像...

int main()
{
    #ifdef SOMETHING
     cout << "Something Defined" << endl;
    #endif
    return 0;
}

Any ideas to make eclipse do it how I want? 有什么想法可以让日食做到我想要的吗?

Pre-ANSI C preprocessor did not allow for space between the start of a line and the "#" character; ANSI C之前的预处理器不允许在行首和“#”字符之间留空格; the leading "#" had to always be placed in the first column. 前导“#”必须始终放在第一列中。

Pre-ANSI C compilers are non-existent these days. 如今,ANSI C之前的编译器不存在。 Use which ever style (space before "#" or space between "#" and the identifier) you prefer. 使用您喜欢的样式(“#”前的空格或“#”与标识符之间的空格)。

But I suggest you do this: 但我建议您这样做: 在此处输入图片说明

Just use Find/Replace dialog and the push "Replace all" 只需使用“查找/替换”对话框,然后按“全部替换”即可

I think there is no option for macro indentation. 我认为宏缩进是没有选择的。 But I see clangformat seems to have option for macro indentation so you can customize your own clang format ( http://clang.llvm.org/docs/ClangFormatStyleOptions.html ) and configure eclipse to use clangformat instead of the default. 但我看到clangformat似乎可以使用宏缩进,因此您可以自定义自己的clang格式( http://clang.llvm.org/docs/ClangFormatStyleOptions.html )并将eclipse配置为使用clangformat而不是默认格式。

To indent the preprocessor you might need to use Neatbens instead. 要缩进预处理程序,您可能需要使用Neatbens It's formatter disregard the preANSIc. 它的格式化程序无视preANSIc。

The Eclipse indentation is correct. Eclipse缩进是正确的。 Preprocessor directives should be on the leftmost column, regardless of the indentation of the surrounding code. 无论周围代码的缩进形式如何,预处理器指令都应位于最左侧的列中。

Like others already pointed out compiler directives # have to start in the first column to be standard conform. 像其他已经指出的那样,编译器指令#必须从第一列开始才能符合标准。 Nevertheless it is allowed to put spaces behind them. 但是,允许在它们后面放置空间。 So my preferred solution looks as follows and then it should no longer be an eclipse issue. 因此,我的首选解决方案如下所示,因此它不再是日食问题。

int main()
{
#  ifdef SOMETHING
     cout << "Something Defined" << endl;
#  endif
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM