eclipse 3.6 allows you to turn off formatting by placing a special comment, like
the on/off features have to be turned "on" in eclipse preferences: java > code style > formatter. click on edit, off/on tags, enable enable off/on tags.
it's also possible to change the magic strings in the preferences — check out the eclipse 3.6 docs here.
java > code style > formatter > edit > off/on tags
this preference allows you to define one tag to disable and one tag to enable the formatter (see the off/on tags tab in your formatter profile):
you also need to enable the flags from java formatting
this hack works:
string x = "s" + //formatter hack
"a" + //
"c" + //
i would suggest not to use the formatter. bad code should look bad not artificially good. good code takes time. you cannot cheat on quality. formatting is part of source code quality.
not so pretty but works with default settings and for the first line as well:
string query = "" +
"select foo, bar, baz" +
" from abc " +
" where bar > 4 ";
the phantom comments, adding
// where you want new lines, are great!
the @formatter: off adds a reference from the code to the editor. the code should, in my opinion, never have such references.
the phantom comments (//) will work regardless of the formatting tool used. regardless of eclipse or intelij or whatever editor you use. this even works with the very nice google java format
actually, you probably do want formatting right? you want to remove mixed tab/space and trailing spaces. you want to indent the lines according to the code standard. what you dont want is a long line. that, and only that, is what the phantom comment gives you!
alternative method: in eclipse 3.6, under "line wrapping" then "general settings" there is an option to "never join already wrapped lines." this means the formatter will wrap long lines but not undo any wrapping you already have.
@xpmatteo has the answer to disabling portions of code, but in addition to this, the default eclipse settings should be set to only format edited lines of code instead of the whole file.
preferences->java->editor->save actions->format source code->format edited lines
this would have prevented it from happening in the first place since your coworkers are reformatting code they didn't actually change. this is a good practice to prevent mishaps that render diff on your source control useless (when an entire file is reformatted because of minor format setting differences).
it would also prevent the reformatting if the on/off tags option was turned off.
i'm using fixed width string-parts (padded with whitespace) to avoid having the formatter mess up my sql string indentation. this gives you mixed results, and won't work where whitespace is not ignored as it is in sql, but can be helpful.
final string sql = "select v.value from properties p "
+ "join property_values v on p.property_id = v.property_id "
+ "where p.product_id = ? "
+ "and v.value is not null ";
end each of the lines with a double slash "//". that will keep eclipse from moving them all onto the same line.
if you put the plus sign on the beginning of the line, it formats differently:
string query =
"select foo, bar, baz"
+ " from abc"
+ " where bar > 4";
you have to turn on the ability to add the formatter tags. in the menubar go to:
windows → preferences java → code style → formatter
press the edit button. choose the last tab. notice the on/off box and enable them with a checkbox.
instead of turning the formatting off, you can configure it not to join already wrapped lines. similar to jitter's response, here's for eclipse sts:
properties → java code style → formatter → enable project specific settings or configure workspace settings → edit → line wrapping (tab) → check "never join already wrapped lines"
see this answer on so.
there is another solution that you can use to suppress the formatting of specific block comments. use
/*- (note the hyphen) at the beginning of the block comment, and the formatting won't be affected if you format the rest of the file.
* here is a block comment with some very special
* formatting that i want indent(1) to ignore.
source: documentation at oracle.
afaik from eclipse 3.5 m4 on the formatter has an option "never join lines" which preserves user lines breaks. maybe that does what you want.
else there is this ugly hack
string query = //
"select foo, bar, baz" + //
" from abc" + //
" where bar > 4";
- How to turn off the Eclipse code formatter for certain sections of Java code?
- How to turn off the Eclipse code formatter for certains sections of maven pom.xml
- Turn off Eclipse formatter for selected code area?
- Did anyone create the Java Code Formatter Profile for Eclipse IDE that conforms to the Android Code Style Rules?
- How to set Code Formatter for Java Anonymous Methods in Eclipse
- How to turn off warnings for @string resource in eclipse for the Android SDK
- How do I turn off the "Visible only for test sources" feature in Eclipse Photon?
- How can I prevent Eclipse from adding the 'final' for certain lines of Java code?
- How to turn off compile time errors for Java in Eclipse IDE
- How to add shortcut keys for java code in eclipse
- How to create the pom.xml for a Java project with Eclipse
- Eclipse keeps re-checking my git repositories. How do I turn this off or change the interval?
- How to turn off the Javadoc hover in Eclipse (or selectively enable it)?
- How can I enable the Task List in Eclipse for a Java Project?
- Java How to beautify the java code inside Eclipse IDE
- How to turn off error reporting by Eclipse for .md files
- How do I set the Java Doc for google drive api for android in Eclipse
- How to install eclipse for java developer,c/c++ developer, php developer at the same time?
- Disable Hot Code Replace for the Eclipse Java debugger
- How to make code completion for Java 8 lambda parameters in Eclipse work?
- How do you add a PHP framework to code completion/assist in Eclipse PDT and can the framework be added as default for all projects?
- How to debug Groovy code via the remote Java application debugging in Eclipse
- How to use Eclipse for capturing the structure of a java software
- How to have Eclipse Java EE automatically generate the exploded WAR for a web project?
- eclipse java code style formatter: prevent line wrapping for the special cases
- How to install/configure Eclipse for Java ME development for the Nokia Series 40? (Windows)
- How do I "add" the c/c++ package to the eclipse for java ee developers?
- How to set up Jacoco code coverage tool in Eclipse for viewing colors in Java code?
- How do I change colors for the Java Docs Hover Over in Eclipse
- how turn off speculative execution in hadoop source code in eclipse
More Query from same tag
- when developing using eclipse or netbeans, do you use phpmyadmin to admin mysql?
- Get data from a specific line in text file using BufferedReader
- Hibernate Reverse Engineering Composite Key Creates Extra ID Class
- Cordova app deployment on device
- In Equinox is it possible to for one feature/bundle to obsolete another?
- IllegalArgument: javax.inject-Extension-Name when deploying webapp
- eclipse UI: sort or filter jars and class folders
- Same workspace with eclipse
- Favorite Google App Engines Tips and Features
- Admob Google Play services. null pointer exception when showing interstitial ads
- Referenced Java Project Compiled Update Doesn't Update Android Library Project
- Run jaxws2.2 webservice in Android
- Tail recursion in scala ,here I am running scala code in eclipse IDE ,but getting no output
- How to apply a stereotype from UML Standard Profile inside QVTo transformation?
- can't push upstream using EGit
- Visual Studio style Output Console Window in Eclipse
- LogCat Stops Running in Eclipse Needs Restart
- ADT destroys git repository on import
- debug a remote wildfly instance from eclipse
- Spring Boot application doesn't start when deployed to Tomcat inside Eclipse
- NoClassDefFound on tomcat in eclipse, but i have added it to classpath
- Eclipse Plugin: Editor for a file type with some simple elements
- Eclipse EE can't expand folder called 'services'
- Unable to execute Isdisplayed() method in Android appium environment
- How to run RCP 4.x application under Eclipse debugger?
- Organise android projects in eclipse and remove closed projects
- Editing Android Layout using Eclipse ADT
- How to connect android project with php files which is saved outside the workspace?
- Long delay in JBoss start time
- Trying to sycnrhonize SVN in eclipse is no longer prompting me for password