Java 使用 IntelliJ 删除源文件注释?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2613432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove source file comments using IntelliJ?
提问by Marcus Leon
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 taskthat can do this.. was looking to do the same from within the IDE. Alternatively a TextPad plugin would work as well..
IntelliJ 中是否有插件或工具可以去除源 .java 文件中的所有注释?我读过一个可以做到这一点的ANT 任务......希望在 IDE 中做同样的事情。或者,TextPad 插件也可以工作..
采纳答案by Behrang Saeedzadeh
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:
但是,我应该警告说,这只适用于 %99.99 次。您可能在文件中定义了一个字符串变量,例如:
String myStr = "/** I am not a comment */";
This regex will turn this to:
这个正则表达式将把它变成:
String myStr = "";
Hope this helps.
希望这可以帮助。
回答by Igor Maznitsa
The Comment Java Preprocessor allows to cut all commentaries from Java sources (the /R option) http://code.google.com/p/java-comment-preprocessor/
评论 Java 预处理器允许从 Java 源(/R 选项)中删除所有评论 http://code.google.com/p/java-comment-preprocessor/
回答by digua
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;" \;
回答by Prashant Bhate
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
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
注意:这可能会弄乱格式,因此您必须重新格式化受影响的代码
回答by Blasanka
Press ctrl + r
and in first textbox type //.*\n
and then press replace allbutton. Then reformat the file by ctrl + alt + l
.
按ctrl + r
和 在第一个文本框中键入//.*\n
,然后按全部替换按钮。然后重新格式化文件ctrl + alt + l
。