Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Eclipse CDT code formatter for a code block

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.

like image 522
Freerobots Avatar asked Jun 02 '13 20:06

Freerobots


People also ask

How do I turn off code formatting in Eclipse?

To change these settings, use Window -> Preferences -> Java -> Code Style -> Formatter. On the last tab of the formatter, you can define on/off tags which allow to you to prevent reformatting of some code.

What is @formatter off?

The @formatter: off adds a reference from the code to the editor.


1 Answers

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"
like image 171
Jouven Avatar answered Nov 08 '22 22:11

Jouven