Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling Eclipse code formatting for part of a javadoc

I have a Java class for which part of the javadoc is actually generated as part of the build process: the return value of a method (a static String value) is inserted into the source file, much like $Revision: $ tags work in some version control software.

While this behaviour may be questionable, such duplication of information is required by the framework I use (WEKA machine learning library). I would like Eclipse's code formatter not to interfere with the generated comments. I am using the Eclipse Indigo release.

I can turn the formatter on/off with special comments //@formatter:on and //@formatter:off. However, the @formatter tags are only functional in 'normal' comments, not in javadoc comments. Obviously, they could be easily confused for javadoc tags. This means I cannot turn off the formatter (for example, automatic line breaking) for the generated part of the javadoc comment, and leave it on for the rest, because the @formatter directives must be placed around the javadoc comment.

It there a workaround to toggle code formatting inside javadoc comments?

like image 508
Kristóf Marussy Avatar asked Aug 18 '12 12:08

Kristóf Marussy


1 Answers

Been a while since this was asked/answered and helped me refine the following answer, hopefully additionally useful. I use this alternative as I wanted to run Javascript in Javadocs.

Eclipse provides the @formatter:off and @formatter:on which needs to be enabled via Windows->Preferences->java->code style->formatter:::edit button::: tab "off/on tags". They may be used in any comments.

Around the doc stuff

// @formatter:off
/**
* javadoc
*/ 
// @formatter:on

But when you want the formatter off within the javadoc use the @formatter:xxx within html comments <!-- xxxxx --> to indicate what you are trying to do. Use the <code>...</code> bloack to ensure no formatting and the inclusion of code as javascript.

Editted the code statements in the example as I wanted this to work on eclipse and netbeans. I found the formatter:off work but then stopped working on a different version of eclipse (yes I use multiple IDE versions).

/** 
* <br><!-- @formatter:off -->
* <code>
* <script type="text/javascript">
* // hash structure for holding variable as name and its text
* var hashText = {};
*  
* // function causes a hyper-link to be created
* function insertLink(varName, text){
*    var link22;
*  
*    hashText[varName] = text;
*  
*    link22 = '<a href="./ConsoleCapture.html#' + varName + '">' + hashText[varName] + '</a>';
*       
*    document.write(link22);
* }
* function insertLinkA(varName){
*    var link22;
*
*    link22 = '<a href="./ConsoleCapture.html#' + varName + '">' + hashText[varName] + '</a>';
*
*    document.write(link22);
* }
*      
* function setLinkPoint(varName, text){
*     hashText[varName] = text;
*      
*     document.write('<a id="' + varName + '"><U>' + hashText[varName] + '</U></a>');
* }
*      
* function setLinkPointA(varName){
*    document.write('<a id="' + varName + '"><U>' + hashText[varName] + '</U></a>');
* }
* </script>
* <code>
* <!-- @formatter:on -->
*
*
*/
like image 74
user3528877 Avatar answered Oct 07 '22 03:10

user3528877