How to disable Eclipse CDT code formatter for a code block
Asked Answered
S

5

19

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.

Spangle answered 2/6, 2013 at 20:16 Comment(1)
NOTE: The previously accepted answer was to use Astyle, as this question was asked before CDT supported this feature natively. Since then support has been added and @greywolf82 recently added an answer with this info, so the Accepted Answer was changed to point to his answer. Thanks @greywolf82! (Using Astyle, clang-format, or some other formatting tool is still a reasonable alternative also.)Spangle
I
3

Yes, you can do it since CDT supports this feature starting from version 9.7. The behavior is exactly the same of JDT.

Involved answered 20/2, 2019 at 6:11 Comment(2)
Thanks! I found the bug that tracked this fix here: bugs.eclipse.org/bugs/show_bug.cgi?id=496249 These days my team is using clang-format and it works well for us, being quite flexible to accommodate our desired style.Spangle
Also, for reference, the tags are as specified in the OP. // @formatter:off<code> // @formatter:onSpangle
M
5

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"
Minivet answered 6/2, 2015 at 11:5 Comment(3)
I totally forgot about astyle! I'll accept this answer if you add some example usage from here: astyle.sourceforge.net/astyle.html#_Disable_FormattingSpangle
Alright, I'll offer up a bit of advice setting this up: when you add this as an external tool in eclipse, use " ${resource_loc}" in the Arguments section to configure it to format the currently open file.Spangle
Does // *INDENT-OFF* work for anyone? In my astyle v2.03 its just ignored.Iota
I
3

Yes, you can do it since CDT supports this feature starting from version 9.7. The behavior is exactly the same of JDT.

Involved answered 20/2, 2019 at 6:11 Comment(2)
Thanks! I found the bug that tracked this fix here: bugs.eclipse.org/bugs/show_bug.cgi?id=496249 These days my team is using clang-format and it works well for us, being quite flexible to accommodate our desired style.Spangle
Also, for reference, the tags are as specified in the OP. // @formatter:off<code> // @formatter:onSpangle
M
1

If you are using OS X or Linux (I haven't checked Windows, but it may be supported), you can use clang-format and CppStyle instead.

clang-format is a formatter utility which is provided with Clang, and it supports on/off comments // clang-format on and // clang-format off in C/C++/ObjC code. An introduction to build Clang and its utility tools can be found here. http://clang.llvm.org/get_started.html You do not need to install whole Clang and LLVM files on your system. Because clang-format is a standalone program which works without Clang. The on/off comments are not supported in old versions, so please use ver 3.7 (available from SVN as of Feb 2015).

CppStyle is an Eclipse plugin which enables us to use clang-format from Eclipse CDT. https://github.com/wangzw/cppstyle

FYI. Here is the same feature request in the CDT Bugzilla. The functionality might be officially supported in future, but using clang-format or Astyle seems to be a better solution at the moment. https://bugs.eclipse.org/bugs/show_bug.cgi?id=453926

Miriam answered 8/2, 2015 at 9:3 Comment(0)
S
0

I guess I could stick these in a file with an extension ignored by the formatter and include this file where appropriate. I tried this out and it works - the data structure gets picked up the indexer (i.e. autocomplete works). Still, it would be nice to have an equivalent to the Java "@formatter:..." syntax.

Spangle answered 2/6, 2013 at 20:16 Comment(1)
Another downside of this method is that the refactoring (rename specifically) does not work. Also, changes to the data structure don't seem to get automatically picked up, thus requiring a clean and rebuild cycle.Spangle
L
0

As far as I know the answer is simply no, such a feature does not exist. You might be able to implement such a feature using the SDK though. Beware that in my experience the documentation is very incomplete and it's very hard to find an Eclipse developer who would be willing to help you fill in the holes. But since the feature exists in the Java formatter and it is an open source product, perhaps you could port over the logic to the C++ formatter.

You could also avoid formatting the whole file, and instead format only by selection.

Let answered 6/2, 2015 at 9:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.