Remove source file comments using IntelliJ?

后端 未结 6 1197
醉话见心
醉话见心 2020-11-29 16:47

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 sa

相关标签:
6条回答
  • 2020-11-29 17:20

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

    0 讨论(0)
  • 2020-11-29 17:21

    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

    0 讨论(0)
  • 2020-11-29 17:25

    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;" \;
    
    0 讨论(0)
  • 2020-11-29 17:44

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

    0 讨论(0)
  • 2020-11-29 17:44

    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

    0 讨论(0)
  • 2020-11-29 17:45

    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.

    0 讨论(0)
提交回复
热议问题