如何禁用代码块的Eclipse CDT代码格式化程序 [英] How to disable Eclipse CDT code formatter for a code block

查看:391
本文介绍了如何禁用代码块的Eclipse CDT代码格式化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CDT代码格式化器有一个相当不错的选择选项,但它似乎没有一个功能,允许人们告诉它忽略一段代码。此功能存在于Java代码格式器中:

  // @formatter:off 
... //代码不应格式化
// @formatter:on

此功能是否存在



在我的特殊情况下,我试图定义数据结构(枚举类型和枚举类型)

解决方案

使用Astyle(艺术风格)格式化,它远远优于Eclipse CDT内置格式化程序,并具有您需要的功能:



http://astyle.sourceforge.net/astyle.html#_Disable_Formatting



示例:

  #include< iostream> 

int main(int argc,char ** argv)
{
// * INDENT-OFF *
std :: cout< <<'\\\
';
// * INDENT-ON *
}

'缩进// INDENT-OFF 和// 之间的代码,但它也会禁用astyle的任何其他格式化功能,这种情况。



我使用它自己配置为外部工具。
唯一的问题,外部工具没有热键,但有一个热键运行最后启动的外部工具,如果你只使用一个外部工具,它的工作原理相同。



有关配置(linux)的更多详细信息:



Astyle:





要使用格式设置设置配置文件:



http://astyle.sourceforge.net/astyle.html#_Options_File



我使用主文件夹变体,只需在$ HOME中创建一个.astylerc,我的包含:

   -  suffix = none 
--style = allman
--indent = tab = 4
--max-code-length = 70
--close-templates
--keep-one-line-blocks
--break-elseifs
--break-closing-brackets
--align-reference = type
--align-pointer = type
--indent-classes
--indent-modifiers
--indent-switches
--indent-cases
--indent-labels
--indent-col1-comments
--min-conditional-indent = 0
--pad-oper
--pad-header
--unpad-paren

Eclipse:



运行菜单 - >外部工具 - >外部工具配置...添加新的程序并在配置窗口中:




  • 位置:/ usr / bin / astyle(使用where或查找此选项)


  • 工作目录:$ {project_loc}


  • 参数:$ {selected_resource_loc}




在同一个窗口中,刷新标签:




  • 完成后刷新资源

    $ b
  • 勾选所选资源




>


  • 显示在收藏夹菜单中,勾选外部工具


The CDT code formatter has a pretty decent selection of options, but it doesn't seem to have to a feature that allows one to tell it to ignore a block of code. This feature exists in the Java code formatter:

// @formatter:off
... // code that should not be formatted
// @formatter:on

Does this feature exist and I just don't know about it, or does anyone know of any decent work-arounds?

In my particular case, I'm trying to define data structures (enum types and arrays of strings) that I want to have specific layouts.

解决方案

Use Astyle (Artistic Style) formatter, it's far superior to the Eclipse CDT built-in formatter and has the feature you require:

http://astyle.sourceforge.net/astyle.html#_Disable_Formatting

Example:

#include <iostream>

int main(int argc, char** argv)
{
// *INDENT-OFF*
std::cout<<"hello world"<<'\n';
// *INDENT-ON*
}

Formatting this using astyle won't indent the code between // INDENT-OFF and // INDENT-ON but it will also disable any other formatting features astyle does, like the spacing of the instructions in this case.

I use it myself configured as an external tool. The only problem, external tools don't have hotkeys, but there is one hotkey to "Run Last Launched External Tool", and if you only use one external tool it works the same.

More details about the configuration (linux):

Astyle:

You can get it easily from your distribution repositories or via the official site.

To setup a configuration file with the formatting settings:

http://astyle.sourceforge.net/astyle.html#_Options_File

I use the home folder variant, just create a .astylerc in your $HOME, mine contains:

--suffix=none
--style=allman
--indent=tab=4
--max-code-length=70
--close-templates
--keep-one-line-blocks
--break-elseifs
--break-closing-brackets
--align-reference=type
--align-pointer=type
--indent-classes
--indent-modifiers
--indent-switches
--indent-cases
--indent-labels
--indent-col1-comments
--min-conditional-indent=0
--pad-oper
--pad-header
--unpad-paren

Eclipse:

"Run" menu --> External tools --> External tools Configurations... Add a new "Program" and in the configuration window:

  • Location: /usr/bin/astyle (use whereis or locate to check this)

  • Working Directory: ${project_loc}

  • Arguments: ${selected_resource_loc}

In the same window, refresh tab:

  • Tick Refresh resources upon completion.

  • Tick "The selected resource"

Same window, common tab:

  • Display in favorites menu, Tick "External tools"

这篇关于如何禁用代码块的Eclipse CDT代码格式化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆