Remove source file comments using IntelliJ?
Asked Answered
P

6

49

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..

Pretentious answered 10/4, 2010 at 12:59 Comment(4)
Why not just run the Ant task in IntelliJ and call it a day?Unimposing
Duffymo that makes too much sense ;-)Pretentious
Well it does make sense, I'd love to find a little IntelliJ plugin that I can use easily on any old file I open that I want to clear out the comments on.Pretentious
This regex removes the stars: ^[ ]*/?\*+/? ?Nappe
D
110

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 = "";
Discordancy answered 10/4, 2010 at 15:53 Comment(9)
The first one? I just double checked it in IDEA 9.0.1 and it is working for me.Discordancy
Hmmm.. I'm trying on this pastebin.com/u0fQub8j file.. doesn't seem to work. Any ideas? I'm on IntelliJ 8.Pretentious
Tried again, the second one works nicely.. doesn't get all the comments but most. Still couldn't get the first to work.Pretentious
Every day what i want , i found Here ( Stack over flow , Rocks ) @intellij , dont forget to check the , Regular Expression check box .Nickles
@marcus, you can select the proper context to make it 100% content.screencast.com/users/bulenkov/folders/Jing/media/…Confection
OMB, this is my favorite answer. I love REGEX more than most people do and I wanted the answer to this question so I found this and wow, just awesome. I wish I could upvote you 2 times for it.Madoc
is there any way to handle having something like /*test1*//*test2*/ in the middle of your code? using the first expression?Latinity
I suggest following the method posted by Prashant Bhate (Structural Search and Replace).Discordancy
WARNING: If you have URLs in your code, this will match with the // in the http-colon-slash-slash pattern. If there are only a few, edit them to be http-colon-star or something, replace, and then edit them back. Or if you like regex (and if you do, you're sick :-)), exclude http-slash-slash itself.Planetstruck
C
25

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

Chesser answered 19/5, 2017 at 16:20 Comment(5)
best answer for meLole
how do you do with xml comments? I'm trying to add <!-- $CommentContent$ --> but it says is malformed.Kobe
This worked perfectly except for one instance. Cheers.Rockaway
best answer. Worked for me really well.Lactoprotein
This ^^. Please make this the accepted answerFancy
N
8

Press ctrl + r and in first textbox type //.*\n and then press replace all button. Then reformat the file by ctrl + alt + l.

Nodose answered 20/1, 2019 at 9:21 Comment(2)
This is a really nice solution. one thing to take into account, if you have a comment on the last line of a comment block. /* /n // comment here */ your code can become jumbled. so be careful with it. maybe edit your regex to fit that scenario as well.Latinity
In newer versions of IntelliJ, remember to click the .* button on the right of the text box to enable Regex mode to use this solution.Cosmopolis
O
3

The Comment Java Preprocessor allows to cut all commentaries from Java sources (the /R option) http://code.google.com/p/java-comment-preprocessor/

Optometry answered 28/12, 2012 at 14:51 Comment(0)
I
3

Press control + r to open replace and replace all box

type //.* in first box, keep in second box empty and then select Regex. Select replace or replace all

That's it.

Enjoy 😎

Independent answered 22/9, 2020 at 12:22 Comment(1)
There are a few flaws in this idea. 1. It won't strip comments in /* this form */. 2. I think the OP is looking to do this for all .java files, not just the currently opened one. 3. You need to turn on the filter 'In Comments', otherwise you'd blindly replace that pattern everywhere, including strings. 4. This is basically the same answer as Blasanka, almost 2 years priorDorladorlisa
P
2

My solution is:

find . -name *.java -type f -exec sh -c "perl -0pe 's#/\*(.|\n)*?\*/##g; s|//.*?\n|\n|g' '{}' > temp.java; cat temp.java > {} ; rm temp.java;" \;
Prevent answered 21/8, 2014 at 6:53 Comment(1)
This is the best one I found so far - I just found that it left blank lines where the // were, so I played with it a bit and got this change to the perl bit: perl -0pe 's#/\*(.|\n)*?\*/##g; s|[^\S\r\n]*//.*?\n||g; s|//.*?\n|\n|g' Pelisse

© 2022 - 2024 — McMap. All rights reserved.