VSCode: delete all comments in a file

不问归期 提交于 2020-02-01 05:37:13

问题


Is there an easy way to delete all comments from an open file in VSCode? Preferably both line and block comments.

Most interested in Java, but also Python and R.


回答1:


Easy way:

  • Open extensions (ctrl-shift-x)
  • type in remove comments in the search box.
  • Install the top pick and read instructions.

Hard way (I guess)

  • search replace(ctrl-h)
  • toggle regex on (alt-r).
  • Learn some regular expressions! https://docs.rs/regex/0.2.5/regex/#syntax

A simple //.* will match all single line comments (and more ;D). #.* could be used to match python comments. And /\*[\s\S\n]*\*/ matches block comments. And you can combine them as well: //.*|/\*[\s\S\n]*\*/ (| in regex means "or", . means any character, * means "0 or more" and shows how many characters to match, so therefor .* means all characters until the end of the line)

Of course with caveats, such as urls (https://...) has double slashes and will match that first rule, and god knows where there are # in code that will match that python-rule. So some reading/adjusting has to be done!

Once you start fiddling with your regexes it can take a lifetime to get them perfect, so be careful. I'd probably go the easy route if you haven't touched regexes before! But knowing some simple regex by heart will do you good, since it's usable almost everywhere.



来源:https://stackoverflow.com/questions/50574731/vscode-delete-all-comments-in-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!