Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove source file comments using IntelliJ?

Is there a plugin or tool in IntelliJ that will strip all comments out of your source .java files? I've read about an ANT task that can do this.. was looking to do the same from within the IDE. Alternatively a TextPad plugin would work as well..

like image 595
Marcus Leon Avatar asked Apr 10 '10 12:04

Marcus Leon


People also ask

How to remove comments from code IntelliJ?

To add or remove a block comment, do one of the following: From the main menu, select Code | Comment with Block Comment. Press Ctrl+Shift+/ .

How do I set default comments in IntelliJ?

Add a new comment Type /** before a declaration and press Enter . The IDE auto-completes the doc comment for you. For information on how to disable this feature, refer to Disable automatic comments.

How do I search comments in IntelliJ?

Go to Edit > Find > Search Structurally... From the top right corner click on gear icon and click on Existing Templates... Then it will add /* $CommentContent$ */ to the Structural Search window as for the first screenshot. This will find all the commented code (including Javadoc) from the selected path.


2 Answers

You can use the "Replace" (or "Replace in Path" if you want to remove comments in multiple files) in the regular expression mode and then use this regular expression in the "Text to find" field:

(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/|[ \t]*//.*) 

and replace it with an empty string. Then press "All" to apply this replacement to the entire file or all the selected files. This will remove all block comments and line comments from your file. If you want only block comments to be removed, use this regex instead:

(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/) 

And if you want to just remove line comments, you can use this regex:

([ \t]*//.*) 

However, I should warn that this works only %99.99 of times. You might have a string variable defined in your file like:

String myStr = "/** I am not a comment */"; 

This regex will turn this to:

String myStr = ""; 

Hope this helps.

like image 67
Behrang Avatar answered Sep 28 '22 07:09

Behrang


Late to the party but there is "Structural Search and Replace Dialogs" option that can be used to search different kind of comments and replace them

Go to Edit => Find => Replace Structurally...

  • Enter one of below in "Search Template:"

Single line

// $CommentContent$  

Multi line

/*   $CommentContent$  */ 

Javadoc

/**   $CommentContent$  */ 
  • Leave the "Replacement Template:" blank

  • Select appropriate Scope

  • Click 'Find' and then 'Replace all'

  • you Should see no more comments

Note: that this might mess up formatting so you will have to reformat affected code

like image 33
Prashant Bhate Avatar answered Sep 28 '22 07:09

Prashant Bhate