在 IntelliJ IDEA 中将 JS 行注释转换为间隔块注释
Convert JS line comments to spaced block comments in IntelliJ IDEA
我正在使用 IntelliJ IDEA 清理一些 Javascript 代码以匹配 a code style standard。
我的目标是改变这个:
// A comment
// About some things
// That spans multiple lines
进入这个:
/**
* A comment
* About some things
* That spans multiple lines
*/
...无需手动编辑每个评论块。
我知道 IntelliJ 有一个 Comment with Block Comment
功能,但是当我 select 一个块并使用它时,我只能得到它来生成这个:
/*A comment
About some things
That spans multiple lines*/
IntelliJ 是否支持 JSDoc 风格的注释?
IntelliJ IDEA 中没有可以自动将现有的 end-of-line 注释序列转换为 JSDoc 注释的功能。要自动执行此类转换,您可以编写一个简单的插件,或者只编写一个脚本,在从命令行调用时执行转换。
您可以使用正则表达式和编程来做到这一点。这是我的 Julia 函数,它同时适用于多行注释和单行注释:
function comment2jsdoc(text)
# match multi-line comment
firstline_m = match( r"(\/\/.*\n)(?:\/\/.*\n)+", text)
if firstline_m !== nothing
# add /** and */
text = replace(text, r"(\/\/.*\n)+\/\/.*\n" => s"/** \n [=10=] */ \n")
# replace // with *
while match( r"((?:\/\/.*\n)+)\/\/(.*\n)", text) !== nothing
text = replace(text, r"((?:\/\/.*\n)+)\/\/(.*\n)" => s" * ")
end
# replace first line
firstline_cap = firstline_m.captures[1]
text = replace(text, firstline_cap => "* $(firstline_cap[3:end])")
end
# match single-line comment
while match(r"\/\/(.*)\n(?!\/\/.*\n)", text) !== nothing
text = replace(text, r"\/\/(.*)\n(?!\/\/.*\n)" => s"/** */")
end
return text
end
例如:
text = """
// A comment
// About some things
// That spans multiple lines
// single line
"""
text = comment2jsdoc(text)
println(text)
将导致:
/**
* A comment
* About some things
* That spans multiple lines
*/
/** single line */
你也可以read/write文本from/to一个文件:
# read file:
text = Base.read("path/to/file", String)
text = comment2jsdoc(text)
# write file
Base.write("path/to/file", text)
我正在使用 IntelliJ IDEA 清理一些 Javascript 代码以匹配 a code style standard。
我的目标是改变这个:
// A comment
// About some things
// That spans multiple lines
进入这个:
/**
* A comment
* About some things
* That spans multiple lines
*/
...无需手动编辑每个评论块。
我知道 IntelliJ 有一个 Comment with Block Comment
功能,但是当我 select 一个块并使用它时,我只能得到它来生成这个:
/*A comment
About some things
That spans multiple lines*/
IntelliJ 是否支持 JSDoc 风格的注释?
IntelliJ IDEA 中没有可以自动将现有的 end-of-line 注释序列转换为 JSDoc 注释的功能。要自动执行此类转换,您可以编写一个简单的插件,或者只编写一个脚本,在从命令行调用时执行转换。
您可以使用正则表达式和编程来做到这一点。这是我的 Julia 函数,它同时适用于多行注释和单行注释:
function comment2jsdoc(text)
# match multi-line comment
firstline_m = match( r"(\/\/.*\n)(?:\/\/.*\n)+", text)
if firstline_m !== nothing
# add /** and */
text = replace(text, r"(\/\/.*\n)+\/\/.*\n" => s"/** \n [=10=] */ \n")
# replace // with *
while match( r"((?:\/\/.*\n)+)\/\/(.*\n)", text) !== nothing
text = replace(text, r"((?:\/\/.*\n)+)\/\/(.*\n)" => s" * ")
end
# replace first line
firstline_cap = firstline_m.captures[1]
text = replace(text, firstline_cap => "* $(firstline_cap[3:end])")
end
# match single-line comment
while match(r"\/\/(.*)\n(?!\/\/.*\n)", text) !== nothing
text = replace(text, r"\/\/(.*)\n(?!\/\/.*\n)" => s"/** */")
end
return text
end
例如:
text = """
// A comment
// About some things
// That spans multiple lines
// single line
"""
text = comment2jsdoc(text)
println(text)
将导致:
/**
* A comment
* About some things
* That spans multiple lines
*/
/** single line */
你也可以read/write文本from/to一个文件:
# read file:
text = Base.read("path/to/file", String)
text = comment2jsdoc(text)
# write file
Base.write("path/to/file", text)