Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 496249

Summary: Tags for disabling/enabling CDT code formatter (feature)
Product: [Tools] CDT Reporter: Timothee Fivaz <timothee.fivaz>
Component: cdt-otherAssignee: Marco Stornelli <marco.stornelli>
Status: RESOLVED FIXED QA Contact: Doug Schaefer <cdtdoug>
Severity: enhancement    
Priority: P3 CC: 525747482, hahoangthach, jonah, marco.stornelli, sagie, timothee.fivaz, webmaster
Version: Next   
Target Milestone: 9.7.0   
Hardware: PC   
OS: Windows 7   
See Also: https://bugs.eclipse.org/bugs/show_bug.cgi?id=27079
https://git.eclipse.org/r/137067
https://git.eclipse.org/c/cdt/org.eclipse.cdt.git/commit/?id=a6d06902b1f2626f3fadbeeb55a52979af22262a
https://git.eclipse.org/r/140489
Whiteboard:
Bug Depends on:    
Bug Blocks: 546391    

Description Timothee Fivaz CLA 2016-06-16 05:53:09 EDT
Eclipse Java code formatter, as well as AStyle, allow to disable and enable the code formatter for a part of the code with tags. This feature would be great for CDT if we want to format some parts of our code in another way than the standard formatter configured. 

This feature has been added to the Java formatter in 2010, as seen in this Bugzilla reference : https://bugs.eclipse.org/27079

The proposed tags could be : 

/* here goes my constants: @@formatter:off */
#define FOO        0
#define FOOBAR     1
#define FOOBARFOO  2
/* @@formatter:on */

In the Java formatter I think the tag can be specify in the settings. this could be interesting as well.
Comment 1 Doug Schaefer CLA 2016-06-16 10:28:08 EDT
This would be really cool. I love how the code formatter cleans up my code but at times, I just want to keep things aligned differently like your example. Seems to be more common in the C world where we often deal with lists of constants.
Comment 2 ha thach CLA 2016-10-01 04:21:03 EDT
+1 , this is the reason that I have to disable auto format when saving. Would love to override formatter when needed.
Comment 3 Eclipse Genie CLA 2019-02-16 05:03:41 EST
New Gerrit change created: https://git.eclipse.org/r/137067
Comment 4 Marco Stornelli CLA 2019-02-16 05:07:46 EST
I created a patch for this feature request. I cannot change the status assigning the bug to myself.
Comment 5 Jonah Graham CLA 2019-02-16 09:04:42 EST
Hi Marco, thanks for having a look at this. I have assigned the bug to you with a provisional target of 9.7.0.
Comment 6 Jonah Graham CLA 2019-02-19 15:52:48 EST
Marco, this is a significant new feature for CDT 9.7. Can you please add it to https://wiki.eclipse.org/CDT/User/NewIn97 (have a look at previous New and Noteworthy entries for an idea of what it can look like https://wiki.eclipse.org/CDT/User/NewInCDT)

PS. If this is your first edit on the Wiki, you will probably be moderated. Let me know and I will clear you through the moderator queue.
Comment 7 Marco Stornelli CLA 2019-02-19 15:55:58 EST
ok, tomorrow I'm going to add a note on the wiki, thanks for help.
Comment 8 Jonah Graham CLA 2019-02-19 15:57:39 EST
(In reply to Marco Stornelli from comment #7)
> ok, tomorrow I'm going to add a note on the wiki, thanks for help.

Great. And the thanks is to you :-)
Comment 10 Nathan Ridge CLA 2019-02-20 03:19:40 EST
*** Bug 327559 has been marked as a duplicate of this bug. ***
Comment 11 Nathan Ridge CLA 2019-03-10 17:43:29 EDT
*** Bug 425248 has been marked as a duplicate of this bug. ***
Comment 12 Sun Quan CLA 2019-04-12 03:23:12 EDT
## I test this sample, but format has some problem
/* @formatter:off */
int xx2(){string s1="abc";string s2="def";}
/* @formatter:on */

/* @formatter:on */
int xx(){string s1="abc";string s2="def";}
/* @formatter:off */

## after format,  the "}" is not at next line!
/* @formatter:off */
int xx2(){string s1="abc";string s2="def";}
/* @formatter:on */

/* @formatter:on */
int xx()
{
    string s1 = "abc";
    string s2 = "def";}
/* @formatter:off */
Comment 13 Marco Stornelli CLA 2019-04-12 06:17:00 EDT
it seems ok, there's no stack concept, the formatter just use the latest tag, since the last one was off, the code is not formatted as expected
Comment 14 Eclipse Genie CLA 2019-04-12 07:24:02 EDT
New Gerrit change created: https://git.eclipse.org/r/140489
Comment 15 Jonah Graham CLA 2019-04-12 07:26:07 EDT
Hi Sun,

I think I understand the problem you are reporting. I wrote a new unit test for your case that fails like this:

junit.framework.ComparisonFailure: expected:<...
	string s2 = "def";[
]}
/* @formatter:off ...> but was:<...
	string s2 = "def";[]}
/* @formatter:off ...>

See https://git.eclipse.org/r/#/c/140489/

Marco or Sun can you review it please?

However as this bug now refers to finished release can a new bug be created to track the fixes in 9.8.0 (or potentially 9.7.1 if we want to push that out)?
Comment 16 Marco Stornelli CLA 2019-04-12 07:57:15 EDT
ok, so the problem is the contrary, the brace is moved on next line. I didn't understand. I think the problem could be related to the ending of no format zone. Currently it is the start of "on comment". Actually reading again the docs maybe we should moving the end to the end of the comment because it says "the formatter is restored *after* the on comment". I will take a look.
Comment 17 Jonah Graham CLA 2019-04-12 08:15:42 EDT
(In reply to Marco Stornelli from comment #16)
> ok, so the problem is the contrary, the brace is moved on next line.

Sorry, I am not sure I understand the comment. The brace *should* be moved onto the next line, but it isn't.

> I didn't understand. I think the problem could be related to the ending of no
> format zone. Currently it is the start of "on comment". Actually reading
> again the docs maybe we should moving the end to the end of the comment
> because it says "the formatter is restored *after* the on comment". I will
> take a look.

I have uploaded an additional minimized test case to show that the problem is the off comment, not the on comment.
Comment 18 Marco Stornelli CLA 2019-04-12 12:06:59 EDT
Ok, got it. The problem is the "new line" removed when scribe enters in inactive state.
Comment 19 Jonah Graham CLA 2019-04-13 14:30:40 EDT
(In reply to Sun Quan from comment #12)
> ## I test this sample, but format has some problem

Hi Sun, Marco has now fixed your use case in Bug 546391. Thank you for reporting it. The fix will be available in CDT 9.8